Package opennlp.tools.stemmer
Class CachingStemmer
java.lang.Object
opennlp.tools.stemmer.CachingStemmer
- All Implemented Interfaces:
opennlp.tools.stemmer.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
clearThreadLocalState() when a pooled thread no longer uses this stemmer.
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final intCovers the high-frequency vocabulary of most corpora while keeping the per-thread footprint small. -
Constructor Summary
ConstructorsConstructorDescriptionCachingStemmer(opennlp.tools.stemmer.StemmerFactory factory) Creates a caching stemmer with the default capacity.CachingStemmer(opennlp.tools.stemmer.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.voidRemoves this thread's payload to prevent classloader leaks in container environments.stem(CharSequence word) stemAll(CharSequence word)
-
Field Details
-
DEFAULT_CAPACITY
public static final int DEFAULT_CAPACITYCovers the high-frequency vocabulary of most corpora while keeping the per-thread footprint small.- See Also:
-
-
Constructor Details
-
CachingStemmer
public CachingStemmer(opennlp.tools.stemmer.StemmerFactory factory) Creates a caching stemmer with the default capacity.- Parameters:
factory- The factory that mints one delegate per thread. Must not benull.- Throws:
IllegalArgumentException- iffactoryisnull.
-
CachingStemmer
public CachingStemmer(opennlp.tools.stemmer.StemmerFactory factory, int capacity) 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- iffactoryisnullorcapacityis not positive.
-
-
Method Details
-
stem
-
stemAll
-
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. -
clearThreadLocalState
public void clearThreadLocalState()Removes this thread's payload to prevent classloader leaks in container environments. Call when the thread is returned to a pool or the stemmer is no longer needed, mirroringclearThreadLocalState()on the thread-safe*MEcomponents.
-