Spaces:
Sleeping
Sleeping
Create latex_formatter.py
Browse files- latex_formatter.py +98 -0
latex_formatter.py
ADDED
|
@@ -0,0 +1,98 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# latex_formatter.py
|
| 2 |
+
import re
|
| 3 |
+
|
| 4 |
+
class LatexFormatter:
|
| 5 |
+
"""LaTeX ์์ ํฌ๋งทํ
์ ์ํ ํด๋์ค"""
|
| 6 |
+
|
| 7 |
+
def __init__(self):
|
| 8 |
+
# LaTeX ํน์ ๋ช
๋ น์ด ๋งคํ
|
| 9 |
+
self.latex_commands = {
|
| 10 |
+
r'\left': r'\\left',
|
| 11 |
+
r'\right': r'\\right',
|
| 12 |
+
r'\bigcirc': r'\\bigcirc',
|
| 13 |
+
r'\square': r'\\square',
|
| 14 |
+
r'\quad': r'\\quad',
|
| 15 |
+
r'\div': r'\\div',
|
| 16 |
+
r'\ldots': r'\\ldots',
|
| 17 |
+
r'\times': r'\\times',
|
| 18 |
+
r'\pm': r'\\pm',
|
| 19 |
+
r'\infty': r'\\infty',
|
| 20 |
+
r'\neq': r'\\neq',
|
| 21 |
+
r'\leq': r'\\leq',
|
| 22 |
+
r'\geq': r'\\geq'
|
| 23 |
+
}
|
| 24 |
+
|
| 25 |
+
# ์ํ ์ฉ์ด ๋งคํ
|
| 26 |
+
self.math_terms = [
|
| 27 |
+
'decimalplaces', 'rounded to', 'What is',
|
| 28 |
+
'Calculate', 'Solve', 'Evaluate', 'Simplify'
|
| 29 |
+
]
|
| 30 |
+
|
| 31 |
+
def format_expression(self, text: str) -> str:
|
| 32 |
+
"""LaTeX ์์ ๋ณํ์ ๋ฉ์ธ ํจ์"""
|
| 33 |
+
# 1. ๊ธฐ์กด LaTeX ์์ ๋ณด์กด
|
| 34 |
+
latex_parts = []
|
| 35 |
+
def save_latex(match):
|
| 36 |
+
latex_parts.append(match.group(0))
|
| 37 |
+
return f"LATEX_{len(latex_parts)-1}_PLACEHOLDER"
|
| 38 |
+
|
| 39 |
+
text = re.sub(r'\$\$.*?\$\$', save_latex, text)
|
| 40 |
+
|
| 41 |
+
# 2. ํน์ ๋ช
๋ น์ด ์ฒ๋ฆฌ
|
| 42 |
+
for cmd, latex_cmd in self.latex_commands.items():
|
| 43 |
+
text = text.replace(cmd, latex_cmd)
|
| 44 |
+
|
| 45 |
+
# 3. ๋จ์ด ๋ถ๋ฆฌ ๋ฐ ํ
์คํธ ์ ๋ฆฌ
|
| 46 |
+
text = self._clean_text(text)
|
| 47 |
+
|
| 48 |
+
# 4. ์์ ์ฒ๋ฆฌ
|
| 49 |
+
text = self._process_math_expressions(text)
|
| 50 |
+
|
| 51 |
+
# 5. LaTeX ์์ ๋ณต์
|
| 52 |
+
for i, latex in enumerate(latex_parts):
|
| 53 |
+
text = text.replace(f"LATEX_{i}_PLACEHOLDER", latex)
|
| 54 |
+
|
| 55 |
+
# 6. ์ต์ข
์ ๋ฆฌ
|
| 56 |
+
if not text.startswith('$$') and not text.endswith('$$'):
|
| 57 |
+
text = f"$${text}$$"
|
| 58 |
+
|
| 59 |
+
return text.replace('\\\\', '\\')
|
| 60 |
+
|
| 61 |
+
def _clean_text(self, text: str) -> str:
|
| 62 |
+
"""ํ
์คํธ ์ ์ฒ๋ฆฌ"""
|
| 63 |
+
# ๋ถ์ด์๋ ๋จ์ด ๋ถ๋ฆฌ
|
| 64 |
+
text = re.sub(r'([a-z])([A-Z])', r'\1 \2', text)
|
| 65 |
+
text = re.sub(r'([A-Za-z])(\d)', r'\1 \2', text)
|
| 66 |
+
text = re.sub(r'(\d)([A-Za-z])', r'\1 \2', text)
|
| 67 |
+
|
| 68 |
+
# ์ํ ์ฉ์ด๋ฅผ LaTeX ํ
์คํธ๋ก ๋ณํ
|
| 69 |
+
for term in self.math_terms:
|
| 70 |
+
text = re.sub(
|
| 71 |
+
rf'\b{term}\b',
|
| 72 |
+
f'\\text{{{term}}}',
|
| 73 |
+
text,
|
| 74 |
+
flags=re.IGNORECASE
|
| 75 |
+
)
|
| 76 |
+
|
| 77 |
+
return text
|
| 78 |
+
|
| 79 |
+
def _process_math_expressions(self, text: str) -> str:
|
| 80 |
+
"""์ํ ํํ์ ์ฒ๋ฆฌ"""
|
| 81 |
+
# ๊ดํธ ์์ ์์ ์ฒ๋ฆฌ
|
| 82 |
+
def process_math(match):
|
| 83 |
+
content = match.group(1)
|
| 84 |
+
|
| 85 |
+
# ์ง์ ์ฒ๋ฆฌ
|
| 86 |
+
if '^' in content:
|
| 87 |
+
base, exp = content.split('^')
|
| 88 |
+
return f'\\left({base}\\right)^{{{exp}}}'
|
| 89 |
+
|
| 90 |
+
# ๋ถ์ ์ฒ๋ฆฌ
|
| 91 |
+
if '/' in content and not any(op in content for op in ['ร', 'รท', '+', '-']):
|
| 92 |
+
num, den = content.split('/')
|
| 93 |
+
return f'\\frac{{{num}}}{{{den}}}'
|
| 94 |
+
|
| 95 |
+
return f'\\left({content}\\right)'
|
| 96 |
+
|
| 97 |
+
text = re.sub(r'\((.*?)\)', process_math, text)
|
| 98 |
+
return text
|