# -*- coding: cp1252 -*- import os import os.path from xml.etree import ElementTree import cPickle path = os.getcwd() pickle_file = "data_pickle.txt" outputPath = os.path.join(path, "output") if not os.path.isdir(outputPath): os.mkdir("output") FILE = open(os.path.join(outputPath, pickle_file), "w") DEBUG_PRINT = False LONG_LENGTH = 500 INTERVAL_LENGTH = 250 if path.find("asus") != -1: width = 1280 height = 860 else: width = 800 height = 600 data = {} grid = 0 goal = 0 primitive = "" method = "" pending = "" trialStart = 0 start = 0 second = 0 end = 0 tap1end = 0 betweenTaps = 0 maxTapDuration = 0 names = [] pressed = "" detectedPrimitives = "" numDict = {"Um": "1", "Dois": "2", u"Três": "3", "Quatro": "4", "Cinco": "5", "Seis": "6", "Sete": "7", "Oito": "8", "Nove": "9", "Asterisco": "*", "Zero": "0", "Cardinal": "#", "Para cima": "1", "Para baixo" : "2", "Para a esquerda": "3", "Para a direita": "4", "No lado direito para cima ": "5", "No lado esquerdo para cima ": "6", "No lado direito para baixo": "7", "No lado esquerdo para baixo": "8", "No topo para a esquerda": "9", "No topo para a direita": "10", "Em baixo para a direita": "11", "Em baixo para a esquerda": "12", "Canto superior esquerdo": "1", "Canto inferior esquerdo": "2", "Canto superior direito": "3", "Canto inferior direito": "4", "Esquerda": "5", "Direita": "6", "Cima": "7", "Baixo": "8", "Centro": "9", "10":"*", "11":"0", "12": "#" } def debug(obj): if DEBUG_PRINT: print obj def getMilli(time): parts=time.strip().split(":") miliseconds = int(parts[0]) * 3600000 + int(parts[1]) * 60000 + int(parts[2].split(".")[0]) * 1000 + int(parts[2].split(".")[1]) return miliseconds def parseDir(user): print "Parsing ", user data[user] = {} for file in os.listdir(user): current = os.path.join(user, file) if current[-3:] == "xml": with open(current, 'rt') as f: tree = ElementTree.parse(f) for node in tree.getiterator(): if node.tag == "session": method = node.attrib["method"] grid = node.attrib["cells"] primitive = node.attrib["primitive"] data[user][(method, grid, primitive)] = {} elif node.tag == "trial": trialStart = getMilli(node.attrib["time"]) first = True detectedPrimitives = "" pressed = "" start = 0 second = 0 end = 0 maxTapDuration = 0 tap1End = 0 duration = 0 betweenTaps = -1 if node.getchildren(): for child in node: if child.tag == "goal": goal = numDict[unicode(child.text)] elif child.tag == "sessions": pass else: if child.tag == "regTap": detectedPrimitives += "Tap" pressed += numDict.get(child.text, child.text) duration = getMilli(child.attrib["time"]) - duration if duration > maxTapDuration: maxTapDuration = duration if tap1End == 0: tap1End = getMilli(child.attrib["time"]) else: temp = getMilli(child.attrib["time"]) - tap1End if betweenTaps != -1: if temp < betweenTaps: betweenTaps = temp else: betweenTaps = temp tap1End = getMilli(child.attrib["time"]) duration = getMilli(child.attrib["time"]) elif child.tag == "regLong": detectedPrimitives += "Long" pressed += numDict.get(child.text, child.text) duration = getMilli(child.attrib["time"]) - duration if duration > maxTapDuration: maxTapDuration = duration if tap1End == 0: tap1End = getMilli(child.attrib["time"]) else: temp = getMilli(child.attrib["time"]) - tap1End if betweenTaps != -1: if temp < betweenTaps: betweenTaps = temp else: betweenTaps = temp tap1End = getMilli(child.attrib["time"]) duration = getMilli(child.attrib["time"]) elif child.tag == "regDoubleTap": detectedPrimitives += "Double" pressed += numDict.get(child.text, child.text) duration = getMilli(child.attrib["time"]) - duration if duration > maxTapDuration: maxTapDuration = duration if tap1End == 0: tap1End = getMilli(child.attrib["time"]) else: temp = getMilli(child.attrib["time"]) - tap1End temp = getMilli(child.attrib["time"]) - tap1End if betweenTaps != -1: if temp < betweenTaps: betweenTaps = temp else: betweenTaps = temp tap1End = getMilli(child.attrib["time"]) duration = getMilli(child.attrib["time"]) elif child.tag == "regTouch": end = getMilli(child.attrib["time"]) - trialStart pending = numDict.get(child.text, child.text) elif child.tag == "regScroll": if primitive != "Flick" and detectedPrimitives[-6:] != "Scroll" and (len(pressed) == 0 or pressed[-1] != child.text): pressed += pending pending = "" detectedPrimitives += "Scroll" if tap1End == 0: tap1End = getMilli(child.attrib["time"]) else: temp = getMilli(child.attrib["time"]) - tap1End if betweenTaps != -1: if temp < betweenTaps: betweenTaps = temp else: betweenTaps = temp else: temp = getMilli(child.attrib["time"]) - duration if temp > maxTapDuration: maxTapDuration = temp tap1End = getMilli(child.attrib["time"]) elif child.tag == "regDown": duration = getMilli(child.attrib["time"]) if first: first = False x = child.attrib["x"] y = child.attrib["y"] start = getMilli(child.attrib["time"]) - trialStart data[user][(method, grid, primitive)][goal] = (pressed, detectedPrimitives, start, end, maxTapDuration, betweenTaps, x, y) for d in os.listdir(path): if d != "output": d = os.path.join(path, d) if os.path.isdir(d): parseDir(d) cPickle.dump(data, FILE) FILE.close()