Exemple #1
0
    def _create_tree_helper(builder: TreeBuilder, sentence: 'Sentence',
                            root: Token,
                            children_tokens: Dict[str, List[Token]]) -> None:
        """
        Method to create a tree from a sentence given the root token.

        Args:
            builder: The TreeBuilder currently being used to create the Tree.
            sentence: The sentence to construct the tree from.
            root: The current token we are constructing the tree at.
            children_tokens: A dictionary from token id to children tokens.

        Returns:
            A Tree constructed given the sentence structure.
        """
        try:
            tokens = children_tokens[root.id]
        except KeyError:
            tokens = []

        for token in tokens:
            builder.add_child(data=token, move=True)
            Sentence._create_tree_helper(builder, sentence, token,
                                         children_tokens)
            builder.move_to_parent()
Exemple #2
0
def test_cannot_move_up_on_root():
    """
    Test that when at the root node, the builder cannot move up to a parent.
    """
    builder = TreeBuilder()

    builder.create_root(0)

    with pytest.raises(ValueError):
        builder.move_to_parent()
Exemple #3
0
def test_cannot_operate_on_rootless():
    """
    Verify that operations do not work on a TreeBuilder when no root is created.
    """
    builder = TreeBuilder()

    with pytest.raises(ValueError):
        builder.move_to_root()

    with pytest.raises(ValueError):
        builder.move_to_parent()

    with pytest.raises(ValueError):
        builder.build()
Exemple #4
0
def test_parent_assigment():
    """
    Test that children tree's parents are properly assigned.
    """
    builder = TreeBuilder()
    builder.create_root(0)
    builder.add_child(2, move=True)
    builder.add_child(13)
    builder.move_to_parent()
    builder.add_child(7)

    t = builder.build()

    assert t.parent is None
    assert t[0].parent == t
    assert t[1].parent == t
    assert t[0][0].parent == t[0]
Exemple #5
0
def test_len_children():
    """
    Test that we can properly get the number of children.
    """
    builder = TreeBuilder()
    builder.create_root(0)

    data = list(range(2, 15, 3))
    subdata = [0, 1, 2, 3, 4]
    for datum in data:
        builder.add_child(datum, move=True)

        for subdatum in subdata:
            builder.add_child(subdatum)

        builder.move_to_parent()
    t = builder.build()

    assert len(t) == len(data)
    for child in t:
        assert len(child) == len(subdata)
Exemple #6
0
def test_after_creation_copy():
    """
    Test that a single TreeBuilder can be used multiple times properly.
    """
    builder = TreeBuilder()
    builder.create_root(0)
    builder.add_child(2, move=True)
    builder.add_child(13)
    builder.move_to_parent()
    builder.add_child(7)

    t1 = builder.build()

    builder.move_to_root()
    builder.set_data(4)
    builder.add_child(3, move=True)
    builder.add_child(15)

    t2 = builder.build()

    assert t2 is not t1
    assert t2[0] is not t1[0]
    assert t2[0][0] is not t1[0][0]
    assert t2[1] is not t1[1]

    assert t2.data == 4
    assert t2[0].data == 2
    assert t2[0][0].data == 13
    assert t2[1].data == 7
    assert t2[2].data == 3
    assert t2[2][0].data == 15

    assert len(t2) == 3
    assert len(t2[0]) == 1
    assert len(t2[1]) == 0
    assert len(t2[2]) == 1