nltk.ParentedTree

class nltk.ParentedTree(node, children=None)[source]

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

Methods

__init__(node[, children])
append(child)
chomsky_normal_form([factor, horzMarkov, ...]) This method can modify a tree in three ways:
collapse_unary([collapsePOS, collapseRoot, ...]) Collapse subtrees with a single child (ie.
convert(tree) Convert a tree between different subtypes of Tree.
copy([deep])
count(...)
draw() Open a new window containing a graphical diagram of this tree.
extend(children)
flatten() Return a flat version of the tree, with all non-root non-terminals removed.
freeze([leaf_freezer])
fromstring(s[, brackets, read_node, ...]) Read a bracketed tree string and return the resulting tree.
height() Return the height of the tree.
index((value, [start, ...) Raises ValueError if the value is not present.
insert(index, child)
label() Return the node label of the tree.
leaf_treeposition(index)
return:The tree position of the index-th leaf in this
leaves() Return the leaves of the tree.
left_sibling() The left sibling of this tree, or None if it has none.
parent() The parent of this tree, or None if it has no parent.
parent_index() The index of this tree in its parent.
pformat([margin, indent, nodesep, parens, ...])
return:A pretty-printed string representation of this tree.
pformat_latex_qtree() Returns a representation of the tree compatible with the LaTeX qtree package.
pop([index])
pos() Return a sequence of pos-tagged words extracted from the tree.
pprint(**kwargs) Print a string representation of this Tree to ‘stream’
pretty_print([sentence, highlight, stream]) Pretty-print this tree as ASCII or Unicode art.
productions() Generate the productions that correspond to the non-terminal nodes of the tree.
remove(child)
reverse L.reverse() – reverse IN PLACE
right_sibling() The right sibling of this tree, or None if it has none.
root() The root of this tree.
set_label(label) Set the node label of the tree.
sort L.sort(cmp=None, key=None, reverse=False) – stable sort IN PLACE;
subtrees([filter]) Generate all the subtrees of this tree, optionally restricted to trees matching the filter function.
treeposition() The tree position of this tree, relative to the root of the tree.
treeposition_spanning_leaves(start, end)
return:The tree position of the lowest descendant of this
treepositions([order])
>>> t = Tree.fromstring("(S (NP (D the) (N dog)) (VP (V chased) (NP (D the) (N cat))))")
un_chomsky_normal_form([expandUnary, ...]) This method modifies the tree in three ways:
unicode_repr()

Attributes

node Outdated method to access the node value; use the label() method instead.