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}")