public abstract class Tree extends java.util.AbstractCollection<Tree> implements Label, Labeled, Scored, java.io.Serializable
Tree
is used to collect all of the
tree types, and acts as a generic extensible type. This is the
standard implementation of inheritance-based polymorphism.
All Tree
objects support accessors for their children (a
Tree[]
), their label (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()
. Notes
that setChildren(Tree[])
is now an optional
operation, whereas it was previously 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.Modifier and Type | Field and Description |
---|---|
static Tree[] |
EMPTY_TREE_ARRAY
A leaf node should have a zero-length array for its
children.
|
Constructor and Description |
---|
Tree() |
Modifier and Type | Method and Description |
---|---|
void |
addChild(int i,
Tree t)
Adds the tree t at the index position among the daughters.
|
void |
addChild(Tree t)
Adds the tree t at the last index position among the daughters.
|
Tree |
ancestor(int height,
Tree root)
Return the ancestor tree node
height nodes up from the current node. |
boolean |
cCommands(Tree t1,
Tree t2)
Given nodes
t1 and t2 which are
dominated by this node, returns true iff
t1 c-commands t2 . |
abstract Tree[] |
children()
Returns an array of children for the current node.
|
java.util.Set<Constituent> |
constituents()
Returns the Constituents generated by the parse tree.
|
java.util.Set<Constituent> |
constituents(ConstituentFactory cf)
Returns the Constituents generated by the parse tree.
|
java.util.Set<Constituent> |
constituents(ConstituentFactory cf,
boolean charLevel)
Returns the Constituents generated by the parse tree.
|
java.util.Set<Constituent> |
constituents(ConstituentFactory cf,
boolean charLevel,
java.util.function.Predicate<Tree> filter) |
java.util.Set<Constituent> |
constituents(ConstituentFactory cf,
int maxDepth)
Returns the Constituents generated by the parse tree.
|
Tree |
deepCopy()
Makes a deep copy of not only the Tree structure but of the labels as well.
|
Tree |
deepCopy(TreeFactory tf)
Makes a deep copy of not only the Tree structure but of the labels as well.
|
Tree |
deepCopy(TreeFactory tf,
LabelFactory lf)
Makes a deep copy of not only the Tree structure but of the labels as well.
|
java.util.Set<Dependency<Label,Label,java.lang.Object>> |
dependencies()
Return a Set of TaggedWord-TaggedWord dependencies, represented as
Dependency objects, for the Tree.
|
java.util.Set<Dependency<Label,Label,java.lang.Object>> |
dependencies(java.util.function.Predicate<Dependency<Label,Label,java.lang.Object>> f) |
java.util.Set<Dependency<Label,Label,java.lang.Object>> |
dependencies(java.util.function.Predicate<Dependency<Label,Label,java.lang.Object>> f,
boolean isConcrete,
boolean copyLabel,
boolean copyPosTag)
Return a set of TaggedWord-TaggedWord dependencies, represented as
Dependency objects, for the Tree.
|
int |
depth()
Finds the depth of the tree.
|
int |
depth(Tree node)
Finds the distance from this node to the specified node.
|
boolean |
dominates(Tree t)
Returns true if
this dominates the Tree passed in
as an argument. |
java.util.List<Tree> |
dominationPath(Tree t)
Returns the path of nodes leading down to a dominated node,
including
this and the dominated node itself. |
boolean |
equals(java.lang.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.
|
java.util.List<Tree> |
getChildrenAsList()
Returns a List of children for the current node.
|
<T extends Tree> |
getLeaves()
Gets the leaves of the tree.
|
<T extends Tree> |
getLeaves(java.util.List<T> list)
Gets the leaves of the tree.
|
Tree |
getNodeNumber(int i)
Fetches the
i th node in the tree, with node numbers defined
as in nodeNumber(Tree) . |
IntPair |
getSpan()
Returns SpanAnnotation of this node, or null if annotation is not assigned.
|
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.
|
Tree |
headTerminal(HeadFinder hf,
Tree parent)
Returns the tree leaf that is the head of the tree.
|
void |
indentedListPrint()
Indented list printing of a tree.
|
void |
indentedListPrint(java.io.PrintWriter pw,
boolean printScores)
Indented list printing of a tree.
|
void |
indentedXMLPrint()
Indented xml printing of a tree.
|
void |
indentedXMLPrint(java.io.PrintWriter pw,
boolean printScores)
Indented xml printing of a tree.
|
void |
indexLeaves()
Assign sequential integer indices to the leaves of the tree
rooted at this
Tree , starting with 1. |
void |
indexLeaves(boolean overWrite)
Index the leaves, and optionally overwrite existing IndexAnnotations if they exist.
|
int |
indexLeaves(int startIndex,
boolean overWrite)
Assign sequential integer indices to the leaves of the subtree
rooted at this
Tree , beginning with
startIndex , and traversing the leaves from left
to right. |
void |
indexSpans()
Index all spans (constituents) in the tree.
|
void |
indexSpans(int startIndex) |
Pair<java.lang.Integer,java.lang.Integer> |
indexSpans(MutableInteger startIndex)
Assigns span indices (BeginIndexAnnotation and EndIndexAnnotation) to all nodes in a tree.
|
void |
insertDtr(Tree dtr,
int position)
insert
dtr after position existing
daughters in this . |
boolean |
isBinarized()
Returns whether this node is the root of a possibly binarized
tree.
|
boolean |
isBinary()
Returns whether this node is the root of a possibly binary tree.
|
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.
|
boolean |
isUnaryRewrite()
Says whether the current node has only one child.
|
java.util.Iterator<Tree> |
iterator()
Returns an iterator over all the nodes of the tree.
|
Tree |
joinNode(Tree t1,
Tree t2)
Given nodes
t1 and t2 which are
dominated by this node, returns their "join node": the node
j such that j dominates both
t1 and t2 , and every other node which
dominates both t1 and t2
dominates j . |
Label |
label()
Returns the label associated with the current node, or null
if there is no label.
|
java.util.List<LabeledWord> |
labeledYield() |
java.util.List<LabeledWord> |
labeledYield(java.util.List<LabeledWord> ty) |
LabelFactory |
labelFactory()
Returns a factory that makes labels of the same type as this one.
|
java.util.Collection<Label> |
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. |
int |
leftCharEdge(Tree node)
Returns the positional index of the left edge of node within the tree,
as measured by characters.
|
Tree |
localTree()
Returns a new Tree that represents the local Tree at a certain node.
|
java.util.Set<Tree> |
localTrees()
Returns a set of one level
Tree s that ares the local trees
of the tree. |
java.util.Set<Dependency<Label,Label,java.lang.Object>> |
mapDependencies(java.util.function.Predicate<Dependency<Label,Label,java.lang.Object>> f,
HeadFinder hf)
Return a set of Label-Label dependencies, represented as
Dependency objects, for the Tree.
|
java.util.Set<Dependency<Label,Label,java.lang.Object>> |
mapDependencies(java.util.function.Predicate<Dependency<Label,Label,java.lang.Object>> f,
HeadFinder hf,
java.lang.String rootName)
Return a set of Label-Label dependencies, represented as
Dependency objects, for the Tree.
|
int |
nodeNumber(Tree root)
Calculates the node's number, defined as the number of nodes traversed in a left-to-right, depth-first search of the
tree starting at
root and ending at this . |
java.lang.String |
nodeString()
Returns the value of the node's label as a String.
|
int |
numChildren()
Says how many children a tree node has in its local tree.
|
int |
objectIndexOf(Tree tree)
Returns the position of a Tree in the children list, if present,
or -1 if it is not present.
|
Tree |
parent()
Return the parent of the tree node.
|
Tree |
parent(Tree root)
Return the parent of the tree node.
|
java.util.List<Tree> |
pathNodeToNode(Tree t1,
Tree t2)
Given nodes
t1 and t2 which are
dominated by this node, returns a list of all the nodes on the
path from t1 to t2, inclusive, or null if none found. |
void |
pennPrint()
Print the tree as done in Penn Treebank merged files.
|
void |
pennPrint(java.io.PrintStream ps)
Print the tree as done in Penn Treebank merged files.
|
void |
pennPrint(java.io.PrintStream ps,
java.util.function.Function<Label,java.lang.String> labelFormatter) |
void |
pennPrint(java.io.PrintWriter pw)
Print the tree as done in Penn Treebank merged files.
|
void |
pennPrint(java.io.PrintWriter pw,
java.util.function.Function<Label,java.lang.String> labelFormatter) |
java.lang.String |
pennString()
Calls
pennPrint() and saves output to a String |
void |
percolateHeadAnnotations(HeadFinder hf)
Finds the head words of each tree and assigns
HeadWordLabelAnnotation on each node pointing to the correct
CoreLabel.
|
void |
percolateHeadIndices()
Percolates terminal indices through a dependency tree.
|
void |
percolateHeads(HeadFinder hf)
Finds the heads of the tree.
|
java.util.List<Tree> |
postOrderNodeList() |
java.util.List<Tree> |
preOrderNodeList() |
java.util.List<Label> |
preTerminalYield()
Gets the preterminal yield (i.e., tags) of the tree.
|
java.util.List<Label> |
preTerminalYield(java.util.List<Label> y)
Gets the preterminal yield (i.e., tags) of the tree.
|
void |
printLocalTree() |
void |
printLocalTree(java.io.PrintWriter pw)
Only prints the local tree structure, does not recurse
|
Tree |
prune(java.util.function.Predicate<Tree> 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(java.util.function.Predicate<Tree> 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.
|
Tree |
removeChild(int i)
Destructively removes the child at some daughter index and returns it.
|
int |
rightCharEdge(Tree node)
Returns the positional index of the right edge of node within the tree,
as measured by characters.
|
double |
score()
Returns the score associated with the current node, or NaN
if there is no score.
|
Tree |
setChild(int i,
Tree t)
Replaces the
i th child of this with the tree t. |
void |
setChildren(java.util.List<? extends Tree> 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 |
setFromString(java.lang.String labelStr)
Set the contents of this label to this
String
representing the
complete contents of the label. |
void |
setLabel(Label label)
Sets the label associated with the current node, if there is one.
|
void |
setLabels(java.util.Collection<Label> c)
Sets the labels associated with this object.
|
void |
setScore(double score)
Sets the score associated with the current node, if there is one.
|
void |
setSpans()
Assign a SpanAnnotation on each node of this tree.
|
void |
setValue(java.lang.String value)
Set the value for the label (if one is stored).
|
java.util.List<Tree> |
siblings(Tree root)
Returns the siblings of this Tree node.
|
int |
size()
Returns the number of nodes the tree contains.
|
Tree |
skipRoot()
Returns first child if this is unary and if the label at the current
node is either "ROOT" or empty.
|
java.lang.String |
spanString()
Return String of leaves spanned by this tree assuming they are CoreLabel's
Throws an IllegalArgumentException if the leaves are not CoreLabels that contain
text info as in the typical use case of a Tree generated by a pipeline
|
Tree |
spliceOut(java.util.function.Predicate<Tree> nodeFilter)
Creates a (partial) deep copy of the tree, where all nodes that the
filter does not accept are spliced out.
|
Tree |
spliceOut(java.util.function.Predicate<Tree> nodeFilter,
TreeFactory tf)
Creates a (partial) deep copy of the tree, where all nodes that the
filter does not accept are spliced out.
|
java.util.List<Tree> |
subTreeList()
Get the list of all subtrees inside the tree by returning a tree
rooted at each node.
|
java.util.Set<Tree> |
subTrees()
Get the set of all subtrees inside the tree by returning a tree
rooted at each node.
|
<T extends java.util.Collection<Tree>> |
subTrees(T n)
Add the set of all subtrees inside a tree (including the tree itself)
to the given
Collection . |
java.util.List<CoreLabel> |
taggedLabeledYield()
Returns a
List<CoreLabel> from the tree. |
java.util.ArrayList<TaggedWord> |
taggedYield()
Gets the tagged yield of the tree.
|
<X extends java.util.List<TaggedWord>> |
taggedYield(X ty)
Gets the tagged yield of the tree -- that is, get the preterminals
as well as the terminals.
|
java.lang.String |
toString()
Converts parse tree to string in Penn Treebank format.
|
java.lang.StringBuilder |
toStringBuilder(java.lang.StringBuilder sb)
Appends the printed form of a parse tree (as a bracketed String)
to a
StringBuilder . |
java.lang.StringBuilder |
toStringBuilder(java.lang.StringBuilder sb,
java.util.function.Function<Label,java.lang.String> labelFormatter)
Appends the printed form of a parse tree (as a bracketed String)
to a
StringBuilder . |
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. |
Tree |
treeSkeletonConstituentCopy()
Returns a deep copy of everything but the leaf labels.
|
Tree |
treeSkeletonConstituentCopy(TreeFactory tf,
LabelFactory lf) |
Tree |
treeSkeletonCopy()
Create a deep copy of the tree structure.
|
Tree |
treeSkeletonCopy(TreeFactory tf)
Create a deep copy of the tree structure.
|
static java.lang.String |
updateBrackets(java.lang.String text) |
Tree |
upperMostUnary(Tree root)
Return the highest node of the (perhaps trivial) unary chain that
this node is part of.
|
java.lang.String |
value()
Return a String representation of just the "main" value of this label.
|
static Tree |
valueOf(java.lang.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(java.lang.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. |
java.util.ArrayList<Label> |
yield()
Gets the yield of the tree.
|
java.util.ArrayList<Label> |
yield(java.util.ArrayList<Label> y)
Gets the yield of the tree.
|
<T> java.util.List<T> |
yield(java.util.List<T> y)
Gets the yield of the tree.
|
<X extends HasWord> |
yieldHasWord() |
<X extends HasWord> |
yieldHasWord(java.util.ArrayList<X> y) |
java.util.ArrayList<Word> |
yieldWords() |
java.util.ArrayList<Word> |
yieldWords(java.util.ArrayList<Word> y) |
add, addAll, clear, contains, containsAll, isEmpty, remove, removeAll, retainAll, toArray, toArray
public static final Tree[] EMPTY_TREE_ARRAY
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().public int numChildren()
Tree
. Being a leaf is defined
as having no children.public boolean isUnaryRewrite()
Tree
.public boolean isPreTerminal()
public boolean isPrePreTerminal()
public boolean isPhrasal()
true
if the node is phrasal; false
otherwisepublic boolean isBinary()
public boolean isBinarized()
public boolean equals(java.lang.Object o)
value()
s, the same number of children, and their children
are pairwise equal.equals
in interface java.util.Collection<Tree>
equals
in class java.lang.Object
o
- The object to compare withpublic int hashCode()
hashCode
in interface java.util.Collection<Tree>
hashCode
in class java.lang.Object
public int objectIndexOf(Tree tree)
tree
- The tree to look for in children listpublic abstract Tree[] children()
isLeaf()
returns
true, or this node has a nonzero number of children.getChildrenAsList()
public java.util.List<Tree> getChildrenAsList()
List<Tree>
of size 0 will
be returned. The list has new list structure but pointers to,
not copies of the children. That is, the returned list is mutable,
and simply adding to or deleting items from it is safe, but beware
changing the contents of the children.public void setChildren(Tree[] children)
EMPTY_TREE_ARRAY
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(java.util.List<? extends Tree> 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
- A list of trees to become children of the node.
This method does not retain the List that you pass it (copying
is done), but it will retain the individual children (they are
not copied).setChildren(Tree[])
public Label label()
null
.public void setLabel(Label label)
public double score()
public void setScore(double score)
score
- The scorepublic Tree firstChild()
null
if none.public Tree lastChild()
null
if none.public Tree upperMostUnary(Tree root)
root
- The root of the tree that contains this subtreepublic void setSpans()
public IntPair getSpan()
setSpans()
to assign SpanAnnotations to a tree.public java.util.Set<Constituent> constituents()
Constituent
public java.util.Set<Constituent> constituents(ConstituentFactory cf)
cf
- ConstituentFactory used to build the Constituent objectsHashSet
public java.util.Set<Constituent> constituents(ConstituentFactory cf, int maxDepth)
cf
- ConstituentFactory used to build the Constituent objectsmaxDepth
- The maximum depth at which to add constituents,
where 0 is the root level. Negative maxDepth
indicates no maximum.HashSet
public java.util.Set<Constituent> constituents(ConstituentFactory cf, boolean charLevel)
cf
- ConstituentFactory used to build the Constituent objectscharLevel
- If true, compute bracketings irrespective of whitespace boundaries.HashSet
public java.util.Set<Constituent> constituents(ConstituentFactory cf, boolean charLevel, java.util.function.Predicate<Tree> filter)
public Tree localTree()
public java.util.Set<Tree> localTrees()
Tree
s that ares the local trees
of the tree.
That is, it builds a new tree that copies the mother and daughter
nodes (but not their Labels), for each phrasal node,
but zeroes out their children.public java.lang.StringBuilder toStringBuilder(java.lang.StringBuilder sb)
StringBuilder
.
The implementation of this may be more efficient than for
toString()
on complex trees.sb
- The StringBuilder
to which the tree will be appendedStringBuilder
passed in with extra stuff in itpublic static java.lang.String updateBrackets(java.lang.String text)
public java.lang.StringBuilder toStringBuilder(java.lang.StringBuilder sb, java.util.function.Function<Label,java.lang.String> labelFormatter)
StringBuilder
.
The implementation of this may be more efficient than for
toString()
on complex trees.sb
- The StringBuilder
to which the tree will be appendedlabelFormatter
- Formatting routine for how to print a LabelStringBuilder
passed in with extra stuff in itpublic java.lang.String toString()
StringBuilder
through all the printing.public void printLocalTree()
public void printLocalTree(java.io.PrintWriter pw)
public void indentedListPrint()
public void indentedListPrint(java.io.PrintWriter pw, boolean printScores)
pw
- The PrintWriter to print the tree toprintScores
- Whether to print the scores (log probs) of tree nodespublic void indentedXMLPrint()
public void indentedXMLPrint(java.io.PrintWriter pw, boolean printScores)
pw
- The PrintWriter to print the tree toprintScores
- Whether to print the scores (log probs) of tree nodespublic java.lang.String nodeString()
toString()
on the value, if it exists. Otherwise,
an empty string is returned.public void pennPrint(java.io.PrintWriter pw)
pw
- The tree is printed to this PrintWriter
public void pennPrint(java.io.PrintWriter pw, java.util.function.Function<Label,java.lang.String> labelFormatter)
public void pennPrint(java.io.PrintStream ps)
ps
- The tree is printed to this PrintStream
public void pennPrint(java.io.PrintStream ps, java.util.function.Function<Label,java.lang.String> labelFormatter)
public java.lang.String pennString()
pennPrint()
and saves output to a Stringpublic java.lang.String spanString()
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 int depth(Tree node)
node
- A subtree contained in this treepublic Tree headTerminal(HeadFinder hf, Tree parent)
hf
- The head-finding algorithm to useparent
- The parent of this treenull
public Tree headTerminal(HeadFinder hf)
hf
- The headfinding algorithm to usenull
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 usenull
java.lang.IllegalArgumentException
- if called on a leaf nodepublic void percolateHeadAnnotations(HeadFinder hf)
public void percolateHeads(HeadFinder hf)
hf
- The headfinding algorithm to usepublic java.util.Set<Dependency<Label,Label,java.lang.Object>> dependencies()
public java.util.Set<Dependency<Label,Label,java.lang.Object>> dependencies(java.util.function.Predicate<Dependency<Label,Label,java.lang.Object>> f)
public java.util.Set<Dependency<Label,Label,java.lang.Object>> dependencies(java.util.function.Predicate<Dependency<Label,Label,java.lang.Object>> f, boolean isConcrete, boolean copyLabel, boolean copyPosTag)
f
- Dependencies are excluded for which the Dependency is not
accepted by the Filterpublic java.util.Set<Dependency<Label,Label,java.lang.Object>> mapDependencies(java.util.function.Predicate<Dependency<Label,Label,java.lang.Object>> f, HeadFinder hf)
f
- Dependencies are excluded for which the Dependency is not
accepted by the Filterhf
- The HeadFinder to use to identify the head of constituents.
The code assumes
that it can use headPreTerminal(hf)
to find a
tag and word to make a CoreLabel.Dependency
between two
CoreLabel
s, which each contain a tag(), word(),
and value(), the last two of which are identical).public java.util.Set<Dependency<Label,Label,java.lang.Object>> mapDependencies(java.util.function.Predicate<Dependency<Label,Label,java.lang.Object>> f, HeadFinder hf, java.lang.String rootName)
f
- Dependencies are excluded for which the Dependency is not
accepted by the Filterhf
- The HeadFinder to use to identify the head of constituents.
The code assumes
that it can use headPreTerminal(hf)
to find a
tag and word to make a CoreLabel.rootName
- Name of the root node.Dependency
between two
CoreLabel
s, which each contain a tag(), word(),
and value(), the last two of which are identical).public java.util.ArrayList<Label> 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 java.util.ArrayList<Label> yield(java.util.ArrayList<Label> 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.
Implementation notes: c. 2003: This has been rewritten to thread, so only one List is used. 2007: This method was duplicated to start to give type safety to Sentence. This method will now make a Word for any Leaf which does not itself implement HasWord, and put the Word into the Sentence, so the Sentence elements MUST implement HasWord.
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 java.util.ArrayList<Word> yieldWords()
public <X extends HasWord> java.util.ArrayList<X> yieldHasWord()
public <X extends HasWord> java.util.ArrayList<X> yieldHasWord(java.util.ArrayList<X> y)
public <T> java.util.List<T> yield(java.util.List<T> 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 java.util.ArrayList<TaggedWord> 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 java.util.List<LabeledWord> labeledYield()
public <X extends java.util.List<TaggedWord>> X taggedYield(X 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.
List<W extends TaggedWord>
.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 java.util.List<LabeledWord> labeledYield(java.util.List<LabeledWord> ty)
public java.util.List<CoreLabel> taggedLabeledYield()
List<CoreLabel>
from the tree.
These are a copy of the complete token representation
that adds the tag as the tag and value.public java.util.List<Label> preTerminalYield()
List
of the data in the tree's pre-leaves.public java.util.List<Label> preTerminalYield(java.util.List<Label> 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 <T extends Tree> java.util.List<T> getLeaves()
List
of the leaves.public <T extends Tree> java.util.List<T> getLeaves(java.util.List<T> list)
list
- The list in which the leaves 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 leaves.public java.util.Collection<Label> labels()
Label
s,
null or otherwise, contained in the tree.public void setLabels(java.util.Collection<Label> c)
Labeled
public Tree flatten()
label()
is 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()
is 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 treepublic java.util.Set<Tree> subTrees()
iterator()
method. This will iterate over the exact same
elements (but perhaps/probably in a different order).Set
of all subtrees in the tree.public java.util.List<Tree> subTreeList()
iterator()
method. This will iterate over the exact same
elements (but perhaps/probably in a different order).List
of all subtrees in the tree.public <T extends java.util.Collection<Tree>> T subTrees(T n)
Collection
.
Note: If you only want to form this Collection so that you can
iterate over it, it is more efficient to simply use the Tree class's
own iterator()
method. This will iterate over the exact same
elements (but perhaps/probably in a different order).n
- A collection of nodes to which the subtrees will be added.public Tree deepCopy()
public Tree deepCopy(TreeFactory tf)
tf
- The TreeFactory used to make all nodes in the copied
tree structurepublic Tree deepCopy(TreeFactory tf, LabelFactory lf)
tf
- The TreeFactory used to make all nodes in the copied
tree structurelf
- The LabelFactory used to make all nodes in the copied
tree structurepublic Tree treeSkeletonCopy()
TreeFactory
that will
produce a Tree
like the input one.public Tree treeSkeletonCopy(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 treeSkeletonConstituentCopy()
public Tree treeSkeletonConstituentCopy(TreeFactory tf, LabelFactory lf)
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 subtreesTree
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 of the structure of the tree 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(java.util.function.Predicate<Tree> nodeFilter)
nodeFilter
- a Filter method which returns true to mean
keep this node, false to mean delete itpublic Tree spliceOut(java.util.function.Predicate<Tree> 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(java.util.function.Predicate<Tree> filter)
Filter
can assume
that it will not be called with a null
argument.
filter
- the filter to be appliednull
if the root node of the tree is filteredpublic Tree prune(java.util.function.Predicate<Tree> filter, TreeFactory tf)
Filter
can assume
that it will not be called with a null
argument.filter
- the filter to be appliedtf
- the TreeFactory to be used to make new Tree nodes if needednull
if the root node of the tree is filteredpublic Tree skipRoot()
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
.root
- The root node of the whole TreeTree
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).public Tree ancestor(int height, Tree root)
height
nodes up from the current node.height
- How many nodes up to go. A parameter of 0 means return
this node, 1 means to return the parent node and so on.root
- The root node that this Tree is embedded underheight
. It returns null
if it does not exist or the tree implementation does not keep track
of parentspublic java.util.Iterator<Tree> 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.)public java.util.List<Tree> postOrderNodeList()
public java.util.List<Tree> preOrderNodeList()
public static Tree valueOf(java.lang.String str)
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
LabeledScoredTreeReaderFactory. This means that "standard"
normalizations (stripping functional categories, indices,
empty nodes, and A-over-A nodes) will be done on it.str
- The tree as a bracketed list in a String.java.lang.RuntimeException
- If Tree format is not validpublic static Tree valueOf(java.lang.String str, TreeReaderFactory trf)
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.trf
- The TreeFactory used to make the new Treejava.lang.RuntimeException
- If the Tree format is not validpublic Tree getChild(int i)
i
- The daughter indexpublic Tree removeChild(int i)
ArrayIndexOutOfBoundsException
if
the daughter index is too big for the list of daughters.i
- The daughter indexpublic void addChild(int i, Tree t)
ArrayIndexOutOfBoundsException
if
the daughter index is too big for the list of daughters.i
- the index position at which to add the new daughtert
- the new daughterpublic void addChild(Tree t)
t
- the new daughterpublic Tree setChild(int i, Tree t)
i
th child of this
with the tree t.
Note
that this method will throw an ArrayIndexOutOfBoundsException
if
the child index is too big for the list of children.i
- The index position at which to replace the childt
- The new childpublic boolean dominates(Tree t)
this
dominates the Tree passed in
as an argument. Object equality (==) rather than .equals() is used
to determine domination.
t.dominates(t) returns false.public java.util.List<Tree> dominationPath(Tree t)
this
and the dominated node itself.
Returns null if t is not dominated by this
. Object
equality (==) is the relevant criterion.
t.dominationPath(t) returns null.public java.util.List<Tree> pathNodeToNode(Tree t1, Tree t2)
t1
and t2
which are
dominated by this node, returns a list of all the nodes on the
path from t1 to t2, inclusive, or null if none found.public Tree joinNode(Tree t1, Tree t2)
t1
and t2
which are
dominated by this node, returns their "join node": the node
j
such that j
dominates both
t1
and t2
, and every other node which
dominates both t1
and t2
dominates j
.
In the special case that t1 dominates t2, return t1, and vice versa.
Return null
if no such node can be found.public boolean cCommands(Tree t1, Tree t2)
t1
and t2
which are
dominated by this node, returns true
iff
t1
c-commands t2
. (A node c-commands
its sister(s) and any nodes below its sister(s).)public java.util.List<Tree> siblings(Tree root)
root
- The root within which this tree node is containedpublic void insertDtr(Tree dtr, int position)
dtr
after position
existing
daughters in this
.public java.lang.String value()
Label
public void setValue(java.lang.String value)
Label
public void setFromString(java.lang.String labelStr)
Label
String
representing the
complete contents of the label. A class implementing label may
throw an UnsupportedOperationException
for this
method (only). Typically, this method would do
some appropriate decoding of the string in a way that sets
multiple fields in an inverse of the toString()
method.setFromString
in interface Label
labelStr
- the String that translates into the content of the
labelpublic LabelFactory labelFactory()
null
if no appropriate factory is known.labelFactory
in interface Label
public int leftCharEdge(Tree node)
public int rightCharEdge(Tree node)
node
- The subtree to look for in this Treepublic int nodeNumber(Tree root)
root
and ending at this
. Returns -1 if root
does not contain this
.root
- the root node of the relevant treeroot
does not contain this
.public Tree getNodeNumber(int i)
i
th node in the tree, with node numbers defined
as in nodeNumber(Tree)
.i
- the node number to fetchi
th node in the treejava.lang.IndexOutOfBoundsException
- if i
is not between 1 and
the number of nodes (inclusive) contained in this
.public void indexLeaves()
Tree
, starting with 1.
The leaves are traversed from left
to right. If the node is already indexed, then it uses the existing index.
This will only work if the leaves extend CoreMap.public void indexLeaves(boolean overWrite)
overWrite
- Whether to replace an existing index for a leaf.public int indexLeaves(int startIndex, boolean overWrite)
Tree
, beginning with
startIndex
, and traversing the leaves from left
to right. If node is already indexed, then it uses the existing index.
This method only works if the labels of the tree implement
CoreLabel!startIndex
- index for this nodeoverWrite
- Whether to replace an existing index for a leaf.public void percolateHeadIndices()
This method assumes CoreLabels!
public void indexSpans()
public void indexSpans(int startIndex)
public Pair<java.lang.Integer,java.lang.Integer> indexSpans(MutableInteger startIndex)
startIndex
- Begin indexing at this value