示例#1
0
    def test_cyk_parse_small_valid2(self):

        s = "the teacher will lecture today in the lecture hall"
        #s = "the teacher gave the lecture"
        #s = "the the teacher gave the lecture"
        #s = "teacher the gave the lecture"

        str_tree1 = inspect.cleandoc("""
            (TOP
                (NP
                  (DT the)
                  (NN teacher)
                )
                (VP
                  (MD will)
                  (VP
                    (VB lecture)
                    (NP
                      (NN today)
                      (PP
                          (IN in)
                          (NP
                            (DT the)
                            (NN lecture)
                            (NN hall)
                          )
                      )
                    )
                  )
                )
                (. .)
            )""")
        tree1 = Tree.from_string(str_tree1)

        rules = []
        t1_col = Tree.collapse_unary(tree1)
        t1_cnf = Tree.chomsky_normal_form(t1_col)
        rules += Tree.productions(t1_cnf)

        print("PCFG: ")
        cyk = Cyk(rules)
        print(cyk.tree.pretty_pcgf(cyk.pcfg))
        cyk.parse(s)

        print("Actual Parse Tree: ")
        print(cyk.parse_tree)

        print("CYK Table: ")
        print(cyk.table_rules)
示例#2
0
    def test_cyk_parse_small(self):

        # rules = [
        #     ('S', ['NP', 'VP', '.']),
        #     ('NP', ['DET', 'N']),
        #     ('NP', ['NP', 'PP']),
        #     ('PP', ['P', 'NP']),
        #     ('VP', ['VP', 'PP']),
        #     ('VP', ['saw']),
        #     ('DET', ['the']),
        #     ('NP', ['I']),
        #     ('N', ['man']),
        #     ('N', ['telescope']),
        #     ('P', ['with']),
        #     ('V', ['saw']),
        #     ('N', ['cat']),
        #     ('N', ['dog']),
        #     ('N', ['pig']),
        #     ('N', ['hill']),
        #     ('N', ['park']),
        #     ('N', ['roof']),
        #     ('P', ['from']),
        #     ('P', ['on']),
        #     ('P', ['in'])
        # ]

        rules = [
            ('S', ['NP', 'VP']),
            ('NP', ['DT', 'NN']),
            ('VP', ['VB', 'NP']),
            ('DT', ['the']),
            ('NN', ['teacher']),
            ('NN', ['lecture']),
            ('VB', ['gave']),
        ]

        cyk = Cyk(rules)
        pcfg = cyk.tree.convert_to_pcfg(rules)
        #cyk.rules =
        cyk.pcfg = pcfg
        pcfg_pretty = cyk.tree.pretty_pcgf(pcfg)
        #actual = format(pcfg)
        print(pcfg_pretty)

        #sentence = "I saw the man with the telescope on the hill"
        #sentence = "I saw"
        sentence = "the teacher gave the lecture"

        cyk.parse(sentence)
示例#3
0
    def test_cyk_parse_small_valid3(self):

        s = "the lecture gave the teacher"
        #s = "the the teacher gave the lecture"
        #s = "teacher the gave the lecture"

        rules = [
            ('S', ['NP', 'VP']),
            ('NP', ['DT', 'NN']),
            ('VP', ['VB', 'NP']),
            ('DT', ['the']),
            ('NN', ['teacher']),
            ('NN', ['lecture']),
            ('VB', ['gave']),
        ]

        print("Expected Parse Tree: ")
        expect_parse_tree = inspect.cleandoc("""
            (S
                (NP
                    (DT the)
                    (NN lecture)
                )
                (VP
                    (VB gave)
                    (NP
                        (DT the)
                        (NN teacher)
                    )
                )
            )""")
        expect_parse_tree = os.linesep.join([st for st in expect_parse_tree.splitlines() if st])
        expect_tree = Tree.from_string(expect_parse_tree)
        expect_parse_tree = expect_tree.pretty()
        print(expect_parse_tree)

        cyk = Cyk(rules)
        #cyk.rules = rules
        cyk.parse(s)

        print("Actual Parse Tree: ")
        print(cyk.parse_tree)

        eq_(cyk.valid, True)
        eq_(cyk.parse_tree, expect_parse_tree)
示例#4
0
    def test_cyk_parse_small_invalid3(self):

        #s = "the teacher gave the lecture"
        #s = "gave lecture the teacher the"
        #s = "the the teacher gave the lecture"
        s = "teacher the gave the lecture"

        rules = [
            ('S', ['NP', 'VP']),
            ('NP', ['DT', 'NN']),
            ('VP', ['VB', 'NP']),
            ('DT', ['the']),
            ('NN', ['teacher']),
            ('NN', ['lecture']),
            ('VB', ['gave']),
        ]

        cyk = Cyk(rules)
        cyk.parse(s)

        eq_(cyk.valid, False)
        eq_(cyk.parse_tree, '')