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