Jintonic92 commited on
Commit
2e090e0
Β·
verified Β·
1 Parent(s): 15e0ac2

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +40 -37
app.py CHANGED
@@ -206,57 +206,60 @@ def clean_text(text: str) -> str:
206
 
207
  return text
208
 
209
- def format_math_expression(text: str) -> str:
210
- """μˆ˜ν•™ ν‘œν˜„μ‹μ„ LaTeX ν˜•μ‹μœΌλ‘œ λ³€ν™˜"""
211
  import re
212
 
213
- # ν…μŠ€νŠΈ μ „μ²˜λ¦¬
214
- text = clean_text(text)
215
-
216
- # κΈ°μ‘΄ LaTeX ν‘œν˜„μ‹μ€ 보쑴
217
- if text.startswith('$$') and text.endswith('$$'):
218
- return text
219
-
220
- # κΈ°λ³Έ μˆ˜ν•™ 기호 λ³€ν™˜
221
- replacements = {
222
- 'Γ·': '\\div',
223
- 'Γ—': '\\times',
224
- '*': '\\cdot',
225
- 'β‰ ': '\\neq',
226
- '≀': '\\leq',
227
- 'β‰₯': '\\geq',
228
- 'β†’': '\\rightarrow',
229
- '←': '\\leftarrow',
230
- 'Β±': '\\pm',
231
- '∞': '\\infty',
232
- '√': '\\sqrt',
233
  }
234
 
235
- # λΆ„μˆ˜ νŒ¨ν„΄ μ°ΎκΈ° 및 λ³€ν™˜
236
- text = re.sub(r'(\d+)/(\d+)', r'\\frac{\1}{\2}', text)
237
-
238
- # μˆ˜ν•™ 기호 λ³€ν™˜
239
- for old, new in replacements.items():
240
  text = text.replace(old, new)
241
 
242
- # μ§€μˆ˜ ν‘œν˜„ 처리 (예: x^2)
243
- text = re.sub(r'(\d+)\^(\d+)', r'$$\1^{\2}$$', text)
 
 
 
 
 
 
 
 
244
 
245
  return text
246
 
247
- def display_math_question(question: str):
248
- """μˆ˜ν•™ 문제λ₯Ό LaTeX ν˜•μ‹μœΌλ‘œ ν‘œμ‹œ"""
249
- formatted_question = format_math_expression(question)
250
- st.markdown(formatted_question)
 
 
 
 
 
 
251
 
252
- def format_math_choices(choices: dict) -> dict:
253
- """μ„ νƒμ§€μ˜ μˆ˜ν•™ ν‘œν˜„μ‹μ„ LaTeX ν˜•μ‹μœΌλ‘œ λ³€ν™˜"""
254
- return {k: format_math_expression(v) for k, v in choices.items()}
 
 
 
 
 
 
255
 
256
  def display_math_question(question: str):
257
  """μˆ˜ν•™ 문제λ₯Ό LaTeX ν˜•μ‹μœΌλ‘œ ν‘œμ‹œ"""
258
  formatted_question = format_math_expression(question)
259
- st.markdown(formatted_question, unsafe_allow_html=True)
260
 
261
  def format_math_choices(choices: dict) -> dict:
262
  """μ„ νƒμ§€μ˜ μˆ˜ν•™ ν‘œν˜„μ‹μ„ LaTeX ν˜•μ‹μœΌλ‘œ λ³€ν™˜"""
 
206
 
207
  return text
208
 
209
+ def clean_text(text: str) -> str:
210
+ """ν…μŠ€νŠΈ μ „μ²˜λ¦¬: λΆˆν•„μš”ν•œ 문자 제거 및 LaTeX ν˜•μ‹μœΌλ‘œ λ³€ν™˜"""
211
  import re
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 latex_commands.items():
 
 
 
 
223
  text = text.replace(old, new)
224
 
225
+ # κ΄„ν˜Έ μ•ˆμ˜ μˆ˜μ‹μ„ LaTeX둜 λ³€ν™˜
226
+ def convert_to_latex(match):
227
+ content = match.group(1)
228
+ # 이미 $둜 λ‘˜λŸ¬μ‹Έμ—¬ μžˆλŠ”μ§€ 확인
229
+ if not content.strip().startswith('$') and not content.strip().endswith('$'):
230
+ return f'$({content})$'
231
+ return f'({content})'
232
+
233
+ # κ΄„ν˜Έ μ•ˆμ˜ λ‚΄μš©μ„ LaTeX둜 λ³€ν™˜
234
+ text = re.sub(r'\((.*?)\)', convert_to_latex, text)
235
 
236
  return text
237
 
238
+ def format_math_expression(text: str) -> str:
239
+ """μˆ˜ν•™ ν‘œν˜„μ‹μ„ LaTeX ν˜•μ‹μœΌλ‘œ λ³€ν™˜"""
240
+ # κΈ°λ³Έ 클리닝
241
+ text = clean_text(text)
242
+
243
+ # LaTeX μˆ˜μ‹ 기호 μΆ”κ°€
244
+ if not text.startswith('$') and not text.endswith('$'):
245
+ text = f'${text}$'
246
+
247
+ return text
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
+ st.markdown(formatted_content)
255
+ else:
256
+ # μ„ νƒμ§€μ˜ 경우 μΆ”κ°€ ν¬λ§·νŒ… ν•„μš”ν•  수 있음
257
+ return formatted_content
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 ν˜•μ‹μœΌλ‘œ λ³€ν™˜"""