-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
195 lines (140 loc) · 5.3 KB
/
Copy pathmain.py
File metadata and controls
195 lines (140 loc) · 5.3 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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
import pathlib
import markdown
import pandas
import pydash
import rdflib
from flask import Flask, render_template
from flask_frozen import Freezer
def extract_entity(uriref, prop):
entity = [o for s, p, o in graph.triples((uriref, prop, None))]
if not len(entity):
raise Exception(f"{prop} not found for {uriref}.")
entity = entity[0]
entity_label = extract_text(entity, rdflib.RDFS.label)
return {"entity_link": entity, "entity_label": entity_label}
def extract_text(uriref, prop):
text = [o for s, p, o in graph.triples((uriref, prop, None))]
if not len(text):
raise Exception(f"{prop} not found for {uriref}.")
return text[0]
# load triples.
graph = rdflib.Graph().parse(pathlib.Path.cwd() / "public.ttl")
graph += rdflib.Graph().parse(pathlib.Path.cwd() / "ontology.ttl")
# external ontologies.
graph += rdflib.Graph().parse("http://www.w3.org/2000/01/rdf-schema")
graph += rdflib.Graph().parse("http://www.w3.org/2002/07/owl")
# nodes.
nodes = pydash.uniq([s for s, p, o in graph.triples((None, None, None))])
nodes = [x for x in nodes if type(x) is rdflib.term.URIRef]
nodes = [x for x in nodes if "paulduchesne.github.io" in x]
node_array = {x: dict() for x in nodes}
# node labels.
for x in node_array.keys():
node_array[x]["label"] = extract_text(x, rdflib.RDFS.label)
# node type.
for x in node_array.keys():
node_array[x]["type"] = extract_entity(x, rdflib.RDF.type)
# node comment.
for x in node_array.keys():
node_array[x]["comment"] = markdown.markdown(extract_text(x, rdflib.RDFS.comment), extensions=['fenced_code'])
# node statements.
extant_props = [rdflib.RDFS.label, rdflib.RDF.type, rdflib.RDFS.comment]
for x in node_array.keys():
props = [p for s, p, o in graph.triples((x, None, None))]
props = [p for p in props if p not in extant_props]
df = pandas.DataFrame(
columns=[
"type",
"property_link",
"property_label",
"entity_link",
"entity_label",
]
)
for p in sorted(props):
for a, b, c in graph.triples((x, p, None)):
if pathlib.Path(p).name == "wikidataIdentifier":
df.loc[len(df)] = [
"entity",
p,
extract_text(p, rdflib.RDFS.label),
f"https://www.wikidata.org/wiki/{c}",
str(c),
]
elif type(c) is rdflib.term.URIRef:
df.loc[len(df)] = [
"entity",
p,
extract_text(p, rdflib.RDFS.label),
c,
str(extract_text(c, rdflib.RDFS.label)),
]
elif type(c) is rdflib.term.Literal:
df.loc[len(df)] = [
"literal",
p,
extract_text(p, rdflib.RDFS.label),
"",
str(c),
]
else:
raise Exception("Type unknown.")
df = df.drop_duplicates()
df.sort_values(
by=["property_label", "entity_label"],
key=lambda col: col.str.lower(),
inplace=True,
)
node_array[x]["statements"] = df.to_dict("records")
# define app.
app = Flask(__name__)
app.config['FREEZER_RELATIVE_URLS'] = True
# index page.
@app.route("/")
def index():
classes = [
{"class_link": s}
for s, p, o in graph.triples((None, rdflib.RDF.type, rdflib.OWL.Class))
]
classes = [x for x in classes if "www.w3.org" not in x["class_link"]]
# TODO: figure out how to generalise to parent classes.
# Rather than "Event X" and "Event Y", "Event".
for c in classes:
c["class_label"] = extract_text(c["class_link"], rdflib.RDFS.label)
instances = [
{"entity_link": s}
for s, p, o in graph.triples((None, rdflib.RDF.type, c["class_link"]))
]
for i in instances:
i["entity_label"] = str(extract_text(i["entity_link"], rdflib.RDFS.label))
c["instances"] = sorted(instances, key=lambda x: x["entity_label"])
classes = [x for x in classes if len(x['instances'])]
classes = sorted(classes, key=lambda x: x["class_label"])
return render_template("index.html", data=classes)
# resource pages.
@app.route("/resource/<resource>.html")
def resource(resource):
node = f"https://paulduchesne.github.io/state/resource/{resource}"
return render_template("resource.html", data=node_array[rdflib.URIRef(node)])
# ontology pages.
@app.route("/ontology/<ontology>.html")
def ontology(ontology):
node = f"https://paulduchesne.github.io/state/ontology/{ontology}"
return render_template("ontology.html", data=node_array[rdflib.URIRef(node)])
# flask freezer.
freezer = Freezer(app)
# render pages.
@freezer.register_generator
def resource_generator():
for n in node_array.keys():
for t in ["resource", "ontology"]:
if t in str(n):
p = pathlib.Path(n).name
yield (t, {t: p})
# transform README to index page.
# with open(pathlib.Path.cwd() / 'README.md') as index_in:
# index_in = index_in.read()
# with open(pathlib.Path.cwd() / 'templates' / 'index.html', 'w') as index_out:
# index_out.write(markdown.markdown(index_in))
if __name__ == "__main__":
freezer.freeze()