Class TermAnalyzer
Terms by segmenting text and applying a configured stack of normalization
Dimensions to each token. The analyzer is the configuration; each Term is the
layered result for one token, with the configured dimensions computed eagerly and any other
dimension computed lazily on first request.
Segmentation uses the Unicode UAX #29 word tokenizer, so the
input does not need to be pre-tokenized. The character-level dimensions (Dimension.NFC
through Dimension.CONFUSABLE_FOLD) have built-in defaults; Dimension.STEM and
Dimension.LEMMA are enabled by supplying a Stemmer or Lemmatizer.
An instance is immutable and is thread-safe when its configured transforms are. The built-in
character normalizers are stateless and the Snowball stemmers are thread-safe. When
Dimension.STEM is enabled through TermAnalyzer.Builder.stem(StemmerFactory), stemming is
routed through a CachingStemmer: the analyzer is safe to share across threads even when
the factory mints stateful stemmers, and repeated words resolve from a bounded per-thread cache
instead of being re-stemmed. A raw Stemmer passed to TermAnalyzer.Builder.stem(Stemmer) is
only safe when that stemmer is itself thread-safe or the analyzer is confined to one thread.
Long-running environments such as application containers should call
clearThreadLocalState() when a pooled thread no longer uses an analyzer whose stemmer is
a CachingStemmer.
-
Nested Class Summary
Nested Classes -
Field Summary
FieldsModifier and TypeFieldDescriptionprivate final Dimensionprivate final Lemmatizerprivate final Stemmerprivate final WordTokenizerprivate final EnumMap<Dimension, CharSequenceNormalizer> -
Constructor Summary
ConstructorsModifierConstructorDescriptionprivateTermAnalyzer(TermAnalyzer.Builder builder) Constructs an analyzer from the builder's configured chain, transforms, and engines. -
Method Summary
Modifier and TypeMethodDescriptionanalyze(CharSequence text) Segmentstextwith the UAX #29 word tokenizer and returns oneTermper word token, in order.Returns oneTermper supplied token, attaching the matching part-of-speech tag so thatDimension.LEMMAcan be computed.(package private) StringApplies one dimension's transform to a single token value.static TermAnalyzer.Builderbuilder()Returns a newTermAnalyzer.Builder.voidRemoves the calling thread's state of the analyzer's stemmer to prevent classloader leaks in container environments.Returns the configured dimensions that are computed eagerly, in pipeline order.(package private) DimensionReturns the last configured dimension in pipeline order, orDimension.ORIGINALwhen none are configured.
-
Field Details
-
chain
-
finalDimension
-
transforms
-
stemmer
-
lemmatizer
-
tokenizer
-
-
Constructor Details
-
TermAnalyzer
Constructs an analyzer from the builder's configured chain, transforms, and engines.
-
-
Method Details
-
builder
Returns a newTermAnalyzer.Builder. The builder starts with no dimensions enabled and the default UAX #29 word tokenizer; enable dimensions and set a stemmer, lemmatizer, or tokenizer on it, then callTermAnalyzer.Builder.build().- Returns:
- a new
TermAnalyzer.Builder
-
analyze
Segmentstextwith the UAX #29 word tokenizer and returns oneTermper word token, in order. The terms carry no part-of-speech tag, soDimension.LEMMAcannot be computed from this entry point: if a lemmatizer is configured, this method throws -- useanalyze(String[], String[])when lemmas are needed.- Parameters:
text- The text to analyze. Must not benull.- Returns:
- The terms.
- Throws:
IllegalArgumentException- iftextisnull.IllegalStateException- ifDimension.LEMMAis configured, because no part-of-speech tags are available from raw text.
-
analyze
Returns oneTermper supplied token, attaching the matching part-of-speech tag so thatDimension.LEMMAcan be computed. The terms have no source span.- Parameters:
tokens- The tokens. Must not benullor containnullelements.tags- The part-of-speech tag for each token; must be the same length astokensand must not benull. Anulltag is only acceptable whenDimension.LEMMAis not computed for that token.- Returns:
- The terms.
- Throws:
IllegalArgumentException- iftokensortagsisnull, if they differ in length, or iftokenscontains anullelement.
-
dimensions
Returns the configured dimensions that are computed eagerly, in pipeline order. The list never includesDimension.ORIGINAL, which is always present.- Returns:
- the configured dimensions that are computed eagerly, in pipeline order
-
clearThreadLocalState
public void clearThreadLocalState()Removes the calling thread's state of the analyzer's stemmer to prevent classloader leaks in container environments. Call when the thread is returned to a pool or the analyzer is no longer needed. The state is held when the stemmer is aCachingStemmer, whether created byTermAnalyzer.Builder.stem(StemmerFactory)or passed toTermAnalyzer.Builder.stem(Stemmer); for any other stemmer, or without one, this method has no effect.- See Also:
-
finalDimension
Dimension finalDimension()Returns the last configured dimension in pipeline order, orDimension.ORIGINALwhen none are configured. This is the layerTerm.normalized()reports.- Returns:
- the last configured dimension in pipeline order, or
Dimension.ORIGINALwhen none are configured
-
apply
Applies one dimension's transform to a single token value.- Parameters:
dimension- The dimension whose transform to apply.input- The token value to transform.posTag- The token's part-of-speech tag; only read byDimension.LEMMAand may benullotherwise.- Returns:
- The transformed value; never
null. - Throws:
IllegalStateException- if a token-level dimension was requested without the engine (or tag) it needs:Dimension.STEMwithout aStemmer,Dimension.LEMMAwithout aLemmatizeror without a tag, or a lemmatizer that returns no lemma. Also thrown for a character-level dimension with neither a default nor a configured normalizer.
-