Skip to content

Commit f0ee32c

Browse files
committed
add notebook with first examples of converting ismi dates to undate.
1 parent 0e36e45 commit f0ee32c

2 files changed

Lines changed: 181 additions & 0 deletions

File tree

Lines changed: 179 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,179 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "markdown",
5+
"id": "0fa36628-ccf2-4977-8c4c-e0a85e2b37b6",
6+
"metadata": {},
7+
"source": [
8+
"# Working with ISMI project dates"
9+
]
10+
},
11+
{
12+
"cell_type": "markdown",
13+
"id": "ffd4b544-8957-494e-9e09-b703d68bb7df",
14+
"metadata": {},
15+
"source": [
16+
"## Load date samples from RDF"
17+
]
18+
},
19+
{
20+
"cell_type": "code",
21+
"execution_count": null,
22+
"id": "a8a36e7f-6057-44d1-8466-6709910d4249",
23+
"metadata": {},
24+
"outputs": [],
25+
"source": [
26+
"from rdflib import Graph, RDF, URIRef\n",
27+
"from rdflib.namespace import Namespace, RDFS\n",
28+
"from undate.undate import Undate\n",
29+
"\n",
30+
"# additional RDF namespaces\n",
31+
"crmNs = Namespace('http://www.cidoc-crm.org/cidoc-crm/')\n",
32+
"\n",
33+
"g = Graph()\n",
34+
"g.bind('crm', crmNs)\n",
35+
"# load ISMI RDF sample data\n",
36+
"g.parse('data/ismi-crm-date-samples.ttl')\n",
37+
"# check: number of triples\n",
38+
"len(g)"
39+
]
40+
},
41+
{
42+
"cell_type": "code",
43+
"execution_count": null,
44+
"id": "c940ca2b-b369-4511-8dc9-420bdaeb3e65",
45+
"metadata": {},
46+
"outputs": [],
47+
"source": [
48+
"date_uris = [u for u in g.subjects(RDF.type, crmNs['E52_Time-Span'])]\n",
49+
"\n",
50+
"for uri in date_uris:\n",
51+
" q = '''SELECT ?uri ?label ?note \n",
52+
" WHERE { \n",
53+
" ?uri crm:P3_has_note ?note ;\n",
54+
" crm:P1_is_identified_by / rdfs:label ?label .\n",
55+
" } limit 10'''\n",
56+
" res = g.query(q, initBindings={'uri': uri})\n",
57+
" for r in res:\n",
58+
" print(f\"uri={str(uri)} label={r.label} note={r.note}\")"
59+
]
60+
},
61+
{
62+
"cell_type": "markdown",
63+
"id": "16361060-657f-431c-b70f-9101d550aa38",
64+
"metadata": {},
65+
"source": [
66+
"## Convert RDF dates to Undate"
67+
]
68+
},
69+
{
70+
"cell_type": "code",
71+
"execution_count": null,
72+
"id": "e443b974-930b-4a5d-8f21-641b4556b159",
73+
"metadata": {},
74+
"outputs": [],
75+
"source": [
76+
"from undate.date import DatePrecision, Date\n",
77+
"import datetime\n",
78+
"\n",
79+
"uri = date_uris[1]\n",
80+
"\n",
81+
"#\n",
82+
"# read date type\n",
83+
"#\n",
84+
"date_type = None\n",
85+
"for date_type_uri in g.objects(uri, crmNs.P2_has_type):\n",
86+
" for dt in ['day', 'year', 'range']:\n",
87+
" if str(date_type_uri) == 'http://content.mpiwg-berlin.mpg.de/ns/ismi/type/date/' + dt:\n",
88+
" date_type = dt\n",
89+
"\n",
90+
"if not date_type:\n",
91+
" raise RuntimeError(f\"Unknown datetype URI {date_type_uri}\")\n",
92+
"\n",
93+
"#\n",
94+
"# read label and calendar\n",
95+
"#\n",
96+
"date_label_uri = next(g.objects(uri, crmNs.P1_is_identified_by))\n",
97+
"date_label = str(next(g.objects(date_label_uri, RDFS.label)))\n",
98+
"for date_label_calendar_uri in g.objects(date_label_uri, crmNs.P2_has_type):\n",
99+
" for ct in ['gregorian', 'julian', 'islamic']:\n",
100+
" if str(date_label_calendar_uri) == 'http://content.mpiwg-berlin.mpg.de/ns/ismi/type/calendar/' + ct:\n",
101+
" calendar_type = ct\n",
102+
"\n",
103+
"if not calendar_type:\n",
104+
" raise RuntimeError(f\"Unknown calendar type URI {date_label_calendar_uri}\")\n",
105+
"\n",
106+
"#\n",
107+
"# create undate\n",
108+
"#\n",
109+
"if date_type == 'day':\n",
110+
" xsd_date = next(g.objects(uri, crmNs.P82_at_some_time_within))\n",
111+
" date = Undate.parse(str(xsd_date), 'ISO8601')\n",
112+
" date.precision = DatePrecision.DAY\n",
113+
" date.label = date_label\n",
114+
"\n",
115+
"if date_type == 'year':\n",
116+
" xsd_date_from = next(g.objects(uri, crmNs.P82a_begin_of_the_begin))\n",
117+
" xsd_date_until = next(g.objects(uri, crmNs.P82b_end_of_the_end))\n",
118+
" date_from = datetime.date.fromisoformat(str(xsd_date_from))\n",
119+
" if calendar_type == 'gregorian':\n",
120+
" # this should be fine\n",
121+
" date = Undate(year=date_from.year)\n",
122+
"\n",
123+
" else:\n",
124+
" # create day precision Undate from end date\n",
125+
" date = Undate.parse(str(xsd_date_until), 'ISO8601')\n",
126+
" # change earliest date\n",
127+
" date.earliest = Date(year=date_from.year, month=date_from.month, day=date_from.day)\n",
128+
"\n",
129+
" # change precision and label\n",
130+
" date.precision = DatePrecision.DAY\n",
131+
" date.label = date_label\n",
132+
"\n",
133+
"if date_type == 'range':\n",
134+
" xsd_date_from = next(g.objects(uri, crmNs.P82a_begin_of_the_begin))\n",
135+
" xsd_date_until = next(g.objects(uri, crmNs.P82b_end_of_the_end))\n",
136+
" # create day precision Undate from start date\n",
137+
" date = Undate.parse(str(xsd_date_from), 'ISO8601')\n",
138+
" # change latest date\n",
139+
" date_until = datetime.date.fromisoformat(str(xsd_date_until))\n",
140+
" date.latest = Date(year=date_until.year, month=date_until.month, day=date_until.day)\n",
141+
" # change precision and label\n",
142+
" date.precision = DatePrecision.DAY\n",
143+
" date.label = date_label\n",
144+
"\n",
145+
"\n",
146+
"print(f\"{date_label=} {date_type=} {calendar_type=} {date=}\")"
147+
]
148+
},
149+
{
150+
"cell_type": "code",
151+
"execution_count": null,
152+
"id": "742ba275-7de6-461b-8891-6f06dbdd89a0",
153+
"metadata": {},
154+
"outputs": [],
155+
"source": []
156+
}
157+
],
158+
"metadata": {
159+
"kernelspec": {
160+
"display_name": "Python 3 (ipykernel)",
161+
"language": "python",
162+
"name": "python3"
163+
},
164+
"language_info": {
165+
"codemirror_mode": {
166+
"name": "ipython",
167+
"version": 3
168+
},
169+
"file_extension": ".py",
170+
"mimetype": "text/x-python",
171+
"name": "python",
172+
"nbconvert_exporter": "python",
173+
"pygments_lexer": "ipython3",
174+
"version": "3.12.5"
175+
}
176+
},
177+
"nbformat": 4,
178+
"nbformat_minor": 5
179+
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
jupyterlab~=4.3.1
2+
rdflib~=7.1.1

0 commit comments

Comments
 (0)