-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathcompile.py
More file actions
103 lines (78 loc) · 3.65 KB
/
compile.py
File metadata and controls
103 lines (78 loc) · 3.65 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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
import pathlib
import rdflib
def convert_vocabulary(input_file):
''' Convert OWL file to SKOS. '''
g = rdflib.Graph().parse(pathlib.Path.cwd() / 'ontology' / input_file)
skos_graph = rdflib.Graph()
# identify concept scheme.
concept_scheme = None
for s,p,o in g.triples((None, rdflib.RDF.type, rdflib.OWL.Class)):
if not len([x for x in g.triples((s, rdflib.RDFS.subClassOf, None))]):
concept_scheme = s
if not concept_scheme:
raise Exception('Concept scheme should have been detected.')
# parse triples.
for s,p,o in g.triples((None, rdflib.RDF.type, rdflib.OWL.Class)):
if not len([x for x in g.triples((s, rdflib.RDFS.subClassOf, None))]):
skos_graph.add((s, rdflib.RDF.type, rdflib.SKOS.ConceptScheme))
else:
skos_graph.add((s, rdflib.RDF.type, rdflib.SKOS.Concept))
for a,b,c in g.triples((s, rdflib.RDFS.label, None)):
skos_graph.add((s, rdflib.SKOS.prefLabel, c))
for a,b,c in g.triples((s, rdflib.DC.description, None)):
skos_graph.add((s, rdflib.SKOS.definition, c))
for a,b,c in g.triples((s, rdflib.RDFS.subClassOf, None)):
skos_graph.add((s, rdflib.SKOS.broader, c))
for a,b,c in g.triples((s, rdflib.RDFS.subClassOf, None)):
skos_graph.add((s, rdflib.SKOS.inScheme, concept_scheme))
skos_graph.serialize(
destination=pathlib.Path.cwd() / 'vocabulary' / input_file,
format="longturtle",
)
# convert ontology vocabularies to SKOS.
for vocab in [
'fiafcore-base.ttl',
'fiafcore-broadcaststandard.ttl',
'fiafcore-colourstandard.ttl',
'fiafcore-country.ttl',
'fiafcore-element.ttl',
'fiafcore-form.ttl',
'fiafcore-format.ttl',
'fiafcore-genre.ttl',
'fiafcore-language.ttl',
'fiafcore-soundstandard.ttl',
'fiafcore-status.ttl']:
convert_vocabulary(vocab)
# build single turtle file from ontology.
g = rdflib.Graph()
ontology_path = pathlib.Path.cwd() / 'ontology'
ttl_files = [x for x in ontology_path.iterdir() if x.suffix == ".ttl"]
for c in ttl_files:
g += rdflib.Graph().parse(c)
# add ontology triples.
g += rdflib.Graph().parse(
data="""
@prefix dc: <http://purl.org/dc/elements/1.1/> .
@prefix dcterms: <http://purl.org/dc/terms/> .
@prefix owl: <http://www.w3.org/2002/07/owl#> .
<https://dev.fiafcore.org/> a owl:Ontology ;
dc:title "FIAFcore" ;
owl:versionInfo "dev-2.0.0" ;
dcterms:license "https://creativecommons.org/licenses/by/4.0/" ;
dc:creator "FIAF Cataloguing and Documentation Commission" ;
. """,
format="ttl",
)
# add labels for borrowed elements.
g += rdflib.Graph().add((rdflib.DC.description, rdflib.RDFS.label, rdflib.Literal("Description")))
g += rdflib.Graph().add((rdflib.DC.source, rdflib.RDFS.label, rdflib.Literal("Source")))
g += rdflib.Graph().add((rdflib.OWL.Class, rdflib.RDFS.label, rdflib.Literal("Class")))
g += rdflib.Graph().add((rdflib.OWL.DatatypeProperty, rdflib.RDFS.label, rdflib.Literal("Datatype Property")))
g += rdflib.Graph().add((rdflib.OWL.ObjectProperty, rdflib.RDFS.label, rdflib.Literal("Object Property")))
g += rdflib.Graph().add((rdflib.RDFS.domain, rdflib.RDFS.label, rdflib.Literal("Domain")))
g += rdflib.Graph().add((rdflib.RDFS.label, rdflib.RDFS.label, rdflib.Literal("Label")))
g += rdflib.Graph().add((rdflib.RDFS.range, rdflib.RDFS.label, rdflib.Literal("Range")))
g += rdflib.Graph().add((rdflib.RDFS.subClassOf, rdflib.RDFS.label, rdflib.Literal("Subclass Of")))
# write ontology graph to disk.
g.serialize(destination=str(pathlib.Path.cwd() / "fiafcore.ttl"), format="longturtle")
print(len(g), "triples.")