Mercurial > hg > waet-hammond-1
comparison new/python/comment_parser.py @ 15:853caf8cd74b
Update
author | Giulio Moro <giuliomoro@yahoo.it> |
---|---|
date | Wed, 04 May 2016 17:25:19 +0100 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
14:f63604ce8f21 | 15:853caf8cd74b |
---|---|
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("./page"): # iterate over pages | |
45 page_name = audioholder.get('ref') # get page name | |
46 | |
47 if page_name is None: # ignore 'empty' audio_holders | |
48 print("WARNING: " + file + " contains empty page. (comment_parser.py)") | |
49 break | |
50 | |
51 if audioholder.get("state") != "complete": | |
52 print("WARNING: " + file + "test page " + page_name + " is not complete, skipping.") | |
53 else: | |
54 # create folder [page_name] if not yet created | |
55 if not os.path.exists(folder_name + "/" + page_name): | |
56 os.makedirs(folder_name + "/" + page_name) | |
57 | |
58 # for page [page_name], print comments related to fragment [id] | |
59 for audioelement in audioholder.findall("./audioelement"): | |
60 if audioelement is not None: # Check it exists | |
61 audio_id = str(audioelement.get('ref')) | |
62 | |
63 csv_name = folder_name +'/' + page_name+'/'+page_name+'-comments-'+audio_id+'.csv' | |
64 | |
65 # If file hasn't been opened yet this time, empty | |
66 if csv_name not in file_history: | |
67 csvfile = open(csv_name, 'w') | |
68 file_history.append(csv_name) # remember this file has been written to this time around | |
69 else: | |
70 # append (!) to file [page_name]/[page_name]-comments-[id].csv | |
71 csvfile = open(csv_name, 'a') | |
72 writer = csv.writer(csvfile, | |
73 delimiter=',', | |
74 dialect="excel", | |
75 quoting=csv.QUOTE_ALL) | |
76 commentstr = audioelement.find("./comment/response").text | |
77 | |
78 if commentstr is None: | |
79 commentstr = '' | |
80 | |
81 # anonymous comments: | |
82 #writer.writerow([commentstr]) # .encode("utf-8") | |
83 # comments with (file) name: | |
84 writer.writerow([file[:-4]] + [commentstr]) | |
85 | |
86 #TODO Replace 'new line' in comment with something else? | |
87 | |
88 # PRO TIP: Change from csv to txt by running this in bash: | |
89 # $ cd folder_where_csvs_are/ | |
90 # $ for i in *.csv; do mv "$i" "${i/.csv}".txt; done |