annotate scripts/comment_parser.py @ 1118:3edcbbea168b

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