nicholas@2586: #!/usr/bin/python nicholas@2586: nicholas@2586: import xml.etree.ElementTree as ET nicholas@2586: import os nicholas@2586: import sys nicholas@2586: import csv nicholas@2586: nicholas@2586: # COMMAND LINE ARGUMENTS nicholas@2586: nicholas@2586: assert len(sys.argv)<3, "score_parser takes at most 1 command line argument\n"+\ nicholas@2586: "Use: python score_parser.py [rating_folder_location]" nicholas@2586: nicholas@2586: # XML results files location nicholas@2586: if len(sys.argv) == 1: nicholas@2586: folder_name = "../saves" # Looks in 'saves/' folder from 'scripts/' folder nicholas@2586: print("Use: python score_parser.py [rating_folder_location]") nicholas@2586: print("Using default path: " + folder_name) nicholas@2586: elif len(sys.argv) == 2: nicholas@2586: folder_name = sys.argv[1] # First command line argument is folder nicholas@2586: nicholas@2586: # check if folder_name exists nicholas@2586: if not os.path.exists(folder_name): nicholas@2586: #the file is not there nicholas@2586: print("Folder '"+folder_name+"' does not exist.") nicholas@2586: sys.exit() # terminate script execution nicholas@2586: elif not os.access(os.path.dirname(folder_name), os.W_OK): nicholas@2586: #the file does exist but write privileges are not given nicholas@2586: print("No write privileges in folder '"+folder_name+"'.") nicholas@2586: nicholas@2586: nicholas@2586: # CODE nicholas@2586: nicholas@2586: storage = {} nicholas@2586: nicholas@2586: # create folder 'ratings' if not yet created nicholas@2586: if not os.path.exists(folder_name + '/ratings'): nicholas@2586: os.makedirs(folder_name + '/ratings') nicholas@2586: nicholas@2586: # Get every XML file in the folder nicholas@2586: for file_name in os.listdir(folder_name): nicholas@2586: if (file_name.endswith(".xml")): nicholas@2586: tree = ET.parse(folder_name + '/' + file_name) nicholas@2586: root = tree.getroot() nicholas@2586: nicholas@2586: subject_id = root.get('key'); nicholas@2586: nicholas@2586: # get the list of the pages this subject evaluated nicholas@2586: for page in root.findall("./page"): # iterate over pages nicholas@2586: page_name = page.get('ref') # get page ID nicholas@2586: nicholas@2586: if page_name is None: # ignore 'empty' audio_holders nicholas@2586: print("WARNING: " + file_name + " contains empty audio holder. (score_parser.py)") nicholas@2586: break nicholas@2586: nicholas@2586: if page.get('state') != "complete": nicholas@2586: print("WARNING: " + file_name + " contains incomplete page " +page_name+ ". (score_parser.py)") nicholas@2586: break; nicholas@2586: nicholas@2586: # Check if page in the store nicholas@2586: if storage.get(page_name) == None: nicholas@2586: storage[page_name] = {'header':[], 'axis':{}} # add to the store nicholas@2586: nicholas@2586: # Get the axis names nicholas@2586: pageConfig = root.find('./waet/page/[@id="'+page_name+'"]') nicholas@2586: for interface in pageConfig.findall('./interface'): # Get the noeds nicholas@2586: interfaceName = interface.get("name"); # Get the axis name nicholas@2586: if interfaceName == None: nicholas@2586: interfaceName = "default" # If name not set, make name 'default' nicholas@2586: if storage[page_name]['axis'].get(interfaceName) == None: nicholas@2586: storage[page_name]['axis'][interfaceName] = {} # If not in store for page, add empty dict nicholas@2586: storage[page_name]['axis'][interfaceName][subject_id] = [] # Add the store for the session nicholas@2586: nicholas@2586: # header: fragment IDs in 'alphabetical' order nicholas@2586: # go to fragment column, or create new column if it doesn't exist yet nicholas@2586: nicholas@2586: # get alphabetical array of fragment IDs from this subject's XML nicholas@2586: fragmentnamelist = [] # make empty list nicholas@2586: for audioelement in page.findall("./audioelement"): # iterate over all audioelements nicholas@2586: fragmentnamelist.append(audioelement.get('ref')) # add to list nicholas@2586: nicholas@2586: fragmentnamelist = sorted(fragmentnamelist); # Sort the list nicholas@2586: storage[page_name]['header'] = fragmentnamelist; nicholas@2586: nicholas@2586: for fragmentname in fragmentnamelist: nicholas@2586: audioElement = page.find("./audioelement/[@ref='"+ fragmentname+ "']") # Get the element nicholas@2586: for value in audioElement.findall('./value'): nicholas@2586: axisName = value.get('interface-name') nicholas@2586: if axisName == None: nicholas@2586: axisName = 'default' nicholas@2586: axisStore = storage[page_name]['axis'][axisName] nicholas@2586: if hasattr(value, 'text'): nicholas@2586: axisStore[subject_id].append(value.text) nicholas@2586: else: nicholas@2586: axisStore[subject_id].append('') nicholas@2586: nicholas@2586: # Now create the individual files nicholas@2586: for page_name in storage: nicholas@2586: for axis_name in storage[page_name]['axis']: nicholas@2586: nicholas@2586: file_name = folder_name+'/ratings/'+page_name+'-'+axis_name+'-ratings.csv' # score file name nicholas@2586: nicholas@2586: # I'm not as elegant, I say burn the files and start again nicholas@2586: headerrow = list(storage[page_name]['header']) # Extract the element IDs nicholas@2586: headerrow.insert(0,'file_keys') nicholas@2586: with open(file_name, 'w') as writefile: nicholas@2586: filewriter = csv.writer(writefile, delimiter=',') nicholas@2586: filewriter.writerow(headerrow) nicholas@2586: nicholas@2586: # open file to write the page nicholas@2586: writefile = open(file_name, 'a') nicholas@2586: filewriter = csv.writer(writefile, delimiter=',') nicholas@2586: nicholas@2586: for subject_id in storage[page_name]['axis'][axis_name]: nicholas@2586: entry = [subject_id] nicholas@2586: for value in storage[page_name]['axis'][axis_name][subject_id]: nicholas@2586: entry.append(value) nicholas@2586: filewriter.writerow(entry) nicholas@2586: writefile.close()