Package opennlp.tools.document
Class ImmutableDocument
java.lang.Object
opennlp.tools.document.ImmutableDocument
- All Implemented Interfaces:
Document
The default
Document implementation: an unmodifiable map from layer key to an
unmodifiable annotation list.
Instances are immutable: with(LayerKey, List) returns a new document that
shares the unchanged layers with its ancestor, copying the map but not the layers, so
documents grown from a common ancestor share their layer lists. The text is captured
as a String at construction, so a mutable CharSequence handed to
empty(CharSequence) cannot change the document afterwards. That immutability
makes instances safe to share between threads.
-
Nested Class Summary
Nested classes/interfaces inherited from interface opennlp.tools.document.Document
Document.DuplicateLayerPolicy -
Field Summary
Fields -
Constructor Summary
ConstructorsModifierConstructorDescriptionprivateImmutableDocument(String text, Map<LayerKey<?>, List<Annotation<?>>> layers) -
Method Summary
Modifier and TypeMethodDescriptionprivate <T> List<Annotation<?>> copyValidated(LayerKey<T> layer, Document from) Returns a validated immutable copy of one offrom's layers, capturing the key's value type.(package private) static ImmutableDocumentempty(CharSequence text) Creates a document without any layers.<T> List<Annotation<T>> Retrieves the annotations of one layer.layers()merge(Document other, Document.DuplicateLayerPolicy duplicateLayers) Returns a document combining this document's layers with another document's layers over the same text, resolving duplicate layer keys withduplicateLayers.text()private <T> voidvalidate(LayerKey<T> layer, List<Annotation<T>> annotations) Checks one layer's annotations against the key's contract: no null elements, values assignable to the key's type, spans present and within the text bounds under a positional key, absent under a document-scoped key.<T> Documentwith(LayerKey<T> layer, List<Annotation<T>> annotations) Returns a new document with one layer added.
-
Field Details
-
text
-
layers
-
-
Constructor Details
-
ImmutableDocument
-
-
Method Details
-
empty
Creates a document without any layers.- Parameters:
text- The original document text, captured as its content at this moment. Must not benull.- Returns:
- An empty
ImmutableDocument. Nevernull. - Throws:
IllegalArgumentException- Thrown iftextisnull.
-
text
-
get
Retrieves the annotations of one layer. -
layers
-
with
Returns a new document with one layer added.- Specified by:
within interfaceDocument- Type Parameters:
T- The type of the layer's annotation values.- Parameters:
layer- The key of the layer to add. Must not benulland must not already be present.annotations- The annotations of the layer. Must not benull, must not containnull, and every value must be assignable to the layer's type. Under a positional key every annotation must carry a span within the text bounds; under a document-scoped key no annotation may carry a span.- Returns:
- A new
Documentsharing this document's text and existing layers. Nevernull.
-
merge
Returns a document combining this document's layers with another document's layers over the same text, resolving duplicate layer keys withduplicateLayers. This implementation copies the layer map once, not once per added layer.- Specified by:
mergein interfaceDocument- Parameters:
other- The document whose layers are added on top of this document's layers. Must not benulland must carry the same text content.duplicateLayers- How to treat a layer key present on both documents. Must not benull.- Returns:
- A
Documentcarrying the layers of both documents. This document itself may be returned whenotheradds no layer. Nevernull; both source documents are left untouched.
-
copyValidated
Returns a validated immutable copy of one offrom's layers, capturing the key's value type.- Returns:
- a validated immutable copy of one of
from's layers, capturing the key's value type
-
validate
Checks one layer's annotations against the key's contract: no null elements, values assignable to the key's type, spans present and within the text bounds under a positional key, absent under a document-scoped key.- Throws:
IllegalArgumentException- Thrown if any check fails; the message names the layer.
-