def setUp(self): self.start = "NP" self.productions = [ Production("NP", ["N"]), Production("NP", ["D", "N"]) ] self.grammar = Grammar(self.start, self.productions)
class TestGrammarProductions(unittest.TestCase): """ Test case for the productions(self, lhs=None) This should returns the ``productions`` of the ``grammar`` filtered by left-hand side """ def setUp(self): self.productions = [ Production("NP", ["N"]), Production("NP", ["D", "N"]), Production("N", ["'fall'"]), Production("N", ["'spring'"]), Production("N", ["'leaves'"]), Production("N", ["'dog'"]), Production("N", ["'cat'"]), Production("D", ["'the'"]) ] self.grammar = Grammar("NP", self.productions) def test_lhs_is_none(self): res = self.productions self.assertIsInstance(self.grammar.productions(), list) self.assertEqual(res, self.grammar.productions(), msg=("Should return all ``productions``\ of this``grammar``")) def test_standard_use_case(self): res = self.productions[:2] self.assertIsInstance(self.grammar.productions(lhs="NP"), list) self.assertCountEqual(res, self.grammar.productions(lhs="NP"))
def setUp(self): self.start = "NP" self.productions = [ Production("NP", ["N"]), Production("NP", ["D", "N"]), Production("N", ["'fall'"]), Production("N", ["'spring'"]), Production("N", ["'leaves'"]), Production("D", ["'the'"]) ] self.grammar = Grammar(self.start, self.productions)
class TestGrammarCalculateIndexes(unittest.TestCase): def setUp(self): self.start = "NP" self.productions = [ Production("NP", ["N"]), Production("NP", ["D", "N"]), Production("N", ["'fall'"]), Production("N", ["'spring'"]), Production("N", ["'leaves'"]), Production("D", ["'the'"]) ] self.grammar = Grammar(self.start, self.productions) def test_calculate_lhs_indexes(self): # TODO: We could remove this """ We'll just check that this ``grammar``._lhs_index is properly set after init """ res = {} res["NP"] = [Production("NP", ["N"]), Production("NP", ["D", "N"])] res["N"] = [ Production("N", ["'fall'"]), Production("N", ["'spring'"]), Production("N", ["'leaves'"]) ] res["D"] = [Production("D", ["'the'"])] self.assertEqual(res, self.grammar._calculate_lhs_index())
def setUp(self): grammar_as_string = """ NP -> N | D N N -> 'fall' | 'spring' | 'leaves' | 'dog' | 'cat' D -> 'the' """ self.grammar = Grammar.parse_grammar(grammar_as_string) self.parser = TopDownParser(self.grammar)
def setUp(self): grammar_as_string = """ NP -> N | D N N -> 'fall' | 'srpring' | 'leaves' D -> 'the' """ grammar = Grammar.parse_grammar(grammar_as_string) self.parser = TopDownParser(grammar) self.tokens = ["hello", "world"]
def test_parse_grammar(self): # TODO: define __eq__ for Grammar object so we can test equality """ Test that the string representation of a grammar obtained using parse_grammar is equal to one created with constructor. """ start = "NP" productions = [ Production("NP", ["N"]), Production("NP", ["D", "N"]), Production("N", ["'fall'"]), Production("N", ["'spring'"]), Production("N", ["'leaves'"]), Production("N", ["'dog'"]), Production("N", ["'cat'"]), Production("D", ["'the'"]) ] res = Grammar(start, productions) self.assertEqual(str(res), str(Grammar.parse_grammar(self.grammar_as_string)))
def setUp(self): grammar_as_string = """ NP -> N | D N N -> 'fall' | 'spring' | 'leaves' D -> 'the' """ grammar = Grammar.parse_grammar(grammar_as_string) self.parser = TopDownParser(grammar) self.tree = Tree("NP", [Tree("D", []), Tree("N", [])]) self.frontier = [(0, ), (1, )] self.tokens = ["'the'", "'fall'"]
def setUp(self): grammar_as_string = """ S -> NP VP NP -> N | D N | Adj N | D Adj N VP -> V NP | V |V NP NP N -> 'fall' | 'spring' | 'leaves' V -> 'spring' | 'leaves' | 'fall' D -> 'the' Adj -> 'fall' | 'spring' | 'purple' """ grammar = Grammar.parse_grammar(grammar_as_string) self.parser = TopDownParser(grammar)
def setUp(self): grammar_as_string = """ S -> NP VP NP -> N | D N | Adj N | D Adj N VP -> V NP | V |V NP NP N -> 'fall' | 'spring' | 'leaves' V -> 'spring' | 'leaves' | 'fall' D -> 'the' Adj -> 'fall' | 'spring' | 'purple' """ grammar = Grammar.parse_grammar(grammar_as_string) self.parser = TopDownParser(grammar) self.tree = Tree("S", []) self.tokens = ["'fall'", "'leaves'", "'fall'"] self.frontier = [()]
class TestGrammar(unittest.TestCase): def setUp(self): self.start = "NP" self.productions = [ Production("NP", ["N"]), Production("NP", ["D", "N"]) ] self.grammar = Grammar(self.start, self.productions) def test_str(self): """ Should return the verbose representation of the ``grammar`` as a string """ res = ("Grammar starting with: \"%s\"\n" "2 productions:\nNP -> N\nNP -> D N" % (self.grammar.start())) self.assertEqual(res, str(self.grammar)) def test_repr(self): """ Should return the concise representation of the ``grammar``as a string """ res = "Grammar with 2 productions starting with \"NP\"" self.assertEqual(res, repr(self.grammar))
def test_return_grammar_object(self): """ parse_grammar should return an array """ self.assertIsInstance(Grammar.parse_grammar(self.grammar_as_string), Grammar)
VP -> V NP | V | V NP NP N -> 'fall' | 'spring' | 'leaves' | 'dog' | 'cat' V -> 'spring' | 'leaves' | 'fall' | 'left' D -> 'the' C -> 'and' Adj -> 'fall' | 'spring' | 'purple' | 'left' """ sentences = [ "Fall leaves fall.", "Fall leaves fall and spring leaves spring.", "The fall leaves left.", "The purple dog left", "The dog and cat left" ] grammar = Grammar.parse_grammar(grammar_as_string) parser = TopDownParser(grammar) tokens = tokenize(sentences[4]) for sentence in sentences: tokens = tokenize(sentence) parse = parser.parse(tokens) results = [p for p in parse] print("==========================") print(sentence) print("--------------------------") for index, parse in enumerate(results): print("Parse #%d:\n%s" % (index, parse)) print("--------------------------") print("Count: %d" % len(results))