Class NameFinderDL
- All Implemented Interfaces:
AutoCloseable,opennlp.tools.namefind.OffsetMappingNameFinder,opennlp.tools.namefind.TokenNameFinder
TokenNameFinder that uses ONNX models.
Tokenization performs BERT basic tokenization (text normalization)
before wordpiece, see BertTokenizer. Input
text is not lower cased by default, because named entity recognition
models are commonly cased: capitalization is a strong signal for entity
boundaries. For uncased models, set
InferenceOptions.setLowerCase(boolean) to true.
This class is thread-safe and may be shared across threads, provided the supplied
SentenceDetector is itself thread-safe (e.g.
invalid reference
opennlp.tools.sentdetect.SentenceDetectorME@ThreadSafe). Inference holds no per-call instance state, the relevant
InferenceOptions values are snapshotted into final fields at construction (so
mutating the passed options afterwards does not affect a shared instance), and the
underlying OrtSession supports concurrent execution. This thread-safety
guarantee applies until AbstractDL.close() is called; callers must not race
close() with inference methods.
- See Also:
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final StringTokens after which the following token attaches directly when span text is reconstructed.Tokens that attach directly to the preceding token when span text is reconstructed.static final StringPrefix used by BIO labels for the first token in an entity span.static final StringPrefix used by BIO labels for continuation tokens in an entity span.static final StringFields inherited from class opennlp.dl.AbstractDL
ATTENTION_MASK, INPUT_IDS, TOKEN_TYPE_IDS -
Constructor Summary
ConstructorsConstructorDescriptionNameFinderDL(File model, File vocabulary, Map<Integer, String> ids2Labels, InferenceOptions inferenceOptions, opennlp.tools.sentdetect.SentenceDetector sentenceDetector) Instantiates aname finderusing ONNX models.NameFinderDL(File model, File vocabulary, Map<Integer, String> ids2Labels, opennlp.tools.sentdetect.SentenceDetector sentenceDetector) Instantiates aname finderusing ONNX models. -
Method Summary
Modifier and TypeMethodDescriptionvoidopennlp.tools.util.Span[]opennlp.tools.util.Span[]findInOriginal(String[] input) Finds names and returns theirspansin coordinates of the original joined input (String.join(" ", input)), regardless of any whitespace or dash normalization applied before inference.Methods inherited from class opennlp.dl.AbstractDL
close, loadVocab
-
Field Details
-
SEPARATOR
- See Also:
-
CLS_TOKEN
- See Also:
-
PREFIX_BEGIN
Prefix used by BIO labels for the first token in an entity span.- See Also:
-
PREFIX_INSIDE
Prefix used by BIO labels for continuation tokens in an entity span.- See Also:
-
NO_SPACE_BEFORE_TOKENS
Tokens that attach directly to the preceding token when span text is reconstructed. -
NO_SPACE_AFTER_TOKENS
Tokens after which the following token attaches directly when span text is reconstructed.
-
-
Constructor Details
-
NameFinderDL
public NameFinderDL(File model, File vocabulary, Map<Integer, String> ids2Labels, opennlp.tools.sentdetect.SentenceDetector sentenceDetector) throws IOException, ai.onnxruntime.OrtExceptionInstantiates aname finderusing ONNX models.- Parameters:
model- The ONNX model file.vocabulary- The model file's vocabulary file.ids2Labels- The mapping of model output indices to BIO labels. This must be exhaustive over the model's output indices; a token whose predicted index is unmapped raises anIllegalStateExceptionduringfind(String[]).sentenceDetector- TheSentenceDetectorto be used.- Throws:
ai.onnxruntime.OrtException- Thrown if themodelcannot be loaded.IOException- Thrown if errors occurred loading themodelorvocabulary.IllegalArgumentException- Thrown ifinferenceOptions,ids2Labels, or the sentence detector isnull.
-
NameFinderDL
public NameFinderDL(File model, File vocabulary, Map<Integer, String> ids2Labels, InferenceOptions inferenceOptions, opennlp.tools.sentdetect.SentenceDetector sentenceDetector) throws IOException, ai.onnxruntime.OrtExceptionInstantiates aname finderusing ONNX models.- Parameters:
model- The ONNX model file.vocabulary- The model file's vocabulary file.ids2Labels- The mapping of model output indices to BIO labels. This must be exhaustive over the model's output indices; a token whose predicted index is unmapped raises anIllegalStateExceptionduringfind(String[]).inferenceOptions-InferenceOptionsto control the inference.sentenceDetector- TheSentenceDetectorto be used.- Throws:
ai.onnxruntime.OrtException- Thrown if themodelcannot be loaded.IOException- Thrown if errors occurred loading themodelorvocabulary.IllegalArgumentException- Thrown ifinferenceOptions,ids2Labels, or the sentence detector isnull.
-
-
Method Details
-
find
Joins the provided tokens with spaces, sentence-splits the joined text, runs each sentence through the ONNX token-classification model, decodes BIO labels into
spans, and resolves those spans to character offsets in the joined text after any optional input normalization.Note: this returns correct original offsets in every case except one. Whitespace folding is length-preserving, so it never moves offsets. Only dash folding can change the input length, and only for a non-BMP dash; so when
normalizeDashesis enabled and the input contains a supplementary-plane dash, the returned spans are offsets into the normalized text rather than the original. For an exact original mapping in that case, usefindInOriginal(String[]).- Specified by:
findin interfaceopennlp.tools.namefind.TokenNameFinder- Throws:
IllegalStateException- Thrown if inference fails, if the model output shape is not the expectedfloat[batch][token][label]form, if the model output contains no usable label score for a token, or if the model's predicted index for a token is not present in the configured label map.IllegalArgumentException- Thrown ifinputisnullor contains anulltoken, or if a token produced for the input is not present in the vocabulary, which indicates the vocabulary file does not match the model.
-
findInOriginal
Finds names and returns theirspansin coordinates of the original joined input (String.join(" ", input)), regardless of any whitespace or dash normalization applied before inference. Spans are mapped back through the normalizationAlignment, so a fold that changes the input length (a supplementary dash shrinking, or an expansion) does not shift the reported offsets. This implementsOffsetMappingNameFinder, so an interface-typed caller can reach the offset-correct path withfinder instanceof OffsetMappingNameFinder.- Specified by:
findInOriginalin interfaceopennlp.tools.namefind.OffsetMappingNameFinder- Parameters:
input- The tokens to search.- Returns:
- The detected spans, in original-input character coordinates.
- Throws:
IllegalStateException- Thrown under the same conditions asfind(String[]).IllegalArgumentException- Thrown under the same conditions asfind(String[]).
-
clearAdaptiveData
public void clearAdaptiveData()- Specified by:
clearAdaptiveDatain interfaceopennlp.tools.namefind.TokenNameFinder
-