nltk.tree.parented module

class nltk.tree.parented.MultiParentedTree[source]

Bases: AbstractParentedTree

A Tree that automatically maintains parent pointers for multi-parented trees. The following are methods for querying the structure of a multi-parented tree: parents(), parent_indices(), left_siblings(), right_siblings(), roots, treepositions.

Each MultiParentedTree may have zero or more parents. In particular, subtrees may be shared. If a single MultiParentedTree is used as multiple children of the same parent, then that parent will appear multiple times in its parents() method.

MultiParentedTrees should never be used in the same tree as Trees or ParentedTrees. Mixing tree implementations may result in incorrect parent pointers and in TypeError exceptions.

__init__(node, children=None)[source]
left_siblings()[source]

A list of all left siblings of this tree, in any of its parent trees. A tree may be its own left sibling if it is used as multiple contiguous children of the same parent. A tree may appear multiple times in this list if it is the left sibling of this tree with respect to multiple parents.

Type

list(MultiParentedTree)

parent_indices(parent)[source]

Return a list of the indices where this tree occurs as a child of parent. If this child does not occur as a child of parent, then the empty list is returned. The following is always true:

for parent_index in ptree.parent_indices(parent):
    parent[parent_index] is ptree
parents()[source]

The set of parents of this tree. If this tree has no parents, then parents is the empty set. To check if a tree is used as multiple children of the same parent, use the parent_indices() method.

Type

list(MultiParentedTree)

right_siblings()[source]

A list of all right siblings of this tree, in any of its parent trees. A tree may be its own right sibling if it is used as multiple contiguous children of the same parent. A tree may appear multiple times in this list if it is the right sibling of this tree with respect to multiple parents.

Type

list(MultiParentedTree)

roots()[source]

The set of all roots of this tree. This set is formed by tracing all possible parent paths until trees with no parents are found.

Type

list(MultiParentedTree)

treepositions(root)[source]

Return a list of all tree positions that can be used to reach this multi-parented tree starting from root. I.e., the following is always true:

for treepos in ptree.treepositions(root):
    root[treepos] is ptree
class nltk.tree.parented.ParentedTree[source]

Bases: AbstractParentedTree

A Tree that automatically maintains parent pointers for single-parented trees. The following are methods for querying the structure of a parented tree: parent, parent_index, left_sibling, right_sibling, root, treeposition.

Each ParentedTree may have at most one parent. In particular, subtrees may not be shared. Any attempt to reuse a single ParentedTree as a child of more than one parent (or as multiple children of the same parent) will cause a ValueError exception to be raised.

ParentedTrees should never be used in the same tree as Trees or MultiParentedTrees. Mixing tree implementations may result in incorrect parent pointers and in TypeError exceptions.

__init__(node, children=None)[source]
copy(deep=False)[source]

Return a shallow copy of the list.

left_sibling()[source]

The left sibling of this tree, or None if it has none.

parent()[source]

The parent of this tree, or None if it has no parent.

parent_index()[source]

The index of this tree in its parent. I.e., ptree.parent()[ptree.parent_index()] is ptree. Note that ptree.parent_index() is not necessarily equal to ptree.parent.index(ptree), since the index() method returns the first child that is equal to its argument.

right_sibling()[source]

The right sibling of this tree, or None if it has none.

root()[source]

The root of this tree. I.e., the unique ancestor of this tree whose parent is None. If ptree.parent() is None, then ptree is its own root.

treeposition()[source]

The tree position of this tree, relative to the root of the tree. I.e., ptree.root[ptree.treeposition] is ptree.