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:
  • 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.
    • 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.
    • equals

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

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

      public String toString()
      Overrides:
      toString in class Object