38894-vm/question_generation_main.py
Pragati Verma 9d2adf338d
Version 1.0 ready (#1)
* FEAT: Basic app completed & file uploading added

* Added pdf/txt support and quiz template

* FEAT: PDF/TEXT uploading and getting file contents done

* Updated .gitignore

* Modified quiz template

* Fix UI - Added CSS to upload page

* Feat Quiz UI - Added CSS to Quiz Page

* Feat: Question Extraction - Extract questions from raw text

* Feat: added incorrect answer generation

* Feat: Tied together question extraction and incorrect anwer generation

* Fix: handle word not in vocabulary error

* Fix: added document as argument to get_questions_dict

* Feat: Integrated uploading file and question generation; needs fixes

* Feat Quiz UI - added correct answer UI

* Feat: Clean text for better processing

* Fix UI - added gradient and spacing

* Feat UI - Added uploading animation

* Fixed options issue on quiz screen

* Fix UI - added fonts, favicon etc

* Fix - Formatted code

* Feat UI - added fork me on github ribbon

* Feat: Added quiz result functionality

* Fix UI - added text to landing page

* Fix UI - added CSS to submit button

* Feat UI - added CSS to results page

Co-authored-by: user86 <kshitij.kotasthane@gmail.com>
Co-authored-by: telescopic <vigneshsuresh1775@gmail.com>
2020-10-11 23:35:13 +05:30

57 lines
1.9 KiB
Python

'''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