Package opennlp.tools.util.normalizer
Class TermAnalyzer.Builder
java.lang.Object
opennlp.tools.util.normalizer.TermAnalyzer.Builder
- Enclosing class:
TermAnalyzer
A builder for
TermAnalyzer.-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate Lemmatizerprivate Stemmerprivate WordTokenizerprivate final EnumMap<Dimension, CharSequenceNormalizer> -
Constructor Summary
ConstructorsModifierConstructorDescriptionprivateBuilder()UseTermAnalyzer.builder()to obtain an instance. -
Method Summary
Modifier and TypeMethodDescriptionEnablesDimension.ACCENT_FOLD.accentFold(Set<Character.UnicodeScript> foldScripts, boolean foldStrokeLetters) EnablesDimension.ACCENT_FOLDrestricted to a specific set of scripts, instead of the default Latin/Greek/Cyrillic.build()Returns a newTermAnalyzerwith this configuration.caseFold()EnablesDimension.CASE_FOLD.EnablesDimension.CASE_FOLDusing the given locale's case rules (for example Turkish dotted/dotless i), instead of the defaultLocale.ROOT.EnablesDimension.CONFUSABLE_FOLD.dash()EnablesDimension.DASH.dash(CharSequenceNormalizer normalizer) EnablesDimension.DASHwith a specific normalizer (a custom dash set or target).EnablesDimension.EMOJI_FOLD, folding emoji to ASCII emoticons.EnablesDimension.FULL_CASE_FOLD, Unicode full case folding.lemmatize(Lemmatizer value) EnablesDimension.LEMMAthrough the given lemmatizer.maxTokenLength(int maxTokenLength) Sets the maximum token length of the tokenizer used byTermAnalyzer.analyze(CharSequence).nfc()EnablesDimension.NFC.nfkc()EnablesDimension.NFKC.private voidrequireNotBothCaseFolds(Dimension adding) Rejects configuring bothcaseFold()andfullCaseFold(), since full case folding already includes case folding plus the expanding folds.EnablesDimension.STEMthrough the given stemmer.stem(StemmerFactory factory) EnablesDimension.STEMthrough aStemmerFactory.stem(StemmerFactory factory, int cacheSize) EnablesDimension.STEMthrough aStemmerFactorywith an explicit stem cache capacity, otherwise identical tostem(StemmerFactory).tokenizer(WordTokenizer value) Sets the tokenizer used byTermAnalyzer.analyze(CharSequence).transform(Dimension dimension, CharSequenceNormalizer normalizer) Enables a character-level dimension with a specific normalizer, overriding its default (for example a locale-specific case fold for a language profile).EnablesDimension.WHITESPACE.whitespace(CharSequenceNormalizer normalizer) EnablesDimension.WHITESPACEwith a specific normalizer, choosing the fold target and behavior.
-
Field Details
-
chain
-
transforms
-
stemmer
-
lemmatizer
-
tokenizer
-
-
Constructor Details
-
Builder
private Builder()UseTermAnalyzer.builder()to obtain an instance.
-
-
Method Details
-
nfc
EnablesDimension.NFC.- Returns:
- this builder
-
nfkc
EnablesDimension.NFKC.- Returns:
- this builder
-
whitespace
EnablesDimension.WHITESPACE.- Returns:
- this builder
-
whitespace
EnablesDimension.WHITESPACEwith a specific normalizer, choosing the fold target and behavior. For a custom class and target use aCharClassmethod reference, for examplewhitespace(CharClass.of(members, replacement)::collapse).- Parameters:
normalizer- The whitespace normalizer to use. Must not benull.- Returns:
- this builder
- Throws:
IllegalArgumentException- ifnormalizerisnull.
-
dash
EnablesDimension.DASH.- Returns:
- this builder
-
dash
EnablesDimension.DASHwith a specific normalizer (a custom dash set or target).- Parameters:
normalizer- The dash normalizer to use. Must not benull.- Returns:
- this builder
- Throws:
IllegalArgumentException- ifnormalizerisnull.
-
caseFold
EnablesDimension.CASE_FOLD.- Returns:
- this builder
- Throws:
IllegalStateException- ifDimension.FULL_CASE_FOLDis already configured.
-
caseFold
EnablesDimension.CASE_FOLDusing the given locale's case rules (for example Turkish dotted/dotless i), instead of the defaultLocale.ROOT.- Parameters:
locale- The locale whose case rules to apply. Must not benull.- Returns:
- this builder
- Throws:
IllegalArgumentException- iflocaleisnull.IllegalStateException- ifDimension.FULL_CASE_FOLDis already configured.
-
fullCaseFold
EnablesDimension.FULL_CASE_FOLD, Unicode full case folding. UnlikecaseFold()it also applies the expanding folds (the sharp s toss, the Latin ligatures), so use one or the other rather than both.- Returns:
- this builder
- Throws:
IllegalStateException- ifDimension.CASE_FOLDis already configured.
-
requireNotBothCaseFolds
Rejects configuring bothcaseFold()andfullCaseFold(), since full case folding already includes case folding plus the expanding folds.- Parameters:
adding- the case-fold dimension being configured.- Throws:
IllegalStateException- if the other case-fold dimension is already configured.
-
accentFold
EnablesDimension.ACCENT_FOLD.- Returns:
- this builder
-
accentFold
public TermAnalyzer.Builder accentFold(Set<Character.UnicodeScript> foldScripts, boolean foldStrokeLetters) EnablesDimension.ACCENT_FOLDrestricted to a specific set of scripts, instead of the default Latin/Greek/Cyrillic.- Parameters:
foldScripts- The scripts whose diacritics to fold. Must not benullor containnullelements.foldStrokeLetters- Whether to also fold stroke letters such as o-slash and l-stroke.- Returns:
- this builder
- Throws:
IllegalArgumentException- iffoldScriptsisnullor contains anullelement.
-
emojiFold
EnablesDimension.EMOJI_FOLD, folding emoji to ASCII emoticons. Only this direction exists as a per-token layer: the reverse (emoticon to emoji) is a pre-tokenization transform, because a tokenizer splits an emoticon such as:-)into punctuation, so it can never arrive as a single token; seeEmoticonToEmojiCharSequenceNormalizer.- Returns:
- this builder
-
confusableFold
EnablesDimension.CONFUSABLE_FOLD.- Returns:
- this builder
-
transform
Enables a character-level dimension with a specific normalizer, overriding its default (for example a locale-specific case fold for a language profile).- Parameters:
dimension- The character-level dimension to enable. Must not benull.normalizer- The normalizer to use for it. Must not benull.- Returns:
- this builder
- Throws:
IllegalArgumentException- ifdimensionornormalizerisnull, or ifdimensionisDimension.ORIGINAL,Dimension.STEM, orDimension.LEMMA.IllegalStateException- ifdimensionisDimension.CASE_FOLDorDimension.FULL_CASE_FOLDand the other one is already configured.
-
stem
EnablesDimension.STEMthrough the given stemmer.- Parameters:
value- The stemmer. Must not benull.- Returns:
- this builder
- Throws:
IllegalArgumentException- ifvalueisnull.
-
stem
EnablesDimension.STEMthrough aStemmerFactory. The analyzer receives aCachingStemmerwith the default cache capacity: it can be shared across threads, and repeated words resolve from a bounded per-thread cache instead of being re-stemmed. The cache is keyed to the thread, so the memoization pays off on threads reused across many tokens (a fixed platform-thread pool); on a virtual-thread-per-task executor every task starts with an empty cache.- Parameters:
factory- The stemmer factory. Must not benull.- Returns:
- this builder
- Throws:
IllegalArgumentException- iffactoryisnull.
-
stem
EnablesDimension.STEMthrough aStemmerFactorywith an explicit stem cache capacity, otherwise identical tostem(StemmerFactory).- Parameters:
factory- The stemmer factory. Must not benull.cacheSize- The maximum number of word-to-stem entries kept per thread; must be positive.- Returns:
- this builder
- Throws:
IllegalArgumentException- iffactoryisnullorcacheSizeis not positive.
-
lemmatize
EnablesDimension.LEMMAthrough the given lemmatizer.- Parameters:
value- The lemmatizer. Must not benull.- Returns:
- this builder
- Throws:
IllegalArgumentException- ifvalueisnull.
-
tokenizer
Sets the tokenizer used byTermAnalyzer.analyze(CharSequence). The parameter is the UAX #29WordTokenizeron purpose: the analyzer segments anyCharSequencewithout copying it to aString, and its terms are defined by UAX #29 word boundaries rather than by an arbitrary tokenizer.- Parameters:
value- The tokenizer. Must not benull.- Returns:
- this builder
- Throws:
IllegalArgumentException- ifvalueisnull.
-
maxTokenLength
Sets the maximum token length of the tokenizer used byTermAnalyzer.analyze(CharSequence). Convenience fortokenizer(new WordTokenizer(maxTokenLength)).- Parameters:
maxTokenLength- The maximum number of characters in a token. Must be at least1.- Returns:
- this builder
- Throws:
IllegalArgumentException- ifmaxTokenLengthis less than1.
-
build
Returns a newTermAnalyzerwith this configuration.- Returns:
- a new
TermAnalyzerwith this configuration
-