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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +43 -22
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(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
 
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():