class documentation

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.

Method __init__ Undocumented
Method left_siblings 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.
Method parent_indices 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:
Method parents 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.
Method right_siblings 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.
Method roots 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.
Method treepositions 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:
Method _delparent Update the parent pointer of child to not point to self. This method is only called if the type of child is Tree; i.e., it is not called when removing a leaf from a tree. This method is always called before the child is actually removed from the child list of ...
Method _frozen_class Undocumented
Method _get_parent_indices Undocumented
Method _get_roots_helper Undocumented
Method _setparent Update the parent pointer of child to point to self. This method is only called if the type of child is Tree; i.e., it is not called when adding a leaf to a tree. This method is always called before the child is actually added to the child list of ...
Instance Variable _parents A list of this tree's parents. This list should not contain duplicates, even if a parent contains this tree multiple times.

Inherited from AbstractParentedTree:

Method __delitem__ Undocumented
Method __delslice__ Undocumented
Method __getslice__ Undocumented
Method __setitem__ Undocumented
Method __setslice__ Undocumented
Method append Undocumented
Method extend Undocumented
Method insert Undocumented
Method pop Undocumented
Method remove Undocumented

Inherited from Tree (via AbstractParentedTree):

Class Method convert Convert a tree between different subtypes of Tree. cls determines which class will be used to encode the new tree.
Class Method fromlist Convert nested lists to a NLTK Tree
Class Method fromstring Read a bracketed tree string and return the resulting tree. Trees are represented as nested brackettings, such as:
Method __add__ Undocumented
Method __copy__ Undocumented
Method __deepcopy__ Undocumented
Method __eq__ Undocumented
Method __getitem__ Undocumented
Method __lt__ Undocumented
Method __mul__ Undocumented
Method __radd__ Undocumented
Method __repr__ Undocumented
Method __rmul__ Undocumented
Method __str__ Undocumented
Method chomsky_normal_form This method can modify a tree in three ways:
Method collapse_unary Collapse subtrees with a single child (ie. unary productions) into a new non-terminal (Tree node) joined by 'joinChar'. This is useful when working with algorithms that do not allow unary productions, and completely removing the unary productions would require loss of useful information...
Method copy Undocumented
Method draw Open a new window containing a graphical diagram of this tree.
Method flatten Return a flat version of the tree, with all non-root non-terminals removed.
Method freeze Undocumented
Method height Return the height of the tree.
Method label Return the node label of the tree.
Method leaf_treeposition No summary
Method leaves Return the leaves of the tree.
Method pformat No summary
Method pformat_latex_qtree Returns a representation of the tree compatible with the LaTeX qtree package. This consists of the string \Tree followed by the tree represented in bracketed notation.
Method pos Return a sequence of pos-tagged words extracted from the tree.
Method pprint Print a string representation of this Tree to 'stream'
Method pretty_print Pretty-print this tree as ASCII or Unicode art. For explanation of the arguments, see the documentation for nltk.treeprettyprinter.TreePrettyPrinter.
Method productions Generate the productions that correspond to the non-terminal nodes of the tree. For each subtree of the form (P: C1 C2 ... Cn) this produces a production of the form P -> C1 C2 ... Cn.
Method set_label Set the node label of the tree.
Method subtrees Generate all the subtrees of this tree, optionally restricted to trees matching the filter function.
Method treeposition_spanning_leaves No summary
Method un_chomsky_normal_form This method modifies the tree in three ways:
Class Variable __ge__ Undocumented
Class Variable __gt__ Undocumented
Class Variable __le__ Undocumented
Class Variable __ne__ Undocumented
Class Variable node Undocumented
Class Method _parse_error Display a friendly error message when parsing a tree string fails. :param s: The string we're parsing. :param match: regexp match of the problem token. :param expecting: what we expected to see instead.
Method _get_node Outdated method to access the node value; use the label() method instead.
Method _pformat_flat Undocumented
Method _repr_png_ Draws and outputs in PNG for ipython. PNG is used instead of PDF, since it can be displayed in the qt console and has wider browser support.
Method _set_node Outdated method to set the node value; use the set_label() method instead.
Instance Variable _label Undocumented
def __init__(self, node, children=None): (source)
def left_siblings(self): (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.

def parent_indices(self, 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
def parents(self): (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.

def right_siblings(self): (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.

def roots(self): (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.

def treepositions(self, 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
def _delparent(self, child, index): (source)

Update the parent pointer of child to not point to self. This method is only called if the type of child is Tree; i.e., it is not called when removing a leaf from a tree. This method is always called before the child is actually removed from the child list of self.

Parameters
child:TreeUndocumented
index:intThe index of child in self.
def _frozen_class(self): (source)

Undocumented

def _get_parent_indices(self): (source)

Undocumented

def _get_roots_helper(self, result): (source)

Undocumented

def _setparent(self, child, index, dry_run=False): (source)

Update the parent pointer of child to point to self. This method is only called if the type of child is Tree; i.e., it is not called when adding a leaf to a tree. This method is always called before the child is actually added to the child list of self.

Parameters
child:TreeUndocumented
index:intThe index of child in self.
dry_runIf true, the don't actually set the child's parent pointer; just check for any error conditions, and raise an exception if one is found.
Raises
TypeErrorIf child is a tree with an impropriate type. Typically, if child is a tree, then its type needs to match the type of self. This prevents mixing of different tree types (single-parented, multi-parented, and non-parented).
_parents: list = (source)

A list of this tree's parents. This list should not contain duplicates, even if a parent contains this tree multiple times.