Mercurial > hg > webaudioevaluationtool
comparison scripts/comment_parser.py @ 718:0b095f66de65
Update to WAC paper references
author | Dave <djmoffat@users.noreply.github.com> |
---|---|
date | Thu, 24 Sep 2015 09:49:01 +0100 |
parents | |
children | 1b6fa37d46a4 235594325b84 b5bf2f57187c |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 718:0b095f66de65 |
---|---|
1 #!/usr/bin/python | |
2 # -*- coding: utf-8 -*- | |
3 | |
4 import xml.etree.ElementTree as ET | |
5 import os | |
6 import csv | |
7 import sys | |
8 | |
9 # COMMAND LINE ARGUMENTS | |
10 | |
11 assert len(sys.argv)<3, "comment_parser takes at most 1 command line argument\n"+\ | |
12 "Use: python score_parser.py [rating_folder_location]" | |
13 | |
14 # XML results files location | |
15 if len(sys.argv) == 1: | |
16 folder_name = "../saves" # Looks in 'saves/' folder from 'scripts/' folder | |
17 print "Use: python comment_parser.py [XML_files_location]" | |
18 print "Using default path: " + folder_name | |
19 elif len(sys.argv) == 2: | |
20 folder_name = sys.argv[1] # First command line argument is folder | |
21 | |
22 # check if folder_name exists | |
23 if not os.path.exists(folder_name): | |
24 #the file is not there | |
25 print "Folder '"+folder_name+"' does not exist." | |
26 sys.exit() # terminate script execution | |
27 elif not os.access(os.path.dirname(folder_name), os.W_OK): | |
28 #the file does exist but write privileges are not given | |
29 print "No write privileges in folder '"+folder_name+"'." | |
30 | |
31 | |
32 # CODE | |
33 | |
34 # remember which files have been opened this time | |
35 file_history = [] | |
36 | |
37 # get every XML file in folder | |
38 for file in os.listdir(folder_name): | |
39 if file.endswith(".xml"): | |
40 tree = ET.parse(folder_name + '/' + file) | |
41 root = tree.getroot() | |
42 | |
43 # get list of all page names | |
44 for audioholder in root.findall("./audioholder"): # iterate over pages | |
45 page_name = audioholder.get('id') # get page name | |
46 | |
47 if page_name is None: # ignore 'empty' audio_holders | |
48 print "WARNING: " + file + " contains empty audio holder. (comment_parser.py)" | |
49 break | |
50 | |
51 # create folder [page_name] if not yet created | |
52 if not os.path.exists(folder_name + "/" + page_name): | |
53 os.makedirs(folder_name + "/" + page_name) | |
54 | |
55 # for page [page_name], print comments related to fragment [id] | |
56 for audioelement in audioholder.findall("./audioelement"): | |
57 if audioelement is not None: # Check it exists | |
58 audio_id = str(audioelement.get('id')) | |
59 | |
60 csv_name = folder_name +'/' + page_name+'/'+page_name+'-comments-'+audio_id+'.csv' | |
61 | |
62 # If file hasn't been opened yet this time, empty | |
63 if csv_name not in file_history: | |
64 csvfile = open(csv_name, 'w') | |
65 file_history.append(csv_name) # remember this file has been written to this time around | |
66 else: | |
67 # append (!) to file [page_name]/[page_name]-comments-[id].csv | |
68 csvfile = open(csv_name, 'a') | |
69 writer = csv.writer(csvfile, | |
70 delimiter=',', | |
71 dialect="excel", | |
72 quoting=csv.QUOTE_ALL) | |
73 commentstr = audioelement.find("./comment/response").text | |
74 | |
75 if commentstr is None: | |
76 commentstr = '' | |
77 | |
78 # anonymous comments: | |
79 #writer.writerow([commentstr.encode("utf-8")]) | |
80 # comments with (file) name: | |
81 writer.writerow([file[:-4]] + [commentstr.encode("utf-8")]) | |
82 | |
83 #TODO Replace 'new line' in comment with something else? | |
84 | |
85 # PRO TIP: Change from csv to txt by running this in bash: | |
86 # $ cd folder_where_csvs_are/ | |
87 # $ for i in *.csv; do mv "$i" "${i/.csv}".txt; done |