b@1548: #!/usr/bin/python b@1550: # -*- coding: utf-8 -*- b@1548: b@1597: import xml.etree.ElementTree as ET b@1597: import os b@1597: import csv b@2078: import sys b@2072: b@2072: # COMMAND LINE ARGUMENTS b@2072: b@2072: assert len(sys.argv)<3, "comment_parser takes at most 1 command line argument\n"+\ b@2072: "Use: python score_parser.py [rating_folder_location]" b@2072: b@2072: # XML results files location b@2072: if len(sys.argv) == 1: b@2072: folder_name = "../saves" # Looks in 'saves/' folder from 'scripts/' folder b@2072: print "Use: python comment_parser.py [XML_files_location]" b@2072: print "Using default path: " + folder_name b@2072: elif len(sys.argv) == 2: b@2072: folder_name = sys.argv[1] # First command line argument is folder b@2072: b@2072: # check if folder_name exists b@2072: if not os.path.exists(folder_name): b@2072: #the file is not there b@2072: print "Folder '"+folder_name+"' does not exist." b@2072: sys.exit() # terminate script execution b@2072: elif not os.access(os.path.dirname(folder_name), os.W_OK): b@2072: #the file does exist but write privileges are not given b@2072: print "No write privileges in folder '"+folder_name+"'." b@2072: b@2072: b@2072: # CODE b@1553: b@2078: # remember which files have been opened this time b@2078: file_history = [] b@2078: b@1597: # get every XML file in folder b@1553: for file in os.listdir(folder_name): b@1597: if file.endswith(".xml"): b@1553: tree = ET.parse(folder_name + '/' + file) b@1597: root = tree.getroot() b@2057: b@1598: # get list of all page names nickjillings@1309: for audioholder in root.findall("./page"): # iterate over pages b@1598: page_name = audioholder.get('id') # get page name b@2036: b@2036: if page_name is None: # ignore 'empty' audio_holders nickjillings@1309: print "WARNING: " + file + " contains empty page. (comment_parser.py)" b@2036: break b@1597: b@1598: # create folder [page_name] if not yet created b@1553: if not os.path.exists(folder_name + "/" + page_name): b@1553: os.makedirs(folder_name + "/" + page_name) b@1597: b@1598: # for page [page_name], print comments related to fragment [id] b@1553: for audioelement in audioholder.findall("./audioelement"): b@2036: if audioelement is not None: # Check it exists b@2036: audio_id = str(audioelement.get('id')) b@1548: b@1553: csv_name = folder_name +'/' + page_name+'/'+page_name+'-comments-'+audio_id+'.csv' b@1597: b@2078: # If file hasn't been opened yet this time, empty b@2078: if csv_name not in file_history: b@2078: csvfile = open(csv_name, 'w') b@2078: file_history.append(csv_name) # remember this file has been written to this time around b@2078: else: b@2078: # append (!) to file [page_name]/[page_name]-comments-[id].csv b@2078: csvfile = open(csv_name, 'a') b@2078: writer = csv.writer(csvfile, b@2078: delimiter=',', b@2078: dialect="excel", b@2078: quoting=csv.QUOTE_ALL) b@2078: commentstr = audioelement.find("./comment/response").text b@1553: b@2078: if commentstr is None: b@2078: commentstr = '' b@2078: b@2078: # anonymous comments: b@2078: #writer.writerow([commentstr.encode("utf-8")]) b@2078: # comments with (file) name: b@2078: writer.writerow([file[:-4]] + [commentstr.encode("utf-8")]) b@1597: b@2078: #TODO Replace 'new line' in comment with something else? b@1548: b@1548: # PRO TIP: Change from csv to txt by running this in bash: b@1548: # $ cd folder_where_csvs_are/ b@1548: # $ for i in *.csv; do mv "$i" "${i/.csv}".txt; done