annotate scripts/comment_parser.py @ 789:3539d6c992e4

Feature #1478: <audioElements> have a gain attribute, in decibels, which controls the playback gain in that page.
author Nicholas Jillings <n.g.r.jillings@se14.qmul.ac.uk>
date Fri, 11 Dec 2015 17:33:14 +0000
parents 519baf8648a9
children 1b6fa37d46a4 235594325b84 b5bf2f57187c
rev   line source
nicholas@763 1 #!/usr/bin/python
nicholas@763 2 # -*- coding: utf-8 -*-
nicholas@763 3
nicholas@763 4 import xml.etree.ElementTree as ET
nicholas@763 5 import os
nicholas@763 6 import csv
nicholas@763 7 import sys
nicholas@763 8
nicholas@763 9 # COMMAND LINE ARGUMENTS
nicholas@763 10
nicholas@763 11 assert len(sys.argv)<3, "comment_parser takes at most 1 command line argument\n"+\
nicholas@763 12 "Use: python score_parser.py [rating_folder_location]"
nicholas@763 13
nicholas@763 14 # XML results files location
nicholas@763 15 if len(sys.argv) == 1:
nicholas@763 16 folder_name = "../saves" # Looks in 'saves/' folder from 'scripts/' folder
nicholas@763 17 print "Use: python comment_parser.py [XML_files_location]"
nicholas@763 18 print "Using default path: " + folder_name
nicholas@763 19 elif len(sys.argv) == 2:
nicholas@763 20 folder_name = sys.argv[1] # First command line argument is folder
nicholas@763 21
nicholas@763 22 # check if folder_name exists
nicholas@763 23 if not os.path.exists(folder_name):
nicholas@763 24 #the file is not there
nicholas@763 25 print "Folder '"+folder_name+"' does not exist."
nicholas@763 26 sys.exit() # terminate script execution
nicholas@763 27 elif not os.access(os.path.dirname(folder_name), os.W_OK):
nicholas@763 28 #the file does exist but write privileges are not given
nicholas@763 29 print "No write privileges in folder '"+folder_name+"'."
nicholas@763 30
nicholas@763 31
nicholas@763 32 # CODE
nicholas@763 33
nicholas@763 34 # remember which files have been opened this time
nicholas@763 35 file_history = []
nicholas@763 36
nicholas@763 37 # get every XML file in folder
nicholas@763 38 for file in os.listdir(folder_name):
nicholas@763 39 if file.endswith(".xml"):
nicholas@763 40 tree = ET.parse(folder_name + '/' + file)
nicholas@763 41 root = tree.getroot()
nicholas@763 42
nicholas@763 43 # get list of all page names
nicholas@763 44 for audioholder in root.findall("./audioholder"): # iterate over pages
nicholas@763 45 page_name = audioholder.get('id') # get page name
nicholas@763 46
nicholas@763 47 if page_name is None: # ignore 'empty' audio_holders
nicholas@763 48 print "WARNING: " + file + " contains empty audio holder. (comment_parser.py)"
nicholas@763 49 break
nicholas@763 50
nicholas@763 51 # create folder [page_name] if not yet created
nicholas@763 52 if not os.path.exists(folder_name + "/" + page_name):
nicholas@763 53 os.makedirs(folder_name + "/" + page_name)
nicholas@763 54
nicholas@763 55 # for page [page_name], print comments related to fragment [id]
nicholas@763 56 for audioelement in audioholder.findall("./audioelement"):
nicholas@763 57 if audioelement is not None: # Check it exists
nicholas@763 58 audio_id = str(audioelement.get('id'))
nicholas@763 59
nicholas@763 60 csv_name = folder_name +'/' + page_name+'/'+page_name+'-comments-'+audio_id+'.csv'
nicholas@763 61
nicholas@763 62 # If file hasn't been opened yet this time, empty
nicholas@763 63 if csv_name not in file_history:
nicholas@763 64 csvfile = open(csv_name, 'w')
nicholas@763 65 file_history.append(csv_name) # remember this file has been written to this time around
nicholas@763 66 else:
nicholas@763 67 # append (!) to file [page_name]/[page_name]-comments-[id].csv
nicholas@763 68 csvfile = open(csv_name, 'a')
nicholas@763 69 writer = csv.writer(csvfile,
nicholas@763 70 delimiter=',',
nicholas@763 71 dialect="excel",
nicholas@763 72 quoting=csv.QUOTE_ALL)
nicholas@763 73 commentstr = audioelement.find("./comment/response").text
nicholas@763 74
nicholas@763 75 if commentstr is None:
nicholas@763 76 commentstr = ''
nicholas@763 77
nicholas@763 78 # anonymous comments:
nicholas@763 79 #writer.writerow([commentstr.encode("utf-8")])
nicholas@763 80 # comments with (file) name:
nicholas@763 81 writer.writerow([file[:-4]] + [commentstr.encode("utf-8")])
nicholas@763 82
nicholas@763 83 #TODO Replace 'new line' in comment with something else?
nicholas@763 84
nicholas@763 85 # PRO TIP: Change from csv to txt by running this in bash:
nicholas@763 86 # $ cd folder_where_csvs_are/
nicholas@763 87 # $ for i in *.csv; do mv "$i" "${i/.csv}".txt; done