-
Notifications
You must be signed in to change notification settings - Fork 1
/
QuestionGenerator.py
205 lines (187 loc) · 7.66 KB
/
QuestionGenerator.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
from treehelpers import *
from nltk.stem.wordnet import WordNetLemmatizer
from nltk.tree import Tree
import nltk
class QuestionGenerator():
def __init__(self, sentence):
self.sentence = sentence
self.lemmatizer = WordNetLemmatizer()
def create(self):
t = self.sentence
questions = []
if ['NP', 'VP', 'PP', '.'] == immediate_labels(t):
question = self.__create_wh_question(t)
if question is not None:
questions.append(question)
if ['NP', 'VP', '.'] == immediate_labels(t):
s = t.copy(deep=True)
(answer, parent) = first_right_np_pp(s[1])
if answer is not None:
if answer.label() == 'NP':
answer_copy = answer.copy(deep=True)
del parent[-1]
new_sentence = Tree('S', children=[s[0], s[1], answer_copy, s[2]])
question = self.__get_who_what_question(new_sentence)
if question is not None:
questions.append(question)
elif answer.label() == 'PP':
answer_copy = answer.copy(deep=True)
del parent[-1]
new_sentence = Tree('S', children=[s[0], s[1], answer_copy, s[2]])
question = self.__create_wh_question(new_sentence)
if question is not None:
questions.append(question)
np1_np2_question = self.__create_np1_np2_question(t)
if np1_np2_question is not None:
questions.append(np1_np2_question)
# if len(questions) == 0:
# print ""
# print sentence_join(t)
# print t
# print ""
nt_filtered_questions = []
for question in questions:
nt_question = question.replace(" n't", "")
nt_filtered_questions.append(nt_question)
return list(set(nt_filtered_questions))
def __create_np1_np2_question(self, t):
try:
sections = []
if len(t) < 3 or (t[0].label() != 'NP' or t[1].label() != 'VP'):
return None
for s in t:
label = s.label()
section = s.leaves()
sections.append((label, section))
question = None
np_phrase = word_list_join(sections[0][1])
vp_phrase = word_list_join(sections[1][1][1:])
if vp_phrase.startswith('also '):
vp_phrase = vp_phrase[5:]
if leftmost(t[0]).label() != 'NNP':
np_phrase = np_phrase[0].lower() + np_phrase[1:]
if (sections[0][0] == 'NP' and sections[1][0] == 'VP'):
if sections[1][1][0] == 'is':
question = ("Is " + np_phrase + ' ' + vp_phrase).strip() + '?'
elif sections[1][1][0] == 'are':
question = ("Are " + np_phrase + ' ' + vp_phrase).strip() + '?'
elif sections[1][1][0] == 'was':
question = ("Was " + np_phrase + ' ' + vp_phrase).strip() + '?'
elif sections[1][1][0] == 'were':
question = ("Were " + np_phrase + ' ' + vp_phrase).strip() + '?'
elif sections[1][1][0] == 'will':
question = ("Will " + np_phrase + ' ' + vp_phrase).strip() + '?'
elif sections[1][1][0] == 'can':
question = ("Can " + np_phrase + ' ' + vp_phrase).strip() + '?'
elif sections[1][1][0] == 'do':
question = ("Do " + np_phrase + ' ' + vp_phrase).strip() + '?'
elif sections[1][1][0] == 'does':
question = ("Does " + np_phrase + ' ' + vp_phrase).strip() + '?'
elif sections[1][1][0] == 'has' and len(t[1]) > 1 and t[1][1].label() in ['VP', 'ADVP']:
question = ("Has " + np_phrase + ' ' + vp_phrase).strip() + '?'
elif sections[1][1][0] == 'had' and len(t[1]) > 1 and t[1][1].label() in ['VP', 'ADVP']:
question = ("Has " + np_phrase + ' ' + vp_phrase).strip() + '?'
elif sections[1][1][0] == 'have' and len(t[1]) > 1 and t[1][1].label() in ['VP', 'ADVP']:
question = ("Have " + np_phrase + ' ' + vp_phrase).strip() + '?'
else:
verb = leftmost(t[1])
do_word = None
if verb.label() == 'VBD':
do_word = 'Did '
elif verb.label() == 'VBZ':
do_word = 'Does '
elif verb.label() == 'VBP':
d_word = 'Do '
if do_word:
verb = self.lemmatizer.lemmatize(t[1].leaves()[0], 'v')
question = (do_word + np_phrase + ' ' + verb + ' ' + word_list_join(t[1].leaves()[1:])).strip() + '?'
return question
except:
return None
def __create_wh_question(self, t):
try:
wh_word = self.__get_wh_word(t[2])
if wh_word == None:
return None
d_word = 'do'
if isinstance(t[1][0], Tree) and t[1][0].label() == 'ADVP':
del t[1][0]
if leftmost(t[1]).label() == 'VBD': # past
d_word = 'did'
elif leftmost(t[1]).label() == 'VBZ': # present third person
d_word = 'does'
elif leftmost(t[1]).label() == 'VBP': # present first person
d_word = 'do'
np_phrase = sentence_join(t[0])
if leftmost(t[0]).label() != 'NNP':
np_phrase = np_phrase[0].lower() + np_phrase[1:]
verb = self.lemmatizer.lemmatize(t[1].leaves()[0], 'v')
vp_rest = word_list_join(t[1].leaves()[1:])
sentence = ''
special_verbs = ['will', 'be', 'can', 'could', 'would', 'have', 'do']
if verb in special_verbs:
sentence = ' '.join([wh_word, t[1].leaves()[0], np_phrase, vp_rest]).strip() + '?'
else:
sentence = ' '.join([wh_word, d_word, np_phrase, verb, vp_rest]).strip() + '?'
# if abs(len(sentence.split()) - 9) > 4:
# sentence = ''
return sentence
except:
return None
def __get_who_what_question(self, t):
try:
wh_word = self.__get_who_or_what_from_np(t[2])
if wh_word == None:
return None
d_word = 'do'
if len(t[1]) > 0 and isinstance(t[1][0], Tree) and t[1][0].label() == 'ADVP':
del t[1][0]
if leftmost(t[1]).label() == 'VBD': # past
d_word = 'did'
elif leftmost(t[1]).label() == 'VBZ': # present third person
d_word = 'does'
elif leftmost(t[1]).label() == 'VBP': # present first person
d_word = 'do'
np_phrase = sentence_join(t[0])
if leftmost(t[0]).label() != 'NNP':
np_phrase = np_phrase[0].lower() + np_phrase[1:]
verb = self.lemmatizer.lemmatize(t[1].leaves()[0], 'v')
vp_rest = word_list_join(t[1].leaves()[1:])
sentence = ''
special_verbs = ['will', 'be', 'can', 'could', 'would', 'have', 'do']
if verb in special_verbs:
sentence = ' '.join([wh_word, t[1].leaves()[0], np_phrase, vp_rest]).strip() + '?'
else:
sentence = ' '.join([wh_word, d_word, np_phrase, verb, vp_rest]).strip() + '?'
return sentence
except:
return None
def __get_wh_word(self, t):
months = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December']
years = map(lambda x: str(x), range(1900, 2017))
when_trigger_words = ['after', 'before', 'ago', 'during', 'age', 'day', 'hour', 'minute', 'when']
where_trigger_words = ['in', 'at', 'behind', 'above', 'next to', 'under', 'inside', 'to', 'below', 'outside', 'front', 'across from', 'right', 'left', 'back']
why_trigger_words = ['because', 'due to']
sentence = sentence_join(t)
for word in why_trigger_words:
if sentence.startswith(word):
return 'Why'
for word in months:
if word in sentence:
return 'When'
for word in years:
if word in sentence:
return 'When'
for word in when_trigger_words:
if word in sentence:
return 'When'
for word in where_trigger_words:
if word in sentence:
return 'Where'
return None
def __get_who_or_what_from_np(self, t):
chunks = nltk.ne_chunk(t.pos())
if isinstance(chunks[0], Tree) and chunks[0].label() == 'PERSON':
return 'Who'
else:
return 'What'