-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
318 lines (248 loc) · 12.6 KB
/
main.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
import csv
v1list = []
v2list = []
pluralnounlist = []
adjectivelist = []
truths = []
with open('verbs.csv') as file:
reader = csv.DictReader(file)
for row in reader:
v1list.append(row['v1'])
v2list.append(row['v2'])
v1list = sorted(v1list)
v2list = sorted(v2list)
with open('pluralnoun.csv') as file:
reader = csv.DictReader(file)
for row in reader:
pluralnounlist.append(row['pluralnoun'])
pluralnounlist = sorted(pluralnounlist)
with open('adjective.csv') as file:
reader = csv.DictReader(file)
for row in reader:
adjectivelist.append(row['adjective'])
adjectivelist = sorted(adjectivelist)
def binary_search(word_list, word):
#Input: wordlist, word #any list of word and any word
#Output: True if word present in wordlist else false
left, right = 0, len(word_list) - 1
while left <= right:
mid = (left + right) // 2
mid_val = word_list[mid]
if mid_val == word:
return True
elif mid_val > word:
right = mid - 1
else:
left = mid + 1
return False
def checkifverb(word):
#Input: word #any word
#Output: true if word is singular(v1) or plural(v2) verb else false
if binary_search(v1list, word):
singular = True
else:
singular = False
if binary_search(v2list, word):
plural = True
else:
plural = False
verb = singular or plural
return verb
def sentence_to_words(sentence):
#Input: sentence #any sentence without fullstop
#Output: list of word that make the sentence
words = sentence.split()
return words
def sentencetoobject(sentence):
#Input: sentence #any sentence
#Output: object with properties subject, verb and object
wordlist = sentence_to_words(sentence)
truth = {}
firsthalf = True
truth['subject'] = ''
truth['object'] = ''
for word in wordlist:
if checkifverb(word):
truth['verb'] = word
firsthalf = False
continue
if firsthalf:
truth['subject'] = truth['subject'] + ' ' + word
else:
truth['object'] = truth['object'] + ' ' + word
truth['subject'] = truth['subject'].strip()
truth['object'] = truth['object'].strip()
return truth
def objectrefiner(truth):
#Input: obj #An object that describes a sentence
#Output: an object that has info who,what for subject and how,what,whom,where,when for object
what = False
for word in sentence_to_words(truth['subject']):
if word == 'A' or word == 'An' or word == 'The':
what = True
if binary_search(pluralnounlist, truth['subject']):
what = True
if what:
truth['subject'] = {'what': truth['subject']}
else:
truth['subject'] = {'who': truth['subject']}
how = False
whom= False
when = False
where = False
whenorwhere = False
if binary_search(adjectivelist, truth['object']):
how = True
objectpronoun = ['me', 'you', 'him', 'her', 'us', 'them', 'whom']
if truth['object'].istitle() or truth['object'] in objectpronoun:
whom = True
for word in sentence_to_words(truth['object']):
if word == 'at' or word == 'in' or word == 'on' or word == 'from' or word == 'to':
whenorwhere = True
partofday = ['morning', 'evening', 'noon']
year = []
for i in range(10001):
year.append(str(i))
season = ["spring", "summer", "fall", "winter"]
month = ["January", "February", "March", "April", "May", "June",
"July", "August", "September", "October", "November", "December"]
day = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"]
if whenorwhere:
for word in sentence_to_words(truth['object']):
if word == 'time' or word == 'oclock' or word == 'am' or word == 'pm' or word in partofday or word in year or word in season or word in month or word in day:
when = True
else:
where = True
if how:
truth['object'] = {'how': truth['object']}
elif whom:
truth['object'] = {'whom': truth['object']}
elif when:
truth['object'] = {'when': truth['object']}
elif where:
truth['object'] = {'where': truth['object']}
else:
truth['object'] = {'what': truth['object']}
return truth
def answer(data, questionpermanent):
#Input: data,question #sentence that can be processed and question that is to be asked on the processed sentence
#Output: answer to the question according to data
question = questionpermanent
for i in range(len(data)):
truths.append(objectrefiner(sentencetoobject(data[i])))
print(truths)
questionwordlist = sentence_to_words(question)
questionobject = sentencetoobject(question)
if questionwordlist[0] == 'Who':
for truth in truths:
question = questionpermanent
questionwordlist = sentence_to_words(question)
questionobject = sentencetoobject(question)
if truth['verb'] == questionobject['verb'] or truth['verb'] == questionobject['verb'] + 's' or truth['verb']+'s' == questionobject['verb'] or (truth['verb']=='is' and questionobject['verb']=='are') or (truth['verb']=='are' and questionobject['verb']=='is'):
if questionobject['object'] in truth['object'].values():
if 'who' in truth['subject'].keys():
return truth['subject']['who']
if questionwordlist[0] == 'What':
for truth in truths:
question = questionpermanent
questionwordlist = sentence_to_words(question)
questionobject = sentencetoobject(question)
questionwordlist.remove('What')
question = ' '.join(questionwordlist)
questionobject = sentencetoobject(question)
if 'does' in questionwordlist:
if 'does' in questionwordlist:
questionwordlist.remove('does')
question = ' '.join(questionwordlist)
questionobject = sentencetoobject(question)
if truth['verb'] == questionobject['verb'] or truth['verb'] == questionobject['verb'] + 's' or truth['verb']+'s' == questionobject['verb'] or (truth['verb']=='is' and questionobject['verb']=='are') or (truth['verb']=='are' and questionobject['verb']=='is') :
if questionobject['subject'].capitalize() in truth['subject'].values():
if 'what' in truth['object'].keys():
return truth['object']['what']
if 'is' in questionwordlist:
if truth['verb'] == questionobject['verb'] or truth['verb'] == questionobject['verb'] + 's' or truth['verb']+'s' == questionobject['verb'] or (truth['verb']=='is' and questionobject['verb']=='are') or (truth['verb']=='are' and questionobject['verb']=='is') :
if questionobject['object'].capitalize() in truth['subject'].values():
if 'what' in truth['object'].keys():
return truth['object']['what']
if truth['verb'] == questionobject['verb'] or truth['verb'] == questionobject['verb'] + 's' or truth['verb']+'s' == questionobject['verb'] or (truth['verb']=='is' and questionobject['verb']=='are') or (truth['verb']=='are' and questionobject['verb']=='is'):
if questionobject['object'] in truth['object'].values():
if 'what' in truth['subject'].keys():
return truth['subject']['what']
print(questionobject)
if questionwordlist[0] == 'How':
for truth in truths:
question = questionpermanent
questionwordlist = sentence_to_words(question)
questionobject = sentencetoobject(question)
questionwordlist.remove('How')
question = ' '.join(questionwordlist)
questionobject = sentencetoobject(question)
print(questionobject)
if truth['verb'] == questionobject['verb'] or truth['verb'] == questionobject['verb'] + 's' or truth['verb']+'s' == questionobject['verb'] or (truth['verb']=='is' and questionobject['verb']=='are') or (truth['verb']=='are' and questionobject['verb']=='is'):
if questionobject['object'].capitalize() in truth['subject'].values():
if 'how' in truth['object'].keys():
return truth['object']['how']
if questionwordlist[0] == 'Whom':
for truth in truths:
question = questionpermanent
questionwordlist = sentence_to_words(question)
questionobject = sentencetoobject(question)
questionwordlist.remove('Whom')
if 'being' in questionwordlist:
questionwordlist.remove('being')
if 'does' in questionwordlist:
questionwordlist.remove('does')
if 'do' in questionwordlist:
questionwordlist.remove('do')
question = ' '.join(questionwordlist)
questionobject = sentencetoobject(question)
print(questionobject)
if truth['verb'] == questionobject['verb'] or truth['verb'] == questionobject['verb'] + 's' or truth['verb']+'s' == questionobject['verb'] or (truth['verb']=='is' and questionobject['verb']=='are') or (truth['verb']=='are' and questionobject['verb']=='is'):
if questionobject['object'].capitalize() in truth['subject'].values() or questionobject['subject'].capitalize() in truth['subject'].values():
if 'whom' in truth['object'].keys():
return truth['object']['whom']
if questionwordlist[0] == 'When':
for truth in truths:
question = questionpermanent
questionwordlist = sentence_to_words(question)
questionobject = sentencetoobject(question)
questionwordlist.remove('When')
if 'being' in questionwordlist:
questionwordlist.remove('being')
if 'does' in questionwordlist:
questionwordlist.remove('does')
if 'do' in questionwordlist:
questionwordlist.remove('do')
question = ' '.join(questionwordlist)
questionobject = sentencetoobject(question)
print(questionobject)
if truth['verb'] == questionobject['verb'] or truth['verb'] == questionobject['verb'] + 's' or truth['verb']+'s' == questionobject['verb'] or (truth['verb']=='is' and questionobject['verb']=='are') or (truth['verb']=='are' and questionobject['verb']=='is'):
if questionobject['object'].capitalize() in truth['subject'].values() or questionobject['subject'].capitalize() in truth['subject'].values():
if 'when' in truth['object'].keys():
return truth['object']['when']
if questionwordlist[0] == 'Where':
for truth in truths:
question = questionpermanent
questionwordlist = sentence_to_words(question)
questionobject = sentencetoobject(question)
questionwordlist.remove('Where')
if 'being' in questionwordlist:
questionwordlist.remove('being')
if 'does' in questionwordlist:
questionwordlist.remove('does')
if 'do' in questionwordlist:
questionwordlist.remove('do')
question = ' '.join(questionwordlist)
questionobject = sentencetoobject(question)
print(questionobject)
if truth['verb'] == questionobject['verb'] or truth['verb'] == questionobject['verb'] + 's' or truth['verb']+'s' == questionobject['verb'] or (truth['verb']=='is' and questionobject['verb']=='are') or (truth['verb']=='are' and questionobject['verb']=='is'):
if questionobject['object'].capitalize() in truth['subject'].values() or questionobject['subject'].capitalize() in truth['subject'].values():
if 'where' in truth['object'].keys():
return truth['object']['where']
arrayofsentence = ['A cat is in the room','He hit a car','He is a dog']
question = 'What is he'
ans = answer(arrayofsentence, question)
print(ans)
#changes needed to be made
#1) A man, A woman, A human ... should be the answer of who not what
#2) here, there should be the answer of where