-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathquickstart.py
More file actions
49 lines (38 loc) · 1.96 KB
/
Copy pathquickstart.py
File metadata and controls
49 lines (38 loc) · 1.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
"""Minimal, runnable example — a tiny generic ontology + a small spec table.
python examples/minimal/quickstart.py
Requires ``rdflib`` (``pip install -e ".[dev]"`` installs it). Demonstrates:
declare a spec table, parse an extension TTL that imports a core, and generate
typed JSON configs holding the extracted named subclasses + individuals.
"""
import json
import tempfile
from pathlib import Path
from ontology_config_generator import ConfigSpec, generate_configs
HERE = Path(__file__).resolve().parent
EXTENSION = HERE / "ontology" / "extension.ttl"
# 1) Declare YOUR spec table: which file binds to which class, under which key.
# (Plain dicts work too — see spec_table.json — these are the typed form.)
spec_table = [
ConfigSpec(filename="regions.json", extract={"regions": "Region"}),
ConfigSpec(filename="categories.json", extract={"categories": "Category"}),
# A "static-only" config the TTL has nothing to extract for keeps its shape.
ConfigSpec(filename="glossary.json", static={"terms": [], "synonyms": []}),
]
with tempfile.TemporaryDirectory() as d:
out = Path(d)
# 2) Generate. The namespace defaults to http://example.org/ontology/core/
# (the IRI the bound class local-names — "Region", "Category" — resolve to).
status = generate_configs(spec_table, EXTENSION, out)
print("generated:", status)
# 3) Inspect what was extracted.
regions = json.loads((out / "regions.json").read_text())
print("\nregions.json:")
for r in regions["regions"]:
print(f" - {r['id']}: {r['label']} — {r['description']}")
print(" provenance:", regions["_generated"])
categories = json.loads((out / "categories.json").read_text())
print("\ncategories.json:")
for c in categories["categories"]:
print(f" - {c['id']}: {c['label']}")
glossary = json.loads((out / "glossary.json").read_text())
print("\nglossary.json (static-only):", {k: v for k, v in glossary.items() if k != "_generated"})