Mercurial > hg > webaudioevaluationtool
annotate scripts/comment_parser.py @ 927:5db0069046d5
Comment parsing: automatically detect which songs - no need to manually enter song IDs
author | Brecht De Man <BrechtDeMan@users.noreply.github.com> |
---|---|
date | Sat, 30 May 2015 18:12:32 +0100 |
parents | |
children | 927d05a43a70 |
rev | line source |
---|---|
BrechtDeMan@927 | 1 import xml.etree.ElementTree as ET |
BrechtDeMan@927 | 2 import os |
BrechtDeMan@927 | 3 import csv |
BrechtDeMan@927 | 4 |
BrechtDeMan@927 | 5 # get every XML file in folder |
BrechtDeMan@927 | 6 for file in os.listdir("."): # You have to put this script in folder where output XML files are. |
BrechtDeMan@927 | 7 if file.endswith(".xml"): |
BrechtDeMan@927 | 8 tree = ET.parse(file) |
BrechtDeMan@927 | 9 root = tree.getroot() |
BrechtDeMan@927 | 10 |
BrechtDeMan@927 | 11 # get list of all songs |
BrechtDeMan@927 | 12 for audioholder in root.findall("./audioholder"): # iterate over songs |
BrechtDeMan@927 | 13 song_name = audioholder.get('id') # get song name |
BrechtDeMan@927 | 14 |
BrechtDeMan@927 | 15 # create folder [song_name] if not yet created |
BrechtDeMan@927 | 16 if not os.path.exists(song_name): |
BrechtDeMan@927 | 17 os.makedirs(song_name) |
BrechtDeMan@927 | 18 |
BrechtDeMan@927 | 19 # for song [song_name], print comments related to mix [id] |
BrechtDeMan@927 | 20 for audioelement in root.findall("*/[@id='"+song_name+"']/audioelement"): |
BrechtDeMan@927 | 21 audio_id = str(audioelement.get('id')) |
BrechtDeMan@927 | 22 # append to file [song_name]/[song_name]-comments-[id].csv |
BrechtDeMan@927 | 23 with open(song_name+'/'+song_name+'-comments-'+audio_id+'.csv', 'a') as csvfile: |
BrechtDeMan@927 | 24 commentstr = root.find("*/[@id='" |
BrechtDeMan@927 | 25 + song_name |
BrechtDeMan@927 | 26 + "']/audioelement/[@id='" |
BrechtDeMan@927 | 27 + audio_id |
BrechtDeMan@927 | 28 + "']/comment/response").text |
BrechtDeMan@927 | 29 writer = csv.writer(csvfile, delimiter=',') |
BrechtDeMan@927 | 30 writer.writerow([commentstr.encode("utf-8")]) |
BrechtDeMan@927 | 31 #TODO Comma doesn't act as delimiter now! |
BrechtDeMan@927 | 32 # (when adding more than just a comment per line): |
BrechtDeMan@927 | 33 # writer.writerow([file + ',' + commentstr.encode("utf-8")]) |
BrechtDeMan@927 | 34 |
BrechtDeMan@927 | 35 #TODO Replace 'new line' with something else? |
BrechtDeMan@927 | 36 |
BrechtDeMan@927 | 37 #TODO 'Append' means duplicate entries if run several times... |