Mercurial > hg > modal-synthesis-of-weapon-sounds
diff Perceptual Evaluation/webaudioevaluationtool/scripts/comment_parser.py @ 0:55c282f01a30 tip
Adding files to Repo. Initial Commit
author | Dave <d.j.moffat@qmul.ac.uk> |
---|---|
date | Fri, 16 Oct 2015 18:04:00 +0100 |
parents | |
children |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Perceptual Evaluation/webaudioevaluationtool/scripts/comment_parser.py Fri Oct 16 18:04:00 2015 +0100 @@ -0,0 +1,56 @@ +#!/usr/bin/python +# -*- coding: utf-8 -*- + +import xml.etree.ElementTree as ET +import os +import csv + +# XML results files location (modify as needed): +folder_name = "../saves" # Looks in 'saves/' folder from 'scripts/' folder + +# get every XML file in folder +for file in os.listdir(folder_name): + if file.endswith(".xml"): + tree = ET.parse(folder_name + '/' + file) + root = tree.getroot() + + # get list of all page names + for audioholder in root.findall("./audioholder"): # iterate over pages + page_name = audioholder.get('id') # get page name + + if page_name is None: # ignore 'empty' audio_holders + print "WARNING: " + file + " contains empty audio holder. (comment_parser.py)" + break + + # create folder [page_name] if not yet created + if not os.path.exists(folder_name + "/" + page_name): + os.makedirs(folder_name + "/" + page_name) + + # for page [page_name], print comments related to fragment [id] + for audioelement in audioholder.findall("./audioelement"): + if audioelement is not None: # Check it exists + audio_id = str(audioelement.get('id')) + + csv_name = folder_name +'/' + page_name+'/'+page_name+'-comments-'+audio_id+'.csv' + + # append (!) to file [page_name]/[page_name]-comments-[id].csv + with open(csv_name, 'a') as csvfile: + writer = csv.writer(csvfile, + delimiter=',', + dialect="excel", + quoting=csv.QUOTE_ALL) + commentstr = audioelement.find("./comment/response").text + + if commentstr is None: + commentstr = ''; + + # anonymous comments: + #writer.writerow([commentstr.encode("utf-8")]) + # comments with (file) name: + writer.writerow([file[:-4]] + [commentstr.encode("utf-8")]) + + #TODO Replace 'new line' in comment with something else? + +# PRO TIP: Change from csv to txt by running this in bash: +# $ cd folder_where_csvs_are/ +# $ for i in *.csv; do mv "$i" "${i/.csv}".txt; done