-
Notifications
You must be signed in to change notification settings - Fork 29
/
Copy pathquestion_generation_main.py
56 lines (46 loc) · 1.86 KB
/
question_generation_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
'''This module ties together the
questions generation and incorrect answer
generation modules
'''
from question_extraction import QuestionExtractor
from incorrect_answer_generation import IncorrectAnswerGenerator
import re
from nltk import sent_tokenize
class QuestionGeneration:
'''This class contains the method
to generate questions
'''
def __init__(self, num_questions, num_options):
self.num_questions = num_questions
self.num_options = num_options
self.question_extractor = QuestionExtractor(num_questions)
def clean_text(self, text):
text = text.replace('\n', ' ') # remove newline chars
sentences = sent_tokenize(text)
cleaned_text = ""
for sentence in sentences:
# remove non alphanumeric chars
cleaned_sentence = re.sub(r'([^\s\w]|_)+', '', sentence)
# substitute multiple spaces with single space
cleaned_sentence = re.sub(' +', ' ', cleaned_sentence)
cleaned_text += cleaned_sentence
if cleaned_text[-1] == ' ':
cleaned_text[-1] = '.'
else:
cleaned_text += '.'
cleaned_text += ' ' # pad with space at end
return cleaned_text
def generate_questions_dict(self, document):
document = self.clean_text(document)
self.questions_dict = self.question_extractor.get_questions_dict(
document)
self.incorrect_answer_generator = IncorrectAnswerGenerator(document)
for i in range(1, self.num_questions + 1):
if i not in self.questions_dict:
continue
self.questions_dict[i]["options"] \
= self.incorrect_answer_generator.get_all_options_dict(
self.questions_dict[i]["answer"],
self.num_options
)
return self.questions_dict