public class TreeGraphNode extends Tree implements HasParent
TreeGraphNode
is simply a {@code Tree}
with some additional functionality. For example, the
parent()
method works without searching from the root.
Labels are always assumed to be {@code CoreLabel}
.
This class makes the horrible mistake of changing the semantics of
equals and hashCode to go back to "==" and System.identityHashCode,
despite the semantics of the superclass's equality.Modifier and Type | Field and Description |
---|---|
protected TreeGraphNode[] |
children
Children of this node.
|
protected TreeGraphNode |
parent
Parent of this node.
|
protected static TreeGraphNode[] |
ZERO_TGN_CHILDREN
A leaf node should have a zero-length array for its
children.
|
EMPTY_TREE_ARRAY
Modifier | Constructor and Description |
---|---|
|
TreeGraphNode(Label label)
Create a new
TreeGraphNode with the supplied
label. |
|
TreeGraphNode(Label label,
java.util.List<Tree> children)
Create a new
TreeGraphNode with the supplied
label and list of child nodes. |
protected |
TreeGraphNode(Tree t,
TreeGraphNode parent)
Create a new
TreeGraphNode having the same tree
structure and label values as an existing tree (but no shared
storage). |
Modifier and Type | Method and Description |
---|---|
void |
addChild(int i,
Tree t)
Adds a child in the ith location.
|
TreeGraphNode[] |
children()
Returns an array of the children of this node.
|
boolean |
equals(java.lang.Object o)
Implements equality for
TreeGraphNode s. |
static TreeFactory |
factory()
Return a
TreeFactory that produces trees of type
TreeGraphNode . |
static TreeFactory |
factory(LabelFactory lf)
Return a
TreeFactory that produces trees of type
TreeGraphNode , with the Label made
by the supplied LabelFactory . |
int |
hashCode()
Implements a hashCode for Tree's.
|
TreeGraphNode |
headWordNode()
Return the node containing the head word for this node (or
null if none), as recorded in this node's {@code CoreLabel} . |
TreeGraphNode |
highestNodeWithSameHead()
Checks the node's ancestors to find the highest ancestor with the
same
headWordNode as this node. |
int |
index()
Get the index for the current node.
|
CoreLabel |
label()
Returns the label associated with the current node, or null
if there is no label.
|
static void |
main(java.lang.String[] args)
Just for testing.
|
TreeGraphNode |
parent()
Get the parent for the current node.
|
void |
percolateHeads(HeadFinder hf)
Uses the specified
{@code HeadFinder}
to determine the heads for this node and all its descendants,
and to store references to the head word node and head tag node
in this node's {@code CoreLabel} and the
CoreLabel s of all its descendants. |
Tree |
removeChild(int i)
Removes the ith child from the TreeGraphNode.
|
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)
Sets the children of this
TreeGraphNode . |
protected void |
setIndex(int index)
Set the index for the current node.
|
void |
setLabel(CoreLabel label)
Sets the label associated with the current node.
|
void |
setLabel(Label label)
Sets the label associated with the current node, if there is one.
|
void |
setParent(TreeGraphNode parent)
Set the parent for the current node.
|
java.lang.String |
toOneLineString()
Returns a
String representation of this node and
its subtree as a one-line parenthesized list. |
java.lang.String |
toPrettyString(int indentLevel)
Returns a
String representation of this node and
its subtree with one node per line, indented according to
indentLevel . |
java.lang.String |
toString()
Converts parse tree to string in Penn Treebank format.
|
java.lang.String |
toString(CoreLabel.OutputFormat format) |
TreeFactory |
treeFactory()
Returns a
TreeFactory that produces
TreeGraphNode s. |
addChild, ancestor, cCommands, constituents, constituents, constituents, constituents, constituents, deepCopy, deepCopy, deepCopy, dependencies, dependencies, dependencies, depth, depth, dominates, dominationPath, firstChild, flatten, flatten, getChild, getChildrenAsList, getLeaves, getLeaves, getNodeNumber, getSpan, headPreTerminal, headTerminal, headTerminal, indentedListPrint, indentedListPrint, indentedXMLPrint, indentedXMLPrint, indexLeaves, indexLeaves, indexLeaves, indexSpans, indexSpans, indexSpans, insertDtr, isBinarized, isBinary, isLeaf, isPhrasal, isPrePreTerminal, isPreTerminal, isUnaryRewrite, iterator, joinNode, labeledYield, labeledYield, labelFactory, labels, lastChild, leftCharEdge, localTree, localTrees, mapDependencies, mapDependencies, nodeNumber, nodeString, numChildren, objectIndexOf, parent, pathNodeToNode, pennPrint, pennPrint, pennPrint, pennPrint, pennPrint, pennString, percolateHeadAnnotations, percolateHeadIndices, postOrderNodeList, preOrderNodeList, preTerminalYield, preTerminalYield, printLocalTree, printLocalTree, prune, prune, rightCharEdge, score, setFromString, setLabels, setScore, setSpans, setValue, siblings, size, skipRoot, spanString, spliceOut, spliceOut, subTreeList, subTrees, subTrees, taggedLabeledYield, taggedLabeledYield, taggedYield, taggedYield, toStringBuilder, toStringBuilder, transform, transform, treeSkeletonConstituentCopy, treeSkeletonConstituentCopy, treeSkeletonCopy, treeSkeletonCopy, updateBrackets, upperMostUnary, value, valueOf, valueOf, yield, yield, yield, yieldHasWord, yieldHasWord, yieldWords, yieldWords
add, addAll, clear, contains, containsAll, isEmpty, remove, removeAll, retainAll, toArray, toArray
protected TreeGraphNode parent
protected TreeGraphNode[] children
protected static final TreeGraphNode[] ZERO_TGN_CHILDREN
public TreeGraphNode(Label label)
TreeGraphNode
with the supplied
label.label
- the label for this node.public TreeGraphNode(Label label, java.util.List<Tree> children)
TreeGraphNode
with the supplied
label and list of child nodes.label
- the label for this node.children
- the list of child TreeGraphNode
s
for this node.protected TreeGraphNode(Tree t, TreeGraphNode parent)
TreeGraphNode
having the same tree
structure and label values as an existing tree (but no shared
storage). Operates recursively to construct an entire
subtree.t
- the tree to copyparent
- the parent nodepublic boolean equals(java.lang.Object o)
TreeGraphNode
s. Unlike
Tree
s, TreeGraphNode
s should be
considered equal only if they are ==. Implementation note:
TODO: This should be changed via introducing a Tree interface with the current Tree and this class implementing it, since what is done here breaks the equals() contract.public int hashCode()
Tree
public CoreLabel label()
public void setLabel(Label label)
Tree
public void setLabel(CoreLabel label)
label
- the new label to use.public int index()
protected void setIndex(int index)
public TreeGraphNode parent()
parent
in interface HasParent
parent
in class Tree
Tree
node or null
Tree.parent(Tree)
public void setParent(TreeGraphNode parent)
public TreeGraphNode[] children()
children
in class Tree
Tree.getChildrenAsList()
public void setChildren(Tree[] children)
TreeGraphNode
. If
given null
, this method sets
the node's children to the canonical zero-length Tree[] array.setChildren
in class Tree
children
- an array of child treesTree.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).setChildren
in class Tree
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).Tree.setChildren(Tree[])
public Tree setChild(int i, Tree t)
Tree
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.public void addChild(int i, Tree t)
public Tree removeChild(int i)
removeChild
in class Tree
i
- The daughter indexpublic void percolateHeads(HeadFinder hf)
{@code HeadFinder}
to determine the heads for this node and all its descendants,
and to store references to the head word node and head tag node
in this node's {@code CoreLabel}
and the
CoreLabel
s of all its descendants.
Note that, in contrast to {@code Tree.percolateHeads()}
, which assumes {@code CategoryWordTag}
labels and therefore stores head
words and head tags merely as String
s, this
method stores references to the actual nodes. This mitigates
potential problems in sentences which contain the same word
more than once.
percolateHeads
in class Tree
hf
- The headfinding algorithm to usepublic TreeGraphNode headWordNode()
null
if none), as recorded in this node's {@code CoreLabel}
. (In contrast to {@code CategoryWordTag}
, we store head words and head
tags as references to nodes, not merely as
String
s.)public TreeGraphNode highestNodeWithSameHead()
headWordNode
as this node.public TreeFactory treeFactory()
TreeFactory
that produces
TreeGraphNode
s. The Label
of
this
is examined, and providing it is not
null
, a LabelFactory
which will
produce that kind of Label
is supplied to the
TreeFactory
. If the Label
is
null
, a
CoreLabel.factory()
will be used. The factories
returned on different calls are different: a new one is
allocated each time.treeFactory
in class Tree
public static TreeFactory factory()
TreeFactory
that produces trees of type
TreeGraphNode
. The factory returned is always
the same one (a singleton).public static TreeFactory factory(LabelFactory lf)
TreeFactory
that produces trees of type
TreeGraphNode
, with the Label
made
by the supplied LabelFactory
. The factory
returned is a different one each time.lf
- The LabelFactory
to usepublic java.lang.String toPrettyString(int indentLevel)
String
representation of this node and
its subtree with one node per line, indented according to
indentLevel
.indentLevel
- how many levels to indent (0 for root node)String
representation of this subtreepublic java.lang.String toOneLineString()
String
representation of this node and
its subtree as a one-line parenthesized list.String
representation of this subtreepublic java.lang.String toString()
Tree
StringBuilder
through all the printing.public java.lang.String toString(CoreLabel.OutputFormat format)
public static void main(java.lang.String[] args)