annotate scripts/comment_parser.py @ 1365:c4156df1dcb5

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