def findAlignment(wordproblem, equation, solution): numberVector = extractNumberVectorFromQuestion(wordproblem) # numberVector = numberVector[:5] alignments = createAllAlignments(numberVector) correctAlignment = [] x0 = Symbol("x0") test = [] # x1 = Symbol('x1') try: i = 0 for alignment in alignments: result = solveEquations(equation, getNumberslots(equation), alignment) test = alignment if len(result) != 0: if len(equation) == 1: if solution[0] == result[x0]: correctAlignment = alignment elif len(equation) == 2: if solution[0] == result[x0]: # and solution[1] == result[x1]: correctAlignment = alignment except Exception as e: print "error: ", e print wordproblem print equation print test return (correctAlignment, numberVector)
def testAlignments(test_data, classifiers): x0 = Symbol('x0') featuresets = [] correctlyAlignedIndicesList = [] for (i, iIndex, wordproblem, equationTemplate, solution) in test_data: try: classifier = classifiers[str(equationTemplate)] except KeyError: continue alignmentFeatures, correctlyAlignedIndices = extractAlignmentFeatures(wordproblem, equationTemplate, solution, i) featuresets.append(alignmentFeatures) alignedString = '' for c in correctlyAlignedIndices: alignedString += str(c) correctlyAlignedIndicesList.append(alignedString) prediction = classifier.predict(featuresets) predictedAlignedIndices = [] correct = 0 for each in prediction: item = [] for char in each: item.append(int(char)) predictedAlignedIndices.append(item) #print 'sdsdsdsdsdsdsd' #print predictedAlignedIndices predictedAlignedValues = [] for x in range(0, len(test_data)): try: i, iIndex,wordproblem, equationTemplate, solution = test_data[x] numberVector = extractNumberVectorFromQuestion(wordproblem) correctAlignValues = [] for each in predictedAlignedIndices[x]: correctAlignValues.append(numberVector[each]) predictedAlignedValues.append(correctAlignValues) #print 'correctalignedvalues' #print correctAlignValues result = solveEquations(equationTemplate, getNumberslots(equationTemplate), correctAlignValues) if solution[0] == result[x0]: correct += 1 except: pass #print 'all values' #print predictedAlignedValues for i in range(0,len(prediction)): print correctlyAlignedIndicesList[i], prediction[i] if correctlyAlignedIndicesList[i] == prediction[i]: correct += 1 print 'Correct: ' + str(correct)