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