nicholas@2744
|
1 #!/usr/bin/python
|
nicholas@2744
|
2
|
nicholas@2744
|
3 import xml.etree.ElementTree as ET
|
nicholas@2744
|
4 import os
|
nicholas@2744
|
5 import sys
|
nicholas@2744
|
6 import csv
|
nicholas@2744
|
7
|
nicholas@2744
|
8 # COMMAND LINE ARGUMENTS
|
nicholas@2744
|
9
|
nicholas@2744
|
10 assert len(sys.argv)<3, "commentquestion_parser takes at most 1 command line argument\nUse: python commentquestion_parser.py [rating_folder_location]"
|
nicholas@2744
|
11
|
nicholas@2744
|
12 # XML results files location
|
nicholas@2744
|
13 if len(sys.argv) == 1:
|
nicholas@2744
|
14 folder_name = "../saves" # Looks in 'saves/' folder from 'scripts/' folder
|
nicholas@2744
|
15 print("Use: python commentquestion_parser.py [rating_folder_location]")
|
nicholas@2744
|
16 print("Using default path: " + folder_name)
|
nicholas@2744
|
17 elif len(sys.argv) == 2:
|
nicholas@2744
|
18 folder_name = sys.argv[1] # First command line argument is folder
|
nicholas@2744
|
19
|
nicholas@2744
|
20 # check if folder_name exists
|
nicholas@2744
|
21 if not os.path.exists(folder_name):
|
nicholas@2744
|
22 #the file is not there
|
nicholas@2744
|
23 print("Folder '"+folder_name+"' does not exist.")
|
nicholas@2744
|
24 sys.exit() # terminate script execution
|
nicholas@2744
|
25 elif not os.access(os.path.dirname(folder_name), os.W_OK):
|
nicholas@2744
|
26 #the file does exist but write privileges are not given
|
nicholas@2744
|
27 print("No write privileges in folder '"+folder_name+"'.")
|
nicholas@2744
|
28
|
nicholas@2744
|
29 # create folder 'ratings' if not yet created
|
nicholas@2745
|
30 if not os.path.exists(folder_name + '/comments'):
|
nicholas@2745
|
31 os.makedirs(folder_name + '/comments')
|
nicholas@2744
|
32
|
nicholas@2744
|
33 pagestore = {}
|
nicholas@2744
|
34
|
nicholas@2744
|
35 for filename in os.listdir(folder_name):
|
nicholas@2744
|
36 if (filename.endswith(".xml")):
|
nicholas@2745
|
37 tree = ET.parse(folder_name + '/' + filename)
|
nicholas@2744
|
38 root = tree.getroot()
|
nicholas@2744
|
39
|
nicholas@2744
|
40 subject_id = root.get('key');
|
nicholas@2744
|
41
|
nicholas@2744
|
42 # get the list of pages
|
nicholas@2744
|
43 for page in root.findall("./page"):
|
nicholas@2744
|
44 pagename = page.get("ref")
|
me@2991
|
45 trackname = page.find('audioelement').get('ref')
|
nicholas@2744
|
46 if pagename is None: # ignore 'empty' audio_holders
|
nicholas@2744
|
47 print("WARNING: " + filename + " contains empty audio holder. (commentquestion_parser.py)")
|
nicholas@2744
|
48 break
|
nicholas@2744
|
49
|
nicholas@2744
|
50 if page.get('state') != "complete":
|
nicholas@2744
|
51 print("WARNING: " + filename + " contains incomplete page " +pagename+ ". (commentquestion_parser.py)")
|
nicholas@2744
|
52 break
|
nicholas@2744
|
53 try:
|
nicholas@2744
|
54 questionStore = pagestore[pagename]
|
nicholas@2745
|
55 except KeyError:
|
nicholas@2744
|
56 questionStore = {}
|
nicholas@2744
|
57 pagestore[pagename] = questionStore
|
nicholas@2744
|
58
|
nicholas@2744
|
59 for cq in page.findall("./comment"):
|
nicholas@2744
|
60 cqid = cq.get("id");
|
nicholas@2744
|
61 response = cq.find("./response").text
|
nicholas@2744
|
62 try:
|
nicholas@2744
|
63 commentStore = questionStore[cqid]
|
nicholas@2745
|
64 except KeyError:
|
nicholas@2744
|
65 commentStore = [];
|
nicholas@2744
|
66 questionStore[cqid] = commentStore
|
me@2991
|
67 commentStore.append({"subject": subject_id, "value": response, "trackName": trackname})
|
nicholas@2745
|
68
|
nicholas@2745
|
69 for page in pagestore.keys():
|
nicholas@2745
|
70 print page
|
nicholas@2745
|
71 pagedir = folder_name + '/comments/'+page
|
nicholas@2745
|
72 if not os.path.exists(pagedir):
|
nicholas@2745
|
73 os.makedirs(pagedir)
|
nicholas@2745
|
74 for comment in pagestore[page].keys():
|
nicholas@2745
|
75 with open(pagedir+"/"+comment+".csv", "w") as csvfile:
|
nicholas@2745
|
76 filewriter = csv.writer(csvfile, delimiter=',')
|
me@2991
|
77 filewriter.writerow(("save_id", "value", "trackName"))
|
nicholas@2745
|
78 for entry in pagestore[page][comment]:
|
me@2991
|
79 filewriter.writerow((entry["subject"], entry["value"], entry["trackName"])) |