Package nltk :: Module tree :: Class MultiParentedTree
[hide private]
[frames] | no frames]

Class MultiParentedTree

source code

  object --+            
           |            
        list --+        
               |        
            Tree --+    
                   |    
AbstractParentedTree --+
                       |
                      MultiParentedTree
Known Subclasses:

A Tree that automatically maintains parent pointers for multi-parented trees. The following read-only property values are automatically updated whenever the structure of a multi-parented tree is modified: 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 property.

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.

Instance Methods [hide private]
 
__init__(self, node_or_str, children=None)
Construct a new tree.
source code
 
_frozen_class(self) source code
 
_get_parent_indices(self) source code
 
_get_left_siblings(self) source code
 
_get_right_siblings(self) source code
 
_get_roots(self) source code
 
_get_roots_helper(self, result) source code
 
parent_indices(self, parent)
Return a list of the indices where this tree occurs as a child of parent.
source code
 
treepositions(self, root)
Return a list of all tree positions that can be used to reach this multi-parented tree starting from root.
source code
 
_delparent(self, child, index)
Update child's parent pointer to not point to self.
source code
 
_setparent(self, child, index, dry_run=False)
Update child's parent pointer to point to self.
source code

Inherited from AbstractParentedTree: __delitem__, __delslice__, __getslice__, __setitem__, __setslice__, append, extend, insert, pop, remove

Inherited from Tree: __add__, __eq__, __ge__, __getitem__, __gt__, __le__, __lt__, __mul__, __ne__, __radd__, __repr__, __rmul__, __str__, chomsky_normal_form, collapse_unary, copy, draw, flatten, freeze, height, leaf_treeposition, leaves, pos, pprint, pprint_latex_qtree, productions, subtrees, treeposition_spanning_leaves, un_chomsky_normal_form

Inherited from Tree (private): _pprint_flat

Inherited from list: __contains__, __getattribute__, __hash__, __iadd__, __imul__, __iter__, __len__, __reversed__, count, index, reverse, sort

Inherited from object: __delattr__, __reduce__, __reduce_ex__, __setattr__

Class Methods [hide private]

Inherited from Tree: convert, parse

Inherited from Tree (private): _parse_error

Static Methods [hide private]

Inherited from Tree: __new__

Instance Variables [hide private]
  _parents
A list of this tree's parents.
Properties [hide private]
list of MultiParentedTree parents
The set of parents of this tree.
list of MultiParentedTree left_siblings
A list of all left siblings of this tree, in any of its parent trees.
list of MultiParentedTree right_siblings
A list of all right siblings of this tree, in any of its parent trees.
list of MultiParentedTree roots
The set of all roots of this tree.

Inherited from object: __class__

Method Details [hide private]

__init__(self, node_or_str, children=None)
(Constructor)

source code 

Construct a new tree. This constructor can be called in one of two ways:

  • Tree(node, children) constructs a new tree with the specified node value and list of children.
  • Tree(s) constructs a new tree by parsing the string s. It is equivalent to calling the class method Tree.parse(s).
Overrides: AbstractParentedTree.__init__

_frozen_class(self)

source code 
Overrides: Tree._frozen_class

parent_indices(self, parent)

source code 

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

treepositions(self, root)

source code 

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
Parameters:
  • order - One of: preorder, postorder, bothorder, leaves.
Overrides: Tree.treepositions

_delparent(self, child, index)

source code 

Update child's parent pointer to not point to self. This method is only called if child's type 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 self's child list.

Parameters:
  • index - The index of child in self.
Overrides: AbstractParentedTree._delparent
(inherited documentation)

_setparent(self, child, index, dry_run=False)

source code 

Update child's parent pointer to point to self. This method is only called if child's type 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 self's child list.

Parameters:
  • index - The index of child in self.
  • dry_run - If 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:
  • TypeError - If child is a tree with an impropriate type. Typically, if child is a tree, then its type needs to match self's type. This prevents mixing of different tree types (single-parented, multi-parented, and non-parented).
Overrides: AbstractParentedTree._setparent
(inherited documentation)

Instance Variable Details [hide private]

_parents

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


Property Details [hide private]

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 property.

Get Method:
unreachable(self)
Type:
list of MultiParentedTree

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.

Get Method:
_get_left_siblings(self)
Type:
list of MultiParentedTree

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.

Get Method:
_get_right_siblings(self)
Type:
list of MultiParentedTree

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.

Get Method:
_get_roots(self)
Type:
list of MultiParentedTree