예제 #1
0
def test_simplify_every_child(lexicon):
    # (LP.LQ.Ax.P(x) -> Q(x))(Lx.Child(x)) -> LQ.Ax.Child(x) -> Q(x)
    tree = Call(lexicon["every"][0].formula, lexicon["child"][0].formula)
    assert tree.simplify() == Lambda(
        "Q",
        ForAll("x",
               IfThen(Call(Var("Child"), Var("x")), Call(Var("Q"), Var("x")))))
예제 #2
0
def test_simplify_super_nested_call():
    # (LP.P(a, b))(Lx.Ly.x & y) -> a & b
    tree = Call(
        Lambda("P", Call(Call(Var("P"), Var("a")), Var("b"))),
        Lambda("x", Lambda("y", And(Var("x"), Var("y")))),
    )
    assert tree.simplify() == And(Var("a"), Var("b"))
예제 #3
0
def test_translate_every_child_is_good(lexicon):
    nodes = translate_sentence("every child is good", lexicon)

    assert len(nodes) == 1

    node = nodes[0]
    assert node.text == "every child is good"
    assert node.formula == ForAll(
        "x", IfThen(Call(Var("Child"), Var("x")), Call(Var("Good"), Var("x"))))
    assert node.type == TYPE_TRUTH_VALUE
예제 #4
0
def test_translate_unknown_word_in_sentence(lexicon):
    nodes = translate_sentence("John is whorlious", lexicon)

    assert len(nodes) == 2

    # TODO [2019-05-20]: For now, this is just wrong (but wrong in the expected way).
    assert nodes[0].text == "John is whorlious"
    assert nodes[0].formula == Call(Var("Whorlious"), Var("john"))
    assert nodes[0].type == TYPE_TRUTH_VALUE

    assert nodes[1].text == "John is whorlious"
    assert nodes[1].formula == Call(Var("John"), Var("whorlious"))
    assert nodes[1].type == TYPE_TRUTH_VALUE
예제 #5
0
def test_combine_every_child(lexicon):
    every = lexicon["every"][0]
    child = lexicon["child"][0]
    node = combine(every, child)
    assert node.text == "every child"
    assert node.formula == Call(every.formula, child.formula)
    assert node.type == ComplexType(TYPE_ET, TYPE_TRUTH_VALUE)
예제 #6
0
def test_shell_display_formula(shell_state):
    with patch("montague.main.translate_sentence") as mock_translate_sentence:
        mock_translate_sentence.return_value = [
            SentenceNode("good", Call(Var("Good"), Var("j")), TYPE_TRUTH_VALUE)
        ]
        response = execute_command("John is good", shell_state)
        assert "Denotation: Good(j)" in response
        assert "Type: t" in response
예제 #7
0
def test_translate_unknown_word(lexicon):
    nodes = translate_sentence("Mikhail", lexicon)

    assert len(nodes) == 3

    assert nodes[0].text == "Mikhail"
    assert nodes[0].formula == Var("mikhail")
    assert nodes[0].type == TYPE_ENTITY

    assert nodes[1].text == "Mikhail"
    assert nodes[1].formula == Lambda("x", Call(Var("Mikhail"), Var("x")))
    assert nodes[1].type == TYPE_ET

    assert nodes[2].text == "Mikhail"
    assert nodes[2].formula == Lambda(
        "x", Lambda("y", Call(Call(Var("Mikhail"), Var("x")), Var("y"))))
    assert nodes[2].type == ComplexType(TYPE_ENTITY, TYPE_ET)
예제 #8
0
def test_parsing_call():
    assert parse_formula("Happy(x)") == Call(Var("Happy"), Var("x"))
    assert parse_formula("Between(x, y & z, [Capital(france)])") == Call(
        Call(Call(Var("Between"), Var("x")), And(Var("y"), Var("z"))),
        Call(Var("Capital"), Var("france")),
    )
    assert parse_formula("(Lx.x)(j)") == Call(Lambda("x", Var("x")), Var("j"))
    assert parse_formula("((Lx.Ly.x & y) (a)) (b)") == Call(
        Call(Lambda("x", Lambda("y", And(Var("x"), Var("y")))), Var("a")),
        Var("b"))
예제 #9
0
def test_translate_the_child(lexicon):
    nodes = translate_sentence("the child", lexicon)

    assert len(nodes) == 1

    node = nodes[0]
    assert node.text == "the child"
    assert node.formula == Iota("x", Call(Var("Child"), Var("x")))
    assert node.type == TYPE_ENTITY
