|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectjava.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 two abstract methods that must be implemented are:
children()
, and treeFactory()
. Note
that setChildren(Tree[])
is now an optional
operation, whereas it was previously was required to be
implemented. 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.
Field Summary | |
protected static Tree[] |
ZEROCHILDREN
|
Constructor Summary | |
Tree()
|
Method Summary | |
abstract Tree[] |
children()
Returns an array of children for the current node. |
int |
compareTo(Object o)
|
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. |
Set |
dependencies()
Return a set of Word-Word dependencies, represented as edu.stanford.nlp.util.Pair objects (where the first() is the head) for the Tree. |
Set |
dependencies(Filter f)
Return a set of Word-Word dependencies, represented as Pair objects (where the first() is the head) for the Tree. |
Set |
dependencies(Filter f,
HeadFinder hf)
Return a set of Word-Word dependencies, represented as Pair objects (where the first() is the head) for the Tree. |
Set |
dependencies(HeadFinder hf)
Return a set of Word-Word dependencies, represented as Pair objects (where the first() is the head) for the Tree. |
int |
depth()
Finds the depth of the tree. |
boolean |
dominates(Tree t)
returns true if this dominates the Tree passed in
as an argument. |
List |
dominationPath(Tree t)
returns the path of nodes leading down to a dominated node, including this and the dominated node itself. |
boolean |
equals(Object o)
Implements equality for Tree's. |
Tree |
firstChild()
Returns the first child of a tree, or null if none. |
Tree |
flatten()
Return a flattened version of a tree. |
Tree |
flatten(TreeFactory tf)
Return a flattened version of a tree. |
Tree |
getChild(int i)
Return the child at some daughter index. |
List |
getChildrenAsList()
Returns an array of children for the current node. |
int |
hashCode()
Implements a hashCode for Tree's. |
Tree |
headPreTerminal(HeadFinder hf)
Returns the preterminal tree that is the head of the tree. |
Tree |
headTerminal(HeadFinder hf)
Returns the tree leaf that is the head of the tree. |
void |
indentedListPrint()
Indented list printing of a tree. |
void |
indentedListPrint(PrintWriter pw)
Indented list printing of a tree. |
void |
indentedListPrint(String indent,
String pad,
PrintWriter pw)
Indented list printing of a tree. |
int |
indexOf(Tree tree)
Returns the position of a Tree in the children list, if present, or -1 if it is not present. |
void |
insertDtr(Tree dtr,
int position)
insert dtr after position existing
daughters in this . |
boolean |
isLeaf()
Says whether a node is a leaf. |
boolean |
isPhrasal()
Return whether this node is a phrasal node or not. |
boolean |
isPrePreTerminal()
Return whether all the children of this node are preterminals or not. |
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. |
String |
nodeString()
|
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 |
pennPrint(PrintWriter pw)
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. |
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. |
List |
subTreeList()
Get the list of all subtrees inside the tree by returning a tree rooted at each node. |
Set |
subTrees()
Get the set of all subtrees inside the tree by returning a tree rooted at each node. |
Collection |
subTrees(Collection n)
Add the set of all subtrees inside a tree (including the tree itself) to the given Collection . |
Set |
taggedDependencies()
Return a Set of TaggedWord-TaggedWord dependencies, represented as edu.stanford.nlp.util.Pair objects (where the first() is the head) for the Tree. |
Set |
taggedDependencies(Filter f)
Return a set of TaggedWord-TaggedWord dependencies, represented as Pair objects (where the first() is the head) for the Tree. |
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. |
static Tree |
valueOf(String str)
This gives you a tree from a String representation (as a bracketed Tree, of the kind produced by toString() ,
pennPrint() , or as in the Penn Treebank). |
static Tree |
valueOf(String str,
TreeReaderFactory trf)
This gives you a tree from a String representation (as a bracketed Tree, of the kind produced by toString() ,
pennPrint() , or as in the Penn Treebank. |
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, finalize, getClass, notify, notifyAll, wait, wait, wait |
Field Detail |
protected static final Tree[] ZEROCHILDREN
Constructor Detail |
public Tree()
Method Detail |
public boolean isLeaf()
Tree
. Being a leaf is defined as having no
children. This must be implemented as returning a zero-length
Tree[] array for children(). This is the preferred
alternative to running meta checks on types, as it works
independent of Tree implementation.
public boolean isPreTerminal()
public boolean isPrePreTerminal()
public boolean isPhrasal()
true
if the node is phrasal;
false
otherwisepublic boolean equals(Object o)
equals
in interface Collection
o
- The object to compare with
public int hashCode()
hashCode
in interface Collection
public int compareTo(Object o)
compareTo
in interface Comparable
public int indexOf(Tree tree)
equals()
.
tree
- The tree to look for in children list
public abstract Tree[] children()
isLeaf()
returns
true, or this node has a nonzero number of children.
getChildrenAsList()
public List getChildrenAsList()
List
of size 0 will be
returned.
public void setChildren(Tree[] children)
ZEROCHILDREN
canonical zero-length Tree[] array
to represent zero children, but it is not required that
leaf nodes use this particular zero-length array to represent
a leaf node.
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 (represented by a canonical zero-length
children() array).
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()
SimpleConstituent
public Set constituents(ConstituentFactory cf)
cf
- ConstituentFactory used to build the Constituent objects
HashSet
public StringBuffer toStringBuffer(StringBuffer sb)
StringBuffer
.
The implementation of this may be more efficient than for
toString()
on complex trees.
sb
- The StringBuffer to append to
StringBuffer
public String toString()
StringBuffer
through it all.
public void indentedListPrint()
public void indentedListPrint(PrintWriter pw)
pw
- The PrintWriter to print the tree topublic void indentedListPrint(String indent, String pad, PrintWriter pw)
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
.pw
- The PrintWriter to print the tree topublic String nodeString()
public void pennPrint(PrintWriter pw)
pw
- The tree is printed to this PrintWriter
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 Tree headTerminal(HeadFinder hf)
hf
- The headfinding algorithm to use
null
public Tree headPreTerminal(HeadFinder hf)
isPreTerminal()
for
the definition of a preterminal node. Beware that some tree nodes may
have no preterminal head.
hf
- The headfinding algorithm to use
null
public void percolateHeads(HeadFinder hf)
hf
- The headfinding algorithm to usepublic Set dependencies()
public Set dependencies(Filter f)
f
- Dependencies are excluded for which the Pair is not
accepted by the Filter
public Set dependencies(HeadFinder hf)
hf
- The HeadFinder to use to identify the head of constituents
Pair
)public Set dependencies(Filter f, HeadFinder hf)
f
- Dependencies are excluded for which the Pair is not
accepted by the Filterhf
- The HeadFinder to use to identify the head of constituents.
If this is null
, then nodes are assumed to already
be marked with their heads.
Pair
)public Set taggedDependencies()
public Set taggedDependencies(Filter f)
Implementation note: It would be nice to generalize this with dependencies() to use a LabelFactory, but it seems impossible with the current setup.
f
- Dependencies are excluded for which the Pair is not
accepted by the Filter
public 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
c
- The set of Label valuespublic Tree flatten()
label()
equal() to
the label()
of its parent, and all its children will
then be promoted to become children of the parent (in the same
position in the sequence of daughters.
public Tree flatten(TreeFactory tf)
label()
equal() to
the label()
of its parent, and all its children will
then be promoted to become children of the parent (in the same
position in the sequence of daughters. Note: In the current implementation, the tree structure is mainly duplicated, but the links between preterminals and terminals aren't.
tf
- TreeFactory used to create tree structure for flattened tree
public Set subTrees()
Set
of all subtrees in the tree.public List subTreeList()
List
of all subtrees in the tree.public Collection subTrees(Collection n)
Collection
.
n
- A collection 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
keep this node, false to mean delete it
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
keep this node, false to mean delete ittf
- 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
null
if the root node of the tree is filteredpublic 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
.
root
- The root node of the whole Tree
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
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
public static Tree valueOf(String str) throws IOException
toString()
,
pennPrint()
, or as in the Penn Treebank).
It's not the most efficient thing to do for heavy duty usage.
The Tree returned is created by a
StringLabeledScoredTreeReaderFactory.
str
- The tree as a bracketed list in a String.
IOException
- If Tree format is not validpublic static Tree valueOf(String str, TreeReaderFactory trf) throws IOException
toString()
,
pennPrint()
, or as in the Penn Treebank.
It's not the most efficient thing to do for heavy duty usage.
str
- The tree as a bracketed list in a String.
IOException
- If Tree format is not validpublic Tree getChild(int i)
i
- The daughter index
public boolean dominates(Tree t)
this
dominates the Tree passed in
as an argument.
public List dominationPath(Tree t)
this
and the dominated node itself.
Returns null if t is not dominated by this
. Object
equality (==) is the relevant criterion.
public void insertDtr(Tree dtr, int position)
dtr
after position
existing
daughters in this
.
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |