quizLM / app.py
EdBoy2202's picture
Update app.py
431839d verified
raw
history blame
3.57 kB
def parse_quiz_content(quiz_content):
"""Parses the quiz content and answer key into a structured format."""
questions = []
answer_key_dict = {}
quiz_parts = quiz_content.split("Answer Key")
if len(quiz_parts) != 2:
st.warning("Could not reliably separate quiz questions and answer key. Parsing might be imperfect.")
return None, None
question_section = quiz_parts[0].strip() # Strip whitespace from question section
answer_key_section = quiz_parts[1].strip() # Strip whitespace from answer key section
# --- Improved Question Block Splitting ---
# Split question section by lines starting with a number followed by a dot and a space (e.g., "1. ")
question_blocks = re.split(r'\n(?=\d+\.\s)', question_section)
for block in question_blocks:
block = block.strip() # Strip whitespace for each block
if not block:
continue # Skip empty blocks
# --- Improved Question and Options Parsing within each block ---
lines = block.split('\n')
question_text = lines[0].strip() # First line is the question
options = {}
for line in lines[1:]: # Start from the second line to parse options
line = line.strip()
if re.match(r'^[A-D]\)\s', line): # Match options starting with A), B), C), D) and a space
option_letter = line[0]
option_text = line[2:].strip() # Take text after the "letter) "
options[option_letter] = option_text
elif re.match(r'^[A-D]\.\s', line): # Also handle options starting with A., B., C., D. and a space (if Gemini uses this format sometimes)
option_letter = line[0]
option_text = line[2:].strip()
options[option_letter] = option_text
if question_text: # Only append if we have a question text (to avoid empty questions from title etc.)
questions.append({'question': question_text, 'options': options})
# --- Answer Key Parsing (No significant change needed, but strip whitespace) ---
answer_lines = answer_key_section.strip().split('\n')
for line in answer_lines:
line = line.strip()
match = re.match(r'(\d+)\.\s*([A-D])', line) # Regex to find question number and answer letter
if match:
question_num = int(match.group(1)) - 1 # Adjust to 0-based index
correct_answer = match.group(2)
answer_key_dict[question_num] = correct_answer
# Basic validation (similar to before)
if not questions or not answer_key_dict:
st.error("Error parsing quiz content. Please try again or check the generated format.")
return None, None
if len(questions) != len(answer_key_dict):
st.warning(f"Number of questions parsed ({len(questions)}) does not match number of answers in answer key ({len(answer_key_dict)}). Parsing might be incomplete.")
# Combine parsed questions and answer key into quiz_data (similar to before)
quiz_data_list = []
for i, q_data in enumerate(questions):
correct_answer = answer_key_dict.get(i)
if correct_answer:
quiz_data_list.append({
'question': q_data['question'],
'options': q_data['options'],
'correct_answer': correct_answer
})
else:
st.warning(f"Could not find correct answer for question {i+1} in the answer key.")
return None, None # Inconsistent data, better to stop
return quiz_data_list, answer_key_dict