annotate Perceptual Evaluation/webaudioevaluationtool/scripts/comment_parser.py @ 0:55c282f01a30 tip

Adding files to Repo. Initial Commit
author Dave <d.j.moffat@qmul.ac.uk>
date Fri, 16 Oct 2015 18:04:00 +0100
parents
children
rev   line source
d@0 1 #!/usr/bin/python
d@0 2 # -*- coding: utf-8 -*-
d@0 3
d@0 4 import xml.etree.ElementTree as ET
d@0 5 import os
d@0 6 import csv
d@0 7
d@0 8 # XML results files location (modify as needed):
d@0 9 folder_name = "../saves" # Looks in 'saves/' folder from 'scripts/' folder
d@0 10
d@0 11 # get every XML file in folder
d@0 12 for file in os.listdir(folder_name):
d@0 13 if file.endswith(".xml"):
d@0 14 tree = ET.parse(folder_name + '/' + file)
d@0 15 root = tree.getroot()
d@0 16
d@0 17 # get list of all page names
d@0 18 for audioholder in root.findall("./audioholder"): # iterate over pages
d@0 19 page_name = audioholder.get('id') # get page name
d@0 20
d@0 21 if page_name is None: # ignore 'empty' audio_holders
d@0 22 print "WARNING: " + file + " contains empty audio holder. (comment_parser.py)"
d@0 23 break
d@0 24
d@0 25 # create folder [page_name] if not yet created
d@0 26 if not os.path.exists(folder_name + "/" + page_name):
d@0 27 os.makedirs(folder_name + "/" + page_name)
d@0 28
d@0 29 # for page [page_name], print comments related to fragment [id]
d@0 30 for audioelement in audioholder.findall("./audioelement"):
d@0 31 if audioelement is not None: # Check it exists
d@0 32 audio_id = str(audioelement.get('id'))
d@0 33
d@0 34 csv_name = folder_name +'/' + page_name+'/'+page_name+'-comments-'+audio_id+'.csv'
d@0 35
d@0 36 # append (!) to file [page_name]/[page_name]-comments-[id].csv
d@0 37 with open(csv_name, 'a') as csvfile:
d@0 38 writer = csv.writer(csvfile,
d@0 39 delimiter=',',
d@0 40 dialect="excel",
d@0 41 quoting=csv.QUOTE_ALL)
d@0 42 commentstr = audioelement.find("./comment/response").text
d@0 43
d@0 44 if commentstr is None:
d@0 45 commentstr = '';
d@0 46
d@0 47 # anonymous comments:
d@0 48 #writer.writerow([commentstr.encode("utf-8")])
d@0 49 # comments with (file) name:
d@0 50 writer.writerow([file[:-4]] + [commentstr.encode("utf-8")])
d@0 51
d@0 52 #TODO Replace 'new line' in comment with something else?
d@0 53
d@0 54 # PRO TIP: Change from csv to txt by running this in bash:
d@0 55 # $ cd folder_where_csvs_are/
d@0 56 # $ for i in *.csv; do mv "$i" "${i/.csv}".txt; done