Class CachedFeatureGenerator

java.lang.Object
opennlp.tools.util.featuregen.CachedFeatureGenerator
All Implemented Interfaces:
AdaptiveFeatureGenerator

@ThreadSafe public class CachedFeatureGenerator extends Object implements AdaptiveFeatureGenerator
Caches features of the aggregated generators.

The cache is maintained per-thread via ThreadLocal, making this class safe for concurrent use from multiple threads. Each thread gets its own independent cache that is cleared when a new sentence (token array) is encountered.

Cache key is reference identity, not content. The "is this still the same sentence?" check uses tokens == state.prevTokens; passing a freshly allocated String[] with the same contents is treated as a new sentence and triggers a cache miss + clear. Reuse the same tokens array across calls when you need the cache to hit.

Note: In container environments with classloader isolation (e.g. Jakarta EE), ThreadLocal state may pin the classloader. Ensure instances do not outlive the application's lifecycle, or call ThreadLocal.remove() on pooled threads.

See Also:
  • Field Details

  • Constructor Details

  • Method Details

    • createFeatures

      public void createFeatures(List<String> features, String[] tokens, int index, String[] previousOutcomes)
      Description copied from interface: AdaptiveFeatureGenerator
      Adds the appropriate features for the token at the specified index with the specified array of previousOutcomes to the specified list of features.
      Specified by:
      createFeatures in interface AdaptiveFeatureGenerator
      Parameters:
      features - The list of features to be added to.
      tokens - The tokens of the sentence or other text unit being processed.
      index - The index of the token which is currently being processed.
      previousOutcomes - The outcomes for the tokens prior to the specified index.
    • updateAdaptiveData

      public void updateAdaptiveData(String[] tokens, String[] outcomes)
      Description copied from interface: AdaptiveFeatureGenerator
      Informs a feature generator that the specified tokens have been classified with the corresponding set of specified outcomes.
      Specified by:
      updateAdaptiveData in interface AdaptiveFeatureGenerator
      Parameters:
      tokens - The tokens of the sentence or other text unit which has been processed.
      outcomes - The outcomes associated with the specified tokens.
    • clearAdaptiveData

      public void clearAdaptiveData()
      Description copied from interface: AdaptiveFeatureGenerator
      Informs a feature generator that the context of the adaptive data (typically a document) is no longer valid.
      Specified by:
      clearAdaptiveData in interface AdaptiveFeatureGenerator
    • getNumberOfCacheHits

      @Deprecated(since="3.0.0") public long getNumberOfCacheHits()
      Deprecated.
      Cache statistics are no longer tracked.
      Returns:
      Retrieves the number of cache hits for the current thread.
    • getNumberOfCacheMisses

      @Deprecated(since="3.0.0") public long getNumberOfCacheMisses()
      Deprecated.
      Cache statistics are no longer tracked.
      Returns:
      Retrieves the number of cache misses for the current thread.
    • getCachedFeatureGenerator

      public AdaptiveFeatureGenerator getCachedFeatureGenerator()