Class DocumentAnnotators

java.lang.Object
opennlp.tools.document.DocumentAnnotators

public final class DocumentAnnotators extends Object
Support methods shared by DocumentAnnotator implementations: the required-layer check and the per-sentence walk over the token layer.

These helpers keep the annotators' shared behavior identical across implementations: an absent required layer is always rejected with the same message naming the layer, and every per-sentence adapter applies the same sentence-to-token mapping, including the loud rejection of a token lying outside every sentence.

Since:
3.0.0
  • Method Details

    • requireLayers

      public static void requireLayers(Document document, LayerKey<?>... layers)
      Verifies that a document is present and carries every given layer.
      Parameters:
      document - The document to check.
      layers - The required layers, in the order they are to be reported.
      Throws:
      IllegalArgumentException - Thrown if document is null, or if a layer is absent; the message names the first absent layer.
    • forEachSentence

      public static void forEachSentence(List<Annotation<String>> sentences, List<Annotation<String>> tokens, DocumentAnnotators.SentenceTokenConsumer consumer)
      Walks the token layer sentence by sentence and hands each sentence's contiguous token run to the consumer.

      Both layers must be in text order. Each sentence consumes the contiguous run of tokens whose spans it encloses; a sentence without tokens is skipped. Every token must belong to a sentence: a token lying outside every sentence is rejected loudly after the walk, so it can never be silently dropped.

      Parameters:
      sentences - The sentence layer, in text order. Must not be null.
      tokens - The token layer, in text order. Must not be null.
      consumer - Receives each token-carrying sentence's run. Must not be null.
      Throws:
      IllegalArgumentException - Thrown if a token lies outside every sentence.