Package opennlp.tools.stemmer
Class CachingStemmer
java.lang.Object
opennlp.tools.stemmer.DelegatingStemmer<CachingStemmer.ThreadState>
opennlp.tools.stemmer.CachingStemmer
- All Implemented Interfaces:
Stemmer
A
Stemmer that memoizes word-to-stem mappings in a bounded per-thread LRU cache.
Each thread gets its own delegate stemmer (minted from the supplied StemmerFactory)
and its own cache, so instances are safe to share regardless of whether the factory's stemmers
are, and memory is bounded to capacity entries per thread that stems. Caching pays off
on threads reused across many words, such as a fixed platform-thread pool; on a
virtual-thread-per-task executor every task starts with an empty cache, so repeats are served
only within one task. stemAll(CharSequence) is forwarded to the delegate uncached.
Long-running environments such as application containers should call
DelegatingStemmer.clearThreadLocalState() when a pooled thread no longer uses this stemmer.
-
Nested Class Summary
Nested Classes -
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final intThe default maximum number of word-to-stem entries cached per thread: 1024.Fields inherited from class opennlp.tools.stemmer.DelegatingStemmer
state -
Constructor Summary
ConstructorsConstructorDescriptionCachingStemmer(StemmerFactory factory) Creates a caching stemmer with the default capacity.CachingStemmer(StemmerFactory factory, int capacity) Creates a caching stemmer. -
Method Summary
Modifier and TypeMethodDescriptionvoidEmpties the calling thread's cache while keeping its delegate, forcing every subsequent word through a fresh stemming pass.stem(CharSequence word) Stemsword.stemAll(CharSequence word) Returns all stem forms forword.private static Supplier<CachingStemmer.ThreadState> threadStateSupplier(StemmerFactory factory, int capacity) Validates the constructor arguments eagerly and returns a supplier that mints one delegate and its per-thread cache, so invalid arguments fail at construction rather than on first use.Methods inherited from class opennlp.tools.stemmer.DelegatingStemmer
clearThreadLocalState, requireFactory
-
Field Details
-
DEFAULT_CAPACITY
public static final int DEFAULT_CAPACITYThe default maximum number of word-to-stem entries cached per thread: 1024.- See Also:
-
-
Constructor Details
-
CachingStemmer
Creates a caching stemmer with the default capacity.- Parameters:
factory- The factory that mints one delegate per thread. Must not benull.- Throws:
IllegalArgumentException- Thrown iffactoryisnull.
-
CachingStemmer
Creates a caching stemmer.- Parameters:
factory- The factory that mints one delegate per thread. Must not benull.capacity- The maximum number of word-to-stem entries kept per thread; must be positive.- Throws:
IllegalArgumentException- Thrown iffactoryisnullorcapacityis not positive.
-
-
Method Details
-
threadStateSupplier
private static Supplier<CachingStemmer.ThreadState> threadStateSupplier(StemmerFactory factory, int capacity) Validates the constructor arguments eagerly and returns a supplier that mints one delegate and its per-thread cache, so invalid arguments fail at construction rather than on first use.- Parameters:
factory- The factory that mints one delegate per thread. Must not benull.capacity- The maximum number of word-to-stem entries kept per thread; must be positive.- Returns:
- a supplier of fresh per-thread state.
- Throws:
IllegalArgumentException- Thrown iffactoryisnullorcapacityis not positive.
-
stem
Stemsword.- Parameters:
word- The input word. Must not benull.- Returns:
- The stemmed form.
-
stemAll
Returns all stem forms forword. Defaults to a single-element list fromStemmer.stem(CharSequence). Dictionary-based engines may override this to return multiple roots; delegating wrappers must forward it to preserve the full list.- Parameters:
word- The input word. Must not benull.- Returns:
- all stem forms for
word
-
clearCache
public void clearCache()Empties the calling thread's cache while keeping its delegate, forcing every subsequent word through a fresh stemming pass. Only the calling thread's cache is affected. A thread that has not stemmed yet has its state initialized by this call, so invoke it on the thread whose cache should be emptied, not from an unrelated maintenance thread.
-