comparison scripts/comment_parser.py @ 883:cd20f076f6a3

Readme: reference to issue tracker; Scripts: comment_parser and score_parser read and write in '/saves' from 'scripts/', condensing XML queries (references from children instead of from root)
author Brecht De Man <BrechtDeMan@users.noreply.github.com>
date Mon, 29 Jun 2015 13:15:00 +0100
parents 24d0d3111c00
children 7a8fcf04aad3
comparison
equal deleted inserted replaced
882:594e617b75ad 883:cd20f076f6a3
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 7
8 # XML results files location (modify as needed):
9 folder_name = "../saves" # Looks in 'saves/' folder from 'scripts/' folder
10
8 # get every XML file in folder 11 # get every XML file in folder
9 for file in os.listdir("."): # You have to put this script in folder where output XML files are. 12 for file in os.listdir(folder_name):
10 if file.endswith(".xml"): 13 if file.endswith(".xml"):
11 tree = ET.parse(file) 14 tree = ET.parse(folder_name + '/' + file)
12 root = tree.getroot() 15 root = tree.getroot()
13 16
14 # get list of all page names 17 # get list of all page names
15 for audioholder in root.findall("./audioholder"): # iterate over pages 18 for audioholder in root.findall("./audioholder"): # iterate over pages
16 page_name = audioholder.get('id') # get page name 19 page_name = audioholder.get('id') # get page name
17 20
18 if page_name is None: # ignore 'empty' audio_holders 21 if page_name is None: # ignore 'empty' audio_holders
19 break 22 break
20 23
21 # create folder [page_name] if not yet created 24 # create folder [page_name] if not yet created
22 if not os.path.exists(page_name): 25 if not os.path.exists(folder_name + "/" + page_name):
23 os.makedirs(page_name) 26 os.makedirs(folder_name + "/" + page_name)
24 27
25 # for page [page_name], print comments related to fragment [id] 28 # for page [page_name], print comments related to fragment [id]
26 for audioelement in root.findall("*/[@id='"+page_name+"']/audioelement"): #TODO in audioholder.findall(...) 29 for audioelement in audioholder.findall("./audioelement"):
27 if audioelement is not None: # Check it exists 30 if audioelement is not None: # Check it exists
28 audio_id = str(audioelement.get('id')) 31 audio_id = str(audioelement.get('id'))
29 32
30 33 csv_name = folder_name +'/' + page_name+'/'+page_name+'-comments-'+audio_id+'.csv'
31 csv_name = page_name+'/'+page_name+'-comments-'+audio_id+'.csv'
32 34
33 # append (!) to file [page_name]/[page_name]-comments-[id].csv 35 # append (!) to file [page_name]/[page_name]-comments-[id].csv
34 with open(csv_name, 'a') as csvfile: 36 with open(csv_name, 'a') as csvfile:
35 writer = csv.writer(csvfile, 37 writer = csv.writer(csvfile,
36 delimiter=',', 38 delimiter=',',
37 dialect="excel", 39 dialect="excel",
38 quoting=csv.QUOTE_ALL) 40 quoting=csv.QUOTE_ALL)
39 commentstr = root.find("*/[@id='" 41 commentstr = audioelement.find("./comment/response").text
40 + page_name 42
41 + "']/audioelement/[@id='"
42 + audio_id
43 + "']/comment/response").text
44 if commentstr is None: 43 if commentstr is None:
45 writer.writerow(['']) 44 commentstr = '';
46 else: 45
47 # anonymous comments: 46 # anonymous comments:
48 writer.writerow([commentstr.encode("utf-8")]) 47 #writer.writerow([commentstr.encode("utf-8")])
49 # comments with (file) name: 48 # comments with (file) name:
50 #writer.writerow([file[:-4]] + [commentstr.encode("utf-8")]) 49 writer.writerow([file[:-4]] + [commentstr.encode("utf-8")])
51 50
52 #TODO Replace 'new line' in comment with something else? 51 #TODO Replace 'new line' in comment with something else?
53 52
54 # PRO TIP: Change from csv to txt by running this in bash: 53 # PRO TIP: Change from csv to txt by running this in bash:
55 # $ cd folder_where_csvs_are/ 54 # $ cd folder_where_csvs_are/