Jintonic92 commited on
Commit
89cb988
Β·
verified Β·
1 Parent(s): cd93920

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +27 -33
app.py CHANGED
@@ -204,51 +204,45 @@ def format_answer_choice(choice: str) -> str:
204
  choice = choice.replace('$', '\\$')
205
  return format_latex_expression(choice)
206
 
207
- def display_math_content(content: str, is_question: bool = True):
208
  """μˆ˜ν•™ λ‚΄μš©μ„ 화면에 ν‘œμ‹œ"""
209
- formatted_content = format_latex_expression(content)
210
- st.markdown(formatted_content, unsafe_allow_html=True)
211
 
212
  def format_latex_expression(text: str) -> str:
213
  """μˆ˜ν•™ ν‘œν˜„μ‹μ„ LaTeX ν˜•μ‹μœΌλ‘œ λ³€ν™˜"""
214
  import re
215
-
216
- # μ΄νƒ€λ¦­μ²΄λ‘œ ν‘œμ‹œν•  μˆ˜ν•™ μš©μ–΄λ“€
217
- math_terms = ['decimalplaces', 'rounded to', 'What is']
218
-
219
- # ν…μŠ€νŠΈ μ „μ²˜λ¦¬
220
- text = text.replace('\\(', '\\left(')
221
- text = text.replace('\\)', '\\right)')
222
-
223
- # μˆ˜ν•™ μš©μ–΄λ₯Ό LaTeX ν…μŠ€νŠΈλ‘œ λ³€ν™˜
224
- for term in math_terms:
225
- text = text.replace(term, f'\\text{{{term}}}')
226
-
227
- # λ‹¬λŸ¬ 기호($) 처리
228
- text = text.replace('$(', '\\$\\left(')
229
- text = text.replace(')$', '\\right)\\$')
230
 
231
- # 특수 νŒ¨ν„΄ 처리
232
- replacements = {
233
- r'\div': '\\div',
234
- r'\ldots': '\\ldots',
235
- r'\dots': '\\dots',
236
- r'\times': '\\times',
237
- }
238
 
239
- for old, new in replacements.items():
240
- text = text.replace(old, new)
 
 
241
 
242
- # κ΄„ν˜Έλ‘œ λ‘˜λŸ¬μ‹ΈμΈ μˆ˜μ‹ 처리
243
  def process_math(match):
244
  content = match.group(1)
245
- return f'\\left({content}\\right)'
 
 
 
 
 
 
 
246
 
247
- text = re.sub(r'\(([\d\.\s]+)\)', process_math, text)
 
248
 
249
- # μ΅œμ’… LaTeX μˆ˜μ‹μœΌλ‘œ 감싸기
250
- if not text.startswith('$') and not text.endswith('$'):
251
- text = f'$${text}$$'
252
 
253
  return text
254
 
 
204
  choice = choice.replace('$', '\\$')
205
  return format_latex_expression(choice)
206
 
207
+ def display_math_content(text: str):
208
  """μˆ˜ν•™ λ‚΄μš©μ„ 화면에 ν‘œμ‹œ"""
209
+ formatted_text = format_latex_expression(text)
210
+ st.markdown(formatted_text, unsafe_allow_html=True)
211
 
212
  def format_latex_expression(text: str) -> str:
213
  """μˆ˜ν•™ ν‘œν˜„μ‹μ„ LaTeX ν˜•μ‹μœΌλ‘œ λ³€ν™˜"""
214
  import re
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
215
 
216
+ # κΈ°μ‘΄ LaTeX μˆ˜μ‹ 보쑴
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
+ # 단어 뢄리 (예: "Tocalculate" -> "To calculate")
224
+ text = re.sub(r'([a-z])([A-Z])', r'\1 \2', text)
225
+ text = re.sub(r'([a-zA-Z])([0-9])', r'\1 \2', text)
226
+ text = re.sub(r'([0-9])([a-zA-Z])', r'\1 \2', text)
227
 
228
+ # μˆ˜ν•™ ν‘œν˜„μ‹ 처리
229
  def process_math(match):
230
  content = match.group(1)
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
+ text = re.sub(r'\((.*?)\)', process_math, text)
242
 
243
+ # LaTeX λΆ€λΆ„ 볡원
244
+ for i, latex in enumerate(latex_parts):
245
+ text = text.replace(f"LATEX_{i}_PLACEHOLDER", latex)
246
 
247
  return text
248