예제 #10
0
def test_translate_john_is_bad(lexicon):
    nodes = translate_sentence("John is bad", lexicon)

    assert len(nodes) == 1

    node = nodes[0]
    assert node.text == "John is bad"
    assert node.formula == Call(Var("Bad"), Var("john"))
    assert node.type == TYPE_TRUTH_VALUE
예제 #11
0
def test_translate_is_good(lexicon):
    # import pdb; pdb.set_trace()
    nodes = translate_sentence("is good", lexicon)

    assert len(nodes) == 1

    node = nodes[0]
    assert node.text == "is good"
    assert node.formula == Lambda("x", Call(Var("Good"), Var("x")))
    assert node.type == TYPE_ET
예제 #12
0
def test_recursive_replace_variable():
    # BFP(x, Lx.x, x & y)
    tree = Call(
        Call(
            Call(Var("BFP"), Var("x")),
            Lambda("x", Var("x")),  # This should not be replaced.
        ),
        And(Var("x"), Var("y")),
    )
    assert tree.replace_variable("x", Var("j")) == Call(
        Call(Call(Var("BFP"), Var("j")), Lambda("x", Var("x"))), And(Var("j"), Var("y"))
    )
예제 #13
0
def test_the_man_is_good_is_true():
    formula = Call(Var("Good"), Iota("x", Call(Var("Man"), Var("x"))))
    assert interpret_formula(formula, test_model)
예제 #14
0
def test_the_man_is_john():
    formula = Iota("x", Call(Var("Man"), Var("x")))
    assert interpret_formula(formula, test_model) == John
예제 #15
0
def test_someone_is_alien_is_false():
    formula = Exists("x", Call(Var("Alien"), Var("x")))
    assert not interpret_formula(formula, test_model)
예제 #16
0
def test_someone_is_bad_is_true():
    formula = Exists("x", Call(Var("Bad"), Var("x")))
    assert interpret_formula(formula, test_model)
예제 #17
0
def test_for_all_to_str():
    tree = ForAll("x", Call(Var("P"), Var("x")))
    assert str(tree) == "∀ x.P(x)"
    assert tree.ascii_str() == "Ax.P(x)"
예제 #18
0
def test_satisfiers_alien_set():
    sset = satisfiers(Call(Var("Alien"), Var("x")), test_model, "x")
    assert sset == set()
예제 #19
0
def test_satisfiers_bad_set():
    sset = satisfiers(Call(Var("Bad"), Var("x")), test_model, "x")
    assert sset == {Mary}
예제 #20
0
def test_the_human_is_undefined():
    formula = Iota("x", Call(Var("Human"), Var("x")))
    assert interpret_formula(formula, test_model) is None
예제 #21
0
def test_satisfiers_good_set():
    sset = satisfiers(Call(Var("Good"), Var("x")), test_model, "x")
    assert sset == {John}
예제 #22
0
def test_john_is_good_is_true():
    formula = Call(Var("Good"), Var("j"))
    assert interpret_formula(formula, test_model)
    assert not interpret_formula(Not(formula), test_model)
예제 #23
0
def test_satisfiers_human_set():
    sset = satisfiers(Call(Var("Human"), Var("x")), test_model, "x")
    assert sset == {John, Mary}
예제 #24
0
def test_john_is_bad_is_false():
    formula = Call(Var("Bad"), Var("j"))
    assert not interpret_formula(formula, test_model)
    assert interpret_formula(Not(formula), test_model)
예제 #25
0
def test_replace_predicate():
    tree = Call(Var("P"), Var("x"))
    assert tree.replace_variable("P", Var("Good")) == Call(Var("Good"), Var("x"))
예제 #26
0
def test_mary_is_bad_and_john_is_good_is_true():
    formula = And(Call(Var("Bad"), Var("m")), Call(Var("Good"), Var("j")))
    assert interpret_formula(formula, test_model)
예제 #27
0
def test_call_to_str():
    assert (
        str(Call(Call(Var("P"), And(Var("a"), Var("b"))), Lambda("x", Var("x"))))
        == "P(a & b, λx.x)"
    )
    assert str(Call(Var("P"), Var("x"))) == "P(x)"
예제 #28
0
def test_everyone_is_bad_is_false():
    formula = ForAll("x", Call(Var("Bad"), Var("x")))
    assert not interpret_formula(formula, test_model)
예제 #29
0
def test_exists_to_str():
    tree = Exists("x", Call(Var("P"), Var("x")))
    assert str(tree) == "∃ x.P(x)"
    assert tree.ascii_str() == "Ex.P(x)"
예제 #30
0
def test_everyone_is_human_is_true():
    formula = ForAll("x", Call(Var("Human"), Var("x")))
    assert interpret_formula(formula, test_model)