Class ArcStandardState

java.lang.Object
opennlp.tools.depparse.ArcStandardState

final class ArcStandardState extends Object
Tracks the state of an arc-standard dependency parse: a stack of tokens being processed, a buffer of unread tokens, and the dependency arcs created so far.

A SHIFT transition moves the next token from the buffer onto the stack. LEFT_ARC and RIGHT_ARC create a dependency arc between the top two stack entries and remove the dependent. Parsing is complete when the buffer is empty and only the artificial root remains on the stack.

The artificial root, represented by ROOT, stays at the bottom of the stack. The final RIGHT_ARC transition attaches the remaining token to this root, ensuring that the sentence has exactly one root token. Stack and buffer accessors return NONE for positions that do not exist.

The transition system is described in Nivre (2004).

Each instance is used for a single parse and is not thread-safe.

Since:
3.0.0
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    private final int[]
     
    private int
     
    private final int[]
     
    private final int[]
     
    (package private) static final int
    The value returned for stack or buffer positions that do not exist.
    private final String[]
     
    private final int[]
     
    (package private) static final int
    The stack value representing the artificial root node.
    private final int[]
     
    private final int
     
    private int
     
  • Constructor Summary

    Constructors
    Modifier
    Constructor
    Description
    (package private)
    ArcStandardState(int tokenCount)
    Initializes the start configuration for a sentence: the artificial root on the stack and every token in the buffer.
    private
    Deep-copies source; used only by copy().
  • Method Summary

    Modifier and Type
    Method
    Description
    (package private) void
    apply(Transition transition)
    Applies a transition, updating stack, buffer, and arcs.
    (package private) int
    assignedDependents(int index)
    Retrieves how many dependents have been attached to a token so far.
    (package private) String
    assignedRelation(int index)
    Retrieves the relation a token was attached under, when it has been attached.
    private void
    attach(int head, int dependent, String relation)
    Records the arc from head to dependent and updates the dependent bookkeeping of head when it is a token rather than the artificial root.
    (package private) int
    buffer(int fromFront)
    Retrieves a buffer element counted from the front.
    (package private) int
     
    (package private) boolean
    canApply(Transition transition)
    Checks whether a transition may be applied in the current configuration.
    private void
    checkTokenIndex(int index)
    Rejects a token index outside [0, tokenCount).
    (package private) ArcStandardState
    Creates an independent copy, so that a search alternative can be advanced without affecting the configuration it was expanded from.
    (package private) boolean
     
    (package private) int
    leftmostDependent(int index)
    Retrieves the leftmost dependent attached to a token so far.
    (package private) int
    rightmostDependent(int index)
    Retrieves the rightmost dependent attached to a token so far.
    (package private) int
    stack(int fromTop)
    Retrieves a stack element counted from the top.
    (package private) int
     
    (package private) int
     
     
    (package private) DependencyTree
    Builds the DependencyTree of a completed parse.

    Methods inherited from class java.lang.Object

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

    • ROOT

      static final int ROOT
      The stack value representing the artificial root node.
      See Also:
    • NONE

      static final int NONE
      The value returned for stack or buffer positions that do not exist.
      See Also:
    • tokenCount

      private final int tokenCount
    • stack

      private final int[] stack
    • heads

      private final int[] heads
    • relations

      private final String[] relations
    • assignedDependents

      private final int[] assignedDependents
    • leftmostDependents

      private final int[] leftmostDependents
    • rightmostDependents

      private final int[] rightmostDependents
    • top

      private int top
    • bufferFront

      private int bufferFront
  • Constructor Details

    • ArcStandardState

      ArcStandardState(int tokenCount)
      Initializes the start configuration for a sentence: the artificial root on the stack and every token in the buffer.
      Parameters:
      tokenCount - The number of tokens in the sentence. Must be greater than zero.
      Throws:
      IllegalArgumentException - Thrown if tokenCount is not positive.
    • ArcStandardState

      private ArcStandardState(ArcStandardState source)
      Deep-copies source; used only by copy().
      Parameters:
      source - The state to copy.
  • Method Details

    • copy

      Creates an independent copy, so that a search alternative can be advanced without affecting the configuration it was expanded from.
      Returns:
      A copy that can be advanced without affecting this state. Never null.
    • isTerminal

      boolean isTerminal()
      Returns:
      true if the buffer is empty and only the artificial root remains on the stack, so the parse is complete.
    • canApply

      boolean canApply(Transition transition)
      Checks whether a transition may be applied in the current configuration.
      Parameters:
      transition - The transition to check. Must not be null.
      Returns:
      true if apply(Transition) would succeed.
      Throws:
      IllegalArgumentException - Thrown if transition is null.
    • apply

      void apply(Transition transition)
      Applies a transition, updating stack, buffer, and arcs.
      Parameters:
      transition - The transition to apply. Must not be null and must be applicable per canApply(Transition).
      Throws:
      IllegalArgumentException - Thrown if the transition is null or not applicable in the current configuration.
    • attach

      private void attach(int head, int dependent, String relation)
      Records the arc from head to dependent and updates the dependent bookkeeping of head when it is a token rather than the artificial root.
      Parameters:
      head - The head token index, or ROOT for the artificial root.
      dependent - The zero-based index of the token being attached.
      relation - The relation label of the arc.
    • stack

      int stack(int fromTop)
      Retrieves a stack element counted from the top.
      Parameters:
      fromTop - Zero for the top element, one for the element below it, and so on. Must not be negative.
      Returns:
      The token index at that position, ROOT for the artificial root, or NONE if the position does not exist.
      Throws:
      IllegalArgumentException - Thrown if fromTop is negative.
    • buffer

      int buffer(int fromFront)
      Retrieves a buffer element counted from the front.
      Parameters:
      fromFront - Zero for the next token to be shifted, one for the token after it, and so on. Must not be negative.
      Returns:
      The token index at that position, or NONE if the position does not exist.
      Throws:
      IllegalArgumentException - Thrown if fromFront is negative.
    • tokenCount

      int tokenCount()
      Returns:
      The number of tokens in this parse.
    • stackSize

      int stackSize()
      Returns:
      The number of stack elements including the artificial root.
    • bufferSize

      int bufferSize()
      Returns:
      The number of tokens still in the buffer.
    • assignedDependents

      int assignedDependents(int index)
      Retrieves how many dependents have been attached to a token so far.
      Parameters:
      index - The zero-based token index. Must be within [0, tokenCount).
      Returns:
      The number of arcs assigned with the token as head.
      Throws:
      IllegalArgumentException - Thrown if index is out of range.
    • checkTokenIndex

      private void checkTokenIndex(int index)
      Rejects a token index outside [0, tokenCount).
      Parameters:
      index - The zero-based token index to check.
      Throws:
      IllegalArgumentException - Thrown if index is out of range.
    • leftmostDependent

      int leftmostDependent(int index)
      Retrieves the leftmost dependent attached to a token so far.
      Parameters:
      index - The zero-based token index. Must be within [0, tokenCount).
      Returns:
      The dependent's token index, or NONE when none is attached.
      Throws:
      IllegalArgumentException - Thrown if index is out of range.
    • rightmostDependent

      int rightmostDependent(int index)
      Retrieves the rightmost dependent attached to a token so far.
      Parameters:
      index - The zero-based token index. Must be within [0, tokenCount).
      Returns:
      The dependent's token index, or NONE when none is attached.
      Throws:
      IllegalArgumentException - Thrown if index is out of range.
    • assignedRelation

      String assignedRelation(int index)
      Retrieves the relation a token was attached under, when it has been attached.
      Parameters:
      index - The zero-based token index. Must be within [0, tokenCount).
      Returns:
      The relation label, or null when the token is still unattached.
      Throws:
      IllegalArgumentException - Thrown if index is out of range.
    • toTree

      DependencyTree toTree()
      Builds the DependencyTree of a completed parse.
      Returns:
      The parsed tree. Never null.
      Throws:
      IllegalStateException - Thrown if the parse is not yet terminal.
    • toString

      public String toString()
      Overrides:
      toString in class Object