-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathonto.py
executable file
·57 lines (43 loc) · 1.63 KB
/
onto.py
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
50
51
52
53
54
55
56
57
#!/usr/bin/env python
# encoding: utf-8
from rdflib.serializer import Serializer
import json
import rdflib
import skosify
import sys
# see docs:
# https://semantic-web.com/2017/08/21/standard-build-knowledge-graphs-12-facts-skos/
# https://skosify.readthedocs.io/en/latest/
if __name__ == "__main__":
# test with:
# ./onto.py adrf.ttl rcc.ttl
# load the graph
ttl_paths = sys.argv[1:]
graph = rdflib.Graph()
for ttl_file in ttl_paths:
print("loading TTL file: {}".format(ttl_file))
graph.parse(ttl_file, format="n3")
# an example lookup
# https://github.com/Coleridge-Initiative/adrf-onto/wiki/Vocabulary#Catalog
print(list(graph[::rdflib.Literal("ADRF Data Catalog")]))
# enumerate the S/V/O relationships within the graph
for subj, pred, obj in graph:
print(subj, pred, obj)
# transform graph into JSON-LD
with open("vocab.json", "r") as f:
context = json.load(f)
with open("tmp.json", "wb") as f:
f.write(graph.serialize(format="json-ld", context=context, indent=2))
# convert, extend, and check the SKOS vocabulary used
config = skosify.config("skosify.cfg")
voc = skosify.skosify(graph, **config)
voc.serialize(destination="tmp.ttl", format="n3")
# validate the inference rules
skosify.infer.skos_related(graph)
skosify.infer.skos_topConcept(graph)
skosify.infer.skos_hierarchical(graph, narrower=True)
skosify.infer.skos_transitive(graph, narrower=True)
skosify.infer.rdfs_classes(graph)
skosify.infer.rdfs_properties(graph)
# for the humans watching, print a note that all steps completed
print("OK")