Class ChunkerAnnotator

java.lang.Object
opennlp.tools.chunker.ChunkerAnnotator
All Implemented Interfaces:
DocumentAnnotator

public final class ChunkerAnnotator extends Object implements DocumentAnnotator
Adapts a Chunker to the document pipeline: reads Layers.SENTENCES, Layers.TOKENS, and Layers.POS_TAGS and provides CHUNKS, one annotation per phrase chunk carrying the chunk type, for example NP or VP, on the span from its first to its last token.

Each sentence is chunked separately with its tokens and tags as one sequence, the way the chunker contract expects its input. A chunker's spans index tokens within the sentence; the adapter maps them onto the token spans, which already refer to the original text, so a chunk covers exactly the text of its tokens. Chunks are emitted in text order.

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

Since:
3.0.0
  • Field Details

    • CHUNKS

      public static final LayerKey<String> CHUNKS
      Phrase chunks; each annotation covers one chunk and carries its type, ordered by text position.
    • CHUNK_SOURCE

      private static final String CHUNK_SOURCE
      Introduces a rejected chunk in exception messages.
      See Also:
    • chunker

      private final Chunker chunker
  • Constructor Details

    • ChunkerAnnotator

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

    • annotate

      public Document annotate(Document document)
      Chunks the document sentence by sentence and adds the CHUNKS layer.

      The required layers must be present, but they may be empty: a document without sentences or tokens yields a present-but-empty chunk layer. The token and tag layers must be aligned one to one.

      Specified by:
      annotate in interface DocumentAnnotator
      Parameters:
      document - The document to annotate. Must not be null and must carry the Layers.SENTENCES, Layers.TOKENS, and Layers.POS_TAGS layers, with every token lying inside a sentence.
      Returns:
      A new Document with the CHUNKS layer added. Never null.
      Throws:
      IllegalArgumentException - Thrown if document is null, a required layer is absent, the token and tag layers differ in size, a token lies outside every sentence, or the chunker returns a span outside the sentence, an empty span, or a span without a type.
    • 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