acdh-cidoc-pyutils


Nameacdh-cidoc-pyutils JSON
Version 0.24 PyPI version JSON
download
home_pagehttps://github.com/acdh-oeaw/acdh-cidoc-pyutils
SummaryHelper functions for the generation of CIDOC CRMish RDF (from XML/TEI data)
upload_time2024-04-14 09:00:58
maintainerNone
docs_urlNone
authorPeter Andorfer
requires_python>=3.8
licenseMIT license
keywords
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            [![flake8 Lint](https://github.com/acdh-oeaw/acdh-cidoc-pyutils/actions/workflows/lint.yml/badge.svg)](https://github.com/acdh-oeaw/acdh-cidoc-pyutils/actions/workflows/lint.yml)
[![Test](https://github.com/acdh-oeaw/acdh-cidoc-pyutils/actions/workflows/test.yml/badge.svg)](https://github.com/acdh-oeaw/acdh-cidoc-pyutils/actions/workflows/test.yml)
[![codecov](https://codecov.io/gh/acdh-oeaw/acdh-cidoc-pyutils/branch/main/graph/badge.svg?token=XRF7ANN1TM)](https://codecov.io/gh/acdh-oeaw/acdh-cidoc-pyutils)
[![PyPI version](https://badge.fury.io/py/acdh-cidoc-pyutils.svg)](https://badge.fury.io/py/acdh-cidoc-pyutils)

# acdh-cidoc-pyutils
Helper functions for the generation of CIDOC CRMish RDF (from XML/TEI data)

## Installation

* install via `pip install acdh-cidoc-pyutils`

## Examples

* For 'real-world-examples' see e.g. [semantic-kraus project](https://github.com/semantic-kraus/lk-data/blob/main/scripts/make_rdf.py)
* also take a look into [test_cidoc_pyutils.py](https://github.com/acdh-oeaw/acdh-cidoc-pyutils/blob/main/tests/test_cidoc_pyutils.py)

### extract `cidoc:P14i_performed FRBROO:F51_ Pursuit` triples from `tei:person/tei:occupation` nodes
```python
import lxml.etree as ET
from rdflib import URIRef
rom acdh_cidoc_pyutils import make_occupations, NSMAP
sample = """
<TEI xmlns="http://www.tei-c.org/ns/1.0">
    <person xml:id="DWpers0091" sortKey="Gulbransson_Olaf_Leonhard">
        <persName type="pref">Gulbransson, Olaf</persName>
        <occupation notBefore="1900-12" notAfter="2000" key="#hansi" xml:lang="it">Bürgermeister</occupation>
        <occupation from="1233-02-03" key="#sumsi">Tischlermeister/Fleischhauer</occupation>
        <occupation key="franzi">Sängerin</occupation>
        <occupation>Bäckerin</occupation>
    </person>
</TEI>"""
g, uris = make_occupations(subj, x, "https://foo.bar", id_xpath="@key")
print(g.serialize())
# returns
```
```ttl
@prefix ns1: <http://www.cidoc-crm.org/cidoc-crm/> .
@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .
@prefix xsd: <http://www.w3.org/2001/XMLSchema#> .

<https://foo/bar/DWpers0091> ns1:P14i_performed <https://foo/bar/DWpers0091/occupation/3>,
        <https://foo/bar/DWpers0091/occupation/franzi>,
        <https://foo/bar/DWpers0091/occupation/hansi>,
        <https://foo/bar/DWpers0091/occupation/sumsi> .

<https://foo/bar/DWpers0091/occupation/3> a <http://iflastandards.info/ns/fr/frbr/frbroo#F51> ;
    rdfs:label "Bäckerin"@de .

<https://foo/bar/DWpers0091/occupation/franzi> a <http://iflastandards.info/ns/fr/frbr/frbroo#F51> ;
    rdfs:label "Sängerin"@de .

<https://foo/bar/DWpers0091/occupation/hansi> a <http://iflastandards.info/ns/fr/frbr/frbroo#F51> ;
    rdfs:label "Bürgermeister"@it ;
    ns1:P4_has_time-span <https://foo/bar/DWpers0091/occupation/hansi/time-span> .

<https://foo/bar/DWpers0091/occupation/hansi/time-span> a ns1:E52_Time-Span ;
    rdfs:label "1900-12 - 2000"^^xsd:string ;
    ns1:P82a_begin_of_the_begin "1900-12"^^xsd:gYearMonth ;
    ns1:P82b_end_of_the_end "2000"^^xsd:gYear .

<https://foo/bar/DWpers0091/occupation/sumsi> a <http://iflastandards.info/ns/fr/frbr/frbroo#F51> ;
    rdfs:label "Tischlermeister/Fleischhauer"@de ;
    ns1:P4_has_time-span <https://foo/bar/DWpers0091/occupation/sumsi/time-span> .

<https://foo/bar/DWpers0091/occupation/sumsi/time-span> a ns1:E52_Time-Span ;
    rdfs:label "1233-02-03 - 1233-02-03"^^xsd:string ;
    ns1:P82a_begin_of_the_begin "1233-02-03"^^xsd:date ;
    ns1:P82b_end_of_the_end "1233-02-03"^^xsd:date .
```

### extract birth/death triples from `tei:person`

```python
import lxml.etree as ET
from rdflib import URIRef
from acdh_cidoc_pyutils import make_birth_death_entities, NSMAP

sample = """
<TEI xmlns="http://www.tei-c.org/ns/1.0">
    <person xml:id="DWpers0091" sortKey="Gulbransson_Olaf_Leonhard">
        <persName type="pref">Gulbransson, Olaf</persName>
        <birth when="1873-05-26">
            26. 5. 1873<placeName key="#DWplace00139">Christiania (Oslo)</placeName>
        </birth>
        <death>
            <date notBefore-iso="1905-07-04" when="1955" to="2000">04.07.1905</date>
            <settlement key="pmb50">
                <placeName type="pref">Wien</placeName>
                <location><geo>48.2066 16.37341</geo></location>
            </settlement>
        </death>
    </person>
</TEI>"""

doc = ET.fromstring(sample)
x = doc.xpath(".//tei:person[1]", namespaces=NSMAP)[0]
xml_id = x.attrib["{http://www.w3.org/XML/1998/namespace}id"].lower()
item_id = f"https://foo/bar/{xml_id}"
subj = URIRef(item_id)
event_graph, birth_uri, birth_timestamp = make_birth_death_entities(
    subj, x, place_id_xpath="//tei:placeName[1]/@key
)
event_graph, birth_uri, birth_timestamp = make_birth_death_entities(
    subj, x, event_type="death", verbose=True, date_node_xpath="/tei:date[1]",
    place_id_xpath="//tei:settlement[1]/@key"
)
event_graph.serialize(format="turtle")
# returns
```
```ttl
@prefix ns1: <http://www.cidoc-crm.org/cidoc-crm/> .
@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .

# birth example

<https://foo/bar/dwpers0091/birth> a ns1:E67_Birth ;
    rdfs:label "Geburt von Gulbransson, Olaf Leonhard"@fr ;
    ns1:P4_has_time-span <https://foo/bar/dwpers0091/birth/time-span> ;
    ns1:P7_took_place_at <https://foo/bar/DWplace00139> ;
    ns1:P98_brought_into_life <https://foo/bar/dwpers0091> .

<https://foo/bar/dwpers0091/birth/time-span> a ns1:E52_Time-Span ;
    rdfs:label "1873-05-26 - 1873-05-26"^^xsd:string ;
    ns1:P82a_begin_of_the_begin "1873-05-26"^^xsd:date ;
    ns1:P82b_end_of_the_end "1873-05-26"^^xsd:date .

# death example

<https://foo/bar/dwpers0091/death> a ns1:E69_Death ;
    rdfs:label "Geburt von Gulbransson, Olaf Leonhard"@fr ;
    ns1:P100_was_death_of <https://foo/bar/dwpers0091> ;
    ns1:P7_took_place_at <https://foo/bar/pmb50>
    ns1:P4_has_time-span <https://foo/bar/dwpers0091/death/time-span> .

<https://foo/bar/dwpers0091/death/time-span> a ns1:E52_Time-Span ;
    rdfs:label "1905-07-04 - 2000"^^xsd:string ;
    ns1:P82a_begin_of_the_begin "1905-07-04"^^xsd:date ;
    ns1:P82b_end_of_the_end "2000"^^xsd:gYear .
```


### create `ns1:P168_place_is_defined_by "Point(456 123)"^^<geo:wktLiteral> .` from tei:coords
```python
import lxml.etree as ET
from rdflib import Graph, URIRef, RDF
from acdh_cidoc_pyutils import coordinates_to_p168, NSMAP, CIDOC
sample = """
<TEI xmlns="http://www.tei-c.org/ns/1.0">
    <place xml:id="DWplace00092">
        <placeName type="orig_name">Reval (Tallinn)</placeName>
        <location><geo>123 456</geo></location>
    </place>
</TEI>"""

doc = ET.fromstring(sample)
g = Graph()
for x in doc.xpath(".//tei:place", namespaces=NSMAP):
    xml_id = x.attrib["{http://www.w3.org/XML/1998/namespace}id"].lower()
    item_id = f"https://foo/bar/{xml_id}"
    subj = URIRef(item_id)
    g.add((subj, RDF.type, CIDOC["E53_Place"]))
    g += coordinates_to_p168(subj, x)
print(g.serialize())
# returns
```
```ttl
...
    ns1:P168_place_is_defined_by "Point(456 123)"^^<geo:wktLiteral> .
...
```
* Function parameter `verbose` prints information in case the given xpath does not return expected results which is a text node with two numbers separated by a given separator (default value is `separator=" "`)
* Function parameter `inverse` (default: `inverse=False`) changes the order of the coordinates.



### date-like-string to casted rdflib.Literal

```python
from acdh_cidoc_pyutils import date_to_literal d
dates = [
    "1900",
    "1900-01",
    "1901-01-01",
    "foo",
]
for x in dates:
    date_literal = date_to_literal(x)
    print((date_literal.datatype))

# returns
# http://www.w3.org/2001/XMLSchema#gYear
# http://www.w3.org/2001/XMLSchema#gYearMonth
# http://www.w3.org/2001/XMLSchema#date
# http://www.w3.org/2001/XMLSchema#string
```

### make some random URI

```python
from acdh_cidoc_pyutils import make_uri

domain = "https://hansi4ever.com/"
version = "1"
prefix = "sumsi"
uri = make_uri(domain=domain, version=version, prefix=prefix)
print(uri)
# https://hansi4ever.com/1/sumsi/6ead32b8-9713-11ed-8065-65787314013c

uri = make_uri(domain=domain)
print(uri)
# https://hansi4ever.com/8b912e66-9713-11ed-8065-65787314013c
```

### create an E52_Time-Span graph

```python
from acdh_cidoc_pyutils import create_e52, make_uri
uri = make_uri()
e52 = create_e52(uri, begin_of_begin="1800-12-12", end_of_end="1900-01")
print(e52.serialize())
# returns
```
```ttl
# @prefix ns1: <http://www.cidoc-crm.org/cidoc-crm/> .
# @prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .
# @prefix xsd: <http://www.w3.org/2001/XMLSchema#> .

# <https://hansi4ever.com/387fb457-971b-11ed-8065-65787314013c> a ns1:E52_Time-Span ;
#     rdfs:label "1800-12-12 - 1900-01"^^xsd:string ;
#     ns1:P82a_begin_of_the_begin "1800-12-12"^^xsd:date ;
#     ns1:P82b_end_of_the_end "1900-01"^^xsd:gYearMonth .
```
### creates E42 from tei:org|place|person

takes a tei:person|place|org node, extracts their `@xml:id` and all `tei:idno` elements, derives `idoc:E42_Identifier` triples and relates them to a passed in subject via `cidoc:P1_is_identified_by`

```python
import lxml.etree as ET
from rdflib import Graph, URIRef, RDF
from acdh_cidoc_pyutils import make_e42_identifiers, NSMAP, CIDOC
sample = """
<TEI xmlns="http://www.tei-c.org/ns/1.0">
    <place xml:id="DWplace00092">
        <placeName type="orig_name">Reval (Tallinn)</placeName>
        <placeName xml:lang="de" type="simple_name">Reval</placeName>
        <placeName xml:lang="und" type="alt_label">Tallinn</placeName>
        <idno type="pmb">https://pmb.acdh.oeaw.ac.at/entity/42085/</idno>
        <idno type="URI" subtype="geonames">https://www.geonames.org/588409</idno>
        <idno subtype="foobarid">12345</idno>
    </place>
</TEI>"""

doc = ET.fromstring(sample)
g = Graph()
for x in doc.xpath(".//tei:place|tei:org|tei:person|tei:bibl", namespaces=NSMAP):
    xml_id = x.attrib["{http://www.w3.org/XML/1998/namespace}id"].lower()
    item_id = f"https://foo/bar/{xml_id}"
    subj = URIRef(item_id)
    g.add((subj, RDF.type, CIDOC["E53_Place"]))
    g += make_e42_identifiers(
        subj, x, type_domain="http://hansi/4/ever", default_lang="it",
    )
print(g.serialize(format="turtle"))
# returns
```
```ttl
@prefix ns1: <http://www.cidoc-crm.org/cidoc-crm/> .
@prefix owl: <http://www.w3.org/2002/07/owl#> .
@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .

<https://foo/bar/dwplace00092> a ns1:E53_Place ;
    ns1:P1_is_identified_by <https://foo/bar/dwplace00092/identifier/DWplace00092>,
        <https://foo/bar/dwplace00092/identifier/idno/0>,
        <https://foo/bar/dwplace00092/identifier/idno/1>,
        <https://foo/bar/dwplace00092/identifier/idno/2> ;
    owl:sameAs <https://pmb.acdh.oeaw.ac.at/entity/42085/>,
        <https://www.geonames.org/588409> .

<http://hansi/4/ever/idno/URI/geonames> a ns1:E55_Type .

<http://hansi/4/ever/idno/foobarid> a ns1:E55_Type .

<http://hansi/4/ever/idno/pmb> a ns1:E55_Type .

<http://hansi/4/ever/xml-id> a ns1:E55_Type .

<https://foo/bar/dwplace00092/identifier/DWplace00092> a ns1:E42_Identifier ;
    rdfs:label "Identifier: DWplace00092"@it ;
    rdf:value "DWplace00092";
    ns1:P2_has_type <http://hansi/4/ever/xml-id> .

<https://foo/bar/dwplace00092/identifier/idno/0> a ns1:E42_Identifier ;
    rdfs:label "Identifier: https://pmb.acdh.oeaw.ac.at/entity/42085/"@it ;
    rdf:value "https://pmb.acdh.oeaw.ac.at/entity/42085/";
    ns1:P2_has_type <http://hansi/4/ever/idno/pmb> .

<https://foo/bar/dwplace00092/identifier/idno/1> a ns1:E42_Identifier ;
    rdfs:label "Identifier: https://www.geonames.org/588409"@it ;
    rdf:value "https://www.geonames.org/588409" 
    ns1:P2_has_type <http://hansi/4/ever/idno/URI/geonames> .

<https://foo/bar/dwplace00092/identifier/idno/2> a ns1:E42_Identifier ;
    rdfs:label "Identifier: 12345"@it ;
    rdf:value "12345";
    ns1:P2_has_type <http://hansi/4/ever/idno/foobarid> .
```

### creates appellations from tei:org|place|person

takes a tei:person|place|org node, extracts `persName, placeName and orgName` texts, `@xml:lang` and custom type values and returns `cidoc:E33_41` and `cidoc:E55` nodes linked via `cidoc:P1_is_identified_by` and `cidoc:P2_has_type`

```python
import lxml.etree as ET
from rdflib import Graph, URIRef, RDF
from acdh_cidoc_pyutils import make_appellations, NSMAP, CIDOC

sample = """
<TEI xmlns="http://www.tei-c.org/ns/1.0">
    <place xml:id="DWplace00092">
        <placeName type="orig_name">Reval (Tallinn)</placeName>
        <placeName xml:lang="de" type="simple_name">Reval</placeName>
        <placeName xml:lang="und" type="alt_label">Tallinn</placeName>
        <idno type="pmb">https://pmb.acdh.oeaw.ac.at/entity/42085/</idno>
    </place>
</TEI>"""

doc = ET.fromstring(sample)
g = Graph()
for x in doc.xpath(".//tei:place|tei:org|tei:person|tei:bibl", namespaces=NSMAP):
    xml_id = x.attrib["{http://www.w3.org/XML/1998/namespace}id"].lower()
    item_id = f"https://foo/bar/{xml_id}"
    subj = URIRef(item_id)
    g.add((subj, RDF.type, CIDOC["E53_Place"]))
    g += make_appellations(
        subj, x, type_domain="http://hansi/4/ever", default_lang="it"
    )

g.serialize(format="ttl")
# returns
```
```ttl
@prefix ns1: <http://www.cidoc-crm.org/cidoc-crm/> .
@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .

<https://foo/bar/dwplace00092> a ns1:E53_Place ;
    ns1:P1_is_identified_by <https://foo/bar/dwplace00092/appellation/0>,
        <https://foo/bar/dwplace00092/appellation/1>,
        <https://foo/bar/dwplace00092/appellation/2> .

<http://hansi/4/ever/alt-label> a ns1:E55_Type ;
    rdfs:label "alt_label" .

<http://hansi/4/ever/orig-name> a ns1:E55_Type ;
    rdfs:label "orig_name" .

<http://hansi/4/ever/simple-name> a ns1:E55_Type ;
    rdfs:label "simple_name" .

<https://foo/bar/dwplace00092/appellation/0> a ns1:E33_E41_Linguistic_Appellation ;
    rdfs:label "Reval (Tallinn)"@it ;
    ns1:P2_has_type <http://hansi/4/ever/orig-name> .

<https://foo/bar/dwplace00092/appellation/1> a ns1:E33_E41_Linguistic_Appellation ;
    rdfs:label "Reval"@de ;
    ns1:P2_has_type <http://hansi/4/ever/simple-name> .

<https://foo/bar/dwplace00092/appellation/2> a ns1:E33_E41_Linguistic_Appellation ;
    rdfs:label "Tallinn"@und ;
    ns1:P2_has_type <http://hansi/4/ever/alt-label> .
```
### normalize_string

```python
from acdh_cidoc_pyutils import normalize_string
string = """\n\nhallo
mein schatz ich liebe    dich
    du bist         die einzige für mich
        """
print(normalize_string(string))
# returns
# hallo mein schatz ich liebe dich du bist die einzige für mich
```

### extract date attributes (begin, end)

expects typical TEI date attributes like `@when, @when-iso, @notBefore, @notAfter, @from, @to, ...` and returns a tuple containg start- and enddate values. If only `@when or @when-iso` or only `@notBefore or @notAfter` are provided, the returned values are the same, unless the default parameter `fill_missing` is set to `False`. 

```python
from lxml.etree import Element
from acdh_cidoc_pyutils import extract_begin_end

date_string = "1900-12-12"
date_object = Element("{http://www.tei-c.org/ns/1.0}tei")
date_object.attrib["when-iso"] = date_string
print(extract_begin_end(date_object))

# returns
# ('1900-12-12', '1900-12-12')

date_string = "1900-12-12"
date_object = Element("{http://www.tei-c.org/ns/1.0}tei")
date_object.attrib["when-iso"] = date_string
print(extract_begin_end(date_object, fill_missing=False))

# returns
# ('1900-12-12', None)

date_object = Element("{http://www.tei-c.org/ns/1.0}tei")
date_object.attrib["notAfter"] = "1900-12-12"
date_object.attrib["notBefore"] = "1800"
print(extract_begin_end(date_object))

# returns
# ('1800', '1900-12-12')
```

## development

* `pip install -r requirements_dev.txt`
* `flake8` -> linting
* `coveage run -m pytest` -> runs tests and creates coverage stats

            

Raw data

            {
    "_id": null,
    "home_page": "https://github.com/acdh-oeaw/acdh-cidoc-pyutils",
    "name": "acdh-cidoc-pyutils",
    "maintainer": null,
    "docs_url": null,
    "requires_python": ">=3.8",
    "maintainer_email": null,
    "keywords": null,
    "author": "Peter Andorfer",
    "author_email": "peter.andorfer@oeaw.ac.at",
    "download_url": "https://files.pythonhosted.org/packages/06/f4/4dfde604e360376fae2dc55b18bdf140c6c1bfdeded4c85c39b7efaea2d2/acdh_cidoc_pyutils-0.24.tar.gz",
    "platform": null,
    "description": "[![flake8 Lint](https://github.com/acdh-oeaw/acdh-cidoc-pyutils/actions/workflows/lint.yml/badge.svg)](https://github.com/acdh-oeaw/acdh-cidoc-pyutils/actions/workflows/lint.yml)\n[![Test](https://github.com/acdh-oeaw/acdh-cidoc-pyutils/actions/workflows/test.yml/badge.svg)](https://github.com/acdh-oeaw/acdh-cidoc-pyutils/actions/workflows/test.yml)\n[![codecov](https://codecov.io/gh/acdh-oeaw/acdh-cidoc-pyutils/branch/main/graph/badge.svg?token=XRF7ANN1TM)](https://codecov.io/gh/acdh-oeaw/acdh-cidoc-pyutils)\n[![PyPI version](https://badge.fury.io/py/acdh-cidoc-pyutils.svg)](https://badge.fury.io/py/acdh-cidoc-pyutils)\n\n# acdh-cidoc-pyutils\nHelper functions for the generation of CIDOC CRMish RDF (from XML/TEI data)\n\n## Installation\n\n* install via `pip install acdh-cidoc-pyutils`\n\n## Examples\n\n* For 'real-world-examples' see e.g. [semantic-kraus project](https://github.com/semantic-kraus/lk-data/blob/main/scripts/make_rdf.py)\n* also take a look into [test_cidoc_pyutils.py](https://github.com/acdh-oeaw/acdh-cidoc-pyutils/blob/main/tests/test_cidoc_pyutils.py)\n\n### extract `cidoc:P14i_performed FRBROO:F51_ Pursuit` triples from `tei:person/tei:occupation` nodes\n```python\nimport lxml.etree as ET\nfrom rdflib import URIRef\nrom acdh_cidoc_pyutils import make_occupations, NSMAP\nsample = \"\"\"\n<TEI xmlns=\"http://www.tei-c.org/ns/1.0\">\n    <person xml:id=\"DWpers0091\" sortKey=\"Gulbransson_Olaf_Leonhard\">\n        <persName type=\"pref\">Gulbransson, Olaf</persName>\n        <occupation notBefore=\"1900-12\" notAfter=\"2000\" key=\"#hansi\" xml:lang=\"it\">B\u00fcrgermeister</occupation>\n        <occupation from=\"1233-02-03\" key=\"#sumsi\">Tischlermeister/Fleischhauer</occupation>\n        <occupation key=\"franzi\">S\u00e4ngerin</occupation>\n        <occupation>B\u00e4ckerin</occupation>\n    </person>\n</TEI>\"\"\"\ng, uris = make_occupations(subj, x, \"https://foo.bar\", id_xpath=\"@key\")\nprint(g.serialize())\n# returns\n```\n```ttl\n@prefix ns1: <http://www.cidoc-crm.org/cidoc-crm/> .\n@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .\n@prefix xsd: <http://www.w3.org/2001/XMLSchema#> .\n\n<https://foo/bar/DWpers0091> ns1:P14i_performed <https://foo/bar/DWpers0091/occupation/3>,\n        <https://foo/bar/DWpers0091/occupation/franzi>,\n        <https://foo/bar/DWpers0091/occupation/hansi>,\n        <https://foo/bar/DWpers0091/occupation/sumsi> .\n\n<https://foo/bar/DWpers0091/occupation/3> a <http://iflastandards.info/ns/fr/frbr/frbroo#F51> ;\n    rdfs:label \"B\u00e4ckerin\"@de .\n\n<https://foo/bar/DWpers0091/occupation/franzi> a <http://iflastandards.info/ns/fr/frbr/frbroo#F51> ;\n    rdfs:label \"S\u00e4ngerin\"@de .\n\n<https://foo/bar/DWpers0091/occupation/hansi> a <http://iflastandards.info/ns/fr/frbr/frbroo#F51> ;\n    rdfs:label \"B\u00fcrgermeister\"@it ;\n    ns1:P4_has_time-span <https://foo/bar/DWpers0091/occupation/hansi/time-span> .\n\n<https://foo/bar/DWpers0091/occupation/hansi/time-span> a ns1:E52_Time-Span ;\n    rdfs:label \"1900-12 - 2000\"^^xsd:string ;\n    ns1:P82a_begin_of_the_begin \"1900-12\"^^xsd:gYearMonth ;\n    ns1:P82b_end_of_the_end \"2000\"^^xsd:gYear .\n\n<https://foo/bar/DWpers0091/occupation/sumsi> a <http://iflastandards.info/ns/fr/frbr/frbroo#F51> ;\n    rdfs:label \"Tischlermeister/Fleischhauer\"@de ;\n    ns1:P4_has_time-span <https://foo/bar/DWpers0091/occupation/sumsi/time-span> .\n\n<https://foo/bar/DWpers0091/occupation/sumsi/time-span> a ns1:E52_Time-Span ;\n    rdfs:label \"1233-02-03 - 1233-02-03\"^^xsd:string ;\n    ns1:P82a_begin_of_the_begin \"1233-02-03\"^^xsd:date ;\n    ns1:P82b_end_of_the_end \"1233-02-03\"^^xsd:date .\n```\n\n### extract birth/death triples from `tei:person`\n\n```python\nimport lxml.etree as ET\nfrom rdflib import URIRef\nfrom acdh_cidoc_pyutils import make_birth_death_entities, NSMAP\n\nsample = \"\"\"\n<TEI xmlns=\"http://www.tei-c.org/ns/1.0\">\n    <person xml:id=\"DWpers0091\" sortKey=\"Gulbransson_Olaf_Leonhard\">\n        <persName type=\"pref\">Gulbransson, Olaf</persName>\n        <birth when=\"1873-05-26\">\n            26. 5. 1873<placeName key=\"#DWplace00139\">Christiania (Oslo)</placeName>\n        </birth>\n        <death>\n            <date notBefore-iso=\"1905-07-04\" when=\"1955\" to=\"2000\">04.07.1905</date>\n            <settlement key=\"pmb50\">\n                <placeName type=\"pref\">Wien</placeName>\n                <location><geo>48.2066 16.37341</geo></location>\n            </settlement>\n        </death>\n    </person>\n</TEI>\"\"\"\n\ndoc = ET.fromstring(sample)\nx = doc.xpath(\".//tei:person[1]\", namespaces=NSMAP)[0]\nxml_id = x.attrib[\"{http://www.w3.org/XML/1998/namespace}id\"].lower()\nitem_id = f\"https://foo/bar/{xml_id}\"\nsubj = URIRef(item_id)\nevent_graph, birth_uri, birth_timestamp = make_birth_death_entities(\n    subj, x, place_id_xpath=\"//tei:placeName[1]/@key\n)\nevent_graph, birth_uri, birth_timestamp = make_birth_death_entities(\n    subj, x, event_type=\"death\", verbose=True, date_node_xpath=\"/tei:date[1]\",\n    place_id_xpath=\"//tei:settlement[1]/@key\"\n)\nevent_graph.serialize(format=\"turtle\")\n# returns\n```\n```ttl\n@prefix ns1: <http://www.cidoc-crm.org/cidoc-crm/> .\n@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .\n\n# birth example\n\n<https://foo/bar/dwpers0091/birth> a ns1:E67_Birth ;\n    rdfs:label \"Geburt von Gulbransson, Olaf Leonhard\"@fr ;\n    ns1:P4_has_time-span <https://foo/bar/dwpers0091/birth/time-span> ;\n    ns1:P7_took_place_at <https://foo/bar/DWplace00139> ;\n    ns1:P98_brought_into_life <https://foo/bar/dwpers0091> .\n\n<https://foo/bar/dwpers0091/birth/time-span> a ns1:E52_Time-Span ;\n    rdfs:label \"1873-05-26 - 1873-05-26\"^^xsd:string ;\n    ns1:P82a_begin_of_the_begin \"1873-05-26\"^^xsd:date ;\n    ns1:P82b_end_of_the_end \"1873-05-26\"^^xsd:date .\n\n# death example\n\n<https://foo/bar/dwpers0091/death> a ns1:E69_Death ;\n    rdfs:label \"Geburt von Gulbransson, Olaf Leonhard\"@fr ;\n    ns1:P100_was_death_of <https://foo/bar/dwpers0091> ;\n    ns1:P7_took_place_at <https://foo/bar/pmb50>\n    ns1:P4_has_time-span <https://foo/bar/dwpers0091/death/time-span> .\n\n<https://foo/bar/dwpers0091/death/time-span> a ns1:E52_Time-Span ;\n    rdfs:label \"1905-07-04 - 2000\"^^xsd:string ;\n    ns1:P82a_begin_of_the_begin \"1905-07-04\"^^xsd:date ;\n    ns1:P82b_end_of_the_end \"2000\"^^xsd:gYear .\n```\n\n\n### create `ns1:P168_place_is_defined_by \"Point(456 123)\"^^<geo:wktLiteral> .` from tei:coords\n```python\nimport lxml.etree as ET\nfrom rdflib import Graph, URIRef, RDF\nfrom acdh_cidoc_pyutils import coordinates_to_p168, NSMAP, CIDOC\nsample = \"\"\"\n<TEI xmlns=\"http://www.tei-c.org/ns/1.0\">\n    <place xml:id=\"DWplace00092\">\n        <placeName type=\"orig_name\">Reval (Tallinn)</placeName>\n        <location><geo>123 456</geo></location>\n    </place>\n</TEI>\"\"\"\n\ndoc = ET.fromstring(sample)\ng = Graph()\nfor x in doc.xpath(\".//tei:place\", namespaces=NSMAP):\n    xml_id = x.attrib[\"{http://www.w3.org/XML/1998/namespace}id\"].lower()\n    item_id = f\"https://foo/bar/{xml_id}\"\n    subj = URIRef(item_id)\n    g.add((subj, RDF.type, CIDOC[\"E53_Place\"]))\n    g += coordinates_to_p168(subj, x)\nprint(g.serialize())\n# returns\n```\n```ttl\n...\n    ns1:P168_place_is_defined_by \"Point(456 123)\"^^<geo:wktLiteral> .\n...\n```\n* Function parameter `verbose` prints information in case the given xpath does not return expected results which is a text node with two numbers separated by a given separator (default value is `separator=\" \"`)\n* Function parameter `inverse` (default: `inverse=False`) changes the order of the coordinates.\n\n\n\n### date-like-string to casted rdflib.Literal\n\n```python\nfrom acdh_cidoc_pyutils import date_to_literal d\ndates = [\n    \"1900\",\n    \"1900-01\",\n    \"1901-01-01\",\n    \"foo\",\n]\nfor x in dates:\n    date_literal = date_to_literal(x)\n    print((date_literal.datatype))\n\n# returns\n# http://www.w3.org/2001/XMLSchema#gYear\n# http://www.w3.org/2001/XMLSchema#gYearMonth\n# http://www.w3.org/2001/XMLSchema#date\n# http://www.w3.org/2001/XMLSchema#string\n```\n\n### make some random URI\n\n```python\nfrom acdh_cidoc_pyutils import make_uri\n\ndomain = \"https://hansi4ever.com/\"\nversion = \"1\"\nprefix = \"sumsi\"\nuri = make_uri(domain=domain, version=version, prefix=prefix)\nprint(uri)\n# https://hansi4ever.com/1/sumsi/6ead32b8-9713-11ed-8065-65787314013c\n\nuri = make_uri(domain=domain)\nprint(uri)\n# https://hansi4ever.com/8b912e66-9713-11ed-8065-65787314013c\n```\n\n### create an E52_Time-Span graph\n\n```python\nfrom acdh_cidoc_pyutils import create_e52, make_uri\nuri = make_uri()\ne52 = create_e52(uri, begin_of_begin=\"1800-12-12\", end_of_end=\"1900-01\")\nprint(e52.serialize())\n# returns\n```\n```ttl\n# @prefix ns1: <http://www.cidoc-crm.org/cidoc-crm/> .\n# @prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .\n# @prefix xsd: <http://www.w3.org/2001/XMLSchema#> .\n\n# <https://hansi4ever.com/387fb457-971b-11ed-8065-65787314013c> a ns1:E52_Time-Span ;\n#     rdfs:label \"1800-12-12 - 1900-01\"^^xsd:string ;\n#     ns1:P82a_begin_of_the_begin \"1800-12-12\"^^xsd:date ;\n#     ns1:P82b_end_of_the_end \"1900-01\"^^xsd:gYearMonth .\n```\n### creates E42 from tei:org|place|person\n\ntakes a tei:person|place|org node, extracts their `@xml:id` and all `tei:idno` elements, derives `idoc:E42_Identifier` triples and relates them to a passed in subject via `cidoc:P1_is_identified_by`\n\n```python\nimport lxml.etree as ET\nfrom rdflib import Graph, URIRef, RDF\nfrom acdh_cidoc_pyutils import make_e42_identifiers, NSMAP, CIDOC\nsample = \"\"\"\n<TEI xmlns=\"http://www.tei-c.org/ns/1.0\">\n    <place xml:id=\"DWplace00092\">\n        <placeName type=\"orig_name\">Reval (Tallinn)</placeName>\n        <placeName xml:lang=\"de\" type=\"simple_name\">Reval</placeName>\n        <placeName xml:lang=\"und\" type=\"alt_label\">Tallinn</placeName>\n        <idno type=\"pmb\">https://pmb.acdh.oeaw.ac.at/entity/42085/</idno>\n        <idno type=\"URI\" subtype=\"geonames\">https://www.geonames.org/588409</idno>\n        <idno subtype=\"foobarid\">12345</idno>\n    </place>\n</TEI>\"\"\"\n\ndoc = ET.fromstring(sample)\ng = Graph()\nfor x in doc.xpath(\".//tei:place|tei:org|tei:person|tei:bibl\", namespaces=NSMAP):\n    xml_id = x.attrib[\"{http://www.w3.org/XML/1998/namespace}id\"].lower()\n    item_id = f\"https://foo/bar/{xml_id}\"\n    subj = URIRef(item_id)\n    g.add((subj, RDF.type, CIDOC[\"E53_Place\"]))\n    g += make_e42_identifiers(\n        subj, x, type_domain=\"http://hansi/4/ever\", default_lang=\"it\",\n    )\nprint(g.serialize(format=\"turtle\"))\n# returns\n```\n```ttl\n@prefix ns1: <http://www.cidoc-crm.org/cidoc-crm/> .\n@prefix owl: <http://www.w3.org/2002/07/owl#> .\n@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .\n\n<https://foo/bar/dwplace00092> a ns1:E53_Place ;\n    ns1:P1_is_identified_by <https://foo/bar/dwplace00092/identifier/DWplace00092>,\n        <https://foo/bar/dwplace00092/identifier/idno/0>,\n        <https://foo/bar/dwplace00092/identifier/idno/1>,\n        <https://foo/bar/dwplace00092/identifier/idno/2> ;\n    owl:sameAs <https://pmb.acdh.oeaw.ac.at/entity/42085/>,\n        <https://www.geonames.org/588409> .\n\n<http://hansi/4/ever/idno/URI/geonames> a ns1:E55_Type .\n\n<http://hansi/4/ever/idno/foobarid> a ns1:E55_Type .\n\n<http://hansi/4/ever/idno/pmb> a ns1:E55_Type .\n\n<http://hansi/4/ever/xml-id> a ns1:E55_Type .\n\n<https://foo/bar/dwplace00092/identifier/DWplace00092> a ns1:E42_Identifier ;\n    rdfs:label \"Identifier: DWplace00092\"@it ;\n    rdf:value \"DWplace00092\";\n    ns1:P2_has_type <http://hansi/4/ever/xml-id> .\n\n<https://foo/bar/dwplace00092/identifier/idno/0> a ns1:E42_Identifier ;\n    rdfs:label \"Identifier: https://pmb.acdh.oeaw.ac.at/entity/42085/\"@it ;\n    rdf:value \"https://pmb.acdh.oeaw.ac.at/entity/42085/\";\n    ns1:P2_has_type <http://hansi/4/ever/idno/pmb> .\n\n<https://foo/bar/dwplace00092/identifier/idno/1> a ns1:E42_Identifier ;\n    rdfs:label \"Identifier: https://www.geonames.org/588409\"@it ;\n    rdf:value \"https://www.geonames.org/588409\" \n    ns1:P2_has_type <http://hansi/4/ever/idno/URI/geonames> .\n\n<https://foo/bar/dwplace00092/identifier/idno/2> a ns1:E42_Identifier ;\n    rdfs:label \"Identifier: 12345\"@it ;\n    rdf:value \"12345\";\n    ns1:P2_has_type <http://hansi/4/ever/idno/foobarid> .\n```\n\n### creates appellations from tei:org|place|person\n\ntakes a tei:person|place|org node, extracts `persName, placeName and orgName` texts, `@xml:lang` and custom type values and returns `cidoc:E33_41` and `cidoc:E55` nodes linked via `cidoc:P1_is_identified_by` and `cidoc:P2_has_type`\n\n```python\nimport lxml.etree as ET\nfrom rdflib import Graph, URIRef, RDF\nfrom acdh_cidoc_pyutils import make_appellations, NSMAP, CIDOC\n\nsample = \"\"\"\n<TEI xmlns=\"http://www.tei-c.org/ns/1.0\">\n    <place xml:id=\"DWplace00092\">\n        <placeName type=\"orig_name\">Reval (Tallinn)</placeName>\n        <placeName xml:lang=\"de\" type=\"simple_name\">Reval</placeName>\n        <placeName xml:lang=\"und\" type=\"alt_label\">Tallinn</placeName>\n        <idno type=\"pmb\">https://pmb.acdh.oeaw.ac.at/entity/42085/</idno>\n    </place>\n</TEI>\"\"\"\n\ndoc = ET.fromstring(sample)\ng = Graph()\nfor x in doc.xpath(\".//tei:place|tei:org|tei:person|tei:bibl\", namespaces=NSMAP):\n    xml_id = x.attrib[\"{http://www.w3.org/XML/1998/namespace}id\"].lower()\n    item_id = f\"https://foo/bar/{xml_id}\"\n    subj = URIRef(item_id)\n    g.add((subj, RDF.type, CIDOC[\"E53_Place\"]))\n    g += make_appellations(\n        subj, x, type_domain=\"http://hansi/4/ever\", default_lang=\"it\"\n    )\n\ng.serialize(format=\"ttl\")\n# returns\n```\n```ttl\n@prefix ns1: <http://www.cidoc-crm.org/cidoc-crm/> .\n@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .\n\n<https://foo/bar/dwplace00092> a ns1:E53_Place ;\n    ns1:P1_is_identified_by <https://foo/bar/dwplace00092/appellation/0>,\n        <https://foo/bar/dwplace00092/appellation/1>,\n        <https://foo/bar/dwplace00092/appellation/2> .\n\n<http://hansi/4/ever/alt-label> a ns1:E55_Type ;\n    rdfs:label \"alt_label\" .\n\n<http://hansi/4/ever/orig-name> a ns1:E55_Type ;\n    rdfs:label \"orig_name\" .\n\n<http://hansi/4/ever/simple-name> a ns1:E55_Type ;\n    rdfs:label \"simple_name\" .\n\n<https://foo/bar/dwplace00092/appellation/0> a ns1:E33_E41_Linguistic_Appellation ;\n    rdfs:label \"Reval (Tallinn)\"@it ;\n    ns1:P2_has_type <http://hansi/4/ever/orig-name> .\n\n<https://foo/bar/dwplace00092/appellation/1> a ns1:E33_E41_Linguistic_Appellation ;\n    rdfs:label \"Reval\"@de ;\n    ns1:P2_has_type <http://hansi/4/ever/simple-name> .\n\n<https://foo/bar/dwplace00092/appellation/2> a ns1:E33_E41_Linguistic_Appellation ;\n    rdfs:label \"Tallinn\"@und ;\n    ns1:P2_has_type <http://hansi/4/ever/alt-label> .\n```\n### normalize_string\n\n```python\nfrom acdh_cidoc_pyutils import normalize_string\nstring = \"\"\"\\n\\nhallo\nmein schatz ich liebe    dich\n    du bist         die einzige f\u00fcr mich\n        \"\"\"\nprint(normalize_string(string))\n# returns\n# hallo mein schatz ich liebe dich du bist die einzige f\u00fcr mich\n```\n\n### extract date attributes (begin, end)\n\nexpects typical TEI date attributes like `@when, @when-iso, @notBefore, @notAfter, @from, @to, ...` and returns a tuple containg start- and enddate values. If only `@when or @when-iso` or only `@notBefore or @notAfter` are provided, the returned values are the same, unless the default parameter `fill_missing` is set to `False`. \n\n```python\nfrom lxml.etree import Element\nfrom acdh_cidoc_pyutils import extract_begin_end\n\ndate_string = \"1900-12-12\"\ndate_object = Element(\"{http://www.tei-c.org/ns/1.0}tei\")\ndate_object.attrib[\"when-iso\"] = date_string\nprint(extract_begin_end(date_object))\n\n# returns\n# ('1900-12-12', '1900-12-12')\n\ndate_string = \"1900-12-12\"\ndate_object = Element(\"{http://www.tei-c.org/ns/1.0}tei\")\ndate_object.attrib[\"when-iso\"] = date_string\nprint(extract_begin_end(date_object, fill_missing=False))\n\n# returns\n# ('1900-12-12', None)\n\ndate_object = Element(\"{http://www.tei-c.org/ns/1.0}tei\")\ndate_object.attrib[\"notAfter\"] = \"1900-12-12\"\ndate_object.attrib[\"notBefore\"] = \"1800\"\nprint(extract_begin_end(date_object))\n\n# returns\n# ('1800', '1900-12-12')\n```\n\n## development\n\n* `pip install -r requirements_dev.txt`\n* `flake8` -> linting\n* `coveage run -m pytest` -> runs tests and creates coverage stats\n",
    "bugtrack_url": null,
    "license": "MIT license",
    "summary": "Helper functions for the generation of CIDOC CRMish RDF (from XML/TEI data)",
    "version": "0.24",
    "project_urls": {
        "Homepage": "https://github.com/acdh-oeaw/acdh-cidoc-pyutils"
    },
    "split_keywords": [],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "7e2f9e980f4a6c55530946e177b9145aa0a1673f5e6e076c35cd84b47e668e7e",
                "md5": "e7c67bd0d26fc23e1bf00069ef93be50",
                "sha256": "653a98d3e4b48d4f82f52e15f9c47a22fb6a5c2fc3101a692556138114e23e9a"
            },
            "downloads": -1,
            "filename": "acdh_cidoc_pyutils-0.24-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "e7c67bd0d26fc23e1bf00069ef93be50",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.8",
            "size": 10647,
            "upload_time": "2024-04-14T09:00:56",
            "upload_time_iso_8601": "2024-04-14T09:00:56.425118Z",
            "url": "https://files.pythonhosted.org/packages/7e/2f/9e980f4a6c55530946e177b9145aa0a1673f5e6e076c35cd84b47e668e7e/acdh_cidoc_pyutils-0.24-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "06f44dfde604e360376fae2dc55b18bdf140c6c1bfdeded4c85c39b7efaea2d2",
                "md5": "32fab0c36be863e94657ce6f0eccdc30",
                "sha256": "bb0c6504321ef4abd38667521b9e24a1ea38239f86d0af64ac2be7998d5ab458"
            },
            "downloads": -1,
            "filename": "acdh_cidoc_pyutils-0.24.tar.gz",
            "has_sig": false,
            "md5_digest": "32fab0c36be863e94657ce6f0eccdc30",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.8",
            "size": 13662,
            "upload_time": "2024-04-14T09:00:58",
            "upload_time_iso_8601": "2024-04-14T09:00:58.207447Z",
            "url": "https://files.pythonhosted.org/packages/06/f4/4dfde604e360376fae2dc55b18bdf140c6c1bfdeded4c85c39b7efaea2d2/acdh_cidoc_pyutils-0.24.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2024-04-14 09:00:58",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "acdh-oeaw",
    "github_project": "acdh-cidoc-pyutils",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": true,
    "lcname": "acdh-cidoc-pyutils"
}
        
Elapsed time: 0.24415s