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@927: 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@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 BrechtDeMan@928: for audioholder in root.findall("./audioholder"): # 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 BrechtDeMan@1051: print "WARNING: " + file + " contains empty audio holder. (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@878: # append (!) to file [page_name]/[page_name]-comments-[id].csv BrechtDeMan@878: with open(csv_name, 'a') as csvfile: BrechtDeMan@878: writer = csv.writer(csvfile, BrechtDeMan@878: delimiter=',', BrechtDeMan@878: dialect="excel", BrechtDeMan@878: quoting=csv.QUOTE_ALL) BrechtDeMan@883: commentstr = audioelement.find("./comment/response").text BrechtDeMan@883: BrechtDeMan@1030: if commentstr is None: BrechtDeMan@883: commentstr = ''; BrechtDeMan@883: BrechtDeMan@883: # anonymous comments: BrechtDeMan@883: #writer.writerow([commentstr.encode("utf-8")]) BrechtDeMan@883: # comments with (file) name: BrechtDeMan@883: writer.writerow([file[:-4]] + [commentstr.encode("utf-8")]) BrechtDeMan@927: BrechtDeMan@878: #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