nickjillings@1542: import xml.etree.ElementTree as ET nickjillings@1542: import os nickjillings@1542: import csv nickjillings@1542: nickjillings@1542: #TODO Remove DEBUG statements nickjillings@1542: nickjillings@1542: # get every XML file in folder nickjillings@1542: for file in os.listdir("."): # You have to put this in folder where output XML files are. nickjillings@1542: if file.endswith(".xml"): nickjillings@1542: tree = ET.parse(file) nickjillings@1542: root = tree.getroot() nickjillings@1542: #print ["DEBUG Reading " + file + "..."] nickjillings@1542: nickjillings@1542: # get subject ID from XML file nickjillings@1542: subject_id = file # file name as subject ID nickjillings@1542: nickjillings@1542: # get list of all pages this subject evaluated nickjillings@1542: for audioholder in root.findall("./audioholder"): # iterate over pages nickjillings@1542: page_name = audioholder.get('id') # get page name nickjillings@1542: #print ["DEBUG page " + page_name] nickjillings@1542: nickjillings@1542: if page_name is None: # ignore 'empty' audio_holders nickjillings@1542: break nickjillings@1542: nickjillings@1542: file_name = 'ratings/'+page_name+'-ratings.csv' # score file name nickjillings@1542: nickjillings@1542: # create folder 'ratings if not yet created nickjillings@1542: if not os.path.exists('ratings'): nickjillings@1542: os.makedirs('ratings') nickjillings@1542: nickjillings@1542: # header: fragment IDs in 'alphabetical' order nickjillings@1542: # go to fragment column, or create new column if it doesn't exist yet nickjillings@1542: nickjillings@1542: # get array of audio elements and number of audio elements nickjillings@1542: audiolist = root.findall("*/[@id='"+page_name+"']/audioelement") nickjillings@1542: n_fragments = len(audiolist) nickjillings@1542: nickjillings@1542: # get alphabetical array of fragment IDs from this subject's XML nickjillings@1542: fragmentnamelist = [] # make empty list nickjillings@1542: for audioelement in audiolist: # iterate over all audioelements nickjillings@1542: fragmentnamelist.append(audioelement.get('id')) # add to list nickjillings@1542: nickjillings@1542: nickjillings@1542: # if file exists, get header and add 'new' fragments nickjillings@1542: if os.path.isfile(file_name): nickjillings@1542: #print ["DEBUG file " + file_name + " already exists - reading header"] nickjillings@1542: with open(file_name, 'r') as readfile: nickjillings@1542: filereader = csv.reader(readfile, delimiter=',') nickjillings@1542: headerrow = filereader.next() nickjillings@1542: #headerrow = headerrow[1:] # remove first column (empty) nickjillings@1542: nickjillings@1542: # Which of the fragmentes are in fragmentnamelist but not in headerrow? nickjillings@1542: newfragments = list(set(fragmentnamelist)-set(headerrow)) nickjillings@1542: newfragments = sorted(newfragments) # new fragments in alphabetical order nickjillings@1542: # If not empty, read file and rewrite adding extra columns nickjillings@1542: if newfragments: # if not empty nickjillings@1542: print ["DEBUG New fragments found: " + str(newfragments)] nickjillings@1542: with open('temp.csv', 'w') as writefile: nickjillings@1542: filewriter = csv.writer(writefile, delimiter=',') nickjillings@1542: filewriter.writerow(headerrow + newfragments) # write new header nickjillings@1542: for row in filereader: # rewrite row plus empty cells for every new fragment name nickjillings@1542: #print ["DEBUG Old row: " + str(row)] nickjillings@1542: filewriter.writerow(row + ['']*len(newfragments)) nickjillings@1542: #print ["DEBUG New row: " + str(row + ['']*len(newfragments))] nickjillings@1542: os.rename('temp.csv', file_name) # replace old file with temp file nickjillings@1542: headerrow = headerrow + newfragments nickjillings@1542: print ["DEBUG New header row: " + str(headerrow)] nickjillings@1542: nickjillings@1542: # if not, create file and make header nickjillings@1542: else: nickjillings@1542: #print ["DEBUG file " + file_name + " doesn't exist yet - making new one"] nickjillings@1542: headerrow = sorted(fragmentnamelist) # sort alphabetically nickjillings@1542: headerrow.insert(0,'') nickjillings@1542: fragmentnamelist = fragmentnamelist[1:] #HACKY FIX inserting in firstrow also affects fragmentnamelist nickjillings@1542: with open(file_name, 'w') as writefile: nickjillings@1542: filewriter = csv.writer(writefile, delimiter=',') nickjillings@1542: filewriter.writerow(headerrow) nickjillings@1542: nickjillings@1542: # open file to write for this page nickjillings@1542: writefile = open(file_name, 'a') nickjillings@1542: filewriter = csv.writer(writefile, delimiter=',') nickjillings@1542: nickjillings@1542: # prepare row to be written for this subject for this page nickjillings@1542: ratingrow = [subject_id] nickjillings@1542: nickjillings@1542: # get scores related to fragment [id] nickjillings@1542: for fragmentname in headerrow[1:]: # iterate over fragments in header (skip first empty column) nickjillings@1542: elementvalue = root.find("*/[@id='" nickjillings@1542: + page_name nickjillings@1542: + "']/audioelement/[@id='" nickjillings@1542: + fragmentname nickjillings@1542: + "']/value") nickjillings@1542: if hasattr(elementvalue, 'text'): # if rating for this fragment exists nickjillings@1542: ratingrow.append(elementvalue.text) # add to rating row nickjillings@1542: else: # if this subject has not rated this fragment nickjillings@1542: ratingrow.append('') # append empty cell nickjillings@1542: nickjillings@1542: # write row: [subject ID, rating fragment ID 1, ..., rating fragment ID M] nickjillings@1542: filewriter.writerow(ratingrow) nickjillings@1542: