Class StopwordFilteringTokenizer

java.lang.Object
opennlp.tools.stopword.StopwordFilteringTokenizer
All Implemented Interfaces:
Tokenizer

@ThreadSafe public final class StopwordFilteringTokenizer extends Object implements Tokenizer
A Tokenizer decorator which delegates tokenization to a wrapped Tokenizer and then removes any tokens identified as stopwords by the supplied StopwordFilter.

Both tokenize(String) and tokenizePos(String) apply the filter using the same greedy longest-match window scan, so single-token (1-gram) and multi-token (n-gram) stopword entries are dropped identically across tokenize(String), tokenizePos(String) and StopwordFilterStream. For tokenizePos(String) the Spans covering a matched entry are dropped while the offsets of the remaining spans are kept intact (they continue to refer to positions in the original input string).

Instances are immutable and therefore safe for concurrent use provided that both the wrapped Tokenizer and the StopwordFilter are thread-safe. DictionaryStopwordFilter is unconditionally thread-safe; combined with a thread-safe delegate tokenizer (e.g. SimpleTokenizer.INSTANCE) the resulting decorator is thread-safe with no further synchronization required.

  • Field Details

  • Constructor Details

  • Method Details

    • tokenize

      public String[] tokenize(String s)
      Tokenizes the supplied string with the wrapped Tokenizer and then removes any tokens which the StopwordFilter considers a stopword.
      Specified by:
      tokenize in interface Tokenizer
      Parameters:
      s - The string to be tokenized.
      Returns:
      The remaining tokens in their original order.
    • tokenizePos

      public Span[] tokenizePos(String s)
      Computes token spans with the wrapped Tokenizer and then drops the spans covering any stopword entry according to the StopwordFilter. A greedy left-to-right window scan mirrors StopwordFilter.filter(String[]): at each position the longest window of consecutive spans whose covered texts form a registered entry is removed; otherwise the current span is kept and the scan advances by one. This way multi-word (n-gram) entries are dropped here exactly as they are by tokenize(String). The relative order and the offsets of the surviving spans are preserved.
      Specified by:
      tokenizePos in interface Tokenizer
      Parameters:
      s - The string to be tokenized.
      Returns:
      The remaining Spans in their original order.