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
|