def segment(self, exp): setattr(exp, "strokes", []) setattr(exp, "segSymbols", []) for s in exp.symbols: for stroke in s.strokes: exp.strokes.append(stroke) strokeNo = 0 for stroke in exp.strokes: s = Symbol('?') s.addStroke(stroke, strokeNo) s.featureVector = getFeatureVector(s.strokes) #s.featureVector = [0,0,0,0,0,0,0,0,0,0,0,0,0,0] strokeNo += 1 exp.segSymbols.append(s)
def readExpression(filename): tree = ET.parse(filename) root = tree.getroot() strokes = {} # Collect all of the traces (strokes) for trace in root.iter(): if(trace.tag == "{http://www.w3.org/2003/InkML}trace"): traceid = trace.attrib['id'] tracetext = trace.text.strip().split(',') tracecoords = [] for t in tracetext: coords = t.strip().split(' ') tracecoords.append( (coords[0],coords[1]) ) # Save the stroke strokes[traceid] = tracecoords # Go through each traceGroup and put together symbol objects. tgroot = root.find("{http://www.w3.org/2003/InkML}traceGroup") rawfilename = filename.split('.')[0] newexpression = Expression(rawfilename) first = True for tg in tgroot.iter(): if(tg.tag == "{http://www.w3.org/2003/InkML}traceGroup"): truth = tg.find("{http://www.w3.org/2003/InkML}annotation").text if(first): # traceGroup for entire segmentation first = False continue newsymbol = Symbol(truth) for elem in tg.iter(): if(elem.tag == "{http://www.w3.org/2003/InkML}traceView"): tvindex = elem.attrib['traceDataRef'] newsymbol.addStroke(strokes[tvindex], tvindex) newexpression.addSymbol(newsymbol) return newexpression