Jintonic92 commited on
Commit
124c3e8
Β·
verified Β·
1 Parent(s): 2e090e0

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +55 -67
app.py CHANGED
@@ -183,94 +183,82 @@ def handle_answer(answer, current_q):
183
  if st.session_state.current_question_index >= 10:
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 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 ν˜•μ‹μœΌλ‘œ λ³€ν™˜"""
266
- return {k: format_math_expression(v) for k, v in choices.items()}
267
-
268
- def display_math_answer(answer: str):
269
- """μˆ˜ν•™ λ‹΅μ•ˆμ„ LaTeX οΏ½οΏ½μ‹μœΌλ‘œ ν‘œμ‹œ"""
270
- formatted_answer = format_math_expression(answer)
271
- # κ΄„ν˜Έ μ•ˆμ˜ μˆ˜μ‹λ§Œ LaTeX둜 처리
272
- st.markdown(formatted_answer)
273
-
274
 
275
  def main():
276
  """메인 μ• ν”Œλ¦¬μΌ€μ΄μ…˜ 둜직"""
 
183
  if st.session_state.current_question_index >= 10:
184
  st.session_state.current_step = 'review'
185
 
186
+ def format_latex_expression(text: str) -> str:
187
+ """μˆ˜ν•™ ν‘œν˜„μ‹μ„ LaTeX ν˜•μ‹μœΌλ‘œ λ³€ν™˜"""
 
188
  import re
189
+
190
+ # μ΄νƒ€λ¦­μ²΄λ‘œ ν‘œμ‹œν•  μˆ˜ν•™ μš©μ–΄λ“€
191
+ math_terms = ['decimalplaces', 'rounded to', 'What is']
192
 
193
+ def replace_math_terms(match):
194
+ term = match.group(0)
195
+ return f'\\text{{{term}}}'
 
 
196
 
197
+ # ν…μŠ€νŠΈ μ „μ²˜λ¦¬
198
+ text = text.replace('\\(', '\\left(')
199
+ text = text.replace('\\)', '\\right)')
200
 
201
+ # μˆ˜ν•™ μš©μ–΄λ₯Ό LaTeX ν…μŠ€νŠΈλ‘œ λ³€ν™˜
202
+ for term in math_terms:
203
+ text = text.replace(term, f'\\text{{{term}}}')
204
 
205
+ # λ‹¬λŸ¬ 기호($) 처리
206
+ text = text.replace('$(', '\\$\\left(')
207
+ text = text.replace(')$', '\\right)\\$')
208
 
209
+ # 특수 νŒ¨ν„΄ 처리
210
+ replacements = {
211
+ r'\\div': '\\div',
212
+ r'\\ldots': '\\ldots',
213
+ r'\\dots': '\\dots',
214
+ r'\\times': '\\times',
 
 
 
 
 
 
 
215
  }
216
 
217
+ for old, new in replacements.items():
218
  text = text.replace(old, new)
219
 
220
+ # κ΄„ν˜Έλ‘œ λ‘˜λŸ¬μ‹ΈμΈ μˆ˜μ‹ 처리
221
+ def process_math(match):
222
  content = match.group(1)
223
+ return f'\\left({content}\\right)'
224
+
225
+ text = re.sub(r'\(([\d\.\s]+)\)', process_math, text)
 
226
 
227
+ # μ΅œμ’… LaTeX μˆ˜μ‹μœΌλ‘œ 감싸기
228
+ if not text.startswith('$') and not text.endswith('$'):
229
+ text = f'$${text}$$'
230
 
231
  return text
232
 
233
+ def format_question(question: str) -> str:
234
+ """문제 ν…μŠ€νŠΈλ₯Ό LaTeX ν˜•μ‹μœΌλ‘œ λ³€ν™˜"""
235
+ # 색상이 μžˆλŠ” ν…μŠ€νŠΈ 처리
236
+ colored_text_pattern = r'\\textcolor{([^}]+)}{([^}]+)}'
237
 
238
+ def process_colored_text(match):
239
+ color = match.group(1)
240
+ text = match.group(2)
241
+ return f'\\textcolor{{{color}}}{{{format_latex_expression(text)}}}'
242
 
243
+ question = re.sub(colored_text_pattern, process_colored_text, question)
244
+ return format_latex_expression(question)
245
+
246
+ def format_answer_choice(choice: str) -> str:
247
+ """선택지 ν…μŠ€νŠΈλ₯Ό LaTeX ν˜•μ‹μœΌλ‘œ λ³€ν™˜"""
248
+ # λ‹¬λŸ¬ κΈ°ν˜Έκ°€ ν¬ν•¨λœ 선택지 νŠΉλ³„ 처리
249
+ if '$' in choice:
250
+ # λ‹¬λŸ¬ 기호λ₯Ό LaTeX λͺ…λ Ήμ–΄λ‘œ λ³€ν™˜
251
+ choice = choice.replace('$', '\\$')
252
+ return format_latex_expression(choice)
253
 
254
  def display_math_content(content: str, is_question: bool = True):
255
+ """μˆ˜ν•™ λ‚΄μš© ν‘œμ‹œ"""
 
 
256
  if is_question:
257
+ formatted_content = format_question(content)
258
  else:
259
+ formatted_content = format_answer_choice(content)
260
+
261
+ return formatted_content
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
262
 
263
  def main():
264
  """메인 μ• ν”Œλ¦¬μΌ€μ΄μ…˜ 둜직"""