Package opennlp.tools.stemmer

# Class CachingStemmer

java.lang.Object
opennlp.tools.stemmer.CachingStemmer

All Implemented Interfaces:
  `opennlp.tools.stemmer.Stemmer`

@ThreadSafe public final class CachingStemmer extends Object
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

  Fields
  Modifier and Type
  Field
  Description
  `static final int`
  `DEFAULT_CAPACITY`
  Covers the high-frequency vocabulary of most corpora while keeping the per-thread footprint small.

- ## Constructor Summary

  Constructors
  Constructor
  Description
  `CachingStemmer(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 Type
  Method
  Description
  `void`
  `clearCache()`
  Empties the calling thread's cache while keeping its delegate, forcing every subsequent word through a fresh stemming pass.
  `void`
  `clearThreadLocalState()`
  Removes this thread's payload to prevent classloader leaks in container environments.
  `CharSequence`
  `stem(CharSequence word)`
  `List<CharSequence>`
  `stemAll(CharSequence word)`

  ### Methods inherited from class java.lang.Object

  `equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

- ## Field Details

  - ### DEFAULT_CAPACITY

    public static final int DEFAULT_CAPACITY
    Covers the high-frequency vocabulary of most corpora while keeping the per-thread footprint small.
    See Also:
      - Constant Field Values

- ## 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 be `null`.
    Throws:
      `IllegalArgumentException` - if `factory` is `null`.

  - ### 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 be `null`.
      `capacity` - The maximum number of word-to-stem entries kept per thread; must be positive.
    Throws:
      `IllegalArgumentException` - if `factory` is `null` or `capacity` is not positive.

- ## Method Details

  - ### stem

    public CharSequence stem(CharSequence word)

  - ### stemAll

    public List<CharSequence> stemAll(CharSequence 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.

  - ### 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, mirroring `clearThreadLocalState()` on the thread-safe `*ME` components.