Exemple #1
0
 def setUp(self):
     self.start = "NP"
     self.productions = [
         Production("NP", ["N"]),
         Production("NP", ["D", "N"])
     ]
     self.grammar = Grammar(self.start, self.productions)
Exemple #2
0
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"))
Exemple #3
0
 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)
Exemple #4
0
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())
Exemple #5
0
 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)
Exemple #6
0
 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"]
Exemple #7
0
 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)))
Exemple #8
0
 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'"]
Exemple #9
0
 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)
Exemple #10
0
 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 = [()]
Exemple #11
0
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))
Exemple #12
0
 def test_return_grammar_object(self):
     """ parse_grammar should return an array
     """
     self.assertIsInstance(Grammar.parse_grammar(self.grammar_as_string),
                           Grammar)
Exemple #13
0
        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))