comparison scripts/comment_parser.py @ 1457:c8a9825aaa05

Merge from branch "WAC2016"
author Nicholas Jillings <nickjillings@users.noreply.github.com>
date Mon, 23 Nov 2015 09:13:12 +0000
parents 04e8a9c07c7e
children 1b6fa37d46a4
comparison
equal deleted inserted replaced
1456:cacd98e7e1ff 1457:c8a9825aaa05
2 # -*- coding: utf-8 -*- 2 # -*- coding: utf-8 -*-
3 3
4 import xml.etree.ElementTree as ET 4 import xml.etree.ElementTree as ET
5 import os 5 import os
6 import csv 6 import csv
7 import sys
7 8
8 # XML results files location (modify as needed): 9 # COMMAND LINE ARGUMENTS
9 folder_name = "../saves" # Looks in 'saves/' folder from 'scripts/' folder 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 = []
10 36
11 # get every XML file in folder 37 # get every XML file in folder
12 for file in os.listdir(folder_name): 38 for file in os.listdir(folder_name):
13 if file.endswith(".xml"): 39 if file.endswith(".xml"):
14 tree = ET.parse(folder_name + '/' + file) 40 tree = ET.parse(folder_name + '/' + file)
31 if audioelement is not None: # Check it exists 57 if audioelement is not None: # Check it exists
32 audio_id = str(audioelement.get('id')) 58 audio_id = str(audioelement.get('id'))
33 59
34 csv_name = folder_name +'/' + page_name+'/'+page_name+'-comments-'+audio_id+'.csv' 60 csv_name = folder_name +'/' + page_name+'/'+page_name+'-comments-'+audio_id+'.csv'
35 61
36 # append (!) to file [page_name]/[page_name]-comments-[id].csv 62 # If file hasn't been opened yet this time, empty
37 with open(csv_name, 'a') as csvfile: 63 if csv_name not in file_history:
38 writer = csv.writer(csvfile, 64 csvfile = open(csv_name, 'w')
39 delimiter=',', 65 file_history.append(csv_name) # remember this file has been written to this time around
40 dialect="excel", 66 else:
41 quoting=csv.QUOTE_ALL) 67 # append (!) to file [page_name]/[page_name]-comments-[id].csv
42 commentstr = audioelement.find("./comment/response").text 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
43 74
44 if commentstr is None: 75 if commentstr is None:
45 commentstr = ''; 76 commentstr = ''
46 77
47 # anonymous comments: 78 # anonymous comments:
48 #writer.writerow([commentstr.encode("utf-8")]) 79 #writer.writerow([commentstr.encode("utf-8")])
49 # comments with (file) name: 80 # comments with (file) name:
50 writer.writerow([file[:-4]] + [commentstr.encode("utf-8")]) 81 writer.writerow([file[:-4]] + [commentstr.encode("utf-8")])
51 82
52 #TODO Replace 'new line' in comment with something else? 83 #TODO Replace 'new line' in comment with something else?
53 84
54 # PRO TIP: Change from csv to txt by running this in bash: 85 # PRO TIP: Change from csv to txt by running this in bash:
55 # $ cd folder_where_csvs_are/ 86 # $ cd folder_where_csvs_are/
56 # $ for i in *.csv; do mv "$i" "${i/.csv}".txt; done 87 # $ for i in *.csv; do mv "$i" "${i/.csv}".txt; done