-
Notifications
You must be signed in to change notification settings - Fork 0
/
Queries4LinearizedGraph.py
344 lines (258 loc) · 10.1 KB
/
Queries4LinearizedGraph.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
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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
from ast import arg
from rdflib import Graph, RDFS, RDF, URIRef, Namespace, Literal, XSD
from owlrl import DeductiveClosure, RDFS_Semantics
import random, sys
import sys
import os
import pandas as pd
from string import Template
import re
import json
def Graph_Generator_baseline_instances(story):
texts = []
text = story.query("""
CONSTRUCT { ?herolabel ns2:hasTitle ?titlelabel.
?herolabel ns2:hasHouse ?houselabel.
?herolabel ns2:hasOccupation ?joblabel.
?villainlabel ns2:Threatens ?targetlabel.
?herolabel ns2:meetsMentor ?mentorlabel.
?herolabel ns2:powerLearned ?powerlabel.
?event4label ns2:Place4 ?labelplace4.
?event4label ns2:Time4 ?labeltime4.
?herolabel ns2:meetsAlly ?allylabel4.
?event5label ns2:Place5 ?place5label.
?event5label ns2:Time5 ?time5label.
?herolabel ns2:fights ?labelvillain5.
?herolabel ns2:helpedby ?labelally5.
?herolabel ns2:usespower ?labelpower.
?herolabel ns2:saves ?target6label.
?herolabel ns2:celebratesvictory ?celebrationlabel.
?herolabel ns2:partywith ?labelally6.
}
WHERE {ns2:Event_01 ns2:hasTitle ?title.
?title rdfs:label ?titlelabel.
ns2:Event_01 ns1:hasActor ?hero.
?hero rdfs:label ?herolabel.
ns2:Event_01 ns2:hasHouse ?house.
?house rdfs:label ?houselabel.
ns2:Event_01 ns2:hasOccupation ?job.
?job rdfs:label ?joblabel.
ns2:Event_02 ns2:villain ?villain.
?villain rdfs:label ?villainlabel.
ns2:Event_02 ns2:Threatens ?target.
?target rdfs:label ?targetlabel.
ns2:Event_03 ns2:meetsMentor ?mentor.
?mentor rdfs:label ?mentorlabel.
ns2:Event_03 ns2:powerLearned ?power.
?power rdfs:label ?powerlabel.
ns2:Event_04 ns2:meetsAlly ?ally4.
?ally4 rdfs:label ?allylabel4.
ns2:Event_04 ns1:hasPlace ?place4.
?place4 rdfs:label ?labelplace4.
ns2:Event_04 ns1:hasTime ?time4.
?time4 rdfs:label ?labeltime4.
ns2:Event_04 rdfs:label ?event4label.
ns2:Event_05 ns1:hasPlace ?place5.
?place5 rdfs:label ?place5label.
ns2:Event_05 ns1:hasTime ?time5.
?time5 rdfs:label ?time5label.
ns2:Event_05 rdfs:label ?event5label.
ns2:Event_05 ns2:herofights ?villain5.
?villain5 rdfs:label ?labelvillain5.
ns2:Event_05 ns2:helpedBy ?ally5.
?ally5 rdfs:label ?labelally5.
ns2:Event_05 ns2:usepower ?power.
?power rdfs:label ?labelpower.
ns2:Event_06 ns2:saves ?target6.
?target rdfs:label ?target6label.
ns2:Event_06 ns2:celebratesvictory ?celebration.
?celebration rdfs:label ?celebrationlabel.
ns2:Event_06 ns2:partywith ?ally6.
?ally6 rdfs:label ?labelally6.
}
""", initNs={'ns1': 'http://semanticweb.cs.vu.nl/2009/11/sem/', 'ns2': 'http://hero_ontology/'})
texts.append(text)
# for s,p,o in text:
# print(s,p,o)
if len(texts) == 0:
print("WRONG smth in the query I am in q4text line 597")
return text
#FROM HERE ON CONSTRUCTS NEED TO BE FIXED BASED ON NEW SHORT STORY
#ok but time and place is a problem :=)
def Graph_Generator_baseline_class(story):
texts = []
text = story.query("""
CONSTRUCT { ?classhero ns2:hasTitle ?classtitle.
?classhero ns2:hasHouse ?classhouse.
?classhero ns2:hasOccupation ?classjob.
?villainlabel ns2:Threatens ?classtarget.
?classhero ns2:meetsMentor ?classmentor.
?classhero ns2:powerLearned ?classpowerlearnt.
?classhero ns2:meetsAlly ?allylabel4.
?classhero ns2:fights ?classvillain5.
?classhero ns2:helpedby ?classally5.
?classhero ns2:usespower ?classpower.
?classhero ns2:saves ?classtarget6.
?classhero ns2:celebratesvictory ?classcelebration.
?classhero ns2:partywith ?classally6.
}
WHERE { ns2:hasTitle rdfs:range ?classtitle.
ns1:hasActor rdfs:range ?classhero.
ns2:hasHouse rdfs:range ?classhouse.
ns2:hasOccupation rdfs:range ?classjob.
ns2:villain rdfs:range ?villainlabel.
ns2:Threatens rdfs:range ?classtarget.
ns2:meetsMentor rdfs:range ?classmentor.
ns2:powerLearned rdfs:range ?classpowerlearnt.
ns2:meetsAlly rdfs:range ?allylabel4.
ns2:herofights rdfs:range ?classvillain5.
ns2:helpedBy rdfs:range ?classally5.
ns2:usepower rdfs:range ?classpower.
ns2:saves rdfs:range ?classtarget6.
ns2:celebratesvictory rdfs:range ?classcelebration.
ns2:partywith rdfs:range ?classally6.
}
""", initNs={'ns1': 'http://semanticweb.cs.vu.nl/2009/11/sem/', 'ns2': 'http://hero_ontology/'})
texts.append(text)
# for s,p,o in text:
# print(s,p,o)
if len(texts) == 0:
print("WRONG smth in the query I am in q4text line 597")
return text
def Graph_Generator_types(story):
text = story.query("""
SELECT ?s ?p ?o
WHERE {VALUES ?p {rdf:type} ?s ?p ?o.
FILTER(?o != ns1:Core). FILTER(?o != rdfs:Resource). FILTER(?o != ns2:Male) . FILTER(?o != ns2:Female). FILTER(?o != ns1:Event).}
ORDER BY ASC(?s)
""", initNs={'ns1': 'http://semanticweb.cs.vu.nl/2009/11/sem/','ns2': 'http://hero_ontology/'})
return text
def Graph_Generator_event(story):
texts = []
text = story.query("""
SELECT ?s ?p ?o
WHERE {VALUES ?s { ns2:Event_01 ns2:Event_02 ns2:Event_03 ns2:Event_04 ns2:Event_05 ns2:Event_06 ns2:Event_08 ns2:Event_09 ns2:Event_10 ns2:Event_11 ns2:Event_12 } ?s ?p ?o.
FILTER(?o != ns1:Core). FILTER(?o != rdfs:Resource) }
ORDER BY ASC(?s)
""", initNs={'ns2': 'http://hero_ontology/','ns1': 'http://semanticweb.cs.vu.nl/2009/11/sem/'})
texts.append(text)
return text
def Graph_Generator_range(story):
texts = []
text = story.query("""
SELECT ?s ?p ?o
WHERE {{VALUES ?s { ns2:Event_01 ns2:Event_02 ns2:Event_03 ns2:Event_04 ns2:Event_05 ns2:Event_06 ns2:Event_08 ns2:Event_09 ns2:Event_010
ns2:Event_11 ns2:Event_12 } ?s ?p ?o } UNION {VALUES ?p { rdf:type rdfs:range} ?s ?p ?o.
} FILTER(?o != ns1:Core). FILTER(?o != rdfs:Resource)}
ORDER BY ASC(?s)
""", initNs={'ns2': 'http://hero_ontology/','ns1': 'http://semanticweb.cs.vu.nl/2009/11/sem/'})
texts.append(text)
return text
#https://networkedplanet.com/blog/2015/10/16/sparql-construct-101.html resource to understand CONSTRUCT
def Graph_Generator_baseline_instances_old(story):
texts = []
text = story.query("""
CONSTRUCT { ?herolabel ns2:hasTitle ?titlelabel.
?herolabel ns2:hasHouse ?houselabel.
?herolabel ns2:hasOccupation ?joblabel.
?villainlabel ns2:Threatens ?targetlabel.
?herolabel ns2:refusesToFight ?villainlabel.
?herolabel ns2:scaredOf ?enemypowerlabel.
?herolabel ns2:meetsMentor ?mentorlabel.
?herolabel ns2:powerLearned ?heropowerlabel.
?herolabel ns2:travelsTo ?place5label.
?herolabel ns2:meetsAlly ?allylabel.
?allylabel ns2:faces ?villainallylabel.
?herolabel ns2:faces ?villainallylabel.
?herolabel ns2:herofights ?villainlabel.
?herolabel ns2:herousespower ?heropowerlabel.
?villainlabel ns2:enemyusespower ?enemypowerlabel.
?herolabel ns2:saves ?targetlabel.
?herolabel ns2:chasedby ?villainlabel.
?herolabel ns2:celebratesvictory ?celebrationlabel.
?herolabel ns2:partywith ?allylabel.
}
WHERE {ns2:Event_01 ns2:hasTitle ?title.
?title rdfs:label ?titlelabel.
ns2:Event_01 ns1:hasActor ?hero.
?hero rdfs:label ?herolabel.
ns2:Event_01 ns2:hasHouse ?house.
?house rdfs:label ?houselabel.
ns2:Event_01 ns2:hasOccupation ?job.
?job rdfs:label ?joblabel.
ns2:Event_02 ns2:villain ?villain.
?villain rdfs:label ?villainlabel.
ns2:Event_02 ns2:Threatens ?target.
?target rdfs:label ?targetlabel.
ns2:Event_03 ns2:scaredOf ?enemypower.
?enemypower rdfs:label ?enemypowerlabel.
ns2:Event_04 ns2:meetsMentor ?mentor.
?mentor rdfs:label ?mentorlabel.
ns2:Event_04 ns2:powerLearned ?heropower.
?heropower rdfs:label ?heropowerlabel.
ns2:Event_05 ns2:travelsTo ?place5.
?place5 rdfs:label ?place5label.
ns2:Event_06 ns2:meetsAlly ?ally.
?ally rdfs:label ?allylabel.
ns2:Event_06 ns2:faces ?villainally.
?villainally rdfs:label ?villainallylabel.
ns2:Event_06 ns2:faces ?villainally.
?villainally rdfs:label ?villainallylabel.
ns2:Event_12 ns2:celebratesvictory ?celebration.
?celebration rdfs:label ?celebrationlabel.
}
""", initNs={'ns1': 'http://semanticweb.cs.vu.nl/2009/11/sem/','ns2': 'http://hero_ontology/'})
texts.append(text)
print(text)
if len(texts)==0:
print("WRONG smth in the query I am in q4text line 597")
return text
def Graph_Generator_baseline_classes_old(story):
texts = []
text = story.query("""
CONSTRUCT {
?classhero ns2:hasTitle ?classtitle.
?classhero ns2:hasHouse ?classhouse.
?classhero ns2:hasOccupation ?classoccupation.
ns2:Event_01 ns1:hasTime ?classtime.
ns2:Event_01 ns1:hasPlace ?classplace.
?classvillain ns2:Threatens ?classtarget.
?classhero ns2:refusesToFight ?classvillain.
?classhero ns2:scaredOf ?classenemypower.
?classhero ns2:meetsMentor ?classmentor.
?classhero ns2:powerLearned ?classheropower.
?classhero ns2:travelsTo ?classplace5.
?classhero ns2:meetsAlly ?classally.
?classally ns2:faces ?classvillainally.
?classhero ns2:faces ?classvillainally.
?classhero ns2:herofights ?classvillain.
?classhero ns2:herousespower ?classheropower.
?classvillain ns2:enemyusespower ?classenemypower.
?classhero ns2:saves ?classtarget.
?classhero ns2:chasedby ?classvillain.
?classhero ns2:celebratesvictory ?classcelebration.
?classhero ns2:partywith ?classally.
}
WHERE {
ns1:hasActor rdfs:range ?classhero .
ns2:hasTitle rdfs:range ?classtitle .
ns2:hasHouse rdfs:range ?classhouse .
ns2:hasOccupation rdfs:range ?classoccupation .
ns1:hasPlace rdfs:range ?classplace.
ns1:hasTime rdfs:range ?classtime.
ns2:Threatens rdfs:range ?classtarget.
ns2:villain rdfs:range ?classvillain.
ns2:refusesToFight rdfs:range ?classvillain.
ns2:scaredOf rdfs:range ?classenemypower.
ns2:meetsMentor rdfs:range ?classmentor.
ns2:powerLearned rdfs:range ?classheropower.
ns2:travelsTo rdfs:range ?classplace5.
ns2:meetsAlly rdfs:range ?classally.
ns2:faces rdfs:range ?classvillainally.
ns2:faces rdfs:range ?classvillainally.
ns2:herofights rdfs:range ?classvillain.
ns2:celebratesvictory rdfs:range ?classcelebration.
}
""", initNs={'ns1': 'http://semanticweb.cs.vu.nl/2009/11/sem/','ns2': 'http://hero_ontology/'})
texts.append(text)
return text