STAM is a library for dealing with standoff annotations on text
Project description
STAM Python binding
STAM is a data model for stand-off text annotation and described in detail here. This is a python library (to be more specific; a python binding written in Rust) to work with the model.
This library offers a higher-level interface than the underlying Rust library. Implementation is currently in a preliminary stage. We aim to implement the full model and most extensions.
Installation
$ pip install stam
Usage
Import the library
import stam
Loading a STAM JSON file containing an annotation store:
store = stam.AnnotationStore(file="example.stam.json")
The annotation store is your workspace, it holds all resources, annotation sets (i.e. keys and annotation data) and of course the actual annotations. It is a memory-based store and you can put as much as you like into it (as long as it fits in memory:).
Retrieving anything by ID:
annotation = store.annotation("my-annotation")
resource = store.resource("my-resource")
annotationset = store.annotationset("my-annotationset")
key = annotationset.key("my-key")
data = annotationset.annotationdata("my-data")
Iterating through all annotations in the store, and outputting a simple tab separated format:
for annotation in store.annotations():
# get the text to which this annotation refers (if any)
try:
text = str(annotation)
except stam.StamError:
text = "n/a"
for data in annotation:
print("\t".join(( annotation.id, data.key().id, str(data.value()), text)))
Adding a resource:
resource = store.add_resource(filename="my-text.txt")
Create a store and annotations from scratch:
from stam import AnnotationStore, Selector, AnnotationDataBuilder
store = AnnotationStore(id="test")
resource = store.add_resource(id="testres", text="Hello world")
store.annotate(id="A1",
target=Selector.textselector(resource, Offset.simple(6,11)),
data=[AnnotationDataBuilder(id="D1", key="pos", value="noun", annotationset="testdataset")])
In the above example, the AnnotationDataSet
, DataKey
and AnnotationData
are created on-the-fly. You can also create them explicitly, as shown in the
next snippet, resulting in the exact same store:
store = AnnotationStore(id="test")
resource = store.add_resource(id="testres", text="Hello world")
annotationset = store.add_annotationset(id="testdataset")
annotationset.add_key("pos")
data = annotationset.add_data("pos","noun","D1")
store.annotate(id="A1",
target=Selector.textselector(resource, Offset.simple(6,11)),
data=[AnnotationDataBuilder.link(data)])
Here we use AnnotationDataBuilder.link()
to link to the existing annotation.
Providing the full AnnotationDataBuilder
as in the earlier example would have
also worked fine, with the same end result, but would be less performant. The
implementation will ensure any already existing AnnotationData
will be reused if
possible, as not duplicating data is one of the core characteristics of the
STAM model.
You can serialize the entire annotation store (including all sets and annotations) to a STAM JSON file:
store.to_file("example.stam.json")
Differences between the rust library and python library and performance considerations
Although this Python binding builds on the Rust library, the API it exposes differs in certain aspects to make it more pythonic and easier to work with. This results in a higher-level API that hides some of the lower-level details that are present in the Rust library. This approach does come at the cost of causing some additional runtime overhead.
In this Python binding, most classes of the model (Annotation
,
AnnotationData
, DataKey
, etc..) are references to the annotation store
(self-containing also a reference to the store itself). None of them can be
instantiated directly, but always via an add_*()
or annotate()
method which
will add them and return the reference.
These instances play a bigger role in the Python API than their equivalents in
the Rust API (which distinguishes owned data, borrowed data aka references, and
so-called handles). In the Rust API, methods for search are mostly implemented on the main
AnnotationStore
or AnnotationDataSet
, reflecting the underlying ownership model more strictly.
In the Python API, they are implemented on the types themselves. Here's a comparison of some common methods:
Python API | Rust API |
---|---|
Annotation.annotations() |
AnnotationStore::annotations_by_annotation() |
Annotation.resources() |
AnnotationStore::resources_by_annotation() |
Annotation.textselections() |
AnnotationStore::textselections_by_annotation() |
Annotation.text() |
AnnotationStore::text_by_annotation() |
TextResource.annotations() |
AnnotationStore::annotations_by_resource() |
TextSelection.annotations() |
AnnotationStore::annotations_by_textselection() |
DataKey.data() |
AnnotationDataSet::data_by_key() |
DataKey.annotationset() |
n/a |
AnnotationData.annotationset() |
n/a |
The Rust methods will return iterators, references or handles whenever they can, moreover it will do so safely. The Python API is often forced to make a local copy. For iterators it sometimes decides to let the entire underlying Rust iterator run its course and then return the result as a whole as a tuple, rather than return a Python generator. Here you gain some speed at the cost of some memory.
Probably needless to say, but using Rust directly will always be more performant than using this Python binding. However, using this Python binding should still be way more performant than if the whole thing were implemented in native Python. The trick is in letting the binding work for you as much as possible, use higher-level methods whenever they are available rather than implementing your logic in Python.
Acknowledgements
This work is conducted at the KNAW Humanities Cluster's Digital Infrastructure department, and funded by the CLARIAH project (CLARIAH-PLUS, NWO grant 184.034.023) as part of the FAIR Annotations track.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distributions
Built Distributions
Hashes for stam-0.0.4-cp311-cp311-musllinux_1_1_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | db3fe6294365a7d92512ea2d3cbccd2018e630ce3e6c12af75481c6ff738b2b0 |
|
MD5 | 308f218604cc882f2c91430520b49d9f |
|
BLAKE2b-256 | d59570ac3576ef558deb4109576798dcf2d251e22522665ef06636885243f678 |
Hashes for stam-0.0.4-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 9c7ac46c4abaef75069cbae82e602a1180dfdf5131fc373f7538253b5b82562f |
|
MD5 | e7558d5fedbe2172edfc3f8a2a6cec47 |
|
BLAKE2b-256 | 699bb8e519c6986af6d9467d6fb04431ab9d66aa4e57966524b775336f230905 |
Hashes for stam-0.0.4-cp310-cp310-musllinux_1_1_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 86bdcfe423ade9f58431714ef9c2b2b8c22002131a08040de9631ee4f8cc54c6 |
|
MD5 | 4b13593dc0a37964fb97d2b323e0f53c |
|
BLAKE2b-256 | d1871f837f012052e095d11d8234bbbeb5073bd4f704350cae692564931f265a |
Hashes for stam-0.0.4-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | aa05db4137f8a2c18370b45f511ac22db2f6bd41b96998daddc8b3fe1d674bee |
|
MD5 | e7ed48c1eed53d6c6c02542f41675c09 |
|
BLAKE2b-256 | 7f12db8c16b8ce4bfbd200cc82ba7336d282a525deb62f811994758f7e361515 |
Hashes for stam-0.0.4-cp39-cp39-musllinux_1_1_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 301d2672a20d9ec04711743cfce34342222bb95cd8f6117a3acecb8587e06033 |
|
MD5 | c80f40de1718e938e958ca1d4c0d3a0a |
|
BLAKE2b-256 | a29fa5ff8b4b23089b345904b1b8a5992a03cd08995856a37e116dbb1a2b6342 |
Hashes for stam-0.0.4-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | df7d1992e03a3804b78819c9029630ac150d04b6bca5c23b549cbfaa50ec816b |
|
MD5 | e858613fb7140dcedcd70a0020c64ff5 |
|
BLAKE2b-256 | b11acb29f13cefde64674f2e1618929f7971be8dbef3e8ff1fce8376e6a129ae |
Hashes for stam-0.0.4-cp38-cp38-musllinux_1_1_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | ca9f722ae3d1b268da0398e7870a2335820fc323ef431ed9ad24a611c18332ee |
|
MD5 | 4c4aa3fc8660f896857f7771e5ef68d9 |
|
BLAKE2b-256 | 87b5d40e852c56dc0cb3ec108d2c4c80b4ef94c637529cc7cc6be7610e0b6461 |
Hashes for stam-0.0.4-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 9add30613877ec36a7ce7237a9c7a619c2529b9c31cf029110049548c5f73591 |
|
MD5 | 79fb927e60702d7033c62157164e91c7 |
|
BLAKE2b-256 | 60356def4fba6cc201b0c55089df5c2b73650105de72c9067855129c12d45a47 |
Hashes for stam-0.0.4-cp37-cp37m-musllinux_1_1_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 7215f9f29969b276d6850d6aeb2b01654003d9df74fc26e28ff0820e20fdfec0 |
|
MD5 | b420453b3eab2d1f701f1140f511daab |
|
BLAKE2b-256 | cd543db6056de54feccbef57186b57961b17541d7e72089dcc58873dc0a6e794 |
Hashes for stam-0.0.4-cp37-cp37m-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 2d9a540e2634bbf199a3099e07a63f1b386d04684755ebc4c33d0f73ea0fdc8b |
|
MD5 | ab9200d48d736bd4e658f0867a556597 |
|
BLAKE2b-256 | 02b3e4f371a6a07419938f90939e6f953278f86a8d1e41fcad2a1332efd92c70 |