annotate scripts/comment_parser.py @ 1533:317faa29ab11

Feature Feature #1270: Outside reference object added. Some small bugs and GUI to solve.
author Nicholas Jillings <nickjillings@users.noreply.github.com>
date Thu, 23 Jul 2015 09:37:27 +0100
parents 766bff1a8f73
children 7b0ce3a9ddc1 99cb3436759e
rev   line source
nickjillings@1528 1 #!/usr/bin/python
nickjillings@1528 2 # -*- coding: utf-8 -*-
nickjillings@1528 3
nickjillings@1528 4 import xml.etree.ElementTree as ET
nickjillings@1528 5 import os
nickjillings@1528 6 import csv
nickjillings@1528 7
nickjillings@1528 8 # XML results files location (modify as needed):
nickjillings@1528 9 folder_name = "../saves" # Looks in 'saves/' folder from 'scripts/' folder
nickjillings@1528 10
nickjillings@1528 11 # get every XML file in folder
nickjillings@1528 12 for file in os.listdir(folder_name):
nickjillings@1528 13 if file.endswith(".xml"):
nickjillings@1528 14 tree = ET.parse(folder_name + '/' + file)
nickjillings@1528 15 root = tree.getroot()
nickjillings@1528 16
nickjillings@1528 17 # get list of all page names
nickjillings@1528 18 for audioholder in root.findall("./audioholder"): # iterate over pages
nickjillings@1528 19 page_name = audioholder.get('id') # get page name
nickjillings@1528 20
nickjillings@1528 21 if page_name is None: # ignore 'empty' audio_holders
nickjillings@1528 22 print "WARNING: " + file + " contains empty audio holder. (comment_parser.py)"
nickjillings@1528 23 break
nickjillings@1528 24
nickjillings@1528 25 # create folder [page_name] if not yet created
nickjillings@1528 26 if not os.path.exists(folder_name + "/" + page_name):
nickjillings@1528 27 os.makedirs(folder_name + "/" + page_name)
nickjillings@1528 28
nickjillings@1528 29 # for page [page_name], print comments related to fragment [id]
nickjillings@1528 30 for audioelement in audioholder.findall("./audioelement"):
nickjillings@1528 31 if audioelement is not None: # Check it exists
nickjillings@1528 32 audio_id = str(audioelement.get('id'))
nickjillings@1528 33
nickjillings@1528 34 csv_name = folder_name +'/' + page_name+'/'+page_name+'-comments-'+audio_id+'.csv'
nickjillings@1528 35
nickjillings@1528 36 # append (!) to file [page_name]/[page_name]-comments-[id].csv
nickjillings@1528 37 with open(csv_name, 'a') as csvfile:
nickjillings@1528 38 writer = csv.writer(csvfile,
nickjillings@1528 39 delimiter=',',
nickjillings@1528 40 dialect="excel",
nickjillings@1528 41 quoting=csv.QUOTE_ALL)
nickjillings@1528 42 commentstr = audioelement.find("./comment/response").text
nickjillings@1528 43
nickjillings@1528 44 if commentstr is None:
nickjillings@1528 45 commentstr = '';
nickjillings@1528 46
nickjillings@1528 47 # anonymous comments:
nickjillings@1528 48 #writer.writerow([commentstr.encode("utf-8")])
nickjillings@1528 49 # comments with (file) name:
nickjillings@1528 50 writer.writerow([file[:-4]] + [commentstr.encode("utf-8")])
nickjillings@1528 51
nickjillings@1528 52 #TODO Replace 'new line' in comment with something else?
nickjillings@1528 53
nickjillings@1528 54 # PRO TIP: Change from csv to txt by running this in bash:
nickjillings@1528 55 # $ cd folder_where_csvs_are/
nickjillings@1528 56 # $ for i in *.csv; do mv "$i" "${i/.csv}".txt; done