def test_ground_atoms(): pk = 973 questions = geoserver_interface.download_questions(pk) question = questions.values()[0] label_data = geoserver_interface.download_labels(pk)[pk] diagram = open_image(question.diagram_path) graph_parse = diagram_to_graph_parse(diagram) match_parse = parse_match_from_known_labels(graph_parse, label_data) AB = v('AB', 'line') AC = v('AC', 'line') BC = v('BC', 'line') ED = v('ED', 'line') AE = v('AE', 'line') E = v('E', 'point') D = v('D', 'point') x = v('x', 'number') p1 = f('LengthOf', AB) == f('LengthOf', AC) p2 = f('IsMidpointOf', E, AB) p3 = f('IsMidpointOf', D, AC) p4 = f('LengthOf', AE) == x p5 = f('LengthOf', ED) == 4 qn = f('LengthOf', BC) grounded_atoms = ground_formula_nodes(match_parse, [p1, p2, p3, p4, p5, qn]) for grounded_atom in grounded_atoms: print grounded_atom graph_parse.core_parse.display_points()
def test_solving(): pk = 973 questions = geoserver_interface.download_questions(pk) question = questions.values()[0] label_data = geoserver_interface.download_labels(pk)[pk] diagram = open_image(question.diagram_path) graph_parse = diagram_to_graph_parse(diagram) match_parse = parse_match_from_known_labels(graph_parse, label_data) AB = v('AB', 'line') AC = v('AC', 'line') BC = v('BC', 'line') ED = v('ED', 'line') AE = v('AE', 'line') E = v('E', 'point') D = v('D', 'point') x = v('x', 'number') p1 = f('LengthOf', AB) == f('LengthOf', AC) p2 = f('IsMidpointOf', E, AB) p3 = f('IsMidpointOf', D, AC) p4 = f('LengthOf', AE) == x p5 = f('LengthOf', ED) == 4 qn = f('LengthOf', BC) confident_atoms = parse_confident_formulas(graph_parse) text_atoms = ground_formula_nodes(match_parse, [p1, p2, p3, p4, p5]) atoms = confident_atoms + text_atoms grounded_qn = ground_formula_nodes(match_parse, [qn])[0] ns = NumericSolver(atoms) print ns.evaluate(grounded_qn)
def _annotated_unit_test(query): questions = geoserver_interface.download_questions(query) all_annotations = geoserver_interface.download_semantics(query) pk, question = questions.items()[0] choice_formulas = get_choice_formulas(question) label_data = geoserver_interface.download_labels(pk)[pk] diagram = open_image(question.diagram_path) graph_parse = diagram_to_graph_parse(diagram) core_parse = graph_parse.core_parse # core_parse.display_points() # core_parse.primitive_parse.display_primitives() match_parse = parse_match_from_known_labels(graph_parse, label_data) match_formulas = parse_match_formulas(match_parse) diagram_formulas = parse_confident_formulas(graph_parse) all_formulas = match_formulas + diagram_formulas for number, sentence_words in question.sentence_words.iteritems(): syntax_parse = stanford_parser.get_best_syntax_parse(sentence_words) annotation_nodes = [annotation_to_semantic_tree(syntax_parse, annotation) for annotation in all_annotations[pk][number].values()] expr_formulas = {key: prefix_to_formula(expression_parser.parse_prefix(expression)) for key, expression in question.sentence_expressions[number].iteritems()} truth_expr_formulas, value_expr_formulas = _separate_expr_formulas(expr_formulas) text_formula_parse = semantic_trees_to_text_formula_parse(annotation_nodes) completed_formulas = complete_formulas(text_formula_parse) grounded_formulas = [ground_formula(match_parse, formula, value_expr_formulas) for formula in completed_formulas+truth_expr_formulas] text_formulas = filter_formulas(flatten_formulas(grounded_formulas)) all_formulas.extend(text_formulas) reduced_formulas = reduce_formulas(all_formulas) for reduced_formula in reduced_formulas: score = evaluate(reduced_formula, core_parse.variable_assignment) scores = [evaluate(child, core_parse.variable_assignment) for child in reduced_formula.children] print reduced_formula, score, scores # core_parse.display_points() ans = solve(reduced_formulas, choice_formulas, assignment=core_parse.variable_assignment) print "ans:", ans if choice_formulas is None: attempted = True if abs(ans - float(question.answer)) < 0.01: correct = True else: correct = False else: attempted = True c = max(ans.iteritems(), key=lambda pair: pair[1].conf)[0] if c == int(question.answer): correct = True else: correct = False result = SimpleResult(query, False, attempted, correct) return result
def test_parse_match_from_known_labels(): questions = geoserver_interface.download_questions(977) for pk, question in questions.iteritems(): label_data = geoserver_interface.download_labels(pk)[pk] diagram = open_image(question.diagram_path) graph_parse = diagram_to_graph_parse(diagram) match_parse = parse_match_from_known_labels(graph_parse, label_data) for key, value in match_parse.match_dict.iteritems(): print key, value graph_parse.core_parse.display_points()
def test_parse_match_atoms(): questions = geoserver_interface.download_questions(977) for pk, question in questions.iteritems(): label_data = geoserver_interface.download_labels(pk)[pk] diagram = open_image(question.diagram_path) graph_parse = diagram_to_graph_parse(diagram) match_parse = parse_match_from_known_labels(graph_parse, label_data) match_atoms = parse_match_formulas(match_parse) for match_atom in match_atoms: print match_atom graph_parse.core_parse.display_points()
def question_to_match_parse(question, label_data): graph_parse = question_to_graph_parse(question) match_parse = parse_match_from_known_labels(graph_parse, label_data) return match_parse