Package opennlp.tools.chunker
Class ChunkerME
java.lang.Object
opennlp.tools.chunker.ChunkerME
- All Implemented Interfaces:
Chunker,Probabilistic
The class represents a maximum-entropy-based
Chunker. A chunker can be used to
find flat structures based on sequence inputs such as noun phrases or named entities.
A chunker instance is thread-safe. One instance can be shared across multiple threads to save memory.
Note: In container environments with classloader isolation (e.g. Jakarta EE), ensure instances do
not outlive the application's lifecycle, as underlying components use ThreadLocal state that may
pin the classloader.
- See Also:
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate final ThreadLocal<Sequence> private final ChunkerContextGeneratorstatic final intprivate final SequenceClassificationModelThe model used to assign chunk tags to a sequence of tokens.private final SequenceValidator<TokenTag> -
Constructor Summary
ConstructorsConstructorDescriptionInitializes aChunkerby downloading a default model.ChunkerME(ChunkerModel model) Initializes aChunkerwith the specifiedChunkerModel. -
Method Summary
Modifier and TypeMethodDescriptionString[]Generates chunk tags for the given sequence returning the result in an array.Span[]chunkAsSpans(String[] toks, String[] tags) voidRemoves thread-local state to prevent classloader leaks in container environments.double[]probs()Retrieves the probabilities of the last decoded sequence.voidprobs(double[] probs) Populates the specified array with the probabilities of the last decoded sequence.Sequence[]topKSequences(String[] sentence, String[] tags) Computes the top k chunksequencesfor the specified sentence with the specified pos-tags.Sequence[]topKSequences(String[] sentence, String[] tags, double minSequenceScore) Computes the top k chunksequencesfor the specified sentence with the specified pos-tags.static ChunkerModeltrain(String lang, ObjectStream<ChunkSample> in, TrainingParameters mlParams, ChunkerFactory factory) Starts a training of aChunkerModelwith the given parameters.
-
Field Details
-
DEFAULT_BEAM_SIZE
public static final int DEFAULT_BEAM_SIZE- See Also:
-
bestSequence
-
model
The model used to assign chunk tags to a sequence of tokens. -
contextGenerator
-
sequenceValidator
-
-
Constructor Details
-
ChunkerME
Initializes aChunkerby downloading a default model.- Parameters:
language- The language of the model.- Throws:
IOException- Thrown if the model cannot be downloaded or saved.
-
ChunkerME
- Parameters:
model- A validmodelinstance.
-
-
Method Details
-
chunk
Description copied from interface:ChunkerGenerates chunk tags for the given sequence returning the result in an array. -
chunkAsSpans
Description copied from interface:Chunker- Specified by:
chunkAsSpansin interfaceChunker- Parameters:
toks- an array of the tokens or words of the sequence.tags- an array of the pos tags of the sequence.- Returns:
- an array of
spanswith chunk tags for each chunk in the sequence.
-
topKSequences
Description copied from interface:ChunkerComputes the top k chunksequencesfor the specified sentence with the specified pos-tags.- Specified by:
topKSequencesin interfaceChunker- Parameters:
sentence- The tokens of the sentence.tags- The pos-tags for the specified sentence.- Returns:
- the top k chunk
sequencesfor the specified sentence.
-
topKSequences
Description copied from interface:ChunkerComputes the top k chunksequencesfor the specified sentence with the specified pos-tags.- Specified by:
topKSequencesin interfaceChunker- Parameters:
sentence- The tokens of the sentence.tags- The pos-tags for the specified sentence.minSequenceScore- A lower bound on the score of a returned sequence.- Returns:
- the top k chunk
sequencesfor the specified sentence.
-
probs
public void probs(double[] probs) Populates the specified array with the probabilities of the last decoded sequence. The sequence was determined based on the previous call tochunk. The specified array should be at least as large as the number of tokens in the previous call tochunk.- Parameters:
probs- An array used to hold the probabilities of the last decoded sequence.
-
probs
public double[] probs()Retrieves the probabilities of the last decoded sequence. The sequence was determined based on the previous call tochunk(String[], String[]).- Specified by:
probsin interfaceProbabilistic- Returns:
- an array with the same number of probabilities as tokens when
chunk(String[], String[])was last called
-
clearThreadLocalState
public void clearThreadLocalState()Removes thread-local state to prevent classloader leaks in container environments. Call when the thread is returned to a pool or the chunker is no longer needed. -
train
public static ChunkerModel train(String lang, ObjectStream<ChunkSample> in, TrainingParameters mlParams, ChunkerFactory factory) throws IOException Starts a training of aChunkerModelwith the given parameters.- Parameters:
lang- The ISO conform language code.in- TheObjectStreamofChunkSampleused as input for training.mlParams- TheTrainingParametersfor the context of the training.factory- TheChunkerFactoryfor creating related objects defined viamlParams.- Returns:
- A valid, trained
ChunkerModelinstance. - Throws:
IOException- Thrown if IO errors occurred.IllegalArgumentException- Thrown if the specifiedTrainerFactory.TrainerTypeis not supported.
-