Package opennlp.tools.parser
Class ParserAnnotator
java.lang.Object
opennlp.tools.parser.ParserAnnotator
- All Implemented Interfaces:
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 ClassesModifier and TypeClassDescriptionstatic final recordOne phrase of a constituency parse: its label, such asNPorVP, and the span of the token that heads it. -
Field Summary
FieldsModifier and TypeFieldDescriptionprivate final Parserstatic final LayerKey<ParserAnnotator.Phrase> Parse phrases; each annotation covers one phrase and carries itsParserAnnotator.Phrase, in pre-order of the parse tree. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionParses the document sentence by sentence and adds thePHRASESlayer.private voidcollect(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.provides()requires()private inttokenIndex(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.toString()Returns the adapter's simple class name, which names it in pipeline validation messages.
-
Field Details
-
PHRASES
Parse phrases; each annotation covers one phrase and carries itsParserAnnotator.Phrase, in pre-order of the parse tree. -
parser
-
-
Constructor Details
-
ParserAnnotator
Initializes the adapter.- Parameters:
parser- The parser to delegate to. Must not benull.- Throws:
IllegalArgumentException- Thrown ifparserisnull.
-
-
Method Details
-
annotate
Parses the document sentence by sentence and adds thePHRASESlayer.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:
annotatein interfaceDocumentAnnotator- Parameters:
document- The document to annotate. Must not benulland must carry theLayers.SENTENCESandLayers.TOKENSlayers, with every token lying inside a sentence.- Returns:
- A new
Documentwith thePHRASESlayer added. Nevernull. - Throws:
IllegalArgumentException- Thrown ifdocumentisnull, 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
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
- Specified by:
requiresin interfaceDocumentAnnotator- Returns:
- The keys of the layers this annotator reads. Never
null.
-
provides
- Specified by:
providesin interfaceDocumentAnnotator- Returns:
- The keys of the layers this annotator adds. Never
null.
-
toString
Returns the adapter's simple class name, which names it in pipeline validation messages.
-