-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathqueries.py
277 lines (228 loc) · 7.12 KB
/
queries.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
import rdflib
class Queries:
def __init__(self):
self.g = rdflib.Graph()
self.g.load("data/three_kingdoms.rdf", format="turtle")
def no_res(self):
return print("对不起,搜索没有结果。")
def birthplace(self, subj, place):
qres = self.g.query(
"""SELECT DISTINCT ?name ?ancient_birthplace ?modern_birthplace
WHERE {
?person foaf:ancient_birthplace ?ancient_birthplace .
?person foaf:modern_birthplace ?modern_birthplace .
?person foaf:name ?name .
}""")
for row in qres:
name = row[0]
ancient_birthplace = row[1]
modern_birthplace = row[2]
if subj:
if str(name) == subj:
print("{}的古代籍贯是{},现代籍贯是{}.".format(name, ancient_birthplace, modern_birthplace))
elif place:
if str(ancient_birthplace) == place or str(modern_birthplace) == place:
print("{}是在{}出生的,现代的{}.".format(name, ancient_birthplace, modern_birthplace))
else:
return self.no_res()
def loyalty(self, subj, pred):
qres = self.g.query(
"""SELECT DISTINCT ?charname ?leadername
WHERE {
?person foaf:loyalty ?leadername .
?person foaf:name ?charname .
}""")
characters = []
for row in qres:
subject = row[0]
predicate = row[1]
if not subj:
if str(predicate) == pred:
characters.append(subject)
elif subj:
if str(subject) == subj:
print("{}忠于{}.".format(subject, predicate))
else:
return self.no_res()
if len(characters) > 0:
loyal_subjects = ', '.join(characters)
num_characters = len(characters)
print("{}被{}个人物效忠:{}".format(pred, num_characters, loyal_subjects))
def lifespan(self, subj):
qres = self.g.query(
"""SELECT DISTINCT ?charname ?lifespan
WHERE{
?person foaf:birth ?lifespan .
?person foaf:name ?charname .
}""")
for row in qres:
name = row[0]
lifespan = row[1]
if str(name) == subj:
print("{}的生命时间: {}。".format(name, lifespan))
else:
return self.no_res()
def gender(self, subj, gender):
qres = self.g.query(
"""SELECT DISTINCT ?charname ?gender
WHERE{
?person foaf:gender ?gender .
?person foaf:name ?charname .
}""")
for row in qres:
name = row[0]
gen = row[1]
if subj:
if str(name) == subj:
print("{}是一个{}生.".format(name, gen))
elif gender:
if str(gen) == gender:
print("{}是一个{}生.".format(name, gen))
else:
return self.no_res()
def about_character(self, subj):
qres = self.g.query(
"""SELECT DISTINCT ?charname ?intro
WHERE{
?person foaf:intro ?intro .
?person foaf:name ?charname .
}""")
for row in qres:
name = row[0]
intro = row[1]
if subj:
if str(name) == subj:
print("{}的简介: {}".format(subj, intro))
else:
return self.no_res()
def fictional(self, subj, fictional):
qres = self.g.query(
"""SELECT DISTINCT ?charname ?fictional
WHERE{
?person foaf:fictional ?fictional .
?person foaf:name ?charname .
}
""")
for row in qres:
name = row[0]
fict = row[1]
if subj:
if str(name) == subj:
print("{}是{}.".format(name, fict))
elif fictional:
if str(fict) == fictional:
print("{}是{}.".format(name, fict))
else:
return self.no_res()
def which_chapter(self, subj, chapt):
qres = self.g.query(
"""SELECT DISTINCT ?eventname ?chapter
WHERE{
?event foaf:chapter ?chapter .
?event foaf:event_name ?eventname .
}""")
for row in qres:
event_name = row[0]
chapter = row[1]
if subj:
if str(event_name) == subj:
print("{}在{}.".format(event_name, chapter))
elif chapt:
if str(chapter) == chapt:
print("{}在{}.".format(event_name, chapter))
else:
return self.no_res()
def event_info(self, subj):
qres = self.g.query(
"""SELECT DISTINCT ?eventname ?info
WHERE{
?event foaf:description ?info .
?event foaf:event_name ?eventname .
}""")
for row in qres:
event_name = row[0]
description = row[1]
if subj:
if str(event_name) == subj:
print("{}的简介: {}".format(event_name, description))
else:
return self.no_res()
def event_history(self, subj):
qres = self.g.query(
"""SELECT DISTINCT ?eventname ?history
WHERE{
?event foaf:history ?history .
?event foaf:event_name ?eventname .
}""")
for row in qres:
event_name = row[0]
history = row[1]
if subj:
if str(event_name) == subj:
print("{}的历史: {}".format(event_name, history))
else:
return self.no_res()
def event_location(self, subj, loc):
qres = self.g.query(
"""SELECT DISTINCT ?eventname ?location
WHERE{
?event foaf:location ?location .
?event foaf:event_name ?eventname .
}""")
for row in qres:
event_name = row[0]
location = row[1]
if subj:
if str(event_name) == subj:
print("{}是在{}发生的。".format(event_name, location))
elif loc:
if str(location) == loc:
print("{}是在{}发生的。".format(event_name, location))
else:
return self.no_res()
def event_time(self, subj, year):
qres = self.g.query(
"""SELECT DISTINCT ?eventname ?year
WHERE{
?event foaf:time ?year .
?event foaf:event_name ?eventname .
}""")
for row in qres:
event_name = row[0]
time = row[1]
if subj:
if str(event_name) == subj:
print("{}是在{}发生的。".format(event_name, time))
elif year:
if str(time) == year:
print("{}是在{}发生的。".format(event_name, time))
else:
return self.no_res()
def event_involved(self, subj, char):
qres = self.g.query(
"""SELECT DISTINCT ?eventname ?char
WHERE{
?event foaf:involved ?involved .
?event foaf:event_name ?eventname .
?involved foaf:name ?char .
}
""")
people_involved = []
events_involved_in = []
for row in qres:
event = row[0]
involved = row[1]
if subj:
if str(event) == subj:
people_involved.append(involved)
elif char:
if str(involved) == char:
events_involved_in.append(event)
people_involved_string = ', '.join(people_involved)
events_involved_in_string = ', '.join(events_involved_in)
if len(people_involved) > 0:
print("{}事件涉及了{}个人物: {}".format(event, len(people_involved), people_involved_string))
elif len(events_involved_in) > 0:
print("{}参与了{}个事件: {}".format(char, len(events_involved_in), events_involved_in_string))
else:
return self.no_res()