Package opennlp.tools.ml
Class BeamSearch
java.lang.Object
opennlp.tools.ml.BeamSearch
- All Implemented Interfaces:
AutoCloseable,opennlp.tools.ml.model.SequenceClassificationModel
@ThreadSafe
public class BeamSearch
extends Object
implements opennlp.tools.ml.model.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:
-
Field Summary
Fields -
Constructor Summary
ConstructorsConstructorDescriptionBeamSearch(int size, opennlp.tools.ml.model.MaxentModel model) Initializes aBeamSearchinstance.BeamSearch(int size, opennlp.tools.ml.model.MaxentModel model, int cacheSize) Initializes aBeamSearchinstance with an optional per-thread contexts cache. -
Method Summary
Modifier and TypeMethodDescription<T> opennlp.tools.util.SequencebestSequence(T[] sequence, Object[] additionalContext, opennlp.tools.util.BeamSearchContextGenerator<T> cg, opennlp.tools.util.SequenceValidator<T> validator) <T> opennlp.tools.util.Sequence[]bestSequences(int numSequences, T[] sequence, Object[] additionalContext, double minSequenceScore, opennlp.tools.util.BeamSearchContextGenerator<T> cg, opennlp.tools.util.SequenceValidator<T> validator) <T> opennlp.tools.util.Sequence[]bestSequences(int numSequences, T[] sequence, Object[] additionalContext, opennlp.tools.util.BeamSearchContextGenerator<T> cg, opennlp.tools.util.SequenceValidator<T> validator) voidclose()ClearsThreadLocalstate for the current thread only.String[]
-
Field Details
-
BEAM_SIZE_PARAMETER
- See Also:
-
-
Constructor Details
-
BeamSearch
public BeamSearch(int size, opennlp.tools.ml.model.MaxentModel model) Initializes aBeamSearchinstance.- Parameters:
size- The size of the beam (k).model- TheMaxentModelfor assigning probabilities to the sequence outcomes.
-
BeamSearch
public BeamSearch(int size, opennlp.tools.ml.model.MaxentModel model, int cacheSize) 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> opennlp.tools.util.Sequence[] bestSequences(int numSequences, T[] sequence, Object[] additionalContext, double minSequenceScore, opennlp.tools.util.BeamSearchContextGenerator<T> cg, opennlp.tools.util.SequenceValidator<T> validator) - Specified by:
bestSequencesin interfaceopennlp.tools.ml.model.SequenceClassificationModel
-
bestSequences
public <T> opennlp.tools.util.Sequence[] bestSequences(int numSequences, T[] sequence, Object[] additionalContext, opennlp.tools.util.BeamSearchContextGenerator<T> cg, opennlp.tools.util.SequenceValidator<T> validator) - Specified by:
bestSequencesin interfaceopennlp.tools.ml.model.SequenceClassificationModel
-
bestSequence
public <T> opennlp.tools.util.Sequence bestSequence(T[] sequence, Object[] additionalContext, opennlp.tools.util.BeamSearchContextGenerator<T> cg, opennlp.tools.util.SequenceValidator<T> validator) - Specified by:
bestSequencein interfaceopennlp.tools.ml.model.SequenceClassificationModel
-
getOutcomes
- Specified by:
getOutcomesin interfaceopennlp.tools.ml.model.SequenceClassificationModel
-
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
-