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 singleMultiParentedTree
is used as multiple children of the same parent, then that parent will appear multiple times in itsparents()
method.MultiParentedTrees
should never be used in the same tree asTrees
orParentedTrees
. Mixing tree implementations may result in incorrect parent pointers and inTypeError
exceptions.- 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 ofparent
, 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 theparent_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)
- 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 singleParentedTree
as a child of more than one parent (or as multiple children of the same parent) will cause aValueError
exception to be raised.ParentedTrees
should never be used in the same tree asTrees
orMultiParentedTrees
. Mixing tree implementations may result in incorrect parent pointers and inTypeError
exceptions.- parent_index()[source]¶
The index of this tree in its parent. I.e.,
ptree.parent()[ptree.parent_index()] is ptree
. Note thatptree.parent_index()
is not necessarily equal toptree.parent.index(ptree)
, since theindex()
method returns the first child that is equal to its argument.