-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQnA.py
More file actions
320 lines (258 loc) · 8.86 KB
/
QnA.py
File metadata and controls
320 lines (258 loc) · 8.86 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
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
#!/usr/bin/python3
import sys
import requests
import re
import spacy
nlp = spacy.load('en_core_web_md')
url = 'https://query.wikidata.org/sparql'
api_url = 'https://www.wikidata.org/w/api.php'
permutation = [(0, 0), (1, 0), (2, 0), (0, 1), (1, 1), (2, 1), (0, 2), (1, 2),
(2, 2)]
count_questions_properties = ['P31', 'P279', 'P361', 'P527', 'P2670']
# Sends a request to the wikidata api, retrieving an entity of a given name
def find_entity(entity, index):
entity.replace("_", " ")
params = {'action': 'wbsearchentities', 'language': 'en', 'format': 'json'}
params['search'] = entity.rstrip()
json = requests.get(api_url, params).json()
return json['search'][index]
# Sends a request to the wikidata api, retrieving a property of a given name
def find_property(property, index):
property.replace("_", " ")
params = {'action': 'wbsearchentities',
'language': 'en',
'format': 'json',
'type': 'property'}
params['search'] = property.rstrip()
json = requests.get(api_url, params).json()
return json['search'][index]
# Finds X of Y, returns string 'no results' if no results found
def get_results(xstring, ystring):
Xparams = {'action': 'wbsearchentities',
'language': 'en',
'format': 'json',
'type': 'property'}
Yparams = {'action': 'wbsearchentities',
'language': 'en',
'format': 'json'}
Yparams['search'] = ystring
Yjson = requests.get(api_url, Yparams).json()
Xparams['search'] = xstring
Xjson = requests.get(api_url, Xparams).json()
# loop through all the search results of X and Y
for result in Yjson['search']:
y = result['id']
for result in Xjson['search']:
x = result['id']
data = get_data(x, y)
if data['results']['bindings'] != []:
print(data)
return data
return ('no results')
# Extracts data from wikidata API
def get_data(x, y):
query = '''
SELECT ?itemLabel
WHERE
{{
wd:{} wdt:{} ?item.
SERVICE wikibase:label {{ bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". }}
}}'''.format(y, x)
data = requests.get(url, params={'query': query, 'format': 'json'}).json()
return data
# Extracts data and puts it into a list
def extract_answer(data):
mylist = []
for item in data['results']['bindings']:
for var in item:
mylist.append(item[var]['value'])
return mylist
def count_questions_get_x_and_y(parse):
x = ""
y = ""
for token in parse:
if token.tag_ == "NNS" and y == "":
x = token.lemma_
if token.dep_ == "compound":
x += "_" + token.head.lemma_
elif x != "" and (token.tag_ == "NN" or token.tag_ == "NNS"):
y = token.lemma_
if token.dep_ == "compound":
y += "_" + token.head.lemma_
break
return x, y
# Highest lowest first last question
def highest_lowest_questions_get_x_and_y(parse):
x = ""
y = ""
for token in parse:
if token.pos_ == "ADJ":
x = token.text
x += "_" + token.head.lemma_
elif token.dep_ == "pobj" and (token.tag_ == "NN" or token.tag_ == "NNS"):
y = token.lemma_
if token.dep_ == "compound":
y += "_" + token.head.lemma_
break
return x, y
# Count query generation, simply plugs in values for x and y
def gen_count_query(x, x_id, y, y_id, property):
x = x.replace(" ", "")
y = y.replace(" ", "")
y = y.replace(".", "")
new_query = ((
"""
SELECT ?%s
WHERE
{
wd:%s p:%s [
ps:%s wd:%s;
pq:P1114 ?%s ].
SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE]". }
}
""")
% (x, y_id, property, property, x_id, x))
return new_query
def gen_highest_lowest_query(x, x_id, y, y_id):
x = x.replace(" ", "")
y = y.replace(" ", "")
y = y.replace(".", "")
new_query = (("""SELECT ?%s ?%s WHERE {wd:%s wdt:%s ?%s"""
""". SERVICE wikibase:label { bd:serviceParam"""
""" wikibase:language "[AUTO_LANGUAGE],en". } }""")
% (x, x+"Label", y_id, x_id, x))
return new_query
# Sends a request to wikidata with a sparql query, returns the result
def run_specific_query(query):
try:
data_request = requests.get(url, params={'query': query,
'format': 'json'})
data = data_request.json()
return data['results']['bindings']
except Exception:
print("Failed to get data...")
return None
# Can print list answers in a single line
def print_answer(answers):
for answer in answers:
print(answer, end='\t', flush=True)
print('')
########################################################################
def isCase_1(parse):
if parse[0].text == 'Is':
return True
elif parse[0].text == 'Are':
return True
elif parse[0].text == 'Was':
return True
elif parse[0].text == 'Were':
return True
elif parse[0].text == 'Does':
return True
elif parse[0].text == 'Did':
return True
else:
return False
def isCase_2(question):
if "highest" in question or "lowest" in question or "first" in question or "last" in question:
return True
return False
def isCase_3(parse):
if parse[0].text == 'How' and parse[1].text == "many":
return True
return False
def isCase_4(parse):
return False
########################################################################
def findAnswerCase_1(parse):
finalAnswer = []
questionAnswer = []
# Test Case: Is Donda West the mother of Kanye West
questionAnswer.append('Donda West')
xstring = 'mother'
ystring = 'Kanye West'
data = get_results(xstring, ystring)
# fail Case
if data == 'no results':
finalAnswer.append('Could not find answer')
else:
if questionAnswer == extract_answer(data):
finalAnswer.append('Yes')
else:
finalAnswer.append('No')
return finalAnswer
def findAnswerCase_2(parse, times=0):
x, y = highest_lowest_questions_get_x_and_y(parse)
print("x: " + x + " y: " + y)
times_tried = permutation[times]
try:
x_property = find_property(x, times_tried[0])
y_element = find_entity(y, times_tried[1])
except Exception:
return findAnswerCase_2(parse, times+1)
sparql_query = gen_highest_lowest_query(x, x_property['id'], y, y_element['id'])
query_result = run_specific_query(sparql_query)
if query_result == []:
return findAnswerCase_2(parse, times+1)
else:
answer = []
for item in query_result:
for idx, var in enumerate(item):
if idx == 0:
continue
answer.append(item[var]['value'])
return answer
def findAnswerCase_3(parse, times=0):
if (times >= 8):
print("Exhausted 8 options, no answer found\n")
return None
x, y = count_questions_get_x_and_y(parse)
print("x: " + x + " y: " + y)
times_tried = permutation[times]
try:
x_property = find_entity(x, times_tried[0])
y_element = find_entity(y, times_tried[1])
except Exception:
return findAnswerCase_3(parse, times+1)
for i in range(len(count_questions_properties)):
sparql_query = gen_count_query(x, x_property['id'], y, y_element['id'], count_questions_properties[i])
query_result = run_specific_query(sparql_query)
if query_result != []:
break
if query_result == []:
return findAnswerCase_3(parse, times+1)
else:
answer = []
for item in query_result:
for var in item:
answer.append(item[var]['value'])
return answer
def findAnswerCase_4(parse):
print('Parse 2: Incomplete Code')
########################################################################
def find_answer(question):
parse = nlp(question)
for w in parse:
print("\t \t".join((w.text, w.lemma_, w.pos_, w.tag_, w.dep_,w.head.lemma_)))
answer = []
# Yes/No Questions
if isCase_1(parse) is True:
answer = findAnswerCase_1(parse)
# Highest/Lowest Questions
elif isCase_2(question) is True:
answer = findAnswerCase_2(parse)
# Count Questions
elif isCase_3(parse) is True:
answer = findAnswerCase_3(parse)
# What/Who/Where/When/How Questions
elif isCase_4(parse) is True:
answer = findAnswerCase_4(parse)
# Fail Case
else:
answer.append('Could not find answer')
print_answer(answer)
def main(argv):
for sentence in sys.stdin:
find_answer(sentence)
if __name__ == "__main__":
main(sys.argv)