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