Ejemplo n.º 1
0
    def test_parse_with_ambiguity(self):
        # Define new rules
        rules = ['S->NP | VP', 'NP->book', 'VP->book']
        earley = EarleyParser(Grammar(rules))
        earley.parse('book')

        self.assertIn(State(earley.DUMMY_CHAR,['S'],1,0,1), earley.chart[1])
        self.assertIn(State('S',['NP'],1,0,1), earley.chart[1])
        self.assertIn(State('S',['VP'],1,0,1), earley.chart[1])

        self.assertItemsEqual([Tree('S', [Tree('NP', ['book'])]), Tree('S', [Tree('VP', ['book'])])], earley.tree())

        print earley.tree()
Ejemplo n.º 2
0
    def setUp(self):
        rules = [
            'S -> NP VP',
            'NP -> Det N',
            'VP -> VT NP',
            'VP -> VI PP',
            'PP -> P NP',
            'Det -> a',
            'N -> circle | square | triangle',
            'VT -> touches',
            'VI -> is',
            'P -> above | below'
        ]
        grammar = Grammar(rules)

        self.earley = EarleyParser(grammar)
        self.earley.words = 'a circle touches a triangle'.split(' ')
        self.earley.chart = [[] for _ in range(5)]