File size: 2,019 Bytes
6256b19
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import json, pathlib
from rdflib import Graph, RDFS, RDF, OWL, Namespace

ROOT = pathlib.Path("/Users/fabio/projects/qwen-ies-ft")
IES = Namespace("http://ies.data.gov.uk/ontology/ies4#")
onto = ROOT/"IES4"/"IES Specification Docs"/"ies4.ttl"

g = Graph()
g.parse(onto, format="turtle")

def local(u): return str(u).split("#")[-1]

classes = {}
for s in set(g.subjects(RDF.type, RDFS.Class)) | set(g.subjects(RDF.type, OWL.Class)):
    if not str(s).startswith(str(IES)): continue
    ln = local(s)
    label = g.value(s, RDFS.label)
    comment = g.value(s, RDFS.comment)
    supers = [local(o) for o in g.objects(s, RDFS.subClassOf) if str(o).startswith(str(IES))]
    classes[ln] = {"label": str(label) if label else ln,
                   "comment": str(comment) if comment else "",
                   "subClassOf": supers}

DP = OWL.DatatypeProperty
props = {}
for s in (set(g.subjects(RDF.type, OWL.ObjectProperty)) | set(g.subjects(RDF.type, RDF.Property))
          | set(g.subjects(RDF.type, DP))):
    if not str(s).startswith(str(IES)): continue
    ln = local(s)
    comment = g.value(s, RDFS.comment)
    dom = [local(o) for o in g.objects(s, RDFS.domain) if str(o).startswith(str(IES))]
    rng = [local(o) for o in g.objects(s, RDFS.range) if str(o).startswith(str(IES))]
    props[ln] = {"comment": str(comment) if comment else "", "domain": dom, "range": rng}

# authoritative membership set: EVERY ies: term appearing as a subject in the ontology
all_terms = sorted({local(s) for s in set(g.subjects()) if str(s).startswith(str(IES))})

vocab = {"classes": classes, "properties": props,
         "class_names": sorted(classes), "property_names": sorted(props),
         "all_terms": all_terms}
(ROOT/"data"/"vocab.json").write_text(json.dumps(vocab, indent=2))
print(f"classes={len(classes)} properties={len(props)} all_terms={len(all_terms)}")
S = set(all_terms)
for t in ["representationValue","inPeriod","isStateOf","hasName","BoundingState","inScheme"]:
    print(f"  {t} in all_terms? {t in S}")