Class ParserAnnotator

java.lang.Object
opennlp.tools.parser.ParserAnnotator
All Implemented Interfaces:
DocumentAnnotator

public final class ParserAnnotator extends Object implements DocumentAnnotator
Adapts a constituency Parser to the document pipeline: reads Layers.SENTENCES and Layers.TOKENS and provides PHRASES, one annotation per phrase node of each sentence's parse, carrying the phrase label and the span of its head token.

Each sentence is parsed from its tokens as one sequence. Every node above the part-of-speech level except the root becomes an annotation on the span from its first to its last token, in pre-order, so an enclosing phrase precedes the phrases it contains and phrases nest by span containment. Part-of-speech nodes are left to the Layers.POS_TAGS layer and token nodes to Layers.TOKENS. The head token is the one the parser's head rules select, so a consumer can read the head of a noun phrase without its own rules.

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

Since:
3.0.0
  • Field Details

  • Constructor Details

    • ParserAnnotator

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

    • annotate

      public Document annotate(Document document)
      Parses the document sentence by sentence and adds the PHRASES layer.

      The required layers must be present, but they may be empty: a document without sentences or tokens yields a present-but-empty phrase layer.

      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, with every token lying inside a sentence.
      Returns:
      A new Document with the PHRASES layer added. Never null.
      Throws:
      IllegalArgumentException - Thrown if document is null, a required layer is absent, a token lies outside every sentence, or the parser returns a node outside the sentence's tokens.
    • collect

      private void collect(Parse node, int first, int[] starts, int length, List<Annotation<String>> tokens, List<Annotation<ParserAnnotator.Phrase>> phrases)
      Emits a node and, in pre-order, every phrase node below it.
    • tokenIndex

      private int tokenIndex(int[] starts, int length, int offset, Parse node)
      Maps an offset in the parse text, the tokens joined by single spaces, to the index of the token it lies in or, for a span end, ends.
    • 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