Jintonic92 commited on
Commit
565499c
Β·
verified Β·
1 Parent(s): d48ae52

Create latex_formatter.py

Browse files
Files changed (1) hide show
  1. 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