|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object | +--java.util.AbstractCollection | +--edu.stanford.nlp.trees.Tree
The abstract class Tree
is used to collect
all of the tree types, and acts as a generic composite type. This
is the standard implementation of inheritance-based polymorphism.
Trees, like standard containers, are not parametrized in any way by
the type of object in the data
field.
All Tree
objects support accessors for their children
(a Tree[]
), their data (a Label
), and their
score (a double
). However, different concrete
implementations may or may not include the latter two, in which case
a default value is returned. The class Tree defines no data fields.
The three abstract methods that must be implemented are:
children()
, setChildren(Tree[])
, and
treeFactory()
.
There is now support for finding the parent of a tree. This may be
done by search from a tree root, or via a directly stored parent.
The Tree
class now implements the Collection
interface: in terms of this, each node of the tree is an
element of the collection; hence one can explore the tree by using
the methods of this interface. A Tree
is regarded as a
read-only Collection
(even though the Tree
class has various methods that modify trees). Moreover, the
implementation is not thread-safe: no attempt is made to detect
and report concurrent modifications.
Constructor Summary | |
Tree()
|
Method Summary | |
abstract Tree[] |
children()
Returns an array of children for the current node. |
Set |
constituents()
Returns the Constituents generated by the parse tree. |
Set |
constituents(ConstituentFactory cf)
Returns the Constituents generated by the parse tree. |
Tree |
deepCopy()
Create a deep copy of the tree. |
Tree |
deepCopy(TreeFactory tf)
Create a deep copy of the tree. |
int |
depth()
Finds the depth of the tree. |
Tree |
firstChild()
Returns the first child of a tree, or null if none |
List |
getChildrenAsList()
Returns an array of children for the current node. |
void |
indentedListPrint()
Indented list printing of a tree. |
void |
indentedListPrint(String indent,
String pad)
Indented list printing of a tree with each line indented |
boolean |
isLeaf()
Returns true if the tree is a leaf, false otherwise. |
boolean |
isPreTerminal()
Return whether this node is a preterminal or not. |
Iterator |
iterator()
Returns an iterator over the nodes of the tree. |
Label |
label()
Returns the label associated with the current node, or null if there is no label. |
Collection |
labels()
Get the set of all node and leaf Label s,
null or otherwise, contained in the tree. |
Tree |
lastChild()
Returns the last child of a tree, or null if none |
Tree |
parent()
Return the parent of the tree node. |
Tree |
parent(Tree root)
Return the parent of the tree node. |
void |
pennPrint()
Print the tree as done in Penn Treebank merged files. |
void |
pennPrint(PrintStream ps)
Print the tree as done in Penn Treebank merged files. |
void |
percolateHeads(HeadFinder hf)
Finds the heads of the tree. |
List |
preTerminalYield()
Gets the preterminal yield (i.e., tags) of the tree. |
List |
preTerminalYield(List y)
Gets the preterminal yield (i.e., tags) of the tree. |
Tree |
prune(Filter filter)
Creates a deep copy of the tree, where all nodes that the filter does not accept and all children of such nodes are pruned. |
Tree |
prune(Filter filter,
TreeFactory tf)
Creates a deep copy of the tree, where all nodes that the filter does not accept and all children of such nodes are pruned. |
double |
score()
Returns the score associated with the current node, or NaN if there is no score. |
void |
setChildren(List childTreesList)
Set the children of this tree node to the given list. |
abstract void |
setChildren(Tree[] children)
Set the children of this node to be the children given in the array. |
void |
setLabel(Label label)
Sets the label associated with the current node, if there is one. |
void |
setLabels(Collection c)
Sets the labels associated with this object. |
void |
setScore(double score)
Sets the score associated with the current node, if there is one |
int |
size()
Returns the number of nodes the tree contains. |
Tree |
spliceOut(Filter nodeFilter)
Creates a (partial) deep copy of the tree, where all nodes that the filter does not accept are spliced out. |
Tree |
spliceOut(Filter nodeFilter,
TreeFactory tf)
Creates a (partial) deep copy of the tree, where all nodes that the filter does not accept are spliced out. |
Set |
subTrees()
Get the set of all subtrees inside the tree by returning a tree rooted at each node. |
Set |
subTrees(Set n)
Add the set of all subtrees inside a tree to the given Set . |
Sentence |
taggedYield()
Gets the tagged yield of the tree. |
List |
taggedYield(List ty)
Gets the tagged yield of the tree -- that is, get the preterminals as well as the terminals. |
String |
toString()
Converts parse tree to string in Penn Treebank format. |
StringBuffer |
toStringBuffer(StringBuffer sb)
Appends the printed form of a parse tree (as a bracketed String) to a StringBuffer . |
Tree |
transform(TreeTransformer transformer)
Create a transformed Tree. |
Tree |
transform(TreeTransformer transformer,
TreeFactory tf)
Create a transformed Tree. |
abstract TreeFactory |
treeFactory()
Return a TreeFactory that produces trees of the
appropriate type. |
Sentence |
yield()
Gets the yield of the tree. |
List |
yield(List y)
Gets the yield of the tree. |
Methods inherited from class java.util.AbstractCollection |
add, addAll, clear, contains, containsAll, isEmpty, remove, removeAll, retainAll, toArray, toArray |
Methods inherited from class java.lang.Object |
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait |
Methods inherited from interface java.util.Collection |
equals, hashCode |
Constructor Detail |
public Tree()
Method Detail |
public boolean isLeaf()
Tree
. Being a leaf is defined as
having no children. This is the preferred alternative
to running meta checks on types, as it works independent of Tree
implementation. If those isLeaf
methods are final
, it will be very efficient, too.
public boolean isPreTerminal()
public abstract Tree[] children()
null
or an array of length 0. Other code in this class will work
correctly for both these cases. A caller may assume that either
isLeaf()
returns true, or this node has a nonzero number
of children. So the following idiom is frequently useful:
if ( ! tr.isLeaf()) { Tree[] kids = children(); for (int i = 0; i < kids.length; i++) { doSomethingTo(kids[i]); } }
getChildrenAsList()
public List getChildrenAsList()
List
of size 0 will be
returned.
public abstract void setChildren(Tree[] children)
null
if there are no children,
and other methods in this class work on either representation.
children
- The array of children, each a Tree
setChildren(List)
public void setChildren(List childTreesList)
Tree
class by converting the
List
into a tree array and calling the array-based
method. Subclasses which use a List
-based representation
of tree children should override this method.
This implementation allows the case that the List
is
null
: it yields a node with no children.
childTreesList
- list of trees to become childrensetChildren(Tree[])
public Label label()
null
.
label
in interface Labeled
public void setLabel(Label label)
setLabel
in interface Labeled
label
- The labelpublic double score()
score
in interface Scored
public void setScore(double score)
score
- The scorepublic Tree firstChild()
null
if none
public Tree lastChild()
null
if none
public Set constituents()
public Set constituents(ConstituentFactory cf)
public StringBuffer toStringBuffer(StringBuffer sb)
StringBuffer
.
The implementation of this may be more efficient than for
toString()
on complex trees.
StringBuffer
public String toString()
StringBuffer
through it all.
toString
in class AbstractCollection
public void indentedListPrint()
public void indentedListPrint(String indent, String pad)
indent
- The base String
(normally just spaces)
to print before each line of treepad
- The additional String
(normally just more
spaces) to add when going to a deeper level of Tree
.
These are used rather than integer levels for efficiency.public void pennPrint(PrintStream ps)
ps
- The tree is printed to this PrintStream
public void pennPrint()
System.out
. The basic deviation
from a bracketed indented tree is to in general
collapse the printing of adjacent preterminals onto one line of
tags and words. Additional complexities are that conjunctions
(tag CC) are not collapsed in this way, and that the unlabeled
outer brackets are collapsed onto the same line as the next
bracket down.
public int depth()
public void percolateHeads(HeadFinder hf)
hf
- The headfinding algorithm to usepublic Sentence yield()
Label
of all leaf nodes
is returned
as a list ordered by the natural left to right order of the
leaves. Null values, if any, are inserted into the list like any
other value.
List
of the data in the tree's leaves.public List yield(List y)
Label
of all leaf nodes
is returned
as a list ordered by the natural left to right order of the
leaves. Null values, if any, are inserted into the list like any
other value. This has been rewritten to thread, so only one List
is used.
y
- The list in which the yield of the tree will be placed.
Normally, this will be empty when the routine is called, but
if not, the new yield is added to the end of the list.
List
of the data in the tree's leaves.public Sentence taggedYield()
Label
of all leaf nodes is returned
as a list ordered by the natural left to right order of the
leaves. Null values, if any, are inserted into the list like any
other value.
List
of the data in the tree's leaves.public List taggedYield(List ty)
Label
of all leaf nodes
is returned
as a list ordered by the natural left to right order of the
leaves. Null values, if any, are inserted into the list like any
other value. This has been rewritten to thread, so only one List
is used.
ty
- The list in which the tagged yield of the tree will be
placed. Normally, this will be empty when the routine is called,
but if not, the new yield is added to the end of the list.
List
of the data in the tree's leaves.public List preTerminalYield()
List
of the data in the tree's pre-leaves.public List preTerminalYield(List y)
y
- The list in which the preterminals of the tree will be
placed. Normally, this will be empty when the routine is called,
but if not, the new yield is added to the end of the list.
List
of the data in the tree's pre-leaves.public Collection labels()
Label
s,
null or otherwise, contained in the tree.
labels
in interface Labeled
Collection
(actually, Set) of all values
in the tree.public void setLabels(Collection c)
Labeled
setLabels
in interface Labeled
public Set subTrees()
Set
of all subtrees in the tree.public Set subTrees(Set n)
Set
.
n
- A set of nodes to which the subtrees will be added
public Tree deepCopy()
TreeFactory
that will
produce a Tree
like the input one.
public Tree deepCopy(TreeFactory tf)
TreeFactory
, this
method can be used to change the type of a Tree
.
tf
- The TreeFactory
to be used for creating
the returned Tree
public Tree transform(TreeTransformer transformer)
TreeTransformer
is called
on each node. It returns some Tree
. The transformed
tree has a new tree structure (i.e., a "deep copy" is done), but it
will usually share its labels with the original tree.
transformer
- The function that transforms tree nodes or subtrees
Tree
public Tree transform(TreeTransformer transformer, TreeFactory tf)
TreeTransformer
is called
on each node. It returns some Tree
. The transformed
tree has a new tree structure (i.e., a "deep copy" is done), but it
will usually share its labels with the original tree.
transformer
- The function that transforms tree nodes or subtreestf
- The TreeFactory
which will be used for creating
new nodes for the returned Tree
Tree
public Tree spliceOut(Filter nodeFilter)
nodeFilter
- a Filter method which returns true to mean delete
this node
public Tree spliceOut(Filter nodeFilter, TreeFactory tf)
Filter
returns false
are removed from the Tree
, but those nodes' children
are kept (assuming they pass the Filter
, and they are
added in the appropriate left-to-right ordering as new children of
the parent node. If the root node is deleted, so that the result
would not be a tree (that is, it's a forest), an empty root node is
generated. If nothing is accepted, null
is returned.
nodeFilter
- a Filter method which returns true to mean delete
this nodetf
- A TreeFactory
for making new trees. Used if
the root node is deleted.
public Tree prune(Filter filter)
Filter
can assume
that it will not be called with a null
argument.
filter
- the filter to be apply
public Tree prune(Filter filter, TreeFactory tf)
Filter
can assume
that it will not be called with a null
argument.
filter
- the filter to be applytf
- the TreeFactory to be used to make new Tree nodes if needed
public abstract TreeFactory treeFactory()
TreeFactory
that produces trees of the
appropriate type.
public Tree parent()
null
meaning simply that the implementation doesn't
know how to determine the parent node, rather than there is no
such node.
Tree
node or null
parent(Tree)
public Tree parent(Tree root)
root
, and will
correctly find the parent, regardless of whether the concrete
class stores parents. It will only return null
if this
node is the root
node, or if this node is not
contained within the tree rooted at root
.
Tree
node if any;
else null
public int size()
size()
function required by the
Collections
interface. The size of the tree is the
number of nodes it contains (of all types, including the leaf nodes
and the root).
size
in interface Collection
size
in class AbstractCollection
depth()
public Iterator iterator()
iterator()
method required by the
Collections
interface. It does a preorder
(children after node) traversal of the tree. (A possible
extension to the class at some point would be to allow different
traversal orderings via variant iterators.)
iterator
in interface Collection
iterator
in class AbstractCollection
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |