Spaces:
Running
Running
DishaKushwah
commited on
Commit
·
15b8ff9
1
Parent(s):
4105a9d
Update mcq_generator.py
Browse files- mcq_generator.py +47 -30
mcq_generator.py
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
|
|
| 1 |
import random
|
| 2 |
import nltk
|
| 3 |
from nltk.corpus import stopwords
|
|
@@ -29,59 +30,74 @@ class AdvancedMCQGenerator:
|
|
| 29 |
key_concepts.append(sentence)
|
| 30 |
return key_concepts[:5] # Return top 5 key concepts
|
| 31 |
|
| 32 |
-
def generate_intelligent_question(self, concept, context):
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 43 |
|
| 44 |
-
def generate_contextual_distractors(self, correct_answer, context):
|
| 45 |
"""Create semantically related but incorrect distractors"""
|
| 46 |
sentences = sent_tokenize(context)
|
| 47 |
distractors = []
|
| 48 |
-
|
| 49 |
potential_distractors = [sent for sent in sentences if correct_answer.lower() not in sent.lower() and len(sent.split()) > 3]
|
| 50 |
-
|
| 51 |
# Generating diverse distractors
|
| 52 |
while len(distractors) < 3:
|
| 53 |
if potential_distractors:
|
| 54 |
-
# Choose a unique distractor
|
| 55 |
distractor = random.choice(potential_distractors)
|
| 56 |
potential_distractors.remove(distractor)
|
| 57 |
words = word_tokenize(distractor)
|
| 58 |
-
|
| 59 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 60 |
else:
|
| 61 |
-
fallback_distractors
|
| 62 |
-
distractor = random.choice(fallback_distractors)
|
| 63 |
-
distractors.append(distractor)
|
| 64 |
return distractors
|
| 65 |
|
| 66 |
-
def generate_mcq(self, context, num_questions=3):
|
| 67 |
"""Generate Multiple Choice Questions"""
|
| 68 |
# Validate context
|
| 69 |
if not context or len(context.split()) < 30:
|
| 70 |
raise ValueError("Context is too short. Provide more detailed text.")
|
| 71 |
|
| 72 |
-
# Generate questions
|
| 73 |
mcq_questions = []
|
| 74 |
key_concepts = self.extract_key_concepts(context)
|
| 75 |
|
| 76 |
for concept in key_concepts[:num_questions]:
|
| 77 |
try:
|
| 78 |
-
question = self.generate_intelligent_question(concept, context)
|
| 79 |
-
answer_result = self.qa_pipeline(question=question, context=context)
|
| 80 |
-
correct_answer = answer_result['answer']
|
| 81 |
-
distractors = self.generate_contextual_distractors(correct_answer, context)
|
| 82 |
-
all_options = [correct_answer] + distractors
|
| 83 |
random.shuffle(all_options)
|
| 84 |
-
correct_index = all_options.index(correct_answer)
|
| 85 |
mcq_questions.append({"question": question,"options": all_options,"correct_answer": correct_index}) # Create MCQ
|
| 86 |
except Exception as e:
|
| 87 |
print(f"Error generating question: {e}")
|
|
@@ -91,8 +107,9 @@ def main():
|
|
| 91 |
generator = AdvancedMCQGenerator()
|
| 92 |
context = input("Enter context text: ")
|
| 93 |
num_questions = int(input("How many questions do you want? "))
|
| 94 |
-
|
| 95 |
-
|
|
|
|
| 96 |
# Display and solve quiz
|
| 97 |
print("\n--- Quiz Started ---")
|
| 98 |
score = 0
|
|
|
|
| 1 |
+
|
| 2 |
import random
|
| 3 |
import nltk
|
| 4 |
from nltk.corpus import stopwords
|
|
|
|
| 30 |
key_concepts.append(sentence)
|
| 31 |
return key_concepts[:5] # Return top 5 key concepts
|
| 32 |
|
| 33 |
+
def generate_intelligent_question(self, concept, context, difficulty):
|
| 34 |
+
if difficulty == 'easy':
|
| 35 |
+
templates = [
|
| 36 |
+
f"What is {concept}?",
|
| 37 |
+
f"Describe {concept}.",
|
| 38 |
+
f"Define {concept}.",
|
| 39 |
+
f"What do you understand by {concept}?",
|
| 40 |
+
f"Give a simple explanation of {concept}."
|
| 41 |
+
]
|
| 42 |
+
elif difficulty == 'hard':
|
| 43 |
+
templates = [
|
| 44 |
+
f"In what way does {concept} reflect a broader implication?",
|
| 45 |
+
f"Critically analyze the role of {concept} in the given context.",
|
| 46 |
+
f"How can {concept} be interpreted in complex scenarios?",
|
| 47 |
+
f"What deeper insights does {concept} provide?",
|
| 48 |
+
f"Discuss the nuanced impact of {concept} in this context."
|
| 49 |
+
]
|
| 50 |
+
else: # medium
|
| 51 |
+
templates = [
|
| 52 |
+
f"What is the primary significance of {concept}?",
|
| 53 |
+
f"How does {concept} impact the broader context?",
|
| 54 |
+
f"What key role does {concept} play in the narrative?",
|
| 55 |
+
f"Explain the importance of {concept} in this context.",
|
| 56 |
+
f"What makes {concept} crucial to understanding the situation?"
|
| 57 |
+
]
|
| 58 |
+
return random.choice(templates)
|
| 59 |
|
| 60 |
+
def generate_contextual_distractors(self, correct_answer, context, difficulty):
|
| 61 |
"""Create semantically related but incorrect distractors"""
|
| 62 |
sentences = sent_tokenize(context)
|
| 63 |
distractors = []
|
|
|
|
| 64 |
potential_distractors = [sent for sent in sentences if correct_answer.lower() not in sent.lower() and len(sent.split()) > 3]
|
| 65 |
+
fallback_distractors = ["A partially related historical context","An alternative interpretation","A peripheral aspect of the main theme"]
|
| 66 |
# Generating diverse distractors
|
| 67 |
while len(distractors) < 3:
|
| 68 |
if potential_distractors:
|
|
|
|
| 69 |
distractor = random.choice(potential_distractors)
|
| 70 |
potential_distractors.remove(distractor)
|
| 71 |
words = word_tokenize(distractor)
|
| 72 |
+
if difficulty == 'easy':
|
| 73 |
+
phrase = ' '.join([w for w in words if w.lower() not in self.stop_words][:2])
|
| 74 |
+
elif difficulty == 'hard':
|
| 75 |
+
phrase = ' '.join([w for w in words if w.lower() not in self.stop_words][:5])
|
| 76 |
+
else: # medium
|
| 77 |
+
phrase = ' '.join([w for w in words if w.lower() not in self.stop_words][:3])
|
| 78 |
+
distractors.append(phrase.strip())
|
| 79 |
else:
|
| 80 |
+
distractors.append(random.choice(fallback_distractors))
|
|
|
|
|
|
|
| 81 |
return distractors
|
| 82 |
|
| 83 |
+
def generate_mcq(self, context, num_questions=3, difficulty='medium'):
|
| 84 |
"""Generate Multiple Choice Questions"""
|
| 85 |
# Validate context
|
| 86 |
if not context or len(context.split()) < 30:
|
| 87 |
raise ValueError("Context is too short. Provide more detailed text.")
|
| 88 |
|
|
|
|
| 89 |
mcq_questions = []
|
| 90 |
key_concepts = self.extract_key_concepts(context)
|
| 91 |
|
| 92 |
for concept in key_concepts[:num_questions]:
|
| 93 |
try:
|
| 94 |
+
question = self.generate_intelligent_question(concept, context, difficulty)
|
| 95 |
+
answer_result = self.qa_pipeline(question=question, context=context)
|
| 96 |
+
correct_answer = answer_result['answer']
|
| 97 |
+
distractors = self.generate_contextual_distractors(correct_answer, context, difficulty)
|
| 98 |
+
all_options = [correct_answer] + distractors
|
| 99 |
random.shuffle(all_options)
|
| 100 |
+
correct_index = all_options.index(correct_answer) # Determine correct option index
|
| 101 |
mcq_questions.append({"question": question,"options": all_options,"correct_answer": correct_index}) # Create MCQ
|
| 102 |
except Exception as e:
|
| 103 |
print(f"Error generating question: {e}")
|
|
|
|
| 107 |
generator = AdvancedMCQGenerator()
|
| 108 |
context = input("Enter context text: ")
|
| 109 |
num_questions = int(input("How many questions do you want? "))
|
| 110 |
+
difficulty = input("Choose difficulty (easy / medium / hard): ").lower().strip()
|
| 111 |
+
|
| 112 |
+
questions = generator.generate_mcq(context, num_questions, difficulty)
|
| 113 |
# Display and solve quiz
|
| 114 |
print("\n--- Quiz Started ---")
|
| 115 |
score = 0
|