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

Class ParentedTree

source code

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

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

Instance Methods [hide private]
 
__init__(self, node_or_str, children=None)
Construct a new tree.
source code
 
_frozen_class(self) source code
 
_get_parent_index(self) source code
 
_get_left_sibling(self) source code
 
_get_right_sibling(self) source code
 
_get_treeposition(self) source code
 
_get_root(self) 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, treepositions, 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]
  _parent
The parent of this Tree, or None if it has no parent.
Properties [hide private]
  parent
The parent of this tree, or None if it has no parent.
  parent_index
The index of this tree in its parent.
  left_sibling
The left sibling of this tree, or None if it has none.
  right_sibling
The right sibling of this tree, or None if it has none.
  root
The root of this tree.
  treeposition
The tree position of this tree, relative to the root of the tree.
  treepos
The tree position of this tree, relative to the root of the 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

_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)

Property Details [hide private]

parent

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

Get Method:
unreachable(self)

parent_index

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.

Get Method:
_get_parent_index(self)

left_sibling

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

Get Method:
_get_left_sibling(self)

right_sibling

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

Get Method:
_get_right_sibling(self)

root

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.

Get Method:
_get_root(self)

treeposition

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

Get Method:
_get_treeposition(self)

treepos

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

Get Method:
_get_treeposition(self)