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