|
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() |
|
answer_key_section = quiz_parts[1].strip() |
|
|
|
|
|
|
|
question_blocks = re.split(r'\n(?=\d+\.\s)', question_section) |
|
|
|
for block in question_blocks: |
|
block = block.strip() |
|
if not block: |
|
continue |
|
|
|
|
|
lines = block.split('\n') |
|
question_text = lines[0].strip() |
|
|
|
options = {} |
|
for line in lines[1:]: |
|
line = line.strip() |
|
if re.match(r'^[A-D]\)\s', line): |
|
option_letter = line[0] |
|
option_text = line[2:].strip() |
|
options[option_letter] = option_text |
|
elif re.match(r'^[A-D]\.\s', line): |
|
option_letter = line[0] |
|
option_text = line[2:].strip() |
|
options[option_letter] = option_text |
|
|
|
|
|
if question_text: |
|
questions.append({'question': question_text, 'options': options}) |
|
|
|
|
|
|
|
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) |
|
if match: |
|
question_num = int(match.group(1)) - 1 |
|
correct_answer = match.group(2) |
|
answer_key_dict[question_num] = correct_answer |
|
|
|
|
|
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.") |
|
|
|
|
|
|
|
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 |
|
|
|
return quiz_data_list, answer_key_dict |