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 nicholas@2228: page_name = audioholder.get('ref') # 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 nicholas@2228: nicholas@2228: if audioholder.get("state") != "complete": nicholas@2228: print "WARNING: " + file + "test page " + page_name + " is not complete, skipping." nicholas@2228: else : nicholas@2228: # create folder [page_name] if not yet created nicholas@2228: if not os.path.exists(folder_name + "/" + page_name): nicholas@2228: os.makedirs(folder_name + "/" + page_name) b@1597: nicholas@2228: # for page [page_name], print comments related to fragment [id] nicholas@2228: for audioelement in audioholder.findall("./audioelement"): nicholas@2228: if audioelement is not None: # Check it exists nicholas@2228: audio_id = str(audioelement.get('ref')) b@1597: nicholas@2228: csv_name = folder_name +'/' + page_name+'/'+page_name+'-comments-'+audio_id+'.csv' b@1597: nicholas@2228: # If file hasn't been opened yet this time, empty nicholas@2228: if csv_name not in file_history: nicholas@2228: csvfile = open(csv_name, 'w') nicholas@2228: file_history.append(csv_name) # remember this file has been written to this time around nicholas@2228: else: nicholas@2228: # append (!) to file [page_name]/[page_name]-comments-[id].csv nicholas@2228: csvfile = open(csv_name, 'a') nicholas@2228: writer = csv.writer(csvfile, nicholas@2228: delimiter=',', nicholas@2228: dialect="excel", nicholas@2228: quoting=csv.QUOTE_ALL) nicholas@2228: commentstr = audioelement.find("./comment/response").text nicholas@2228: nicholas@2228: if commentstr is None: nicholas@2228: commentstr = '' nicholas@2228: nicholas@2228: # anonymous comments: nicholas@2228: #writer.writerow([commentstr.encode("utf-8")]) nicholas@2228: # comments with (file) name: nicholas@2228: 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