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@1811: import sys b@1805: b@1805: # COMMAND LINE ARGUMENTS b@1805: b@1805: assert len(sys.argv)<3, "comment_parser takes at most 1 command line argument\n"+\ b@1805: "Use: python score_parser.py [rating_folder_location]" b@1805: b@1805: # XML results files location b@1805: if len(sys.argv) == 1: b@1805: folder_name = "../saves" # Looks in 'saves/' folder from 'scripts/' folder b@1805: print "Use: python comment_parser.py [XML_files_location]" b@1805: print "Using default path: " + folder_name b@1805: elif len(sys.argv) == 2: b@1805: folder_name = sys.argv[1] # First command line argument is folder b@1805: b@1805: # check if folder_name exists b@1805: if not os.path.exists(folder_name): b@1805: #the file is not there b@1805: print "Folder '"+folder_name+"' does not exist." b@1805: sys.exit() # terminate script execution b@1805: elif not os.access(os.path.dirname(folder_name), os.W_OK): b@1805: #the file does exist but write privileges are not given b@1805: print "No write privileges in folder '"+folder_name+"'." b@1805: b@1805: b@1805: # CODE b@1553: b@1811: # remember which files have been opened this time b@1811: file_history = [] b@1811: 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@1790: 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@1769: b@1769: if page_name is None: # ignore 'empty' audio_holders nickjillings@1309: print "WARNING: " + file + " contains empty page. (comment_parser.py)" b@1769: 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@1769: if audioelement is not None: # Check it exists b@1769: 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@1811: # If file hasn't been opened yet this time, empty b@1811: if csv_name not in file_history: b@1811: csvfile = open(csv_name, 'w') b@1811: file_history.append(csv_name) # remember this file has been written to this time around b@1811: else: b@1811: # append (!) to file [page_name]/[page_name]-comments-[id].csv b@1811: csvfile = open(csv_name, 'a') b@1811: writer = csv.writer(csvfile, b@1811: delimiter=',', b@1811: dialect="excel", b@1811: quoting=csv.QUOTE_ALL) b@1811: commentstr = audioelement.find("./comment/response").text b@1553: b@1811: if commentstr is None: b@1811: commentstr = '' b@1811: b@1811: # anonymous comments: b@1811: #writer.writerow([commentstr.encode("utf-8")]) b@1811: # comments with (file) name: b@1811: writer.writerow([file[:-4]] + [commentstr.encode("utf-8")]) b@1597: b@1811: #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