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 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 TypeFieldDescriptionstatic final opennlp.tools.document.LayerKey<ParserAnnotator.Phrase> Parse phrases; each annotation covers one phrase and carries itsParserAnnotator.Phrase, in pre-order of the parse tree. -
Constructor Summary
ConstructorsConstructorDescriptionParserAnnotator(opennlp.tools.parser.Parser parser) Initializes the adapter. -
Method Summary
Modifier and TypeMethodDescriptionopennlp.tools.document.Documentannotate(opennlp.tools.document.Document document) Parses the document sentence by sentence and adds thePHRASESlayer.Set<opennlp.tools.document.LayerKey<?>> provides()Set<opennlp.tools.document.LayerKey<?>> requires()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.
-
-
Constructor Details
-
ParserAnnotator
public ParserAnnotator(opennlp.tools.parser.Parser parser) Initializes the adapter.- Parameters:
parser- The parser to delegate to. Must not benull.- Throws:
IllegalArgumentException- Thrown ifparserisnull.
-
-
Method Details
-
annotate
public opennlp.tools.document.Document annotate(opennlp.tools.document.Document document) 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 interfaceopennlp.tools.document.DocumentAnnotator- 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.
-
requires
- Specified by:
requiresin interfaceopennlp.tools.document.DocumentAnnotator
-
provides
- Specified by:
providesin interfaceopennlp.tools.document.DocumentAnnotator
-
toString
-