-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathYes_or_No.py
More file actions
299 lines (239 loc) · 8.28 KB
/
Yes_or_No.py
File metadata and controls
299 lines (239 loc) · 8.28 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
#!/usr/bin/python3
import sys
import requests
import re
import spacy
nlp = spacy.load('en')
#Finds X of Y, returns string 'no results' if no results found
def get_results(xstring, ystring):
url = 'https://www.wikidata.org/w/api.php'
Xparams = {'action':'wbsearchentities',
'language':'en',
'format':'json',
'type':'property'}
Yparams = {'action':'wbsearchentities',
'language':'en',
'format':'json',}
try:
Yparams['search'] = ystring
Yjson = requests.get(url,Yparams).json()
Xparams['search'] = xstring
Xjson = requests.get(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'] != []:
return data
except Exception:
return ('no results')
def get_xstring_data(ystring, zstring):
url3 = 'https://www.wikidata.org/w/api.php'
Yparams = {'action':'wbsearchentities',
'language':'en',
'format':'json',}
Zparams = {'action':'wbsearchentities',
'language':'en',
'format':'json',}
try:
Zparams['search'] = zstring
Zjson = requests.get(url3,Zparams).json()
Yparams['search'] = ystring
Yjson = requests.get(url3,Yparams).json()
#loop through all the search results of Y and Z
for result in Yjson['search']:
y = result['id']
for result in Zjson['search']:
z = result['id']
data = get_property_data(y,z)
if data['results']['bindings'] != []:
return data
except Exception:
return ('no results')
def get_property_data(y,z):
url4 = 'https://query.wikidata.org/sparql'
query='''
SELECT ?itemLabel
WHERE
{{
wd:{} ?itemLabel wd:{}.
SERVICE wikibase:label {{ bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". }}
}}'''.format(y,z)
data = requests.get(url4, params={'query': query, 'format': 'json'}).json()
return data
#Extracts data from wikidata API
def get_data(x, y):
url2 = 'https://query.wikidata.org/sparql'
query='''
SELECT ?itemLabel
WHERE
{{
wd:{} wdt:{} ?item.
SERVICE wikibase:label {{ bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". }}
}}'''.format(y,x)
data = requests.get(url2, params={'query': query, 'format': 'json'}).json()
return data
#Extracts data and puts it into a list
def extract_answer(data):
try:
mylist = []
for item in data['results']['bindings']:
for var in item :
mylist.append(item[var]['value'])
return mylist
except Exception:
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 check_be_z_the_x_of_y(parse):
for w in parse:
if w.dep_== "pobj":
return True
return False
def find_string(w, parse, string):
for x in parse:
if x.dep_ == "compound" and x.head.lemma_ == w.lemma_ :
if string == "":
string = x.text
else:
string += " " + x.text
elif x.text == w.text and string == "":
string = w.text
else:
if x.text == w.text:
string += " " + w.text
return string
########################################################################
def find_do_z_x_y(parse):
xstring = ""; ystring = ""; zstring = "";
for w in parse:
#Find ystring
if w.dep_ == "nsubj" :
zstring = find_string(w, parse, zstring)
#Find xstring
if w.dep_ == "ROOT" and (w.pos_ == "VERB" or w.pos_ == "NOUN"):
xstring = w.text
#Find preposition if any
for x in w.subtree:
if x.dep_ == "prep" and x.head.lemma_ == w.lemma_:
xstring += " " + x.text
#Find zstring
if w.dep_ == "pobj" or w.dep_=="dobj":
ystring = find_string(w, parse, ystring)
return (xstring, ystring, zstring)
def find_be_z_the_x_of_y(parse,question):
xstring = ""; ystring = ""; zstring = "";
if parse[0].text == 'Is':
m = re.search('Is (.*) the (.*) of (.*)?', question)
if parse[0].text == 'Are':
m = re.search('Are (.*) the (.*) of (.*)?', question)
if parse[0].text == 'Was':
m = re.search('Was (.*) the (.*) of (.*)?', question)
if parse[0].text == 'Were':
m = re.search('Were (.*) the (.*) of (.*)?', question)
xstring = m.group(2)
ystring = m.group(3)
zstring = m.group(1)
ystring = ystring.replace("?","")
return (xstring, ystring, zstring)
def find_be_y_z(parse):
xstring = ""; ystring = ""; zstring = "";
xstring = "is a"
for w in parse:
#find ystring
if w.dep_ == "nsubj":
ystring = find_string(w, parse, ystring)
#find zstring
if (w.dep_ == "appos" or w.dep_ =="attr" or w.dep_ == "acomp"):
zstring = find_string(w, parse, zstring)
data = get_xstring_data(ystring, zstring)
myList = extract_answer(data)
xstring = myList[0]
return xstring, ystring, zstring
########################################################################
def yes_no_questions_get_x_y_z(parse,question):
xstring = ""; ystring = ""; zstring = "";
#Do Z X Y? (Did B. B. King influence Jimi Hendrix?)
if parse[0].lemma_ == "do":
xstring, ystring, zstring = find_do_z_x_y(parse)
#Be Z the X of Y? (Is Donda West the mother of Kanye West)
elif check_be_z_the_x_of_y(parse) == True:
xstring, ystring, zstring = find_be_z_the_x_of_y(parse,question)
#Be Z Y? (Is Shakira a model, Is Michael Jackson alive)
else:
xstring, ystring, zstring = find_be_y_z(parse)
return xstring, ystring, zstring
########################################################################
def isCase_1(parse):
if parse[0].lemma_ == 'be' or parse[0].lemma_ == 'do':
return True
else:
return False
def isCase_2(question):
return False
def isCase_3(question):
return False
def isCase_4(question):
return False
########################################################################
def findAnswerCase_1(parse,question):
finalAnswer = []
candidateAnswer = []
xstring, ystring, zstring = yes_no_questions_get_x_y_z(parse,question)
print("x: " + xstring)
print("y: " + ystring)
print("z: " + zstring)
candidateAnswer.append(zstring)
data = get_results(xstring,ystring)
listAnswers = extract_answer(data)
#fail Case
if data == 'no results':
finalAnswer.append('Could not find answer')
return finalAnswer
else:
if zstring in listAnswers:
finalAnswer.append('Yes')
elif candidateAnswer == extract_answer(data):
finalAnswer.append('Yes')
else:
finalAnswer.append('No')
return finalAnswer
def findAnswerCase_2(parse):
print('Incomplete Code')
def findAnswerCase_3(parse):
print('Incomplete Code')
def findAnswerCase_4(parse):
print('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) == True:
answer = findAnswerCase_1(parse,question)
#Highest/Lowest Questions
elif isCase_2(question) == True:
answer = findAnswerCase_2(parse)
#Count Questions
elif isCase_3(question) == True:
answer = findAnswerCase_3(parse)
#What/Who/Where/When/How Questions
elif isCase_4(question) == 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)