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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +49 -15
app.py CHANGED
@@ -188,45 +188,79 @@ def format_math_expression(text: str) -> str:
188
  """์ˆ˜ํ•™ ํ‘œํ˜„์‹์„ LaTeX ํ˜•์‹์œผ๋กœ ๋ณ€ํ™˜"""
189
  import re
190
 
191
- # ๊ธฐ๋ณธ์ ์ธ ์ˆ˜ํ•™ ์—ฐ์‚ฐ์ž ๋ณ€ํ™˜
 
 
 
 
 
 
 
 
 
 
192
  replacements = {
193
- r'\รท': r'\div',
194
- r'\ร—': r'\times',
195
- r'\-': r'-',
196
- r'\+': r'+',
197
- r'=': r'=',
 
 
 
 
 
 
198
  }
199
 
200
- # ๋ถ„์ˆ˜ ํŒจํ„ด ์ฐพ๊ธฐ (์˜ˆ: "1/2" โ†’ "\frac{1}{2}")
201
  text = re.sub(r'(\d+)/(\d+)', r'\\frac{\1}{\2}', text)
202
 
203
- # ๊ด„ํ˜ธ ์ฒ˜๋ฆฌ
204
- text = re.sub(r'\(([^)]+)\)', r'\(\1\)', text)
205
 
206
- # ๋‚˜๋จธ์ง€ ์ˆ˜ํ•™ ์—ฐ์‚ฐ์ž ๋ณ€ํ™˜
 
 
 
207
  for old, new in replacements.items():
208
  text = text.replace(old, new)
209
 
210
- # ์ˆ˜์‹ ๋ถ€๋ถ„ ์ฐพ์•„์„œ LaTeX๋กœ ๊ฐ์‹ธ๊ธฐ
211
  def wrap_math(match):
212
  expr = match.group(1)
213
- return f'${expr}$'
 
 
 
 
 
 
214
 
215
- # ์ˆ˜์‹ ํŒจํ„ด ์ฐพ์•„์„œ LaTeX๋กœ ๋ณ€ํ™˜
216
- text = re.sub(r'\(([^)]+)\)', wrap_math, text)
 
 
 
217
 
218
  return text
219
 
220
  def display_math_question(question: str):
221
  """์ˆ˜ํ•™ ๋ฌธ์ œ๋ฅผ LaTeX ํ˜•์‹์œผ๋กœ ํ‘œ์‹œ"""
222
  formatted_question = format_math_expression(question)
223
- st.markdown(formatted_question)
 
 
 
 
224
 
225
  def display_math_answer(answer: str):
226
  """์ˆ˜ํ•™ ๋‹ต์•ˆ์„ LaTeX ํ˜•์‹์œผ๋กœ ํ‘œ์‹œ"""
227
  formatted_answer = format_math_expression(answer)
228
  # ๊ด„ํ˜ธ ์•ˆ์˜ ์ˆ˜์‹๋งŒ LaTeX๋กœ ์ฒ˜๋ฆฌ
229
  st.markdown(formatted_answer)
 
 
230
  def main():
231
  """๋ฉ”์ธ ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜ ๋กœ์ง"""
232
  st.title("MisconcepTutor")
 
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',
209
+ 'โ†’': '\\rightarrow',
210
+ 'โ†': '\\leftarrow',
211
+ 'ยฑ': '\\pm',
212
+ 'โˆž': '\\infty',
213
+ 'โˆš': '\\sqrt',
214
  }
215
 
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)
251
+ st.markdown(formatted_question, unsafe_allow_html=True)
252
+
253
+ def format_math_choices(choices: dict) -> dict:
254
+ """์„ ํƒ์ง€์˜ ์ˆ˜ํ•™ ํ‘œํ˜„์‹์„ LaTeX ํ˜•์‹์œผ๋กœ ๋ณ€ํ™˜"""
255
+ return {k: format_math_expression(v) for k, v in choices.items()}
256
 
257
  def display_math_answer(answer: str):
258
  """์ˆ˜ํ•™ ๋‹ต์•ˆ์„ LaTeX ํ˜•์‹์œผ๋กœ ํ‘œ์‹œ"""
259
  formatted_answer = format_math_expression(answer)
260
  # ๊ด„ํ˜ธ ์•ˆ์˜ ์ˆ˜์‹๋งŒ LaTeX๋กœ ์ฒ˜๋ฆฌ
261
  st.markdown(formatted_answer)
262
+
263
+
264
  def main():
265
  """๋ฉ”์ธ ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜ ๋กœ์ง"""
266
  st.title("MisconcepTutor")