forked from geological-survey-of-queensland/vocabularies
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvocab_listing_by_theme.py
51 lines (45 loc) · 1.36 KB
/
vocab_listing_by_theme.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
from os.path import join, dirname
import os
import glob
from pathlib import Path
from rdflib import Graph
from rdflib.namespace import RDF, SKOS
import pickle
VOCABS_DIR = Path(__file__).parent.parent.resolve().glob("vocabularies-*/*.ttl")
VOCABS_PICKLE = Path(__file__).parent / "all_vocabs.pickle"
if Path(VOCABS_PICKLE).is_file():
print("reading all vocabs from pickle file")
with open(VOCABS_PICKLE, "rb") as f:
g = pickle.load(f)
else:
print("no pickle file so creating it from RDF source files")
g = Graph()
for vocab in VOCABS_DIR:
print(f"parsing {vocab}")
g.parse(vocab)
print(len(g))
print(f"pickleing all vocabs to {VOCABS_PICKLE}")
pickle.dump(g, VOCABS_PICKLE.open("wb"))
print(len(g))
q = """
PREFIX dcat: <http://www.w3.org/ns/dcat#>
PREFIX skos: <http://www.w3.org/2004/02/skos/core#>
PREFIX themes: <https://linked.data.gov.au/def/fsdf/themes/>
SELECT ?cs ?theme
WHERE {
?cs a skos:ConceptScheme ;
dcat:theme ?theme ;
.
VALUES ?theme {
themes:buildings-and-settlements
themes:geocoded-addressing
themes:place-names
themes:positioning
themes:spatial
themes:transport
}
}
ORDER BY ?theme ?cs
"""
for r in g.query(q):
print(f'{r["cs"]}: {r["theme"]}')