Questgen - An open source NLP library for Question generation algorithms.
By: Ramsri Goutham
Tags: Naturallanguageprocessing, Data Science, Question Generation, Machine Learning, Nlproc

Questgen - An open source NLP library for Question generation algorithms.

Image by Author

Question answering is a very popular task in Natural language processing but question generation is novel and hasn’t been explored much yet.

Question generation has a lot of use cases with the most prominent one being the ability to generate quick assessments from any given content. It would help school teachers in generating worksheets from any given chapter quickly and decrease their work burden during Covid-19.

I along with two other awesome interns Parth Chokhra and Vaibhav Tiwari built an easy-to-use, open-source library to advance the research in question generation using the state-of-the-art T5 transformer model from Hugging Face library.

The currently supported question generation capabilities of the library are MCQs, Yes/No questions, FAQs, Paraphrasing, and Question Answering.

Image by Author

Without further delay let’s dive into the details.

All the code and an easy to use Google Colab can be found here :

Installation

Install the libraries:

!pip install git+https://github.com/ramsrigouthamg/Questgen.ai

!pip install sense2vec==1.0.2

!pip install git+https://github.com/boudinfl/pke.git



!python -m nltk.downloader universal_tagset

!python -m spacy download en

Download and extract zip of Sense2vec word vectors that are used for the generation of multiple choices.

!wget https://github.com/explosion/sense2vec/releases/download/v1.0.0/s2v_reddit_2015_md.tar.gz !tar -xvf s2v_reddit_2015_md.tar.gz !ls s2v_old

Running the code

1. Generate boolean (Yes/No) Questions

Image by Author

from pprint import pprint

from Questgen import main

qe= main.BoolQGen()

payload = {

"input_text": "Sachin Ramesh Tendulkar is a former international cricketer from India and a former captain of the Indian national team. He is widely regarded as one of the greatest batsmen in the history of cricket. He is the highest run scorer of all time in International cricket."

}

output = qe.predict_boolq(payload)

pprint (output)

Output:

'Boolean Questions': ['Is sachin ramesh tendulkar the highest run scorer in '

'cricket?',

'Is sachin ramesh tendulkar the highest run scorer in '

'cricket?',

'Is sachin tendulkar the highest run scorer in '

'cricket?']

2. Generate MCQ Questions

Image by author

qg = main.QGen()

output = qg.predict_mcq(payload)

pprint (output)

Output:

{'questions': [{'answer': 'cricketer',

'context': 'Sachin Ramesh Tendulkar is a former international '

'cricketer from India and a former captain of the '

'Indian national team.',

'extra_options': ['Mark Waugh',

'Sharma',

'Ricky Ponting',

'Afridi',

'Kohli',

'Dhoni'],

'id': 1,

'options': ['Brett Lee', 'Footballer', 'International Cricket'],

'options_algorithm': 'sense2vec',

'question_statement': "What is Sachin Ramesh Tendulkar's "

'career?',

'question_type': 'MCQ'},

{'answer': 'india',

'context': 'Sachin Ramesh Tendulkar is a former international '

'cricketer from India and a former captain of the '

'Indian national team.',

'extra_options': ['Pakistan',

'South Korea',

'Nepal',

'Philippines',

'Zimbabwe'],

'id': 2,

'options': ['Bangladesh', 'Indonesia', 'China'],

'options_algorithm': 'sense2vec',

'question_statement': 'Where is Sachin Ramesh Tendulkar from?',

'question_type': 'MCQ'},

{'answer': 'batsmen',

'context': 'He is widely regarded as one of the greatest '

'batsmen in the history of cricket.',

'extra_options': ['Ashwin', 'Dhoni', 'Afridi', 'Death Overs'],

'id': 3,

'options': ['Bowlers', 'Wickets', 'Mccullum'],

'options_algorithm': 'sense2vec',

'question_statement': 'What is the best cricketer?',

'question_type': 'MCQ'}]}

3. Generate FAQ Questions

Image by Author

output = qg.predict_shortq(payload)

pprint (output)

Output :

{'questions': [{'Answer': 'cricketer',

'Question': "What is Sachin Ramesh Tendulkar's career?",

'context': 'Sachin Ramesh Tendulkar is a former international '

'cricketer from India and a former captain of the '

'Indian national team.',

'id': 1},

{'Answer': 'india',

'Question': 'Where is Sachin Ramesh Tendulkar from?',

'context': 'Sachin Ramesh Tendulkar is a former international '

'cricketer from India and a former captain of the '

'Indian national team.',

'id': 2},

{'Answer': 'batsmen',

'Question': 'What is the best cricketer?',

'context': 'He is widely regarded as one of the greatest '

'batsmen in the history of cricket.',

'id': 3}]

}

4. Paraphrasing Questions

Image by Author

payload2 = {

"input_text" : "What is Sachin Tendulkar profession?",

"max_questions": 5

}

output = qg.paraphrase(payload2)

pprint (output)

Output:

{'Paraphrased Questions': ["ParaphrasedTarget: What is Sachin Tendulkar's profession?",

"ParaphrasedTarget: What is Sachin Tendulkar's career?",

"ParaphrasedTarget: What is Sachin Tendulkar's job?",

'ParaphrasedTarget: What is Sachin Tendulkar?',

"ParaphrasedTarget: What is Sachin Tendulkar's occupation?"],

'Question': 'What is Sachin Tendulkar profession?'}

5. Question Answering (Simple)

Image by Author

answer = main.AnswerPredictor()

payload3 = {

"input_text" : '''Sachin Ramesh Tendulkar is a former international cricketer from

India and a former captain of the Indian national team. He is widely regarded

as one of the greatest batsmen in the history of cricket. He is the highest

run scorer of all time in International cricket.''',

"input_question" : "Who is Sachin tendulkar ? "



}

output = answer.predict_answer(payload3)

Output:

Sachin ramesh tendulkar is a former international cricketer from india and a former captain of the indian national team.

6. Question Answering (Boolean)

payload4 = {

"input_text" : '''Sachin Ramesh Tendulkar is a former international cricketer from

India and a former captain of the Indian national team. He is widely regarded

as one of the greatest batsmen in the history of cricket. He is the highest

run scorer of all time in International cricket.''',

"input_question" : "Is Sachin tendulkar a former cricketer? "

}

output = answer.predict_answer(payload4)

print (output)

Output: