-
Notifications
You must be signed in to change notification settings - Fork 0
/
sdgio.py
85 lines (63 loc) · 2.78 KB
/
sdgio.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
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
import rdflib
from rdflib.resource import Resource
class SDGIO():
# Parse the SDGIO Ontology
def __init__(self):
self.sdgio_graph = rdflib.Graph().parse("http://purl.unep.org/sdg/sdgio.owl", format='xml')
# Return parsed sdgio graph
def getGraph(self):
return self.sdgio_graph
# Return a list of goals sorted by uri
def getGoals(self):
goal_results = self.sdgio_graph.query(
"""PREFIX sdg: <http://purl.unep.org/sdg/>
PREFIX oboInOwl: <http://www.geneontology.org/formats/oboInOwl#>
SELECT ?uri ?goal_long ?goal_short ?label
WHERE {
?uri rdf:type <http://purl.unep.org/sdg/SDGIO_00000000>.
?uri oboInOwl:hasExactSynonym ?goal_long.
?uri oboInOwl:hasExactSynonym ?goal_short.
?uri rdfs:label ?label.
FILTER(?goal_long != ?goal_short).
FILTER(?goal_long > ?goal_short).
}""")
goals = list(map(lambda g: {"uri": ("%s" % g[0]), "goal_long": ("%s" % g[1]), "goal_short": ("%s" % g[2]), "description": ("%s" % g[3])}, goal_results))
return sorted(goals, key=lambda g: g["uri"])
# Return a list of targets sorted by uri
def getTargets(self):
target_results = self.sdgio_graph.query(
"""PREFIX sdg: <http://purl.unep.org/sdg/>
SELECT ?uri ?target ?label
WHERE {
?uri rdf:type <http://purl.unep.org/sdg/SDGIO_00000001>.
?uri sdg:SDGIO_00000074 ?target.
?uri rdfs:label ?label.
}""")
targets = list(map(lambda t: {"uri": ("%s" % t[0]), "target": ("%s" % t[1]), "description": ("%s" % t[2])}, target_results))
return sorted(targets, key=lambda t: t["uri"])
# Return a list of goals sorted by uri
def getIndicators(self):
indicator_results = self.sdgio_graph.query(
"""PREFIX sdg: <http://purl.unep.org/sdg/>
SELECT ?uri ?indicator ?label
WHERE {
?uri rdfs:subClassOf <http://purl.unep.org/sdg/SDGIO_00000003>.
?uri sdg:SDGIO_00000242 ?indicator.
?uri rdfs:label ?label.
}""")
indicators = list(map(lambda i: {"uri": ("%s" % i[0]), "indicator": ("%s" % i[1]), "description": ("%s" % i[2])}, indicator_results))
return sorted(indicators, key=lambda i: i["uri"])
if __name__ == "__main__":
sdgio = SDGIO()
goals = sdgio.getGoals()
targets = sdgio.getTargets()
indicators = sdgio.getIndicators()
print("Sample Goal:")
print(goals[0])
print()
print("Sample Target:")
print(targets[0])
print()
print("Sample Goal:")
print(indicators[0])
print()