Interface Document
A layer is a list of annotations identified by a
LayerKey. The container itself knows nothing about specific layers; every
analysis capability contributes its results as one more layer without any change to
this interface, which is what keeps new capabilities additive. All spans refer to
text() as supplied, never to a derived form. A
document-scoped layer carries whole-document values
without spans, for example a language id.
A document is never modified in place: with(LayerKey, List) leaves its
receiver untouched and returns a new document. Thread safety is implementation
specific.
Three invariants make index-based references sound. A layer preserves its insertion order, and the container never sorts or reorders it. A layer is immutable once added: the returned lists reject modification and are detached from the caller's input list. Providing a layer that already exists is rejected loudly: the add is once-only, and the exception names the offending key. An annotation that references another annotation by its index within a layer, for example a dependency arc naming its head token, therefore stays valid for the lifetime of the document.
- Since:
- 3.0.0
-
Nested Class Summary
Nested ClassesModifier and TypeInterfaceDescriptionstatic enumHowmerge(Document, DuplicateLayerPolicy)treats a layer key that is present on both documents. -
Method Summary
Modifier and TypeMethodDescription<T> List<Annotation<T>> Retrieves the annotations of one layer.layers()default DocumentReturns a new document combining this document's layers with another document's layers over the same text, joining documents grown independently, for example by pipelines that ran in parallel.default Documentmerge(Document other, Document.DuplicateLayerPolicy duplicateLayers) Returns a new document combining this document's layers with another document's layers over the same text, resolving duplicate layer keys withduplicateLayers.static Documentof(CharSequence text) Creates an emptyDocumentover a text.text()<T> Documentwith(LayerKey<T> layer, List<Annotation<T>> annotations) Returns a new document with one layer added.
-
Method Details
-
of
Creates an emptyDocumentover a text. The returned document is immutable and safe to share between threads: it captures the text's content at construction, so later changes to a mutableCharSequencedo not reach the document.- Parameters:
text- The original document text. Must not benull.- Returns:
- A
Documentwithout any layers. Nevernull. - Throws:
IllegalArgumentException- Thrown iftextisnull.
-
text
CharSequence text()- Returns:
- The original text of the document. Never
null.
-
get
Retrieves the annotations of one layer.- Type Parameters:
T- The type of the layer's annotation values.- Parameters:
layer- The layer to read. Must not benull.- Returns:
- The layer's annotations in their layer order, or an empty list when the
layer is absent. Never
null; the list is unmodifiable. - Throws:
IllegalArgumentException- Thrown iflayerisnull.
-
layers
-
with
Returns a new document with one layer added.- 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. - Throws:
IllegalArgumentException- Thrown if any of the above constraints is violated.
-
merge
Returns a new document combining this document's layers with another document's layers over the same text, joining documents grown independently, for example by pipelines that ran in parallel.- Parameters:
other- The document whose layers are added on top of this document's layers. Must not benull, must carry the same text content, and must not provide a layer this document already has.- Returns:
- A new
Documentcarrying the layers of both documents. Nevernull; both source documents are left untouched. - Throws:
IllegalArgumentException- Thrown ifotherisnull, if its text content differs, or if a layer key is present on both documents; the exception names the offending key.
-
merge
Returns a new document combining this document's layers with another document's layers over the same text, resolving duplicate layer keys withduplicateLayers.- 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 new
Documentcarrying the layers of both documents. Nevernull; both source documents are left untouched. - Throws:
IllegalArgumentException- Thrown if either argument isnull, if the text content differs, or if a layer key is present on both documents and the policy does not keep it; the exception names the offending key.
-