annotate scripts/comment_parser.py @ 2248:9c7f09d3364d

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