nicholas@2744: #!/usr/bin/python nicholas@2744: nicholas@2744: import xml.etree.ElementTree as ET nicholas@2744: import os nicholas@2744: import sys nicholas@2744: import csv nicholas@2744: nicholas@2744: # COMMAND LINE ARGUMENTS nicholas@2744: nicholas@2744: assert len(sys.argv)<3, "commentquestion_parser takes at most 1 command line argument\nUse: python commentquestion_parser.py [rating_folder_location]" nicholas@2744: nicholas@2744: # XML results files location nicholas@2744: if len(sys.argv) == 1: nicholas@2744: folder_name = "../saves" # Looks in 'saves/' folder from 'scripts/' folder nicholas@2744: print("Use: python commentquestion_parser.py [rating_folder_location]") nicholas@2744: print("Using default path: " + folder_name) nicholas@2744: elif len(sys.argv) == 2: nicholas@2744: folder_name = sys.argv[1] # First command line argument is folder nicholas@2744: nicholas@2744: # check if folder_name exists nicholas@2744: if not os.path.exists(folder_name): nicholas@2744: #the file is not there nicholas@2744: print("Folder '"+folder_name+"' does not exist.") nicholas@2744: sys.exit() # terminate script execution nicholas@2744: elif not os.access(os.path.dirname(folder_name), os.W_OK): nicholas@2744: #the file does exist but write privileges are not given nicholas@2744: print("No write privileges in folder '"+folder_name+"'.") nicholas@2744: nicholas@2744: # create folder 'ratings' if not yet created nicholas@2745: if not os.path.exists(folder_name + '/comments'): nicholas@2745: os.makedirs(folder_name + '/comments') nicholas@2744: nicholas@2744: pagestore = {} nicholas@2744: nicholas@2744: for filename in os.listdir(folder_name): nicholas@2744: if (filename.endswith(".xml")): nicholas@2745: tree = ET.parse(folder_name + '/' + filename) nicholas@2744: root = tree.getroot() nicholas@2744: nicholas@2744: subject_id = root.get('key'); nicholas@2744: nicholas@2744: # get the list of pages nicholas@2744: for page in root.findall("./page"): nicholas@2744: pagename = page.get("ref") me@2991: trackname = page.find('audioelement').get('ref') nicholas@2744: if pagename is None: # ignore 'empty' audio_holders nicholas@2744: print("WARNING: " + filename + " contains empty audio holder. (commentquestion_parser.py)") nicholas@2744: break nicholas@2744: nicholas@2744: if page.get('state') != "complete": nicholas@2744: print("WARNING: " + filename + " contains incomplete page " +pagename+ ". (commentquestion_parser.py)") nicholas@2744: break nicholas@2744: try: nicholas@2744: questionStore = pagestore[pagename] nicholas@2745: except KeyError: nicholas@2744: questionStore = {} nicholas@2744: pagestore[pagename] = questionStore nicholas@2744: nicholas@2744: for cq in page.findall("./comment"): nicholas@2744: cqid = cq.get("id"); nicholas@2744: response = cq.find("./response").text nicholas@2744: try: nicholas@2744: commentStore = questionStore[cqid] nicholas@2745: except KeyError: nicholas@2744: commentStore = []; nicholas@2744: questionStore[cqid] = commentStore me@2991: commentStore.append({"subject": subject_id, "value": response, "trackName": trackname}) nicholas@2745: nicholas@2745: for page in pagestore.keys(): nicholas@2745: print page nicholas@2745: pagedir = folder_name + '/comments/'+page nicholas@2745: if not os.path.exists(pagedir): nicholas@2745: os.makedirs(pagedir) nicholas@2745: for comment in pagestore[page].keys(): nicholas@2745: with open(pagedir+"/"+comment+".csv", "w") as csvfile: nicholas@2745: filewriter = csv.writer(csvfile, delimiter=',') me@2991: filewriter.writerow(("save_id", "value", "trackName")) nicholas@2745: for entry in pagestore[page][comment]: me@2991: filewriter.writerow((entry["subject"], entry["value"], entry["trackName"]))