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