def _test_case(parser, sentence, lf, scene=None): model = Model(scene or {"objects": []}, ontology) results = parser.parse(sentence.split()) for result in results: semantics = result.label()[0].semantics() print("Predicted semantics:", semantics) if str(semantics) == lf: printCCGDerivation(result) return True ok_( False, "Expected successful parse with correct lf. Found %i parses; none matched LF." % len(results))
def ecTaskAsPyCCGUpdate(task, ontology): """ Converts an EC task into a PyCCG update. Assumes the task has a single scene and the instructions as a string. :return: tokenized_instruction, model, goal. """ remove_punctuation = str.maketrans('', '', string.punctuation) tokenized = task.features.translate(remove_punctuation).lower().split() scene, goal = task.examples[0] # Unpack the examples. scene, goal = scene[0], goal return tokenized, Model(scene, ontology), goal
def _test_case(scene, expression, expected, msg=None): from pprint import pprint print("Objects:") pprint(scene['objects']) model = Model(scene, ontology) expr = Expression.fromstring(expression) value = model.evaluate(expr) print(expr) print("Expected:", expected) print("Observed:", value) eq_(value, expected, msg)
def _make_mock_model(learner): """ Mock learner does not have any grounding -- just build a spurious Model instance. """ return Model({"objects": []}, learner.ontology)
def iter_data(): """Helper function for iterating dataset and inject the ontology to make the scene a model for execution.""" for v, q, a in dataset: yield (Model(v, ontology), q.split(), a)
""", ontology, include_semantics=True) ####### # Execute on a scene. scene = { "objects": [ Object("sphere", "big", "rubber"), Object("cube", "small", "metal"), Object("cylinder", "small", "rubber"), ] } model = Model(scene, ontology) print("the ball") print(model.evaluate(Expression.fromstring(r"unique(\x.has_shape(x,sphere))"))) ###### # Parse an utterance and execute. learner = WordLearner(lex) # Update with distant supervision. learner.update_with_distant("the cube".split(), model, scene['objects'][1]) parser = learner.make_parser() results = parser.parse("the cube".split()) printCCGDerivation(results[0])