Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -183,94 +183,82 @@ def handle_answer(answer, current_q):
|
|
| 183 |
if st.session_state.current_question_index >= 10:
|
| 184 |
st.session_state.current_step = 'review'
|
| 185 |
|
| 186 |
-
|
| 187 |
-
|
| 188 |
-
"""ν
μ€νΈ μ μ²λ¦¬: λΆνμν μ€κ΄νΈ μ κ±° λ° κΈ°λ³Έ ν΄λ¦¬λ"""
|
| 189 |
import re
|
|
|
|
|
|
|
|
|
|
| 190 |
|
| 191 |
-
|
| 192 |
-
|
| 193 |
-
|
| 194 |
-
latex_parts.append(match.group(0))
|
| 195 |
-
return f"LATEX_{len(latex_parts)-1}_PLACEHOLDER"
|
| 196 |
|
| 197 |
-
#
|
| 198 |
-
text =
|
|
|
|
| 199 |
|
| 200 |
-
#
|
| 201 |
-
|
|
|
|
| 202 |
|
| 203 |
-
#
|
| 204 |
-
|
| 205 |
-
|
| 206 |
|
| 207 |
-
|
| 208 |
-
|
| 209 |
-
|
| 210 |
-
|
| 211 |
-
|
| 212 |
-
|
| 213 |
-
# LaTeX λͺ
λ Ήμ΄λ₯Ό μν μ΄μ€μΌμ΄ν μ²λ¦¬
|
| 214 |
-
latex_commands = {
|
| 215 |
-
r'\div': r'\\div',
|
| 216 |
-
r'\ldots': r'\\ldots',
|
| 217 |
-
r'\dots': r'\\ldots',
|
| 218 |
-
r'\cdot': r'\\cdot',
|
| 219 |
-
r'\times': r'\\times'
|
| 220 |
}
|
| 221 |
|
| 222 |
-
for old, new in
|
| 223 |
text = text.replace(old, new)
|
| 224 |
|
| 225 |
-
#
|
| 226 |
-
def
|
| 227 |
content = match.group(1)
|
| 228 |
-
|
| 229 |
-
|
| 230 |
-
|
| 231 |
-
return f'({content})'
|
| 232 |
|
| 233 |
-
#
|
| 234 |
-
|
|
|
|
| 235 |
|
| 236 |
return text
|
| 237 |
|
| 238 |
-
def
|
| 239 |
-
"""
|
| 240 |
-
#
|
| 241 |
-
|
| 242 |
|
| 243 |
-
|
| 244 |
-
|
| 245 |
-
text =
|
|
|
|
| 246 |
|
| 247 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 248 |
|
| 249 |
def display_math_content(content: str, is_question: bool = True):
|
| 250 |
-
"""μν λ΄μ©
|
| 251 |
-
formatted_content = format_math_expression(content)
|
| 252 |
-
|
| 253 |
if is_question:
|
| 254 |
-
|
| 255 |
else:
|
| 256 |
-
|
| 257 |
-
|
| 258 |
-
|
| 259 |
-
def display_math_question(question: str):
|
| 260 |
-
"""μν λ¬Έμ λ₯Ό LaTeX νμμΌλ‘ νμ"""
|
| 261 |
-
formatted_question = format_math_expression(question)
|
| 262 |
-
st.markdown(formatted_question)
|
| 263 |
-
|
| 264 |
-
def format_math_choices(choices: dict) -> dict:
|
| 265 |
-
"""μ νμ§μ μν ννμμ LaTeX νμμΌλ‘ λ³ν"""
|
| 266 |
-
return {k: format_math_expression(v) for k, v in choices.items()}
|
| 267 |
-
|
| 268 |
-
def display_math_answer(answer: str):
|
| 269 |
-
"""μν λ΅μμ LaTeX οΏ½οΏ½μμΌλ‘ νμ"""
|
| 270 |
-
formatted_answer = format_math_expression(answer)
|
| 271 |
-
# κ΄νΈ μμ μμλ§ LaTeXλ‘ μ²λ¦¬
|
| 272 |
-
st.markdown(formatted_answer)
|
| 273 |
-
|
| 274 |
|
| 275 |
def main():
|
| 276 |
"""λ©μΈ μ ν리μΌμ΄μ
λ‘μ§"""
|
|
|
|
| 183 |
if st.session_state.current_question_index >= 10:
|
| 184 |
st.session_state.current_step = 'review'
|
| 185 |
|
| 186 |
+
def format_latex_expression(text: str) -> str:
|
| 187 |
+
"""μν ννμμ LaTeX νμμΌλ‘ λ³ν"""
|
|
|
|
| 188 |
import re
|
| 189 |
+
|
| 190 |
+
# μ΄ν€λ¦μ²΄λ‘ νμν μν μ©μ΄λ€
|
| 191 |
+
math_terms = ['decimalplaces', 'rounded to', 'What is']
|
| 192 |
|
| 193 |
+
def replace_math_terms(match):
|
| 194 |
+
term = match.group(0)
|
| 195 |
+
return f'\\text{{{term}}}'
|
|
|
|
|
|
|
| 196 |
|
| 197 |
+
# ν
μ€νΈ μ μ²λ¦¬
|
| 198 |
+
text = text.replace('\\(', '\\left(')
|
| 199 |
+
text = text.replace('\\)', '\\right)')
|
| 200 |
|
| 201 |
+
# μν μ©μ΄λ₯Ό LaTeX ν
μ€νΈλ‘ λ³ν
|
| 202 |
+
for term in math_terms:
|
| 203 |
+
text = text.replace(term, f'\\text{{{term}}}')
|
| 204 |
|
| 205 |
+
# λ¬λ¬ κΈ°νΈ($) μ²λ¦¬
|
| 206 |
+
text = text.replace('$(', '\\$\\left(')
|
| 207 |
+
text = text.replace(')$', '\\right)\\$')
|
| 208 |
|
| 209 |
+
# νΉμ ν¨ν΄ μ²λ¦¬
|
| 210 |
+
replacements = {
|
| 211 |
+
r'\\div': '\\div',
|
| 212 |
+
r'\\ldots': '\\ldots',
|
| 213 |
+
r'\\dots': '\\dots',
|
| 214 |
+
r'\\times': '\\times',
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 215 |
}
|
| 216 |
|
| 217 |
+
for old, new in replacements.items():
|
| 218 |
text = text.replace(old, new)
|
| 219 |
|
| 220 |
+
# κ΄νΈλ‘ λλ¬μΈμΈ μμ μ²λ¦¬
|
| 221 |
+
def process_math(match):
|
| 222 |
content = match.group(1)
|
| 223 |
+
return f'\\left({content}\\right)'
|
| 224 |
+
|
| 225 |
+
text = re.sub(r'\(([\d\.\s]+)\)', process_math, text)
|
|
|
|
| 226 |
|
| 227 |
+
# μ΅μ’
LaTeX μμμΌλ‘ κ°μΈκΈ°
|
| 228 |
+
if not text.startswith('$') and not text.endswith('$'):
|
| 229 |
+
text = f'$${text}$$'
|
| 230 |
|
| 231 |
return text
|
| 232 |
|
| 233 |
+
def format_question(question: str) -> str:
|
| 234 |
+
"""λ¬Έμ ν
μ€νΈλ₯Ό LaTeX νμμΌλ‘ λ³ν"""
|
| 235 |
+
# μμμ΄ μλ ν
μ€νΈ μ²λ¦¬
|
| 236 |
+
colored_text_pattern = r'\\textcolor{([^}]+)}{([^}]+)}'
|
| 237 |
|
| 238 |
+
def process_colored_text(match):
|
| 239 |
+
color = match.group(1)
|
| 240 |
+
text = match.group(2)
|
| 241 |
+
return f'\\textcolor{{{color}}}{{{format_latex_expression(text)}}}'
|
| 242 |
|
| 243 |
+
question = re.sub(colored_text_pattern, process_colored_text, question)
|
| 244 |
+
return format_latex_expression(question)
|
| 245 |
+
|
| 246 |
+
def format_answer_choice(choice: str) -> str:
|
| 247 |
+
"""μ νμ§ ν
μ€νΈλ₯Ό LaTeX νμμΌλ‘ λ³ν"""
|
| 248 |
+
# λ¬λ¬ κΈ°νΈκ° ν¬ν¨λ μ νμ§ νΉλ³ μ²λ¦¬
|
| 249 |
+
if '$' in choice:
|
| 250 |
+
# λ¬λ¬ κΈ°νΈλ₯Ό LaTeX λͺ
λ Ήμ΄λ‘ λ³ν
|
| 251 |
+
choice = choice.replace('$', '\\$')
|
| 252 |
+
return format_latex_expression(choice)
|
| 253 |
|
| 254 |
def display_math_content(content: str, is_question: bool = True):
|
| 255 |
+
"""μν λ΄μ© νμ"""
|
|
|
|
|
|
|
| 256 |
if is_question:
|
| 257 |
+
formatted_content = format_question(content)
|
| 258 |
else:
|
| 259 |
+
formatted_content = format_answer_choice(content)
|
| 260 |
+
|
| 261 |
+
return formatted_content
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 262 |
|
| 263 |
def main():
|
| 264 |
"""λ©μΈ μ ν리μΌμ΄μ
λ‘μ§"""
|