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
    The default maximum number of word-to-stem entries cached per thread: 1024.
  • 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
    Empties the calling thread's cache while keeping its delegate, forcing every subsequent word through a fresh stemming pass.
    void
    Clears the calling thread's state to prevent classloader leaks in container environments: a per-thread payload is removed, and the owner payload is reset but stays referenced by this instance.

    Methods inherited from class Object

    equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

    • DEFAULT_CAPACITY

      public static final int DEFAULT_CAPACITY
      The default maximum number of word-to-stem entries cached per thread: 1024.
      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 be null.
      Throws:
      IllegalArgumentException - Thrown 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 - Thrown 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()
      Clears the calling thread's state to prevent classloader leaks in container environments: a per-thread payload is removed, and the owner payload is reset but stays referenced by this instance. Call when the thread is returned to a pool or the stemmer is no longer needed, mirroring clearThreadLocalState() on the thread-safe *ME components.