Ejemplo n.º 1
0
 def setUp(self):
     self.node = "node"
     self.child_1 = Tree(node="child_1", children=["grandchild_1",
                                                   "grandchild_2",
                                                   "grandchild_3"])
     self.child_2 = Tree(node="child_2")
     self.child_3 = "child_3"
     self.tree = Tree(node=self.node, children=[self.child_1, self.child_2,
                                                self.child_3])
Ejemplo n.º 2
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'"]
Ejemplo n.º 3
0
class TestTree(unittest.TestCase):
    """ Basic tests for instantiation of ``tree``
    """

    def setUp(self):
        self.node = "root_node"
        self.child_1 = Tree(node="child_1", children=["grandchild_1",
                                                      "grandchild_2",
                                                      "grandchild_3"])
        self.child_2 = Tree(node="child_2")
        self.child_3 = "child_3"
        self.children = [self.child_1, self.child_2, self.child_3]
        self.tree = Tree(node=self.node, children=self.children)

    def test_node(self):
        res = self.node
        self.assertEqual(res, self.tree.node(), "Should return the root node \
                      of the tree")

    def test_children(self):
        res = tuple(self.children)
        self.assertEqual(res, self.tree.children(), "Should return \
                         the children of the tree")

    def test_str_is_string(self):
        self.assertIsInstance(str(self.tree), str, msg="String expected")

    def test_str(self):
        res = ("(root_node, (child_1, grandchild_1, grandchild_2, "
               "grandchild_3), (child_2), child_3)")
        self.assertEqual(res, str(self.tree))

    def test_repr_is_string(self):
        self.assertIsInstance(repr(self.tree), str, msg="String expected")

    def test_repr(self):
        res = "(%r, %r)" % (self.node, tuple(self.children))
        self.assertEqual(res, repr(self.tree))

    def test_copy(self):
        copy = self.tree.copy()
        self.assertEqual(copy, self.tree, "Copy should be equal")

    def test_len(self):
        res = 3
        self.assertEqual(res, len(self.tree),
                         "Length of tree should be equal to 6")
Ejemplo n.º 4
0
 def test_parse_with_final_match(self):
     self.tree = Tree("N", ["'fall'"])
     self.tokens = ["fall"]
     self.frontier = [(0, )]
     res = self.tree
     parse = self.parser._parse(self.tokens, self.tree, self.frontier)
     new_tree, new_frontier = next(parse)
     self.assertListEqual(res, new_tree)
Ejemplo n.º 5
0
class TestLeaves(unittest.TestCase):
    """ Test case for getting the leaves from a ``tree``
    """

    def setUp(self):
        self.node = "node"
        self.child_1 = Tree(node="child_1", children=["grandchild1-1",
                                                      "grandchild1-2",
                                                      "grandchild1-3"])
        self.child_2 = Tree(node="child_2")
        self.child_3 = "child_3"
        self.tree = Tree(node=self.node, children=[self.child_1, self.child_2,
                                                   self.child_3])

    def test_leaves(self):
        res = []
        # TODO: improve this (better flattening of all lists)
        res += self.child_1.children()
        res.append(self.child_2.node())
        res.append(self.child_3)
        self.assertEqual(res, self.tree.leaves())
Ejemplo n.º 6
0
    def parse(self, tokens):
        """ Parse a list of tokens and return the possible tree based on the
            ``grammar of this ``Parser``
            Args:
                tokens: a string
        """
        if len(tokens) == 0:
            raise ValueError("Tokens can't be empy")

        # Tokenize if needed
        if not isinstance(tokens, list):
            tokens = tokenize(tokens)
        root_tree = Tree(self._grammar.start(), [])
        frontier = [()]
        return self._parse(tokens, root_tree, frontier)
Ejemplo n.º 7
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 = [()]
Ejemplo n.º 8
0
 def test_tree_from_production_with_terminal(self):
     self.production = Production("N", ["'fall'"])
     res = Tree("N", ["'fall'"])
     self.assertEqual(res, tree_from_production(self.production))
Ejemplo n.º 9
0
 def test_tree_from_production(self):
     res = Tree("S", [Tree("NP", []), Tree("VP", [])])
     self.assertEqual(res, tree_from_production(self.production))
Ejemplo n.º 10
0
 def test_set_array(self):
     """ Test with a list of length > 1 """
     res = Tree("S", [Tree("NP", ["D", self.new_child]), "VP"])
     index = [0, 1]
     self.tree[index] = self.new_child
     self.assertEqual(res, self.tree)
Ejemplo n.º 11
0
 def test_set_tuple(self):
     """ Test with a tuple of length > 1 """
     res = Tree("S", [Tree("NP", ["D", self.new_child]), "VP"])
     index = (0, 1)
     self.tree[index] = self.new_child
     self.assertEqual(res, self.tree)
Ejemplo n.º 12
0
 def test_expand_with_empty_frontier(self):
     """ Assert ValueError is raised if frontier is empty """
     tree = Tree("S", [])
     frontier = []
     with self.assertRaises(ValueError):
         next(self.parser._expand(self.tokens, tree, frontier))
Ejemplo n.º 13
0
 def test_syntactically_ambiguous(self):
     tokens = ["fall", "leaves", "fall"]
     tree_1 = Tree("S",
                   [Tree("NP", [Tree("N", ["'fall'"])]),
                    Tree("VP", [Tree("V", ["'leaves'"]),
                                Tree("NP", [Tree("N", ["'fall'"])])])])
     tree_2 = Tree("S",
                   [Tree("NP", [Tree("Adj", ["'fall'"]),
                                Tree("N", ["'leaves'"])]),
                    Tree("VP", [Tree("V", ["'fall'"])])])
     res = [(tree_1, []), (tree_2, [])]
     parse = self.parser.parse(tokens)
     self.assertListEqual(res, [p for p in parse])
Ejemplo n.º 14
0
 def test_syntactically_unambiguous(self):
     tokens = ["fall", "leaves"]
     res = Tree("S", [Tree("NP", [Tree("N", ["'fall'"])]),
                      Tree("VP", [Tree("V", ["'leaves'"])])])
     parse = self.parser.parse(tokens)
     self.assertEqual(res, next(parse)[0])
Ejemplo n.º 15
0
 def test_expand_non_expandable(self):
     tree = Tree("NP", [Tree("D", ["'the'"]), Tree("N", [])])
     frontier = [(0, 0), (1, )]
     with self.assertRaises(StopIteration):
         next(self.parser._expand(self.tokens, tree, frontier))
Ejemplo n.º 16
0
 def test_expand_with_empty_tokens(self):
     tree = Tree("S", [])
     frontier = [()]
     self.tokens = []
     with self.assertRaises(StopIteration):
         next(self.parser._expand(self.tokens, tree, frontier))
Ejemplo n.º 17
0
 def setUp(self):
     self.tree = Tree("S", [Tree("NP", ["D", "N"]), "VP"])
     self.new_child = Tree("VP", ["V", "N"])
Ejemplo n.º 18
0
 def test_set_existing(self):
     """ Test set/replace an existing node of the tree with an integer index
     """
     res = Tree("S", [Tree("NP", ["D", "N"]), self.new_child])
     self.tree[1] = self.new_child
     self.assertEqual(res, self.tree)