-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmakeWESROntology.py
executable file
·54 lines (37 loc) · 1.59 KB
/
makeWESROntology.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
#!/usr/bin/env python
import sys
import time
import re
import requests
import sdgio
BASE_URL = "https://environmentlive.unep.org/api/"
INDICATOR_LIST_URL = BASE_URL + "indicators.php"
def cleanString(s):
if isinstance(s, float): return str(int(s))
return "\"" + s.encode("ascii", "replace").rstrip() + "\""
def getIndicators():
print("Fetching indicator list.")
r = requests.get(INDICATOR_LIST_URL)
if r.status_code != 200:
raise Exception("Received status code {} while fetching indicator list.".format(r.status_code))
return r.json()
def makeIndicatorClass(indicator):
text = ':Indicator_{} a owl:Class;\n'.format(indicator["indicator_id"].zfill(4))
text += ' rdfs:label "Indicator {}" ;\n'.format(indicator["indicator_id"])
text += ' skos:definition "{}" ;\n'.format(indicator["indicator_short"].rstrip())
if("SDG " in indicator["indicator_short"]):
found_indicators = re.findall(r'\d+\.\w+\.\w+\.?\w?', indicator["indicator_short"])
for fi in found_indicators:
filtered = list(filter(lambda s: s["indicator"] == fi, sdgio_indicators))
if(len(filtered) > 0):
text += ' rdfs:seeAlso <{}>;\n'.format(filtered[0]["uri"])
text += ' rdfs:subClassOf <http://purl.org/seva/WESRIndicator> .\n\n'
return text
print("Loading SDGIO ontology.")
sdgio_indicators = sdgio.SDGIO().getIndicators()
print("Successfully loaded SDGIO ontology")
indicators = getIndicators()
print("Writing indicator turtle.")
f = open("ssio.owl", "w")
for ind in indicators:
f.write(makeIndicatorClass(ind))