Exemplo n.º 1
0
def test_augment_lexicon_unification():
  ontology = _make_simple_mock_ontology()
  lex = Lexicon.fromstring(r"""
  :- S

  and => S\S/S {\f g x.and_(f(x),g(x))}
  blue => S {foo}
  black => S {bar}
  """, ontology=ontology, include_semantics=True)

  # NB in this case, all words are novel words.
  sentence = "red AND white".split()
  lf = l.Expression.fromstring(r"\x.and_(foo(x),bar(x))")
  ontology.typecheck(lf)
  new_lex = augment_lexicon_unification(lex, sentence, ontology, lf)

  exprs = {token: {str(entry.semantics()) for entry in new_lex.get_entries(token)}
           for token in ["red", "AND", "white"]}
  ok_("foo" in exprs["red"])
  ok_("foo" in exprs["white"])
  ok_("bar" in exprs["red"])
  ok_("foo" in exprs["white"])
  ok_(r"\z2 z1 x.and_(z1(x),z2(x))" in exprs["AND"])

  print("%i entries induced for 'red'" % len(new_lex.get_entries("red")))

  old_results = WeightedCCGChartParser(lex).parse(sentence)
  new_results = WeightedCCGChartParser(new_lex).parse(sentence)
  eq_(len(old_results), 0, "Parse should fail before augmenting")
  ok_(len(new_results) > 0, "Parse should succeed after augmenting")
Exemplo n.º 2
0
def test_multiple_starts():
    """
  Support lexicons which allow any of several root categories, signaled in
  `fromstring` with a colon.
  """
    lex = Lexicon.fromstring(r"""
    :- S:N,P

    the => N/N
    boy => N
    eats => S\N
    """)

    eq_(len(lex.start_categories), 2)

    parser = WeightedCCGChartParser(lex)

    assert len(parser.parse("the boy".split())) > 0
    assert len(parser.parse("the boy eats".split())) > 0
Exemplo n.º 3
0
def test_cases():
    parser = WeightedCCGChartParser(lexicon)
    for sentence, lf in sentences:
        yield _test_case, parser, sentence, lf
Exemplo n.º 4
0
        for v, q, a in iter_data():
            learner.update_with_distant(
                q,
                v,
                a,
                augment_lexicon_args=dict(iter_expression_kwargs=dict()))
    except Exception as e:
        print(e)
        raise e
    finally:
        del iter_data

    return learner.lexicon


lex = learn_dataset(initial_lex, 1000)

print('Learned lexicon:')
print('-' * 120)
lex.debug_print()
print()

print('Example:')
print('-' * 120)
parser = WeightedCCGChartParser(lex)
results = parser.parse("any cube".split())
printCCGDerivation(results[0])

root_token, _ = results[0].label()
print(root_token.semantics())
Exemplo n.º 5
0
  horse => N {\x.horse(x)}
  rock => N {\x.rock(x)}
  rock => N {unique(\x.rock(x))}
  cell => N {\x.true}
  spade => N {\x.spade(x)}
  spade => N {unique(\x.spade(x))}
  heart => N {\x.heart(x)}
  heart => N {unique(\x.heart(x))}
  circle => N {\x.circle(x)}
  # triangle => N {\x.triangle(x)}
""",
                                 ontology,
                                 include_semantics=True)
initial_lex.debug_print()

p = WeightedCCGChartParser(initial_lex)

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)