Package opennlp.tools.depparse
Class DependencyTree
java.lang.Object
opennlp.tools.depparse.DependencyTree
- All Implemented Interfaces:
Serializable
An immutable dependency tree over one sentence: for every token, the index of its head
and the label of the relation to that head.
Token indices are zero-based positions in the sentence the tree was built for.
Exactly one token carries the head value DependencyArc.ROOT_HEAD, marking it as
the sentence root. Instances are immutable and safe to share between threads.
- Since:
- 3.0.0
- See Also:
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate final intprivate final int[]private final String[]private final intprivate static final longprivate static final byteTraversal state of a token whose head chain has not been followed yet.private static final byteTraversal state of a token whose head chain is known to reach the root.private static final byteTraversal state of a token on the head chain currently being followed. -
Constructor Summary
ConstructorsModifierConstructorDescriptionprivateDependencyTree(int[] heads, String[] relations, int root) Wraps already validated arrays; instances are created throughof(int[], java.lang.String[]). -
Method Summary
Modifier and TypeMethodDescriptionarcs()private static voidcheckAcyclic(int[] heads) Rejects a cycle that is disconnected from the single root by following every token's head chain until it reaches the root or a token already known to reach it.private voidcheckIndex(int index) Rejects a token index outside[0, size()).booleaninthashCode()intheadOf(int index) Retrieves the head of a token.static DependencyTreeCreates aDependencyTreefrom parallel head and relation arrays.private ObjectReplaces a deserialized instance by a validated one, so the arrays and the cached hash read from a stream pass the same checks asof(int[], String[]).relationOf(int index) Retrieves the relation label of a token.introot()intsize()toString()
-
Field Details
-
serialVersionUID
private static final long serialVersionUID- See Also:
-
UNVISITED
private static final byte UNVISITEDTraversal state of a token whose head chain has not been followed yet.- See Also:
-
VISITING
private static final byte VISITINGTraversal state of a token on the head chain currently being followed.- See Also:
-
VISITED
private static final byte VISITEDTraversal state of a token whose head chain is known to reach the root.- See Also:
-
heads
private final int[] heads -
relations
-
root
private final int root -
hash
private final int hash
-
-
Constructor Details
-
DependencyTree
Wraps already validated arrays; instances are created throughof(int[], java.lang.String[]).- Parameters:
heads- The validated head array, owned by the new instance.relations- The validated relation array, owned by the new instance.root- The index of the single token whose head isDependencyArc.ROOT_HEAD.
-
-
Method Details
-
of
Creates aDependencyTreefrom parallel head and relation arrays.- Parameters:
heads- For each token, the zero-based index of its head token, orDependencyArc.ROOT_HEADfor the sentence root. Must not benullor empty, every value must be a valid token index orDependencyArc.ROOT_HEAD, no token may head itself, and exactly one token must be the root.relations- For each token, the label of the relation to its head. Must not benull, must have the same length asheads, and no entry may benullor blank.- Returns:
- A validated
DependencyTree. Nevernull. - Throws:
IllegalArgumentException- Thrown if any of the above constraints is violated.
-
checkAcyclic
private static void checkAcyclic(int[] heads) Rejects a cycle that is disconnected from the single root by following every token's head chain until it reaches the root or a token already known to reach it.- Parameters:
heads- The head array, already checked for range, self-heads, and root count.- Throws:
IllegalArgumentException- Thrown if a head chain returns to a token on that same chain.
-
size
public int size()- Returns:
- The number of tokens the tree spans.
-
headOf
public int headOf(int index) Retrieves the head of a token.- Parameters:
index- The zero-based token index. Must be within[0, size()).- Returns:
- The zero-based index of the head token, or
DependencyArc.ROOT_HEADwhen the token is the sentence root. - Throws:
IllegalArgumentException- Thrown ifindexis out of range.
-
relationOf
Retrieves the relation label of a token.- Parameters:
index- The zero-based token index. Must be within[0, size()).- Returns:
- The label of the relation between the token and its head. Never
null. - Throws:
IllegalArgumentException- Thrown ifindexis out of range.
-
root
public int root()- Returns:
- The zero-based index of the sentence root token.
-
arcs
- Returns:
- All arcs of the tree in token order, one per token, as a new unmodifiable
list allocated on every call. Never
null.
-
checkIndex
private void checkIndex(int index) Rejects a token index outside[0, size()).- Parameters:
index- The zero-based token index to check.- Throws:
IllegalArgumentException- Thrown ifindexis out of range.
-
equals
-
hashCode
public int hashCode() -
readResolve
Replaces a deserialized instance by a validated one, so the arrays and the cached hash read from a stream pass the same checks asof(int[], String[]).- Returns:
- A validated tree with the same heads and relations.
- Throws:
InvalidObjectException- Thrown if the stream does not hold a valid tree.
-
toString
-