EdBoy2202 commited on
Commit
b5da343
·
verified ·
1 Parent(s): 155cc1f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +20 -31
app.py CHANGED
@@ -46,39 +46,28 @@ def parse_quiz_content(quiz_content):
46
  question_section = quiz_parts[0].strip()
47
  answer_key_section = quiz_parts[1].strip()
48
 
49
- # --- Split into blocks by double newline ---
50
- blocks = re.split(r'\n\n+', question_section) # Split by one or more double newlines
51
-
52
- # --- Handle Potential Title (First Block) ---
53
- if blocks:
54
- first_block = blocks[0].strip()
55
- if not re.match(r'^\d+\.\s', first_block): # Check if the first block DOES NOT start with a question number
56
- # Assume the first block is the title and remove it
57
- blocks = blocks[1:] # Slice to remove the first block (title)
58
- else:
59
- st.warning("Unexpected format: First block starts with a question number. Title might not be removed correctly.")
60
-
61
-
62
- # --- Process Question Blocks ---
63
- for block in blocks:
64
- block = block.strip()
65
- if not block:
66
- continue
67
-
68
- lines = block.split('\n')
69
- # --- First line is question text (after removing bold markdown) ---
70
- question_text = re.sub(r'\*\*|\*', '', lines[0]).strip()
71
-
72
- options = {}
73
- for line in lines[1:]:
74
- line = line.strip()
75
- if re.match(r'^[A-D]\.\s', line): # Options are like "A. Elephant"
76
  option_letter = line[0]
77
- option_text = line[2:].strip()
78
- options[option_letter] = option_text
 
 
79
 
80
- if question_text: # Only append if we have a question text
81
- questions.append({'question': question_text, 'options': options})
82
 
83
 
84
  # --- Answer Key Parsing (No change needed) ---
 
46
  question_section = quiz_parts[0].strip()
47
  answer_key_section = quiz_parts[1].strip()
48
 
49
+ question_lines = question_section.strip().split('\n')
50
+ current_question = None
51
+ for line in question_lines:
52
+ line = line.strip()
53
+ if not line:
54
+ continue # Skip empty lines
55
+
56
+ if re.match(r'^\d+\.\s', line): # Start of a new question
57
+ if current_question: # Save previous question if exists
58
+ questions.append(current_question)
59
+ current_question = {'options': {}} # Start new question
60
+ current_question['question'] = re.sub(r'\*\*|\*', '', line).strip() # Question text, remove bold
61
+ elif re.match(r'^[A-D]\)\s', line) or re.match(r'^[A-D]\.\s', line): # Option line (handle both ')' and '.' after letter)
62
+ if current_question:
 
 
 
 
 
 
 
 
 
 
 
 
 
63
  option_letter = line[0]
64
+ option_text = line[2:].strip() # Remove "A) " or "A. "
65
+ current_question['options'][option_letter] = option_text
66
+ elif re.match(r'^\*\*.*?\*\*$', line) or re.match(r'^\*.*?\*$', line) : # Check for title again, just in case after split. Remove bold title if found in lines.
67
+ pass # Ignore title lines within question section if any.
68
 
69
+ if current_question: # Append the last question
70
+ questions.append(current_question)
71
 
72
 
73
  # --- Answer Key Parsing (No change needed) ---