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