-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdata-process.py
More file actions
176 lines (142 loc) · 6.49 KB
/
data-process.py
File metadata and controls
176 lines (142 loc) · 6.49 KB
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
import json, random, re
from fractions import Fraction
from bs4 import BeautifulSoup
output = {
"math": [],
"english": []
}
# Output structure
"""
let flashcard = {
question: question.question,
paragraph: question.paragraph,
choices: Object.keys(question.choices).map(key => (`${key}): ${question.choices[key]}`)),
answer: question.correct_answer,
explanation: question.explanation
};
"""
with open("cb-digital-questions.json", "r") as f:
data = json.load(f)
mathCount = 0
englishCount = 0
def cleanUp(text: str) -> str:
if isinstance(text, dict):
if "answer" in text:
if type(text["answer"]) == list and len(text["answer"]) == 1:
text["answer"] = text["answer"][0]
if isinstance(text, str):
pass
elif isinstance(text, dict):
return {k: cleanUp(v) for k, v in text.items()}
elif isinstance(text, list):
return [cleanUp(i) for i in text]
properCss = " color: black !important; overflow-wrap: break-word !important; display: inline !important; text-wrap-mode: wrap !important;"
soup = BeautifulSoup(text, 'html.parser')
color = "black"
for tag in soup.find_all(['p', 'span']):
if tag.has_attr('style'):
tag['style'] += "; "+properCss
else:
tag['style'] = properCss
for i in range(10):
tag["style"] = tag["style"].replace(";;", ";")
html = soup.prettify()
html = html.replace("<mfenced>", '<mrow> <mo fence="true">(</mo>').replace("</mfenced>", '<mo fence="true">)</mo></mrow>')
html = re.sub(r'([ABCDabcd]\):)(\n+)', r'\1', html, count=1)
return html
for id, question in data.items():
flashcard = {}
content = question["content"]
if question["module"] == "math":
mathCount += 1
elif question["module"] == "english":
englishCount += 1
if content.get('answer') and content.get('answer').get('choices') and question["module"] == "math":
# Multiple choice math question
choices = [
f"{key}): {content['answer']['choices'][key].get('body', '')}"
for key in content['answer']['choices']
]
# Only include if there's a correct answer
if content['answer'].get('correct_choice'):
flashcard = {
'id': id,
'question': content.get('prompt', content.get('stem', '')),
'paragraph': content.get('body', ''), # Extract paragraph if needed
'choices': choices,
'answer': content['answer'].get('correct_choice', ''),
'explanation': content['answer'].get('rationale', '')
}
output[question["module"]].append(cleanUp(flashcard))
elif content.get('correct_answer') and (len(content.get('answerOptions')) > 0) and question["module"] == "math":
# Multiple choice math question
choices = [
f"{key}): {content['answerOptions'][idx].get('content', '')}"
for idx, key in enumerate("ABCDEFGHIJKLMNOPQRSTUVWXYZ"[:len(content['answerOptions'])])
]
# Only include if there's a correct answer
if content['correct_answer']:
flashcard = {
'id': id,
'question': content.get('prompt', content.get('stem', '')),
'paragraph': content.get('body', ''), # Extract paragraph if needed
'choices': choices,
'answer': content['correct_answer'],
'explanation': content['rationale']
}
output[question["module"]].append(cleanUp(flashcard))
elif content.get('correct_answer') and content.get("type", "") == "spr" and question["module"] == "math":
# Input type math question, needs to be converted to multiple choice
answer = content.get('correct_answer')[0]
# print(answer)
answerNum = float(Fraction(answer))
precision = len(str(answerNum).split('.')[1])
answerLetter = random.choice("ABCD")
answerStr = f"{answerLetter}): {answerNum:.{precision}f}"
choices = [
# f"{key}): {content['answer']['choices'][key].get('body', '')}"
# for key in content['answer']['choices']
f"{letter}): {(answerNum * random.uniform(0.8, 1.2)):.{precision}f}" if letter != answerLetter else answerStr for letter in "ABCD"
]
flashcard = {
'id': id,
'question': content.get('prompt', content.get('stem', '')),
'paragraph': content.get('body', ''), # Extract paragraph if needed
'choices': choices,
'answer': answerStr,
'explanation': content.get('rationale', '')
}
output[question["module"]].append(cleanUp(flashcard))
else:
# Process English questions
if (question.get("module") == "english" and
content.get("type") == "mcq" and
content.get("answerOptions") and
content.get("correct_answer")):
# Only process if there are multiple choices and a correct answer
if len(content.get("answerOptions", [])) > 0 and len(content.get("correct_answer", [])) > 0:
# Extract stimulus as paragraph if available
paragraph = content.get("stimulus", "")
# Format choices
choices = []
for i, option in enumerate(content.get("answerOptions", [])):
# Use index to create letter (0->A, 1->B, etc.)
letter = chr(65 + i) # ASCII: A=65, B=66, etc.
choices.append(f"{letter}): {option.get('content', '')}")
# Get correct answer
answer = content.get("correct_answer", [""])[0]
flashcard = {
'id': id,
'question': content.get('stem', ''),
'paragraph': paragraph,
'choices': choices,
'answer': answer,
'explanation': content.get('rationale', '')
}
output["english"].append(cleanUp(flashcard))
print(f"Math questions: {len(output['math'])}/{mathCount}")
print(f"English questions: {len(output['english'])}/{englishCount}")
with open("questions.json", "w+") as f:
json.dump(output, f, indent=4)
with open("./extension/chrome/questions.json", "w+") as f:
json.dump(output, f, indent=4)