Package opennlp.tools.ml
Class BeamSearch
java.lang.Object
opennlp.tools.ml.BeamSearch
- All Implemented Interfaces:
AutoCloseable,SequenceClassificationModel
@ThreadSafe
public class BeamSearch
extends Object
implements SequenceClassificationModel, AutoCloseable
Performs k-best search over a sequence.
This is based on the description in Ratnaparkhi (1998), PhD diss, Univ. of Pennsylvania.
This implementation is thread-safe. The contexts cache and probability buffer
are maintained per-thread via ThreadLocal.
Note: In container environments with classloader isolation (e.g. Jakarta EE),
ThreadLocal state may pin the classloader. Ensure instances do not outlive
the application's lifecycle, or call ThreadLocal.remove() on pooled threads.
- See Also:
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionprivate static final classprivate static final classImmutable node in a backward-linked chain of outcome candidates, used only insidebestSequences(int, Object[], Object[], double, BeamSearchContextGenerator, SequenceValidator). -
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final Stringprivate final intprivate static final Object[]private final MaxentModelprivate final intprivate final ThreadLocal<BeamSearch.CacheState> private static final int -
Constructor Summary
ConstructorsConstructorDescriptionBeamSearch(int size, MaxentModel model) Initializes aBeamSearchinstance.BeamSearch(int size, MaxentModel model, int cacheSize) Initializes aBeamSearchinstance with an optional per-thread contexts cache. -
Method Summary
Modifier and TypeMethodDescription<T> SequencebestSequence(T[] sequence, Object[] additionalContext, BeamSearchContextGenerator<T> cg, SequenceValidator<T> validator) Finds theSequencewith the highest probability.<T> Sequence[]bestSequences(int numSequences, T[] sequence, Object[] additionalContext, double minSequenceScore, BeamSearchContextGenerator<T> cg, SequenceValidator<T> validator) Finds the n most probablesequenceswith the highest probability.<T> Sequence[]bestSequences(int numSequences, T[] sequence, Object[] additionalContext, BeamSearchContextGenerator<T> cg, SequenceValidator<T> validator) Finds the n most probablesequenceswith the highest probability.voidclose()ClearsThreadLocalstate for the current thread only.String[]
-
Field Details
-
BEAM_SIZE_PARAMETER
- See Also:
-
EMPTY_ADDITIONAL_CONTEXT
-
size
private final int size -
model
-
ZERO_LOG
private static final int ZERO_LOG- See Also:
-
cacheSize
private final int cacheSize -
threadState
-
-
Constructor Details
-
BeamSearch
Initializes aBeamSearchinstance.- Parameters:
size- The size of the beam (k).model- TheMaxentModelfor assigning probabilities to the sequence outcomes.
-
BeamSearch
Initializes aBeamSearchinstance with an optional per-thread contexts cache.- Parameters:
size- The size of the beam (k).model- TheMaxentModelfor assigning probabilities to the sequence outcomes.cacheSize- The capacity of the per-thread contexts cache. Use0to disable only that cache; per-thread score buffers are still allocated so evaluation stays thread-safe (seeBeamSearch.CacheState).
-
-
Method Details
-
bestSequences
public <T> Sequence[] bestSequences(int numSequences, T[] sequence, Object[] additionalContext, double minSequenceScore, BeamSearchContextGenerator<T> cg, SequenceValidator<T> validator) Finds the n most probablesequenceswith the highest probability.- Specified by:
bestSequencesin interfaceSequenceClassificationModel- Parameters:
numSequences- The number of sequences to compute.sequence- Thesequenceused as input.additionalContext- An array that provides additional information (context).minSequenceScore- The minimum score to achieve.cg- TheBeamSearchContextGeneratorto use.validator- TheSequenceValidatorto validate with.- Returns:
- The
sequenceswith the highest probability.
-
bestSequences
public <T> Sequence[] bestSequences(int numSequences, T[] sequence, Object[] additionalContext, BeamSearchContextGenerator<T> cg, SequenceValidator<T> validator) Finds the n most probablesequenceswith the highest probability.- Specified by:
bestSequencesin interfaceSequenceClassificationModel- Parameters:
numSequences- The number of sequences to compute.sequence- Thesequenceused as input.additionalContext- An array that provides additional information (context).cg- TheBeamSearchContextGeneratorto use.validator- TheSequenceValidatorto validate with.- Returns:
- The
sequenceswith the highest probability.
-
bestSequence
public <T> Sequence bestSequence(T[] sequence, Object[] additionalContext, BeamSearchContextGenerator<T> cg, SequenceValidator<T> validator) Finds theSequencewith the highest probability.- Specified by:
bestSequencein interfaceSequenceClassificationModel- Parameters:
sequence- Thesequenceused as input.additionalContext- An array that provides additional information (context).cg- TheBeamSearchContextGeneratorto use.validator- TheSequenceValidatorto validate with.- Returns:
- The
Sequencewith the highest probability.
-
getOutcomes
- Specified by:
getOutcomesin interfaceSequenceClassificationModel- Returns:
- Retrieves all possible outcomes.
-
close
public void close()ClearsThreadLocalstate for the current thread only. This is intentionally not a "shut down theBeamSearchinstance" operation: a singleBeamSearchis typically shared across many pool threads, and each one owns an independentBeamSearch.CacheStateentry.Typical usage patterns:
- Worker thread returning to a pool: call
close()(or wrap a single decode call in try-with-resources) on each pool thread that has touched the instance. - Application shutdown / classloader unload:
close()on a single thread is not sufficient to release every per-thread slot — those die with their owning threads, or must be cleared on each thread before the application classloader is released.
Same lifecycle contract as
clearThreadLocalState()on the seven ME classes.- Specified by:
closein interfaceAutoCloseable
- Worker thread returning to a pool: call
-