Class SnowballStemmer

java.lang.Object
opennlp.tools.stemmer.snowball.SnowballStemmer
All Implemented Interfaces:
opennlp.tools.stemmer.Stemmer

@ThreadSafe public class SnowballStemmer extends Object implements opennlp.tools.stemmer.Stemmer
A Stemmer backed by the generated Snowball engines.

The generated engines hold mutable per-call buffers, so this class routes each call to a per-thread engine, the same pattern the thread-safe *ME components use. A single SnowballStemmer instance is therefore safe to share across threads.

As with the *ME components, long-running environments such as application containers should call clearThreadLocalState() when a pooled thread no longer uses this stemmer; otherwise the thread retains its engine until the instance is unreachable, which can pin the defining classloader on redeploys.

  • Constructor Details

    • SnowballStemmer

      public SnowballStemmer(SnowballStemmer.ALGORITHM algorithm, int repeat)
      Creates a stemmer for the given algorithm and repeat count.
      Parameters:
      algorithm - The Snowball algorithm. Must not be null.
      repeat - How many times to apply the stemmer per word; must be positive.
      Throws:
      IllegalArgumentException - Thrown if algorithm is null or repeat is not positive.
    • SnowballStemmer

      public SnowballStemmer(SnowballStemmer.ALGORITHM algorithm)
      Creates a stemmer for the given algorithm with repeat = 1.
      Parameters:
      algorithm - The Snowball algorithm. Must not be null.
      Throws:
      IllegalArgumentException - Thrown if algorithm is null.
  • Method Details

    • stem

      public CharSequence stem(CharSequence word)
      Specified by:
      stem in interface opennlp.tools.stemmer.Stemmer
    • clearThreadLocalState

      public void clearThreadLocalState()
      Removes this thread's engine 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.