Skip to main content

STAM is a library for dealing with standoff annotations on text, this is the python binding.

Project description

stam logo

Docs PyPI PyPI GitHub build GitHub release Project Status: Active – The project has reached a stable, usable state and is being actively developed. Technology Readiness Level 7/9 - Release Candidate - Technology ready enough and in initial use by end-users in intended scholarly environments. Further validation in progress.

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.

What can you do with this library?

  • Keep, build and manipulate an efficient in-memory store of texts and annotations on texts
  • Search in annotations, data and text:
    • Search annotations by data, textual content, relations between text fragments (overlap, embedding, adjacency, etc),
    • Search in text (incl. via regular expressions) and find annotations targeting found text selections.
    • Search in data (set,key,value) and find annotations that use the data.
    • Elementary text operations with regard for text offsets (splitting text on a delimiter, stripping text).
    • Convert between different kind of offsets (absolute, relative to other structures, UTF-8 bytes vs unicode codepoints, etc)
  • Read and write resources and annotations from/to STAM JSON, STAM CSV, or an optimised binary (CBOR) representation
    • The underlying STAM model aims to be clear and simple. It is flexible and does not commit to any vocabulary or annotation paradigm other than stand-off annotation.

This STAM library is intended as a foundation upon which further applications can be built that deal with stand-off annotations on text. We implement all the low-level logic in dealing this so you no longer have to and can focus on your actual application.

Installation

$ pip install stam

Or if you feel adventurous and have the necessary build-time dependencies installed (Rust), you can try the latest development release from Github:

$ pip install git+https://github.com/annotation/stam-python

Documentation

Usage

Import the library

import stam

Loading a STAM JSON (or CSV) 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).

You can optionally pass configuration parameters upon loading a store, as follows:

store = stam.AnnotationStore(file="example.stam.json", config={"debug": True})

Once loaded, you can retrieve anything by its public ID:

annotation = store.annotation("my-annotation")
resource = store.resource("my-resource")
dataset = store.dataset("my-annotationset")
key = dataset.key("my-key")
data = dataset.annotationdata("my-data")

You can also iterate through all annotations in the store, and output 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={ "id": "D1", "key": "pos", "value": "noun", "set": "testdataset"})

In the above example, the AnnotationDataSet , DataKey and AnnotationData are created on-the-fly. You can also create them explicitly within the set first, 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")
dataset = store.add_dataset(id="testdataset")
dataset.add_key("pos")
data = dataset.add_data("pos","noun","D1")
store.annotate(id="A1", 
    target=Selector.textselector(resource, Offset.simple(6,11)),
    data=data)

Providing the full data dictionary as in the earlier example would have also worked fine, with the same end result, but would be less performant than passing an AnnotationData instance directly. The implementation will always 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.set_filename("example.stam.json")
store.save()

For more documentation, please read: STAM Tutorial: Standoff Text Annotation for Pythonistas.

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.

The Rust methods will often 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 we often decide 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


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

stam-0.8.4.tar.gz (80.6 kB view hashes)

Uploaded Source

Built Distributions

stam-0.8.4-cp312-none-win_amd64.whl (2.1 MB view hashes)

Uploaded CPython 3.12 Windows x86-64

stam-0.8.4-cp312-none-win32.whl (1.9 MB view hashes)

Uploaded CPython 3.12 Windows x86

stam-0.8.4-cp312-cp312-musllinux_1_2_x86_64.whl (2.8 MB view hashes)

Uploaded CPython 3.12 musllinux: musl 1.2+ x86-64

stam-0.8.4-cp312-cp312-musllinux_1_2_aarch64.whl (2.7 MB view hashes)

Uploaded CPython 3.12 musllinux: musl 1.2+ ARM64

stam-0.8.4-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (2.6 MB view hashes)

Uploaded CPython 3.12 manylinux: glibc 2.17+ x86-64

stam-0.8.4-cp312-cp312-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (2.6 MB view hashes)

Uploaded CPython 3.12 manylinux: glibc 2.17+ ARM64

stam-0.8.4-cp312-cp312-macosx_11_0_arm64.whl (2.2 MB view hashes)

Uploaded CPython 3.12 macOS 11.0+ ARM64

stam-0.8.4-cp312-cp312-macosx_10_12_x86_64.whl (2.4 MB view hashes)

Uploaded CPython 3.12 macOS 10.12+ x86-64

stam-0.8.4-cp311-none-win_amd64.whl (2.1 MB view hashes)

Uploaded CPython 3.11 Windows x86-64

stam-0.8.4-cp311-none-win32.whl (1.9 MB view hashes)

Uploaded CPython 3.11 Windows x86

stam-0.8.4-cp311-cp311-musllinux_1_2_x86_64.whl (2.8 MB view hashes)

Uploaded CPython 3.11 musllinux: musl 1.2+ x86-64

stam-0.8.4-cp311-cp311-musllinux_1_2_aarch64.whl (2.7 MB view hashes)

Uploaded CPython 3.11 musllinux: musl 1.2+ ARM64

stam-0.8.4-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (2.6 MB view hashes)

Uploaded CPython 3.11 manylinux: glibc 2.17+ x86-64

