Class ParserAnnotator

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

public final class ParserAnnotator extends Object implements opennlp.tools.document.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
  • Nested Class Summary

    Nested Classes
    Modifier and Type
    Class
    Description
    static final record 
    One phrase of a constituency parse: its label, such as NP or VP, and the span of the token that heads it.
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    static final opennlp.tools.document.LayerKey<ParserAnnotator.Phrase>
    Parse phrases; each annotation covers one phrase and carries its ParserAnnotator.Phrase, in pre-order of the parse tree.
  • Constructor Summary

    Constructors
    Constructor
    Description
    ParserAnnotator(opennlp.tools.parser.Parser parser)
    Initializes the adapter.
  • Method Summary

    Modifier and Type
    Method
    Description
    opennlp.tools.document.Document
    annotate(opennlp.tools.document.Document document)
    Parses the document sentence by sentence and adds the PHRASES layer.
    Set<opennlp.tools.document.LayerKey<?>>
    Set<opennlp.tools.document.LayerKey<?>>
    Returns the adapter's simple class name, which names it in pipeline validation messages.

    Methods inherited from class Object

    equals, getClass, hashCode, notify, notifyAll, wait, wait, wait
  • Field Details

  • Constructor Details

    • ParserAnnotator

      public ParserAnnotator(opennlp.tools.parser.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 opennlp.tools.document.Document annotate(opennlp.tools.document.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 opennlp.tools.document.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.
    • requires

      public Set<opennlp.tools.document.LayerKey<?>> requires()
      Specified by:
      requires in interface opennlp.tools.document.DocumentAnnotator
    • provides

      public Set<opennlp.tools.document.LayerKey<?>> provides()
      Specified by:
      provides in interface opennlp.tools.document.DocumentAnnotator
    • 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