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)
예제 #2
0
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))
예제 #3
0
파일: utils.py 프로젝트: lcary/puddle-world
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
예제 #4
0
def _make_mock_model(learner):
  """
  Mock learner does not have any grounding -- just build a spurious Model
  instance.
  """
  return Model({"objects": []}, learner.ontology)
예제 #5
0
 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)
예제 #6
0
""",
                         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])
예제 #7
0
learner = WordLearner(initial_lex)
i = 100
from pprint import pprint
for idx in sorted_idxs:
    _, objects_i, instruction_i, goal_i = dataset[idx]
    goal_i = tuple(goal_i)

    if instruction_i != "one above triangle":
        continue

    print(i)
    print(instruction_i)

    scene = process_scene(objects_i)
    model = Model(scene, ontology)

    print(np.array(objects_i))
    print(goal_i)

    instruction_i = instruction_i.split()
    results = learner.update_with_distant(instruction_i, model, goal_i)

    if results:
        printCCGDerivation(results[0][0])

    assert False

print("Lexicon:")
learner.lexicon.debug_print()
print()