Class TokenizerAnnotator

java.lang.Object
opennlp.tools.tokenize.TokenizerAnnotator
All Implemented Interfaces:
DocumentAnnotator

public final class TokenizerAnnotator extends Object implements DocumentAnnotator
Adapts a Tokenizer to the document pipeline: provides Layers.TOKENS.

When Layers.SENTENCES is present, each sentence is tokenized separately and the token spans are shifted back to document coordinates; a present-but-empty sentence layer therefore yields a present-but-empty token layer. Only when the sentence layer is absent is the whole text tokenized at once. Either way, every token span refers to the original document text.

The adapter holds no per-call state; it is as thread-safe as the tokenizer it wraps.

Since:
3.0.0
  • Field Details

    • tokenizer

      private final Tokenizer tokenizer
  • Constructor Details

    • TokenizerAnnotator

      public TokenizerAnnotator(Tokenizer tokenizer)
      Initializes the adapter.
      Parameters:
      tokenizer - The tokenizer to delegate to. Must not be null.
      Throws:
      IllegalArgumentException - Thrown if tokenizer is null.
  • Method Details

    • annotate

      public Document annotate(Document document)
      Tokenizes the document and adds the Layers.TOKENS layer, sentence by sentence when a sentence layer is present and over the whole text otherwise.
      Specified by:
      annotate in interface DocumentAnnotator
      Parameters:
      document - The document to annotate. Must not be null.
      Returns:
      A new Document with the Layers.TOKENS layer added. Never null.
      Throws:
      IllegalArgumentException - Thrown if document is null or already carries the Layers.TOKENS layer.
    • addTokens

      private void addTokens(List<Annotation<String>> tokens, String text, int offset)
      Tokenizes one stretch of text and appends its tokens, shifted back into document coordinates.
      Parameters:
      tokens - The layer under construction.
      text - The stretch to tokenize.
      offset - The stretch's start offset in the document text.
    • provides

      public Set<LayerKey<?>> provides()
      Specified by:
      provides in interface DocumentAnnotator
      Returns:
      The keys of the layers this annotator adds. Never null.
    • toString

      public String toString()
      Returns the adapter's simple class name, which names it in pipeline validation messages.
      Overrides:
      toString in class Object
      Returns:
      the adapter's simple class name, which names it in pipeline validation messages