Class LemmatizerAnnotator

java.lang.Object
opennlp.tools.lemmatizer.LemmatizerAnnotator
All Implemented Interfaces:
DocumentAnnotator

public final class LemmatizerAnnotator extends Object implements DocumentAnnotator
Adapts a Lemmatizer to the document pipeline: reads Layers.SENTENCES, Layers.TOKENS, and Layers.POS_TAGS and provides LEMMAS, one annotation per token on the token's span.

Each sentence is lemmatized separately, the way the lemmatizer contract expects its input, so lemmatization decisions never cross a sentence boundary. Token spans already refer to the original document text, so only the token and tag sequences handed to the lemmatizer are sliced per sentence; the produced lemma layer stays aligned with Layers.TOKENS by position.

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

Since:
3.0.0
  • Field Details

    • LEMMAS

      public static final LayerKey<String> LEMMAS
      The lemma layer. It is aligned with the token layer by position, and each annotation carries the lemma of its token on that token's span.
    • lemmatizer

      private final Lemmatizer lemmatizer
  • Constructor Details

    • LemmatizerAnnotator

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

    • annotate

      public Document annotate(Document document)
      Lemmatizes the document sentence by sentence and adds the LEMMAS layer.

      For every sentence, the tokens whose spans lie inside the sentence span are lemmatized as one sequence together with their tags, and each lemma is emitted on its token's span. The required layers must be present, but they may be empty: a document without sentences or tokens yields a present-but-empty lemma layer, and a sentence containing no tokens contributes nothing.

      Specified by:
      annotate in interface DocumentAnnotator
      Parameters:
      document - The document to annotate. Must not be null and must carry the Layers.SENTENCES and Layers.TOKENS layers and a Layers.POS_TAGS layer with exactly one tag per token, with every token lying inside a sentence.
      Returns:
      A new Document with the LEMMAS layer added. Never null.
      Throws:
      IllegalArgumentException - Thrown if document is null, the sentence layer, the token layer, or the tag layer is absent, the tag layer does not have exactly one tag per token, a token lies outside every sentence, or the lemmatizer does not return one lemma per token of a sentence.
    • requires

      public Set<LayerKey<?>> requires()
      Specified by:
      requires in interface DocumentAnnotator
      Returns:
      The keys of the layers this annotator reads. Never null.
    • 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