stam-0.8.4-cp311-cp311-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (2.6 MB view hashes)

Uploaded CPython 3.11 manylinux: glibc 2.17+ ARM64

stam-0.8.4-cp311-cp311-macosx_11_0_arm64.whl (2.2 MB view hashes)

Uploaded CPython 3.11 macOS 11.0+ ARM64

stam-0.8.4-cp311-cp311-macosx_10_12_x86_64.whl (2.4 MB view hashes)

Uploaded CPython 3.11 macOS 10.12+ x86-64

stam-0.8.4-cp310-none-win_amd64.whl (2.1 MB view hashes)

Uploaded CPython 3.10 Windows x86-64

stam-0.8.4-cp310-none-win32.whl (1.9 MB view hashes)

Uploaded CPython 3.10 Windows x86

stam-0.8.4-cp310-cp310-musllinux_1_2_x86_64.whl (2.8 MB view hashes)

Uploaded CPython 3.10 musllinux: musl 1.2+ x86-64

stam-0.8.4-cp310-cp310-musllinux_1_2_aarch64.whl (2.7 MB view hashes)

Uploaded CPython 3.10 musllinux: musl 1.2+ ARM64

stam-0.8.4-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (2.6 MB view hashes)

Uploaded CPython 3.10 manylinux: glibc 2.17+ x86-64

stam-0.8.4-cp310-cp310-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (2.6 MB view hashes)

Uploaded CPython 3.10 manylinux: glibc 2.17+ ARM64

stam-0.8.4-cp310-cp310-macosx_11_0_arm64.whl (2.2 MB view hashes)

Uploaded CPython 3.10 macOS 11.0+ ARM64

stam-0.8.4-cp310-cp310-macosx_10_12_x86_64.whl (2.4 MB view hashes)

Uploaded CPython 3.10 macOS 10.12+ x86-64

stam-0.8.4-cp39-none-win_amd64.whl (2.1 MB view hashes)

Uploaded CPython 3.9 Windows x86-64

stam-0.8.4-cp39-none-win32.whl (1.9 MB view hashes)

Uploaded CPython 3.9 Windows x86

stam-0.8.4-cp39-cp39-musllinux_1_2_x86_64.whl (2.8 MB view hashes)

Uploaded CPython 3.9 musllinux: musl 1.2+ x86-64

stam-0.8.4-cp39-cp39-musllinux_1_2_aarch64.whl (2.7 MB view hashes)

Uploaded CPython 3.9 musllinux: musl 1.2+ ARM64

stam-0.8.4-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (2.6 MB view hashes)

Uploaded CPython 3.9 manylinux: glibc 2.17+ x86-64

stam-0.8.4-cp39-cp39-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (2.6 MB view hashes)

Uploaded CPython 3.9 manylinux: glibc 2.17+ ARM64

stam-0.8.4-cp39-cp39-macosx_11_0_arm64.whl (2.2 MB view hashes)

Uploaded CPython 3.9 macOS 11.0+ ARM64

stam-0.8.4-cp39-cp39-macosx_10_12_x86_64.whl (2.4 MB view hashes)

Uploaded CPython 3.9 macOS 10.12+ x86-64

stam-0.8.4-cp38-none-win_amd64.whl (2.1 MB view hashes)

Uploaded CPython 3.8 Windows x86-64

stam-0.8.4-cp38-none-win32.whl (1.9 MB view hashes)

Uploaded CPython 3.8 Windows x86

stam-0.8.4-cp38-cp38-musllinux_1_2_x86_64.whl (2.8 MB view hashes)

Uploaded CPython 3.8 musllinux: musl 1.2+ x86-64

stam-0.8.4-cp38-cp38-musllinux_1_2_aarch64.whl (2.7 MB view hashes)

Uploaded CPython 3.8 musllinux: musl 1.2+ ARM64

stam-0.8.4-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (2.6 MB view hashes)

Uploaded CPython 3.8 manylinux: glibc 2.17+ x86-64

stam-0.8.4-cp38-cp38-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (2.6 MB view hashes)

Uploaded CPython 3.8 manylinux: glibc 2.17+ ARM64

stam-0.8.4-cp37-none-win_amd64.whl (2.1 MB view hashes)

Uploaded CPython 3.7 Windows x86-64

stam-0.8.4-cp37-none-win32.whl (1.9 MB view hashes)

Uploaded CPython 3.7 Windows x86

stam-0.8.4-cp37-cp37m-musllinux_1_2_x86_64.whl (2.8 MB view hashes)

Uploaded CPython 3.7m musllinux: musl 1.2+ x86-64

stam-0.8.4-cp37-cp37m-musllinux_1_2_aarch64.whl (2.7 MB view hashes)

Uploaded CPython 3.7m musllinux: musl 1.2+ ARM64

stam-0.8.4-cp37-cp37m-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (2.6 MB view hashes)

Uploaded CPython 3.7m manylinux: glibc 2.17+ x86-64

stam-0.8.4-cp37-cp37m-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (2.6 MB view hashes)

Uploaded CPython 3.7m manylinux: glibc 2.17+ ARM64

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page