Class DependencyTree

java.lang.Object
opennlp.tools.depparse.DependencyTree
All Implemented Interfaces:
Serializable

@ThreadSafe public final class DependencyTree extends Object implements 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

    Fields
    Modifier and Type
    Field
    Description
    private final int
     
    private final int[]
     
    private final String[]
     
    private final int
     
    private static final long
     
    private static final byte
    Traversal state of a token whose head chain has not been followed yet.
    private static final byte
    Traversal state of a token whose head chain is known to reach the root.
    private static final byte
    Traversal state of a token on the head chain currently being followed.
  • Constructor Summary

    Constructors
    Modifier
    Constructor
    Description
    private
    DependencyTree(int[] heads, String[] relations, int root)
    Wraps already validated arrays; instances are created through of(int[], java.lang.String[]).
  • Method Summary

    Modifier and Type
    Method
    Description
     
    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.
    private void
    checkIndex(int index)
    Rejects a token index outside [0, size()).
    boolean
    int
    int
    headOf(int index)
    Retrieves the head of a token.
    of(int[] heads, String[] relations)
    Creates a DependencyTree from parallel head and relation arrays.
    private Object
    Replaces a deserialized instance by a validated one, so the arrays and the cached hash read from a stream pass the same checks as of(int[], String[]).
    relationOf(int index)
    Retrieves the relation label of a token.
    int
     
    int
     

    Methods inherited from class java.lang.Object

    clone, finalize, getClass, notify, notifyAll, wait, wait, wait
  • Field Details

    • serialVersionUID

      private static final long serialVersionUID
      See Also:
    • UNVISITED

      private static final byte UNVISITED
      Traversal state of a token whose head chain has not been followed yet.
      See Also:
    • VISITING

      private static final byte VISITING
      Traversal state of a token on the head chain currently being followed.
      See Also:
    • VISITED

      private static final byte VISITED
      Traversal state of a token whose head chain is known to reach the root.
      See Also:
    • heads

      private final int[] heads
    • relations

      private final String[] relations
    • root

      private final int root
    • hash

      private final int hash
  • Constructor Details

    • DependencyTree

      private DependencyTree(int[] heads, String[] relations, int root)
      Wraps already validated arrays; instances are created through of(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 is DependencyArc.ROOT_HEAD.
  • Method Details

    • of

      public static DependencyTree of(int[] heads, String[] relations)
      Creates a DependencyTree from parallel head and relation arrays.
      Parameters:
      heads - For each token, the zero-based index of its head token, or DependencyArc.ROOT_HEAD for the sentence root. Must not be null or empty, every value must be a valid token index or DependencyArc.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 be null, must have the same length as heads, and no entry may be null or blank.
      Returns:
      A validated DependencyTree. Never null.
      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_HEAD when the token is the sentence root.
      Throws:
      IllegalArgumentException - Thrown if index is out of range.
    • relationOf

      public String relationOf(int index)
      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 if index is out of range.
    • root

      public int root()
      Returns:
      The zero-based index of the sentence root token.
    • arcs

      public List<DependencyArc> 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 if index is out of range.
    • equals

      public boolean equals(Object obj)
      Overrides:
      equals in class Object
    • hashCode

      public int hashCode()
      Overrides:
      hashCode in class Object
    • readResolve

      private Object readResolve() throws InvalidObjectException
      Replaces a deserialized instance by a validated one, so the arrays and the cached hash read from a stream pass the same checks as of(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

      public String toString()
      Overrides:
      toString in class Object