Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -204,46 +204,67 @@ def format_answer_choice(choice: str) -> str:
|
|
| 204 |
choice = choice.replace('$', '\\$')
|
| 205 |
return format_latex_expression(choice)
|
| 206 |
|
| 207 |
-
def display_math_content(
|
| 208 |
"""μν λ΄μ©μ νλ©΄μ νμ"""
|
| 209 |
-
|
| 210 |
-
|
|
|
|
| 211 |
|
| 212 |
def format_latex_expression(text: str) -> str:
|
| 213 |
-
"""
|
| 214 |
import re
|
| 215 |
|
| 216 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 217 |
latex_parts = []
|
| 218 |
def save_latex(match):
|
| 219 |
latex_parts.append(match.group(0))
|
| 220 |
return f"LATEX_{len(latex_parts)-1}_PLACEHOLDER"
|
|
|
|
| 221 |
text = re.sub(r'\$\$.*?\$\$', save_latex, text)
|
| 222 |
|
| 223 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
| 224 |
text = re.sub(r'([a-z])([A-Z])', r'\1 \2', text)
|
| 225 |
-
text = re.sub(r'([
|
| 226 |
-
text = re.sub(r'(
|
| 227 |
|
| 228 |
-
#
|
| 229 |
-
|
| 230 |
-
|
| 231 |
-
# μ§μ μ²λ¦¬
|
| 232 |
-
if '^' in content:
|
| 233 |
-
base, exp = content.split('^')
|
| 234 |
-
return f'${base}^{{{exp}}}$'
|
| 235 |
-
# κ³±μ
, λ§μ
λ± μ²λ¦¬
|
| 236 |
-
content = content.replace('Γ', '\\times')
|
| 237 |
-
content = content.replace('+', '+')
|
| 238 |
-
return f'${content}$'
|
| 239 |
|
| 240 |
-
|
| 241 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 242 |
|
| 243 |
-
|
|
|
|
|
|
|
| 244 |
for i, latex in enumerate(latex_parts):
|
| 245 |
text = text.replace(f"LATEX_{i}_PLACEHOLDER", latex)
|
| 246 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 247 |
return text
|
| 248 |
|
| 249 |
def main():
|
|
|
|
| 204 |
choice = choice.replace('$', '\\$')
|
| 205 |
return format_latex_expression(choice)
|
| 206 |
|
| 207 |
+
def display_math_content(content: str):
|
| 208 |
"""μν λ΄μ©μ νλ©΄μ νμ"""
|
| 209 |
+
formatted_content = format_latex_expression(content)
|
| 210 |
+
# LaTeX λ λλ§μ μν μΆκ° μ€μ
|
| 211 |
+
st.markdown(formatted_content, unsafe_allow_html=True)
|
| 212 |
|
| 213 |
def format_latex_expression(text: str) -> str:
|
| 214 |
+
"""볡μ‘ν LaTeX μμκ³Ό νΉμ κΈ°νΈλ₯Ό μ²λ¦¬νλ ν¨μ"""
|
| 215 |
import re
|
| 216 |
|
| 217 |
+
# LaTeX νΉμ λͺ
λ Ήμ΄ λ§€ν
|
| 218 |
+
latex_commands = {
|
| 219 |
+
r'\left': r'\\left',
|
| 220 |
+
r'\right': r'\\right',
|
| 221 |
+
r'\bigcirc': r'\\bigcirc',
|
| 222 |
+
r'\square': r'\\square',
|
| 223 |
+
r'\quad': r'\\quad'
|
| 224 |
+
}
|
| 225 |
+
|
| 226 |
+
# 1. μ΄λ―Έ μ‘΄μ¬νλ LaTeX μμ 보쑴
|
| 227 |
latex_parts = []
|
| 228 |
def save_latex(match):
|
| 229 |
latex_parts.append(match.group(0))
|
| 230 |
return f"LATEX_{len(latex_parts)-1}_PLACEHOLDER"
|
| 231 |
+
|
| 232 |
text = re.sub(r'\$\$.*?\$\$', save_latex, text)
|
| 233 |
|
| 234 |
+
# 2. νΉμ λͺ
λ Ήμ΄ μ²λ¦¬
|
| 235 |
+
for cmd, latex_cmd in latex_commands.items():
|
| 236 |
+
text = text.replace(cmd, latex_cmd)
|
| 237 |
+
|
| 238 |
+
# 3. λΆμ΄μλ λ¨μ΄ λΆλ¦¬
|
| 239 |
text = re.sub(r'([a-z])([A-Z])', r'\1 \2', text)
|
| 240 |
+
text = re.sub(r'([A-Za-z])(\d)', r'\1 \2', text)
|
| 241 |
+
text = re.sub(r'(\d)([A-Za-z])', r'\1 \2', text)
|
| 242 |
|
| 243 |
+
# 4. λ¬Έμ₯ λΆμ
|
| 244 |
+
sentences = text.split('$$')
|
| 245 |
+
formatted_sentences = []
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 246 |
|
| 247 |
+
for i, sentence in enumerate(sentences):
|
| 248 |
+
if i % 2 == 0: # μΌλ° ν
μ€νΈ
|
| 249 |
+
# μμμ΄ μλ λΆλΆμ νΉμ λ¬Έμ μ²λ¦¬
|
| 250 |
+
for cmd, latex_cmd in latex_commands.items():
|
| 251 |
+
if cmd in sentence:
|
| 252 |
+
sentence = f"$${sentence}$$"
|
| 253 |
+
break
|
| 254 |
+
formatted_sentences.append(sentence)
|
| 255 |
+
else: # μμ
|
| 256 |
+
formatted_sentences.append(f"$${sentence}$$")
|
| 257 |
|
| 258 |
+
text = ''.join(formatted_sentences)
|
| 259 |
+
|
| 260 |
+
# 5. LaTeX μμ 볡μ
|
| 261 |
for i, latex in enumerate(latex_parts):
|
| 262 |
text = text.replace(f"LATEX_{i}_PLACEHOLDER", latex)
|
| 263 |
|
| 264 |
+
# 6. λ§μ§λ§ μ 리
|
| 265 |
+
text = text.replace('\\\\', '\\') # μ€λ³΅λ λ°±μ¬λμ μ κ±°
|
| 266 |
+
text = re.sub(r'\s+', ' ', text) # μ¬λ¬ κ°μ 곡백μ νλλ‘
|
| 267 |
+
|
| 268 |
return text
|
| 269 |
|
| 270 |
def main():
|