def test_simple_production_union(): cfg = CFG([ ("S", PUnion([PTerminal("foo"), PTerminal("bar")])), ]) expect = [("foo", ), ("bar", )] result = list(cfg.permutation_values("S")) assert expect == result
def test_coerce_features(): """ we should be able to coerce common feature descriptions. """ # string as value assert PTerminal("foo", features={"num": "1"}).features == {"num": {"1"}} # list as value assert PTerminal("foo", features={ "num": ["1", "2"] }).features == { "num": {"1", "2"} } # coerce all else as strings. assert PTerminal("foo", features={"num": 1}).features == {"num": {"1"}}
def test_full_example(): my_cfg = CFG([("PRONOUN", PTerminal("I")), ("VERB", PTerminal("have")), ("NOUN", PTerminal("candy")), ("NOUN", PTerminal("bonbons")), ("SENTENCE", PList([PRef("PRONOUN"), PRef("VERB"), PRef("NOUN")]))]) result = set(my_cfg.permutation_values("SENTENCE")) expect = set([ ("I", "have", "candy"), ("I", "have", "bonbons"), ])
def test_feature_mismatch(): """ a feature mismatch should result """ my_cfg = CFG([("PRONOUN", PTerminal("I", features={"person": set("1")})), ("PRONOUN", PTerminal("You", features={"person": set("2")})), ("VERB", PTerminal("have", features={"person": set("2")})), ("NOUN", PTerminal("candy")), ("SENTENCE", PList([PRef("PRONOUN"), PRef("VERB"), PRef("NOUN")]))]) result = set(my_cfg.permutation_values("SENTENCE")) expect = set([("You", "have", "candy")]) assert result == expect
def test_passing_features(features, expected): """ asking for a permutation with specific features should work. """ cfg = CFG([("S", PTerminal("I", features={"person": 1})), ("S", PTerminal("you", features={"person": 2})), ("S", PTerminal("he", features={ "person": 3, "gender": "m" })), ("S", PTerminal("she", features={ "person": 3, "gender": "f" }))]) print(expected) print(set(cfg.permutation_values("S", features=features))) assert set(cfg.permutation_values("S", features=features)) == expected
def test_simple_production_ref(): cfg = CFG([ ("S", PTerminal("foo")), ]) expect = [("foo",)] result = list(cfg.permutation_values(PRef("S"))) assert expect == result
def test_list_with_features(): """ if a feature is passed into the list, it will only accept a collected feature set that matches. """ cfg = CFG([("AB_UPPER", PList([PRef("A"), PRef("B")], features={"case": "upper"})), ("AB_LOWER", PList([PRef("A"), PRef("B")], features={"case": "lower"})), ("A", PUnion([ PTerminal("a", features={"case": "lower"}), PTerminal("A", features={"case": "upper"}) ])), ("B", PUnion([ PTerminal("b", features={"case": "lower"}), PTerminal("B", features={"case": "upper"}) ]))]) assert list(cfg.permutation_values("AB_UPPER")) == [("A", "B")] assert list(cfg.permutation_values("AB_LOWER")) == [("a", "b")]
def test_equality(): assert (PList([PTerminal("foo"), PTerminal("bar") ]) == PList([PTerminal("foo"), PTerminal("bar")])) assert not (PList([PTerminal("foo"), PTerminal("bar")]) != PList( [PTerminal("foo"), PTerminal("bar")]))
def test_multiple_productions_same_rule(): cfg = CFG([("S", PTerminal("foo")), ("S", PTerminal("bar"))]) result = cfg.permutation_values("S") expect = [("foo", ), ("bar", )] assert set(result) == set(expect)
def test_equality(): lhs = CFG([("S", PTerminal("foo")), ("S", PTerminal("bar"))]) rhs = CFG([("S", PTerminal("foo")), ("S", PTerminal("bar"))]) assert lhs == rhs
def test_equal(): assert (PTerminal("I", features={"person": "1"}) == PTerminal("I", features={"person": "1"}))
def test_simple_terminal(): cfg = CFG([("S", PTerminal("foo"))]) assert list(cfg.permutation_values("S")) == [("foo", )]
def test_equality(): assert (PUnion([PTerminal("foo"), PTerminal("bar") ]) == PUnion([PTerminal("foo"), PTerminal("bar")]))