annotate python/commentquestion_parser.py @ 3063:6b95437ae672

Close #109
author Nicholas Jillings <nicholas.jillings@mail.bcu.ac.uk>
date Tue, 31 Oct 2017 16:21:58 +0000
parents 44a6a61de7fc
children 77114de617ba
rev   line source
nicholas@2744 1 #!/usr/bin/python
nicholas@2744 2
nicholas@2744 3 import xml.etree.ElementTree as ET
nicholas@2744 4 import os
nicholas@2744 5 import sys
nicholas@2744 6 import csv
nicholas@2744 7
nicholas@2744 8 # COMMAND LINE ARGUMENTS
nicholas@2744 9
nicholas@2744 10 assert len(sys.argv)<3, "commentquestion_parser takes at most 1 command line argument\nUse: python commentquestion_parser.py [rating_folder_location]"
nicholas@2744 11
nicholas@2744 12 # XML results files location
nicholas@2744 13 if len(sys.argv) == 1:
nicholas@2744 14 folder_name = "../saves" # Looks in 'saves/' folder from 'scripts/' folder
nicholas@2744 15 print("Use: python commentquestion_parser.py [rating_folder_location]")
nicholas@2744 16 print("Using default path: " + folder_name)
nicholas@2744 17 elif len(sys.argv) == 2:
nicholas@2744 18 folder_name = sys.argv[1] # First command line argument is folder
nicholas@2744 19
nicholas@2744 20 # check if folder_name exists
nicholas@2744 21 if not os.path.exists(folder_name):
nicholas@2744 22 #the file is not there
nicholas@2744 23 print("Folder '"+folder_name+"' does not exist.")
nicholas@2744 24 sys.exit() # terminate script execution
nicholas@2744 25 elif not os.access(os.path.dirname(folder_name), os.W_OK):
nicholas@2744 26 #the file does exist but write privileges are not given
nicholas@2744 27 print("No write privileges in folder '"+folder_name+"'.")
nicholas@2744 28
nicholas@2744 29 # create folder 'ratings' if not yet created
nicholas@2745 30 if not os.path.exists(folder_name + '/comments'):
nicholas@2745 31 os.makedirs(folder_name + '/comments')
nicholas@2744 32
nicholas@2744 33 pagestore = {}
nicholas@2744 34
nicholas@2744 35 for filename in os.listdir(folder_name):
nicholas@2744 36 if (filename.endswith(".xml")):
nicholas@2745 37 tree = ET.parse(folder_name + '/' + filename)
nicholas@2744 38 root = tree.getroot()
nicholas@2744 39
nicholas@2744 40 subject_id = root.get('key');
nicholas@2744 41
nicholas@2744 42 # get the list of pages
nicholas@2744 43 for page in root.findall("./page"):
nicholas@2744 44 pagename = page.get("ref")
nicholas@2744 45 if pagename is None: # ignore 'empty' audio_holders
nicholas@2744 46 print("WARNING: " + filename + " contains empty audio holder. (commentquestion_parser.py)")
nicholas@2744 47 break
nicholas@2744 48
nicholas@2744 49 if page.get('state') != "complete":
nicholas@2744 50 print("WARNING: " + filename + " contains incomplete page " +pagename+ ". (commentquestion_parser.py)")
nicholas@2744 51 break
nicholas@2744 52 try:
nicholas@2744 53 questionStore = pagestore[pagename]
nicholas@2745 54 except KeyError:
nicholas@2744 55 questionStore = {}
nicholas@2744 56 pagestore[pagename] = questionStore
nicholas@2744 57
nicholas@2744 58 for cq in page.findall("./comment"):
nicholas@2744 59 cqid = cq.get("id");
nicholas@2744 60 response = cq.find("./response").text
nicholas@2744 61 try:
nicholas@2744 62 commentStore = questionStore[cqid]
nicholas@2745 63 except KeyError:
nicholas@2744 64 commentStore = [];
nicholas@2744 65 questionStore[cqid] = commentStore
nicholas@2744 66 commentStore.append({"subject": subject_id, "value": response})
nicholas@2745 67
nicholas@2745 68 for page in pagestore.keys():
nicholas@2745 69 print page
nicholas@2745 70 pagedir = folder_name + '/comments/'+page
nicholas@2745 71 if not os.path.exists(pagedir):
nicholas@2745 72 os.makedirs(pagedir)
nicholas@2745 73 for comment in pagestore[page].keys():
nicholas@2745 74 with open(pagedir+"/"+comment+".csv", "w") as csvfile:
nicholas@2745 75 filewriter = csv.writer(csvfile, delimiter=',')
nicholas@2745 76 filewriter.writerow(("save_id", "value"))
nicholas@2745 77 for entry in pagestore[page][comment]:
nicholas@2745 78 filewriter.writerow((entry["subject"], entry["value"]))