dkpro-cassis
============
.. image:: https://github.com/dkpro/dkpro-cassis/actions/workflows/run_tests.yml/badge.svg
:target: https://github.com/dkpro/dkpro-cassis/actions/workflows/run_tests.yml
.. image:: https://readthedocs.org/projects/cassis/badge/?version=latest
:target: https://cassis.readthedocs.io/en/latest/?badge=latest
:alt: Documentation Status
.. image:: https://codecov.io/gh/dkpro/dkpro-cassis/branch/master/graph/badge.svg
:target: https://codecov.io/gh/dkpro/dkpro-cassis
.. image:: https://img.shields.io/pypi/l/dkpro-cassis.svg
:alt: PyPI - License
:target: https://pypi.org/project/dkpro-cassis/
.. image:: https://img.shields.io/pypi/pyversions/dkpro-cassis.svg
:alt: PyPI - Python Version
:target: https://pypi.org/project/dkpro-cassis/
.. image:: https://img.shields.io/pypi/v/dkpro-cassis.svg
:alt: PyPI
:target: https://pypi.org/project/dkpro-cassis/
.. image:: https://img.shields.io/badge/code%20style-black-000000.svg
:target: https://github.com/ambv/black
DKPro **cassis** (pronunciation: [ka.sis]) provides a pure-Python implementation of the *Common Analysis System* (CAS)
as defined by the `UIMA <https://uima.apache.org>`_ framework. The CAS is a data structure representing an object to
be enriched with annotations (the co-called *Subject of Analysis*, short *SofA*).
This library enables the creation and manipulation of annotated documents (CAS objects) and their associated type systems as well as loading
and saving them in the `CAS XMI XML representation <https://uima.apache.org/d/uimaj-current/references.html#ugr.ref.xmi>`_
or the `CAS JSON representation <https://github.com/apache/uima-uimaj-io-jsoncas#readme>`_ in Python programs. This can ease in particular the integration of Python-based Natural Language Processing (e.g.
`spacy <https://spacy.io>`_ or `NLTK <https://www.nltk.org>`_) and Machine Learning librarys (e.g.
`scikit-learn <https://scikit-learn.org/stable/>`_ or `Keras <https://keras.io>`_) in UIMA-based text analysis workflows.
An example of cassis in action is the `spacy recommender for INCEpTION <https://github.com/inception-project/external-recommender-spacy>`_,
which wraps the spacy NLP library as a web service which can be used in conjunction with the `INCEpTION <https://inception-project.github.io>`_
text annotation platform to automatically generate annotation suggestions.
Features
--------
Currently supported features are:
- Text SofAs
- Deserializing/serializing UIMA CAS from/to XMI
- Deserializing/serializing UIMA CAS from/to JSON
- Deserializing/serializing type systems from/to XML
- Selecting annotations, selecting covered annotations, adding annotations
- Type inheritance
- Multiple SofA support
- Type system can be changed after loading
- Primitive and reference features and arrays of primitives and references
Some features are still under development, e.g.
- Proper type checking
- XML/XMI schema validation
Installation
------------
To install the package with :code:`pip`, just run
pip install dkpro-cassis
Usage
-----
Example CAS XMI and types system files can be found under :code:`tests\test_files`.
Reading a CAS file
~~~~~~~~~~~~~~~~~~
**From XMI:** A CAS can be deserialized from the UIMA CAS XMI (XML 1.0) format either
by reading from a file or string using :code:`load_cas_from_xmi`.
.. code:: python
from cassis import *
with open('typesystem.xml', 'rb') as f:
typesystem = load_typesystem(f)
with open('cas.xmi', 'rb') as f:
cas = load_cas_from_xmi(f, typesystem=typesystem)
**From JSON:** The UIMA JSON CAS format is also supported and can be loaded using :code:`load_cas_from_json`.
Most UIMA JSON CAS files come with an embedded typesystem, so it is not necessary to specify one.
.. code:: python
from cassis import *
with open('cas.json', 'rb') as f:
cas = load_cas_from_json(f)
Writing a CAS file
~~~~~~~~~~~~~~~~~~
**To XMI:** A CAS can be serialized to XMI either by writing to a file or be
returned as a string using :code:`cas.to_xmi()`.
.. code:: python
from cassis import *
# Returned as a string
xmi = cas.to_xmi()
# Written to file
cas.to_xmi("my_cas.xmi")
**To JSON:** A CAS can also be written to JSON using :code:`cas.to_json()`.
.. code:: python
from cassis import *
# Returned as a string
xmi = cas.to_json()
# Written to file
cas.to_json("my_cas.json")
Creating a CAS
~~~~~~~~~~~~~~
A CAS (Common Analysis System) object typically represents a (text) document. When using cassis,
you will likely most often reading existing CAS files, modify them and then
writing them out again. But you can also create CAS objects from scratch,
e.g. if you want to convert some data into a CAS object in order to create a pre-annotated text.
If you do not have a pre-defined typesystem to work with, you will have to define one.
.. code:: python
typesystem = TypeSystem()
cas = Cas(
sofa_string = "Joe waited for the train . The train was late .",
document_language = "en",
typesystem = typesystem)
print(cas.sofa_string)
print(cas.sofa_mime)
print(cas.document_language)
Adding annotations
~~~~~~~~~~~~~~~~~~
**Note:** type names used below are examples only. The actual CAS files you will be
dealing with will use other names! You can get a list of the types using
:code:`cas.typesystem.get_types()`.
Given a type system with a type :code:`cassis.Token` that has an :code:`id` and
:code:`pos` feature, annotations can be added in the following:
.. code:: python
from cassis import *
with open('typesystem.xml', 'rb') as f:
typesystem = load_typesystem(f)
with open('cas.xmi', 'rb') as f:
cas = load_cas_from_xmi(f, typesystem=typesystem)
Token = typesystem.get_type('cassis.Token')
tokens = [
Token(begin=0, end=3, id='0', pos='NNP'),
Token(begin=4, end=10, id='1', pos='VBD'),
Token(begin=11, end=14, id='2', pos='IN'),
Token(begin=15, end=18, id='3', pos='DT'),
Token(begin=19, end=24, id='4', pos='NN'),
Token(begin=25, end=26, id='5', pos='.'),
]
for token in tokens:
cas.add(token)
Selecting annotations
~~~~~~~~~~~~~~~~~~~~~
.. code:: python
from cassis import *
with open('typesystem.xml', 'rb') as f:
typesystem = load_typesystem(f)
with open('cas.xmi', 'rb') as f:
cas = load_cas_from_xmi(f, typesystem=typesystem)
for sentence in cas.select('cassis.Sentence'):
for token in cas.select_covered('cassis.Token', sentence):
print(token.get_covered_text())
# Annotation values can be accessed as properties
print('Token: begin={0}, end={1}, id={2}, pos={3}'.format(token.begin, token.end, token.id, token.pos))
Getting and setting (nested) features
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
If you want to access a variable but only have its name as a string or have nested feature structures,
e.g. a feature structure with feature :code:`a` that has a
feature :code:`b` that has a feature :code:`c`, some of which can be :code:`None`, then you can use the
following:
.. code:: python
fs.get("var_name") # Or
fs["var_name"]
Or in the nested case,
.. code:: python
fs.get("a.b.c")
fs["a.b.c"]
If :code:`a` or :code:`b` or :code:`c` are :code:`None`, then this returns instead of
throwing an error.
Another example would be a StringList containing :code:`["Foo", "Bar", "Baz"]`:
.. code:: python
assert lst.get("head") == "foo"
assert lst.get("tail.head") == "bar"
assert lst.get("tail.tail.head") == "baz"
assert lst.get("tail.tail.tail.head") == None
assert lst.get("tail.tail.tail.tail.head") == None
The same goes for setting:
.. code:: python
# Functional
lst.set("head", "new_foo")
lst.set("tail.head", "new_bar")
lst.set("tail.tail.head", "new_baz")
assert lst.get("head") == "new_foo"
assert lst.get("tail.head") == "new_bar"
assert lst.get("tail.tail.head") == "new_baz"
# Bracket access
lst["head"] = "newer_foo"
lst["tail.head"] = "newer_bar"
lst["tail.tail.head"] = "newer_baz"
assert lst["head"] == "newer_foo"
assert lst["tail.head"] == "newer_bar"
assert lst["tail.tail.head"] == "newer_baz"
Creating types and adding features
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
.. code:: python
from cassis import *
typesystem = TypeSystem()
parent_type = typesystem.create_type(name='example.ParentType')
typesystem.create_feature(domainType=parent_type, name='parentFeature', rangeType=TYPE_NAME_STRING)
child_type = typesystem.create_type(name='example.ChildType', supertypeName=parent_type.name)
typesystem.create_feature(domainType=child_type, name='childFeature', rangeType=TYPE_NAME_INTEGER)
annotation = child_type(parentFeature='parent', childFeature='child')
When adding new features, these changes are propagated. For example,
adding a feature to a parent type makes it available to a child type.
Therefore, the type system does not need to be frozen for consistency.
The type system can be changed even after loading, it is not frozen
like in UIMAj.
Sofa support
~~~~~~~~~~~~
A Sofa represents some form of an unstructured artifact that is processed in a UIMA pipeline. It contains for instance
the document text. Currently, new Sofas can be created. This is automatically done when creating a new view. Basic
properties of the Sofa can be read and written:
.. code:: python
cas = Cas(
sofa_string = "Joe waited for the train . The train was late .",
document_language = "en")
print(cas.sofa_string)
print(cas.sofa_mime)
print(cas.document_language)
Array support
~~~~~~~~~~~~~
Array feature values are not simply Python arrays, but they are wrapped in a feature structure of
a UIMA array type such as :code:`uima.cas.FSArray`.
.. code:: python
from cassis import *
from cassis.typesystem import TYPE_NAME_FS_ARRAY, TYPE_NAME_ANNOTATION
typesystem = TypeSystem()
ArrayHolder = typesystem.create_type(name='example.ArrayHolder')
typesystem.create_feature(domainType=ArrayHolder, name='array', rangeType=TYPE_NAME_FS_ARRAY)
cas = Cas(typesystem=typesystem)
Annotation = cas.typesystem.get_type(TYPE_NAME_ANNOTATION)
FSArray = cas.typesystem.get_type(TYPE_NAME_FS_ARRAY)
ann = Annotation(begin=0, end=1)
cas.add(ann1)
holder = ArrayHolder(array=FSArray(elements=[ann, ann, ann]))
cas.add(holder)
Managing views
~~~~~~~~~~~~~~
A view into a CAS contains a subset of feature structures and annotations. One view corresponds to exactly one Sofa. It
can also be used to query and alter information about the Sofa, e.g. the document text. Annotations added to one view
are not visible in another view. A view Views can be created and changed. A view has the same methods and attributes
as a :code:`Cas` .
.. code:: python
from cassis import *
with open('typesystem.xml', 'rb') as f:
typesystem = load_typesystem(f)
Token = typesystem.get_type('cassis.Token')
# This creates automatically the view `_InitialView`
cas = Cas()
cas.sofa_string = "I like cheese ."
cas.add_all([
Token(begin=0, end=1),
Token(begin=2, end=6),
Token(begin=7, end=13),
Token(begin=14, end=15)
])
print([x.get_covered_text() for x in cas.select_all()])
# Create a new view and work on it.
view = cas.create_view('testView')
view.sofa_string = "I like blackcurrant ."
view.add_all([
Token(begin=0, end=1),
Token(begin=2, end=6),
Token(begin=7, end=19),
Token(begin=20, end=21)
])
print([x.get_covered_text() for x in view.select_all()])
Merging type systems
~~~~~~~~~~~~~~~~~~~~
Sometimes, it is desirable to merge two type systems. With **cassis**, this can be
achieved via the :code:`merge_typesystems` function. The detailed rules of merging can be found
`here <https://uima.apache.org/d/uimaj-2.10.4/references.html#ugr.ref.cas.typemerging>`_.
.. code:: python
from cassis import *
with open('typesystem.xml', 'rb') as f:
typesystem = load_typesystem(f)
ts = merge_typesystems([typesystem, load_dkpro_core_typesystem()])
Type checking
~~~~~~~~~~~~~
When adding annotations, no type checking is performed for simplicity reasons.
In order to check types, call the :code:`cas.typecheck()` method. Currently, it only
checks whether elements in `uima.cas.FSArray` are
adhere to the specified :code:`elementType`.
DKPro Core Integration
----------------------
A CAS using the DKPro Core Type System can be created via
.. code:: python
from cassis import *
cas = Cas(typesystem=load_dkpro_core_typesystem())
for t in cas.typesystem.get_types():
print(t)
Miscellaneous
-------------
If feature names clash with Python magic variables
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
If your type system defines a type called :code:`self` or :code:`type`, then it will be made
available as a member variable :code:`self_` or :code:`type_` on the respective type:
.. code:: python
from cassis import *
from cassis.typesystem import *
typesystem = TypeSystem()
ExampleType = typesystem.create_type(name='example.Type')
typesystem.create_feature(domainType=ExampleType, name='self', rangeType=TYPE_NAME_STRING)
typesystem.create_feature(domainType=ExampleType, name='type', rangeType=TYPE_NAME_STRING)
annotation = ExampleType(self_="Test string1", type_="Test string2")
print(annotation.self_)
print(annotation.type_)
Leniency
~~~~~~~~
If the type for a feature structure is not found in the typesystem, it will raise an exception by default.
If you want to ignore these kind of errors, you can pass :code:`lenient=True` to the :code:`Cas` constructor or
to :code:`load_cas_from_xmi`.
Large XMI files
~~~~~~~~~~~~~~~
If you try to parse large XMI files and get an error message like :code:`XMLSyntaxError: internal error: Huge input lookup`,
then you can disable this security check by passing :code:`trusted=True` to your calls to :code:`load_cas_from_xmi`.
Citing & Authors
----------------
If you find this repository helpful, feel free to cite
.. code:: bibtex
@software{klie2020_cassis,
author = {Jan-Christoph Klie and
Richard Eckart de Castilho},
title = {DKPro Cassis - Reading and Writing UIMA CAS Files in Python},
publisher = {Zenodo},
doi = {10.5281/zenodo.3994108},
url = {https://github.com/dkpro/dkpro-cassis}
}
Development
-----------
The required dependencies are managed by **pip**. A virtual environment
containing all needed packages for development and production can be
created and activated by
::
virtualenv venv --python=python3 --no-site-packages
source venv/bin/activate
pip install -e ".[test, dev, doc]"
The tests can be run in the current environment by invoking
::
make test
or in a clean environment via
::
tox
Release
-------
- Make sure all issues for the milestone are completed, otherwise move them to the next
- Checkout the ``main`` branch
- Bump the version in ``cassis/__version__.py`` to a stable one, e.g. ``__version__ = "0.6.0"``, commit and push, wait until the build completed. An example commit message would be ``No issue. Release 0.6.0``
- Create a tag for that version via e.g. ``git tag v0.6.0`` and push the tags via ``git push --tags``. Pushing a tag triggers the release to pypi
- Bump the version in ``cassis/__version__.py`` to the next development version, e.g. ``0.7.0-dev``, commit and push that. An example commit message would be ``No issue. Bump version after release``
- Once the build has completed and pypi accepted the new version, go to the Github release and write the changelog based on the issues in the respective milestone
- Create a new milestone for the next version
Raw data
{
"_id": null,
"home_page": "https://dkpro.github.io",
"name": "dkpro-cassis",
"maintainer": "",
"docs_url": null,
"requires_python": ">=3.7.0",
"maintainer_email": "",
"keywords": "uima dkpro cas xmi",
"author": "The DKPro cassis team",
"author_email": "dkpro-core-user@googlegroups.com",
"download_url": "https://files.pythonhosted.org/packages/be/44/d46f01432495f2c917d67c4cda99d93278370134a280714a19ee80811eb9/dkpro-cassis-0.9.0.tar.gz",
"platform": null,
"description": "\ndkpro-cassis\n============\n\n.. image:: https://github.com/dkpro/dkpro-cassis/actions/workflows/run_tests.yml/badge.svg\n :target: https://github.com/dkpro/dkpro-cassis/actions/workflows/run_tests.yml\n\n.. image:: https://readthedocs.org/projects/cassis/badge/?version=latest\n :target: https://cassis.readthedocs.io/en/latest/?badge=latest\n :alt: Documentation Status\n\n.. image:: https://codecov.io/gh/dkpro/dkpro-cassis/branch/master/graph/badge.svg\n :target: https://codecov.io/gh/dkpro/dkpro-cassis\n\n.. image:: https://img.shields.io/pypi/l/dkpro-cassis.svg\n :alt: PyPI - License\n :target: https://pypi.org/project/dkpro-cassis/\n\n.. image:: https://img.shields.io/pypi/pyversions/dkpro-cassis.svg\n :alt: PyPI - Python Version\n :target: https://pypi.org/project/dkpro-cassis/\n\n.. image:: https://img.shields.io/pypi/v/dkpro-cassis.svg\n :alt: PyPI\n :target: https://pypi.org/project/dkpro-cassis/\n\n.. image:: https://img.shields.io/badge/code%20style-black-000000.svg\n :target: https://github.com/ambv/black\n \nDKPro **cassis** (pronunciation: [ka.sis]) provides a pure-Python implementation of the *Common Analysis System* (CAS)\nas defined by the `UIMA <https://uima.apache.org>`_ framework. The CAS is a data structure representing an object to\nbe enriched with annotations (the co-called *Subject of Analysis*, short *SofA*).\n\nThis library enables the creation and manipulation of annotated documents (CAS objects) and their associated type systems as well as loading\nand saving them in the `CAS XMI XML representation <https://uima.apache.org/d/uimaj-current/references.html#ugr.ref.xmi>`_\nor the `CAS JSON representation <https://github.com/apache/uima-uimaj-io-jsoncas#readme>`_ in Python programs. This can ease in particular the integration of Python-based Natural Language Processing (e.g.\n`spacy <https://spacy.io>`_ or `NLTK <https://www.nltk.org>`_) and Machine Learning librarys (e.g.\n`scikit-learn <https://scikit-learn.org/stable/>`_ or `Keras <https://keras.io>`_) in UIMA-based text analysis workflows.\n\nAn example of cassis in action is the `spacy recommender for INCEpTION <https://github.com/inception-project/external-recommender-spacy>`_,\nwhich wraps the spacy NLP library as a web service which can be used in conjunction with the `INCEpTION <https://inception-project.github.io>`_\ntext annotation platform to automatically generate annotation suggestions.\n\nFeatures\n--------\n\nCurrently supported features are:\n\n- Text SofAs\n- Deserializing/serializing UIMA CAS from/to XMI\n- Deserializing/serializing UIMA CAS from/to JSON\n- Deserializing/serializing type systems from/to XML\n- Selecting annotations, selecting covered annotations, adding annotations\n- Type inheritance\n- Multiple SofA support\n- Type system can be changed after loading\n- Primitive and reference features and arrays of primitives and references\n\nSome features are still under development, e.g.\n\n- Proper type checking\n- XML/XMI schema validation\n\nInstallation\n------------\n\nTo install the package with :code:`pip`, just run\n\n pip install dkpro-cassis\n\nUsage\n-----\n\nExample CAS XMI and types system files can be found under :code:`tests\\test_files`.\n\nReading a CAS file\n~~~~~~~~~~~~~~~~~~\n\n**From XMI:** A CAS can be deserialized from the UIMA CAS XMI (XML 1.0) format either\nby reading from a file or string using :code:`load_cas_from_xmi`.\n\n.. code:: python\n\n from cassis import *\n\n with open('typesystem.xml', 'rb') as f:\n typesystem = load_typesystem(f)\n \n with open('cas.xmi', 'rb') as f:\n cas = load_cas_from_xmi(f, typesystem=typesystem)\n\n**From JSON:** The UIMA JSON CAS format is also supported and can be loaded using :code:`load_cas_from_json`.\nMost UIMA JSON CAS files come with an embedded typesystem, so it is not necessary to specify one.\n\n.. code:: python\n\n from cassis import *\n\n with open('cas.json', 'rb') as f:\n cas = load_cas_from_json(f)\n\nWriting a CAS file\n~~~~~~~~~~~~~~~~~~\n\n**To XMI:** A CAS can be serialized to XMI either by writing to a file or be\nreturned as a string using :code:`cas.to_xmi()`.\n\n.. code:: python\n\n from cassis import *\n\n # Returned as a string\n xmi = cas.to_xmi()\n\n # Written to file\n cas.to_xmi(\"my_cas.xmi\")\n\n**To JSON:** A CAS can also be written to JSON using :code:`cas.to_json()`.\n\n.. code:: python\n\n from cassis import *\n\n # Returned as a string\n xmi = cas.to_json()\n\n # Written to file\n cas.to_json(\"my_cas.json\")\n\nCreating a CAS\n~~~~~~~~~~~~~~\n\nA CAS (Common Analysis System) object typically represents a (text) document. When using cassis,\nyou will likely most often reading existing CAS files, modify them and then\nwriting them out again. But you can also create CAS objects from scratch,\ne.g. if you want to convert some data into a CAS object in order to create a pre-annotated text.\nIf you do not have a pre-defined typesystem to work with, you will have to define one.\n\n.. code:: python\n\n typesystem = TypeSystem()\n\n cas = Cas(\n sofa_string = \"Joe waited for the train . The train was late .\",\n document_language = \"en\",\n typesystem = typesystem)\n\n print(cas.sofa_string)\n print(cas.sofa_mime)\n print(cas.document_language)\n\nAdding annotations\n~~~~~~~~~~~~~~~~~~\n\n**Note:** type names used below are examples only. The actual CAS files you will be\ndealing with will use other names! You can get a list of the types using\n:code:`cas.typesystem.get_types()`.\n\nGiven a type system with a type :code:`cassis.Token` that has an :code:`id` and\n:code:`pos` feature, annotations can be added in the following:\n\n.. code:: python\n\n from cassis import *\n\n with open('typesystem.xml', 'rb') as f:\n typesystem = load_typesystem(f)\n \n with open('cas.xmi', 'rb') as f:\n cas = load_cas_from_xmi(f, typesystem=typesystem)\n \n Token = typesystem.get_type('cassis.Token')\n\n tokens = [\n Token(begin=0, end=3, id='0', pos='NNP'),\n Token(begin=4, end=10, id='1', pos='VBD'),\n Token(begin=11, end=14, id='2', pos='IN'),\n Token(begin=15, end=18, id='3', pos='DT'),\n Token(begin=19, end=24, id='4', pos='NN'),\n Token(begin=25, end=26, id='5', pos='.'),\n ]\n\n for token in tokens:\n cas.add(token)\n\nSelecting annotations\n~~~~~~~~~~~~~~~~~~~~~\n\n.. code:: python\n\n from cassis import *\n\n with open('typesystem.xml', 'rb') as f:\n typesystem = load_typesystem(f)\n \n with open('cas.xmi', 'rb') as f:\n cas = load_cas_from_xmi(f, typesystem=typesystem)\n\n for sentence in cas.select('cassis.Sentence'):\n for token in cas.select_covered('cassis.Token', sentence):\n print(token.get_covered_text())\n \n # Annotation values can be accessed as properties\n print('Token: begin={0}, end={1}, id={2}, pos={3}'.format(token.begin, token.end, token.id, token.pos)) \n\nGetting and setting (nested) features\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n\nIf you want to access a variable but only have its name as a string or have nested feature structures,\ne.g. a feature structure with feature :code:`a` that has a\nfeature :code:`b` that has a feature :code:`c`, some of which can be :code:`None`, then you can use the\nfollowing:\n\n.. code:: python\n\n fs.get(\"var_name\") # Or\n fs[\"var_name\"]\n\nOr in the nested case,\n\n.. code:: python\n\n fs.get(\"a.b.c\")\n fs[\"a.b.c\"]\n\n\nIf :code:`a` or :code:`b` or :code:`c` are :code:`None`, then this returns instead of\nthrowing an error.\n\nAnother example would be a StringList containing :code:`[\"Foo\", \"Bar\", \"Baz\"]`:\n\n.. code:: python\n\n assert lst.get(\"head\") == \"foo\"\n assert lst.get(\"tail.head\") == \"bar\"\n assert lst.get(\"tail.tail.head\") == \"baz\"\n assert lst.get(\"tail.tail.tail.head\") == None\n assert lst.get(\"tail.tail.tail.tail.head\") == None\n\nThe same goes for setting:\n\n.. code:: python\n\n # Functional\n lst.set(\"head\", \"new_foo\")\n lst.set(\"tail.head\", \"new_bar\")\n lst.set(\"tail.tail.head\", \"new_baz\")\n\n assert lst.get(\"head\") == \"new_foo\"\n assert lst.get(\"tail.head\") == \"new_bar\"\n assert lst.get(\"tail.tail.head\") == \"new_baz\"\n\n # Bracket access\n lst[\"head\"] = \"newer_foo\"\n lst[\"tail.head\"] = \"newer_bar\"\n lst[\"tail.tail.head\"] = \"newer_baz\"\n\n assert lst[\"head\"] == \"newer_foo\"\n assert lst[\"tail.head\"] == \"newer_bar\"\n assert lst[\"tail.tail.head\"] == \"newer_baz\"\n\n\nCreating types and adding features\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n\n.. code:: python\n\n from cassis import *\n\n typesystem = TypeSystem()\n\n parent_type = typesystem.create_type(name='example.ParentType')\n typesystem.create_feature(domainType=parent_type, name='parentFeature', rangeType=TYPE_NAME_STRING)\n\n child_type = typesystem.create_type(name='example.ChildType', supertypeName=parent_type.name)\n typesystem.create_feature(domainType=child_type, name='childFeature', rangeType=TYPE_NAME_INTEGER)\n\n annotation = child_type(parentFeature='parent', childFeature='child')\n\nWhen adding new features, these changes are propagated. For example,\nadding a feature to a parent type makes it available to a child type.\nTherefore, the type system does not need to be frozen for consistency.\nThe type system can be changed even after loading, it is not frozen\nlike in UIMAj.\n\nSofa support\n~~~~~~~~~~~~\n\nA Sofa represents some form of an unstructured artifact that is processed in a UIMA pipeline. It contains for instance\nthe document text. Currently, new Sofas can be created. This is automatically done when creating a new view. Basic\nproperties of the Sofa can be read and written:\n\n.. code:: python\n\n cas = Cas(\n sofa_string = \"Joe waited for the train . The train was late .\",\n document_language = \"en\")\n\n print(cas.sofa_string)\n print(cas.sofa_mime)\n print(cas.document_language)\n\nArray support\n~~~~~~~~~~~~~\n\nArray feature values are not simply Python arrays, but they are wrapped in a feature structure of\na UIMA array type such as :code:`uima.cas.FSArray`.\n\n.. code:: python\n\n from cassis import *\n from cassis.typesystem import TYPE_NAME_FS_ARRAY, TYPE_NAME_ANNOTATION\n\n typesystem = TypeSystem()\n\n ArrayHolder = typesystem.create_type(name='example.ArrayHolder')\n typesystem.create_feature(domainType=ArrayHolder, name='array', rangeType=TYPE_NAME_FS_ARRAY)\n\n cas = Cas(typesystem=typesystem)\n\n Annotation = cas.typesystem.get_type(TYPE_NAME_ANNOTATION)\n FSArray = cas.typesystem.get_type(TYPE_NAME_FS_ARRAY)\n\n ann = Annotation(begin=0, end=1)\n cas.add(ann1)\n holder = ArrayHolder(array=FSArray(elements=[ann, ann, ann]))\n cas.add(holder)\n\nManaging views\n~~~~~~~~~~~~~~\n\nA view into a CAS contains a subset of feature structures and annotations. One view corresponds to exactly one Sofa. It\ncan also be used to query and alter information about the Sofa, e.g. the document text. Annotations added to one view\nare not visible in another view. A view Views can be created and changed. A view has the same methods and attributes\nas a :code:`Cas` .\n\n.. code:: python\n\n from cassis import *\n\n with open('typesystem.xml', 'rb') as f:\n typesystem = load_typesystem(f)\n Token = typesystem.get_type('cassis.Token')\n\n # This creates automatically the view `_InitialView`\n cas = Cas()\n cas.sofa_string = \"I like cheese .\"\n\n cas.add_all([\n Token(begin=0, end=1),\n Token(begin=2, end=6),\n Token(begin=7, end=13),\n Token(begin=14, end=15)\n ])\n\n print([x.get_covered_text() for x in cas.select_all()])\n\n # Create a new view and work on it.\n view = cas.create_view('testView')\n view.sofa_string = \"I like blackcurrant .\"\n\n view.add_all([\n Token(begin=0, end=1),\n Token(begin=2, end=6),\n Token(begin=7, end=19),\n Token(begin=20, end=21)\n ])\n\n print([x.get_covered_text() for x in view.select_all()])\n\nMerging type systems\n~~~~~~~~~~~~~~~~~~~~\n\nSometimes, it is desirable to merge two type systems. With **cassis**, this can be\nachieved via the :code:`merge_typesystems` function. The detailed rules of merging can be found\n`here <https://uima.apache.org/d/uimaj-2.10.4/references.html#ugr.ref.cas.typemerging>`_.\n\n.. code:: python\n\n from cassis import *\n\n with open('typesystem.xml', 'rb') as f:\n typesystem = load_typesystem(f)\n\n ts = merge_typesystems([typesystem, load_dkpro_core_typesystem()])\n\nType checking\n~~~~~~~~~~~~~\n\nWhen adding annotations, no type checking is performed for simplicity reasons.\nIn order to check types, call the :code:`cas.typecheck()` method. Currently, it only\nchecks whether elements in `uima.cas.FSArray` are\nadhere to the specified :code:`elementType`.\n\nDKPro Core Integration\n----------------------\n\nA CAS using the DKPro Core Type System can be created via\n\n.. code:: python\n\n from cassis import *\n\n cas = Cas(typesystem=load_dkpro_core_typesystem())\n\n for t in cas.typesystem.get_types():\n print(t)\n\nMiscellaneous\n-------------\n\nIf feature names clash with Python magic variables\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n\nIf your type system defines a type called :code:`self` or :code:`type`, then it will be made\navailable as a member variable :code:`self_` or :code:`type_` on the respective type:\n\n.. code:: python\n\n from cassis import *\n from cassis.typesystem import *\n\n typesystem = TypeSystem()\n\n ExampleType = typesystem.create_type(name='example.Type')\n typesystem.create_feature(domainType=ExampleType, name='self', rangeType=TYPE_NAME_STRING)\n typesystem.create_feature(domainType=ExampleType, name='type', rangeType=TYPE_NAME_STRING)\n\n annotation = ExampleType(self_=\"Test string1\", type_=\"Test string2\")\n\n print(annotation.self_)\n print(annotation.type_)\n\nLeniency\n~~~~~~~~\n\nIf the type for a feature structure is not found in the typesystem, it will raise an exception by default.\nIf you want to ignore these kind of errors, you can pass :code:`lenient=True` to the :code:`Cas` constructor or\nto :code:`load_cas_from_xmi`.\n\nLarge XMI files\n~~~~~~~~~~~~~~~\n\nIf you try to parse large XMI files and get an error message like :code:`XMLSyntaxError: internal error: Huge input lookup`,\nthen you can disable this security check by passing :code:`trusted=True` to your calls to :code:`load_cas_from_xmi`.\n\nCiting & Authors\n----------------\n\nIf you find this repository helpful, feel free to cite\n\n.. code:: bibtex\n\n @software{klie2020_cassis,\n author = {Jan-Christoph Klie and\n Richard Eckart de Castilho},\n title = {DKPro Cassis - Reading and Writing UIMA CAS Files in Python},\n publisher = {Zenodo},\n doi = {10.5281/zenodo.3994108},\n url = {https://github.com/dkpro/dkpro-cassis}\n }\n\nDevelopment\n-----------\n\nThe required dependencies are managed by **pip**. A virtual environment\ncontaining all needed packages for development and production can be\ncreated and activated by\n\n::\n\n virtualenv venv --python=python3 --no-site-packages\n source venv/bin/activate\n pip install -e \".[test, dev, doc]\"\n\nThe tests can be run in the current environment by invoking\n\n::\n\n make test\n\nor in a clean environment via\n\n::\n\n tox\n\nRelease\n-------\n\n- Make sure all issues for the milestone are completed, otherwise move them to the next\n- Checkout the ``main`` branch\n- Bump the version in ``cassis/__version__.py`` to a stable one, e.g. ``__version__ = \"0.6.0\"``, commit and push, wait until the build completed. An example commit message would be ``No issue. Release 0.6.0``\n- Create a tag for that version via e.g. ``git tag v0.6.0`` and push the tags via ``git push --tags``. Pushing a tag triggers the release to pypi\n- Bump the version in ``cassis/__version__.py`` to the next development version, e.g. ``0.7.0-dev``, commit and push that. An example commit message would be ``No issue. Bump version after release``\n- Once the build has completed and pypi accepted the new version, go to the Github release and write the changelog based on the issues in the respective milestone\n- Create a new milestone for the next version\n\n",
"bugtrack_url": null,
"license": "Apache License 2.0",
"summary": "UIMA CAS processing library in Python",
"version": "0.9.0",
"project_urls": {
"Bug Tracker": "https://github.com/dkpro/dkpro-cassis/issues",
"Documentation": "https://cassis.readthedocs.org/",
"Homepage": "https://dkpro.github.io",
"Source Code": "https://github.com/dkpro/dkpro-cassis"
},
"split_keywords": [
"uima",
"dkpro",
"cas",
"xmi"
],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "be44d46f01432495f2c917d67c4cda99d93278370134a280714a19ee80811eb9",
"md5": "16e68b9dc97a403a863231060285dbb4",
"sha256": "97cfdddf32a27ae1995ba3e610a6f391edefc40ea49da6d6bf49096e3d993b14"
},
"downloads": -1,
"filename": "dkpro-cassis-0.9.0.tar.gz",
"has_sig": false,
"md5_digest": "16e68b9dc97a403a863231060285dbb4",
"packagetype": "sdist",
"python_version": "source",
"requires_python": ">=3.7.0",
"size": 77272,
"upload_time": "2024-02-04T11:21:56",
"upload_time_iso_8601": "2024-02-04T11:21:56.173523Z",
"url": "https://files.pythonhosted.org/packages/be/44/d46f01432495f2c917d67c4cda99d93278370134a280714a19ee80811eb9/dkpro-cassis-0.9.0.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2024-02-04 11:21:56",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "dkpro",
"github_project": "dkpro-cassis",
"travis_ci": false,
"coveralls": true,
"github_actions": true,
"tox": true,
"lcname": "dkpro-cassis"
}