Jintonic92 commited on
Commit
15e0ac2
ยท
verified ยท
1 Parent(s): 2043ed4

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +39 -31
app.py CHANGED
@@ -184,25 +184,44 @@ def handle_answer(answer, current_q):
184
  st.session_state.current_step = 'review'
185
 
186
  # ์ˆ˜์‹ ํ‘œํ˜„
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
187
  def format_math_expression(text: str) -> str:
188
  """์ˆ˜ํ•™ ํ‘œํ˜„์‹์„ LaTeX ํ˜•์‹์œผ๋กœ ๋ณ€ํ™˜"""
189
  import re
190
 
191
- # LaTeX ํ‘œ ์ฒ˜๋ฆฌ
192
- def format_table(match):
193
- table_content = match.group(1)
194
- # ํ‘œ ๋‚ด์šฉ์„ LaTeX ํ˜•์‹์œผ๋กœ ์ •๋ฆฌ
195
- formatted_table = table_content.replace('\\\\', '\n')
196
- return f'$$\\begin{{array}}{{|c|c|}} \\hline {formatted_table} \\hline \\end{{array}}$$'
197
 
198
- # ๊ธฐ์กด LaTeX ํ‘œํ˜„ ๋ณด์กด
199
- text = re.sub(r'\\begin{tabular}(.*?)\\end{tabular}', format_table, text, flags=re.DOTALL)
 
200
 
201
  # ๊ธฐ๋ณธ ์ˆ˜ํ•™ ๊ธฐํ˜ธ ๋ณ€ํ™˜
202
  replacements = {
203
  'รท': '\\div',
204
  'ร—': '\\times',
205
- '*': '\\cdot', # ๊ณฑ์…ˆ ์ 
206
  'โ‰ ': '\\neq',
207
  'โ‰ค': '\\leq',
208
  'โ‰ฅ': '\\geq',
@@ -216,35 +235,24 @@ def format_math_expression(text: str) -> str:
216
  # ๋ถ„์ˆ˜ ํŒจํ„ด ์ฐพ๊ธฐ ๋ฐ ๋ณ€ํ™˜
217
  text = re.sub(r'(\d+)/(\d+)', r'\\frac{\1}{\2}', text)
218
 
219
- # y=mx+b ํ˜•ํƒœ์˜ ๋ฐฉ์ •์‹ ์ฒ˜๋ฆฌ
220
- text = re.sub(r'y\s*=\s*([-\d.]+)x\s*([+-]\s*\d+)?', r'y = \1x\2', text)
221
-
222
- # ํ‘œํ˜„์‹ ๋‚ด์˜ ๋ณ€์ˆ˜ ์ดํƒค๋ฆญ์ฒด๋กœ ๋ณ€ํ™˜
223
- text = re.sub(r'([a-zA-Z])(\d*)', r'{\1}\2', text)
224
-
225
  # ์ˆ˜ํ•™ ๊ธฐํ˜ธ ๋ณ€ํ™˜
226
  for old, new in replacements.items():
227
  text = text.replace(old, new)
228
 
229
- # ๊ด„ํ˜ธ ์•ˆ์˜ ์ˆ˜์‹์„ LaTeX๋กœ ์ฒ˜๋ฆฌ
230
- def wrap_math(match):
231
- expr = match.group(1)
232
- # ์ด๋ฏธ LaTeX ๋‹ฌ๋Ÿฌ ๊ธฐํ˜ธ๊ฐ€ ์žˆ๋Š”์ง€ ํ™•์ธ
233
- if not expr.startswith('$') and not expr.endswith('$'):
234
- return f'$${expr}$$'
235
- return expr
236
-
237
- # ๊ด„ํ˜ธ๋กœ ๋‘˜๋Ÿฌ์‹ธ์ธ ์ˆ˜์‹ ์ฐพ์•„์„œ LaTeX๋กœ ๋ณ€ํ™˜
238
- text = re.sub(r'\((.*?)\)', wrap_math, text)
239
-
240
- # ์ด๋ฏธ LaTeX๋กœ ์ฒ˜๋ฆฌ๋œ ๋ถ€๋ถ„์€ ๊ฑด๋„ˆ๋›ฐ๊ณ , ๋‚˜๋จธ์ง€ ์ˆ˜์‹์„ LaTeX๋กœ ์ฒ˜๋ฆฌ
241
- def add_math_delimiters(text):
242
- if '$$' not in text and '$' not in text:
243
- return f'$${text}$$'
244
- return text
245
 
246
  return text
247
 
 
 
 
 
 
 
 
 
 
248
  def display_math_question(question: str):
249
  """์ˆ˜ํ•™ ๋ฌธ์ œ๋ฅผ LaTeX ํ˜•์‹์œผ๋กœ ํ‘œ์‹œ"""
250
  formatted_question = format_math_expression(question)
 
184
  st.session_state.current_step = 'review'
185
 
186
  # ์ˆ˜์‹ ํ‘œํ˜„
187
+ def clean_text(text: str) -> str:
188
+ """ํ…์ŠคํŠธ ์ „์ฒ˜๋ฆฌ: ๋ถˆํ•„์š”ํ•œ ์ค‘๊ด„ํ˜ธ ์ œ๊ฑฐ ๋ฐ ๊ธฐ๋ณธ ํด๋ฆฌ๋‹"""
189
+ import re
190
+
191
+ # LaTeX ์ˆ˜์‹ ๋ถ€๋ถ„์„ ์ž„์‹œ ์ €์žฅ
192
+ latex_parts = []
193
+ def save_latex(match):
194
+ latex_parts.append(match.group(0))
195
+ return f"LATEX_{len(latex_parts)-1}_PLACEHOLDER"
196
+
197
+ # LaTeX ์ˆ˜์‹ ๋ถ€๋ถ„์„ ์ž„์‹œ๋กœ ์น˜ํ™˜
198
+ text = re.sub(r'\$\$.*?\$\$', save_latex, text)
199
+
200
+ # ๊ฐ ๋ฌธ์ž๋ฅผ ๊ฐ์‹ธ๊ณ  ์žˆ๋Š” ์ค‘๊ด„ํ˜ธ ์ œ๊ฑฐ
201
+ text = re.sub(r'\{(\w)\}', r'\1', text)
202
+
203
+ # LaTeX ์ˆ˜์‹ ๋ถ€๋ถ„ ๋ณต์›
204
+ for i, latex in enumerate(latex_parts):
205
+ text = text.replace(f"LATEX_{i}_PLACEHOLDER", latex)
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',
 
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)