-
Notifications
You must be signed in to change notification settings - Fork 29
/
yesno.py
48 lines (46 loc) · 1.77 KB
/
yesno.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
import sys
import nltk.data
import nltk
def answeryesno(article, question):
prev = "no"
questionstr = ' '.join(question)
questionstr = questionstr.lower()
question = nltk.pos_tag(question)
answer = "no"
keyword = ""
for (word,pos) in question:
if (pos == 'NN' or pos == 'NNS' or pos == 'NNP' or pos == 'NNPS'):
keyword = word.lower()
answer = "no"
for sentence in article:
# print sentence
if answer == "yes":
break
s = nltk.word_tokenize(sentence.lower())
if keyword in s:
#print sentence
answer = "yes"
for (word,pos) in question:
if answer == 'no':
break
if (pos != '.') and (word.lower() not in s) and (pos != 'DT') and (word != 'does') and (word != 'do'):
answer = 'no'
#print word, pos
if pos[0] == 'V':
tempword = nltk.stem.wordnet.WordNetLemmatizer().lemmatize(word,'v')
for (w,p) in nltk.pos_tag(s):
if p[0] == 'V':
tempword2 = nltk.stem.wordnet.WordNetLemmatizer().lemmatize(w,'v')
if tempword == tempword2:
answer = 'yes'
elif word in article[0]:
answer = "yes"
if prev == "yes":
if (word == "no" or word =="not"):
answer = "no"
if pos[0] == 'V':
prev = "yes"
else:
prev = "no"
#print questionstr,answer
print answer