Package opennlp.tools.depparse
Class ArcStandardState
java.lang.Object
opennlp.tools.depparse.ArcStandardState
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
FieldsModifier and TypeFieldDescriptionprivate final int[]private intprivate final int[]private final int[](package private) static final intThe value returned for stack or buffer positions that do not exist.private final String[]private final int[](package private) static final intThe stack value representing the artificial root node.private final int[]private final intprivate int -
Constructor Summary
ConstructorsModifierConstructorDescription(package private)ArcStandardState(int tokenCount) Initializes the start configuration for a sentence: the artificial root on the stack and every token in the buffer.privateArcStandardState(ArcStandardState source) Deep-copiessource; used only bycopy(). -
Method Summary
Modifier and TypeMethodDescription(package private) voidapply(Transition transition) Applies a transition, updating stack, buffer, and arcs.(package private) intassignedDependents(int index) Retrieves how many dependents have been attached to a token so far.(package private) StringassignedRelation(int index) Retrieves the relation a token was attached under, when it has been attached.private voidRecords the arc fromheadtodependentand updates the dependent bookkeeping ofheadwhen it is a token rather than the artificial root.(package private) intbuffer(int fromFront) Retrieves a buffer element counted from the front.(package private) int(package private) booleancanApply(Transition transition) Checks whether a transition may be applied in the current configuration.private voidcheckTokenIndex(int index) Rejects a token index outside[0, tokenCount).(package private) ArcStandardStatecopy()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) intleftmostDependent(int index) Retrieves the leftmost dependent attached to a token so far.(package private) intrightmostDependent(int index) Retrieves the rightmost dependent attached to a token so far.(package private) intstack(int fromTop) Retrieves a stack element counted from the top.(package private) int(package private) inttoString()(package private) DependencyTreetoTree()Builds theDependencyTreeof a completed parse.
-
Field Details
-
ROOT
static final int ROOTThe stack value representing the artificial root node.- See Also:
-
NONE
static final int NONEThe 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
-
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 iftokenCountis not positive.
-
ArcStandardState
Deep-copiessource; used only bycopy().- Parameters:
source- The state to copy.
-
-
Method Details
-
copy
ArcStandardState 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:
trueif the buffer is empty and only the artificial root remains on the stack, so the parse is complete.
-
canApply
Checks whether a transition may be applied in the current configuration.- Parameters:
transition- The transition to check. Must not benull.- Returns:
trueifapply(Transition)would succeed.- Throws:
IllegalArgumentException- Thrown iftransitionisnull.
-
apply
Applies a transition, updating stack, buffer, and arcs.- Parameters:
transition- The transition to apply. Must not benulland must be applicable percanApply(Transition).- Throws:
IllegalArgumentException- Thrown if the transition isnullor not applicable in the current configuration.
-
attach
Records the arc fromheadtodependentand updates the dependent bookkeeping ofheadwhen it is a token rather than the artificial root.- Parameters:
head- The head token index, orROOTfor 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,
ROOTfor the artificial root, orNONEif the position does not exist. - Throws:
IllegalArgumentException- Thrown iffromTopis 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
NONEif the position does not exist. - Throws:
IllegalArgumentException- Thrown iffromFrontis 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 ifindexis 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 ifindexis 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
NONEwhen none is attached. - Throws:
IllegalArgumentException- Thrown ifindexis 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
NONEwhen none is attached. - Throws:
IllegalArgumentException- Thrown ifindexis out of range.
-
assignedRelation
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
nullwhen the token is still unattached. - Throws:
IllegalArgumentException- Thrown ifindexis out of range.
-
toTree
DependencyTree toTree()Builds theDependencyTreeof a completed parse.- Returns:
- The parsed tree. Never
null. - Throws:
IllegalStateException- Thrown if the parse is not yet terminal.
-
toString
-