File size: 3,571 Bytes
ff40b9b
 
 
 
 
 
 
 
 
 
431839d
 
 
 
 
 
ff40b9b
 
431839d
 
ff40b9b
431839d
 
 
 
 
ff40b9b
431839d
ff40b9b
431839d
 
 
 
 
ff40b9b
 
 
 
431839d
 
 
 
 
 
ff40b9b
 
 
 
 
 
 
 
 
431839d
ff40b9b
 
 
 
431839d
ff40b9b
 
431839d
ff40b9b
 
 
 
 
 
 
 
 
 
 
 
 
431839d
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
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