Package nltk_lite :: Package parse :: Module tree :: Class Tree
[hide private]
[frames] | no frames]

Class Tree

source code

object --+    
         |    
      list --+
             |
            Tree
Known Subclasses:
ImmutableTree, ProbabilisticTree, contrib.hole.FOLTree

A hierarchical structure.

Each Tree represents a single hierarchical grouping of leaves and subtrees. For example, each constituent in a syntax tree is represented by a single Tree.

A tree's children are encoded as a list of leaves and subtrees, where a leaf is a basic (non-tree) value; and a subtree is a nested Tree.

Any other properties that a Tree defines are known as node properties, and are used to add information about individual hierarchical groupings. For example, syntax trees use a NODE property to label syntactic constituents with phrase tags, such as "NP" and"VP".

Several Tree methods use tree positions to specify children or descendants of a tree. Tree positions are defined as follows:

I.e., every tree position is either a single index i, specifying self[i]; or a sequence (i1, i2, ..., iN), specifying self[i1][i2]...[iN].

Instance Methods [hide private]
 
__add__(self, v)
x+y
source code
 
__cmp__(self, other) source code
 
__delitem__(self, index)
del x[y]
source code
 
__eq__(self, other)
x==y
source code
 
__ge__(self, other)
x>=y
source code
 
__getitem__(self, index)
x[y]
source code
 
__gt__(self, other)
x>y
source code
 
__init__(self, node, children)
Construct a new tree.
source code
 
__le__(self, other)
x<=y
source code
 
__lt__(self, other)
x<y
source code
 
__mul__(self, v)
x*n
source code
 
__ne__(self, other)
x!=y
source code
 
__radd__(self, v) source code
 
__repr__(self)
repr(x)
source code
 
__rmul__(self, v)
n*x
source code
 
__setitem__(self, index, value)
x[i]=y
source code
 
__str__(self)
str(x)
source code
 
_frozen_class(self) source code
 
_ppflat(self, nodesep, parens, quotes) source code
 
copy(self, deep=True) source code
 
draw(self)
Open a new window containing a graphical diagram of this tree.
source code
Tree
flatten(self)
Returns: a tree consisting of this tree's root connected directly to its leaves, omitting all intervening non-terminal nodes.
source code
 
freeze(self, leaf_freezer=None) source code
int
height(self)
Returns: The height of this tree.
source code
list
leaves(self)
Returns: a list containing this tree's leaves.
source code
string
pp(self, margin=70, indent=0, nodesep=':', parens='()', quotes=True)
Returns: A pretty-printed string representation of this tree.
source code
string
pp_latex_qtree(self)
Returns a representation of the tree compatible with the LaTeX qtree package.
source code
 
pp_treebank(self, margin=70, indent=0) source code
list of Productions
productions(self)
Generate the productions that correspond to the non-terminal nodes of the tree.
source code
 
subtrees(self, filter=None)
Generate all the subtrees of this tree, optionally restricted to trees matching the filter function.
source code
 
treepositions(self, order='preorder') source code

Inherited from list: __contains__, __delslice__, __getattribute__, __getslice__, __hash__, __iadd__, __imul__, __iter__, __len__, __new__, __reversed__, __setslice__, append, count, extend, index, insert, pop, remove, reverse, sort

Inherited from object: __delattr__, __reduce__, __reduce_ex__, __setattr__

Class Methods [hide private]
 
convert(cls, val)
Convert a tree between different subtypes of Tree.
source code
Properties [hide private]

Inherited from object: __class__

Method Details [hide private]

__add__(self, v)
(Addition operator)

source code 

x+y

Overrides: list.__add__
(inherited documentation)

__delitem__(self, index)
(Index deletion operator)

source code 

del x[y]

Overrides: list.__delitem__
(inherited documentation)

__eq__(self, other)
(Equality operator)

source code 

x==y

Overrides: list.__eq__
(inherited documentation)

__ge__(self, other)
(Greater-than-or-equals operator)

source code 

x>=y

Overrides: list.__ge__
(inherited documentation)

__getitem__(self, index)
(Indexing operator)

source code 

x[y]

Overrides: list.__getitem__
(inherited documentation)

__gt__(self, other)
(Greater-than operator)

source code 

x>y

Overrides: list.__gt__
(inherited documentation)

__init__(self, node, children)
(Constructor)

source code 

Construct a new tree.

Returns:
new list

Overrides: list.__init__

__le__(self, other)
(Less-than-or-equals operator)

source code 

x<=y

Overrides: list.__le__
(inherited documentation)

__lt__(self, other)
(Less-than operator)

source code 

x<y

Overrides: list.__lt__
(inherited documentation)

__mul__(self, v)

source code 

x*n

Overrides: list.__mul__
(inherited documentation)

__ne__(self, other)

source code 

x!=y

Overrides: list.__ne__
(inherited documentation)

__repr__(self)
(Representation operator)

source code 

repr(x)

Overrides: list.__repr__
(inherited documentation)

__rmul__(self, v)

source code 

n*x

Overrides: list.__rmul__
(inherited documentation)

__setitem__(self, index, value)
(Index assignment operator)

source code 

x[i]=y

Overrides: list.__setitem__
(inherited documentation)

__str__(self)
(Informal representation operator)

source code 

str(x)

Overrides: object.__str__
(inherited documentation)

convert(cls, val)
Class Method

source code 

Convert a tree between different subtypes of Tree. cls determines which class will be used to encode the new tree.

Parameters:
  • val (Tree) - The tree that should be converted.
Returns:
The new Tree.

flatten(self)

source code 
Returns: Tree
a tree consisting of this tree's root connected directly to its leaves, omitting all intervening non-terminal nodes.

height(self)

source code 
Returns: int
The height of this tree. The height of a tree containing no children is 1; the height of a tree containing only leaves is 2; and the height of any other tree is one plus the maximum of its children's heights.

leaves(self)

source code 
Returns: list
a list containing this tree's leaves. The order of leaves in the tuple reflects the order of the leaves in the tree's hierarchical structure.

pp(self, margin=70, indent=0, nodesep=':', parens='()', quotes=True)

source code 
Parameters:
  • margin (int) - The right margin at which to do line-wrapping.
  • indent (int) - The indentation level at which printing begins. This number is used to decide how far to indent subsequent lines.
  • nodesep - A string that is used to separate the node from the children. E.g., the default value ':' gives trees like (S: (NP: I) (VP: (V: saw) (NP: it))).
Returns: string
A pretty-printed string representation of this tree.

pp_latex_qtree(self)

source code 

Returns a representation of the tree compatible with the LaTeX qtree package. This consists of the string \Tree followed by the parse tree represented in bracketed notation.

For example, the following result was generated from a parse tree of the sentence The announcement astounded us:

 \Tree [.I'' [.N'' [.D The ] [.N' [.N announcement ] ] ]
     [.I' [.V'' [.V' [.V astounded ] [.N'' [.N' [.N us ] ] ] ] ] ] ]

See http://www.ling.upenn.edu/advice/latex.html for the LaTeX style file for the qtree package.

Returns: string
A latex qtree representation of this tree.

productions(self)

source code 

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.

Returns: list of Productions

treepositions(self, order='preorder')

source code 
Parameters:
  • order - One of: preorder, postorder, bothorder, leaves.