Update latex_processor.py
Browse files- latex_processor.py +310 -248
latex_processor.py
CHANGED
|
@@ -1,268 +1,330 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import re
|
| 2 |
-
from typing import List, Tuple
|
| 3 |
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
r'\b(sin|cos|tan|log|ln|exp|lim|integral|derivative)\b',
|
| 13 |
-
r'[a-z]\s*=\s*[a-z0-9]',
|
| 14 |
-
r'\^|\d+_\d+',
|
| 15 |
-
]
|
| 16 |
-
|
| 17 |
-
def detect_mathematical_content(self, text: str) -> bool:
|
| 18 |
-
"""
|
| 19 |
-
Detect if text contains mathematical/scientific content
|
| 20 |
-
|
| 21 |
-
Args:
|
| 22 |
-
text: Text to analyze
|
| 23 |
-
|
| 24 |
-
Returns:
|
| 25 |
-
True if mathematical content is detected
|
| 26 |
-
"""
|
| 27 |
-
text_lower = text.lower()
|
| 28 |
-
|
| 29 |
-
for pattern in self.MATH_INDICATORS:
|
| 30 |
-
if re.search(pattern, text_lower, re.IGNORECASE):
|
| 31 |
-
return True
|
| 32 |
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 42 |
"""
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
a LaTeX-focused prompt
|
| 46 |
-
|
| 47 |
-
Args:
|
| 48 |
-
content: Original document content
|
| 49 |
-
user_instructions: User's specific instructions
|
| 50 |
-
doc_type: Type of document (auto, academic, technical, business, etc.)
|
| 51 |
-
include_latex: Whether to include LaTeX formatting
|
| 52 |
-
|
| 53 |
-
Returns:
|
| 54 |
-
Complete prompt for Gemini
|
| 55 |
"""
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
"- Number important equations as needed",
|
| 72 |
-
"- Ensure all mathematical notation is professional and consistent",
|
| 73 |
-
""
|
| 74 |
-
])
|
| 75 |
-
|
| 76 |
-
# Add document type specific instructions
|
| 77 |
-
if doc_type == "academic":
|
| 78 |
-
prompt_parts.extend([
|
| 79 |
-
"π Document Type: Academic/Research Paper",
|
| 80 |
-
"- Use formal academic tone",
|
| 81 |
-
"- Structure with clear sections (Abstract, Introduction, Methods, Results, Discussion, Conclusion)",
|
| 82 |
-
"- Include proper citations where needed (use [Author, Year] format)",
|
| 83 |
-
"- Ensure technical accuracy",
|
| 84 |
-
""
|
| 85 |
-
])
|
| 86 |
-
elif doc_type == "technical":
|
| 87 |
-
prompt_parts.extend([
|
| 88 |
-
"π§ Document Type: Technical Documentation",
|
| 89 |
-
"- Use clear, precise technical language",
|
| 90 |
-
"- Include code examples in proper formatting if relevant",
|
| 91 |
-
"- Use numbered lists for procedures",
|
| 92 |
-
"- Add technical diagrams descriptions where helpful",
|
| 93 |
-
""
|
| 94 |
-
])
|
| 95 |
-
elif doc_type == "business":
|
| 96 |
-
prompt_parts.extend([
|
| 97 |
-
"πΌ Document Type: Business Document",
|
| 98 |
-
"- Use professional business tone",
|
| 99 |
-
"- Focus on clarity and conciseness",
|
| 100 |
-
"- Highlight key points and actionable items",
|
| 101 |
-
"- Use bullet points for readability",
|
| 102 |
-
""
|
| 103 |
-
])
|
| 104 |
-
|
| 105 |
-
# Add user instructions
|
| 106 |
-
if user_instructions:
|
| 107 |
-
prompt_parts.extend([
|
| 108 |
-
f"π€ User's Specific Instructions:",
|
| 109 |
-
f"{user_instructions}",
|
| 110 |
-
""
|
| 111 |
-
])
|
| 112 |
-
|
| 113 |
-
# Add the content
|
| 114 |
-
prompt_parts.extend([
|
| 115 |
-
"π Original Document Content:",
|
| 116 |
-
"=" * 60,
|
| 117 |
-
content,
|
| 118 |
-
"=" * 60,
|
| 119 |
-
"",
|
| 120 |
-
"β¨ Please provide the ENHANCED version following all guidelines above.",
|
| 121 |
-
"Maintain the document structure but improve quality, clarity, and professionalism.",
|
| 122 |
-
"Convert all math to proper LaTeX notation if applicable.",
|
| 123 |
-
"Return ONLY the enhanced content, no explanations or meta-commentary.",
|
| 124 |
-
])
|
| 125 |
-
|
| 126 |
-
return "\n".join(prompt_parts)
|
| 127 |
-
|
| 128 |
-
def process_latex_content(self, content: str) -> str:
|
| 129 |
"""
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
Args:
|
| 133 |
-
content: Content potentially containing LaTeX
|
| 134 |
-
|
| 135 |
-
Returns:
|
| 136 |
-
Processed content with valid LaTeX
|
| 137 |
"""
|
| 138 |
-
|
| 139 |
-
|
| 140 |
-
|
| 141 |
-
|
| 142 |
-
|
| 143 |
-
|
| 144 |
-
|
| 145 |
-
|
| 146 |
-
|
| 147 |
-
|
| 148 |
-
|
| 149 |
-
|
| 150 |
-
|
| 151 |
-
|
| 152 |
-
|
| 153 |
-
|
| 154 |
-
|
| 155 |
-
|
| 156 |
-
return
|
| 157 |
-
|
| 158 |
-
|
|
|
|
|
|
|
|
|
|
| 159 |
"""
|
| 160 |
-
|
| 161 |
-
|
| 162 |
-
Args:
|
| 163 |
-
content: Content containing LaTeX
|
| 164 |
-
|
| 165 |
-
Returns:
|
| 166 |
-
List of tuples (equation_type, equation_content)
|
| 167 |
-
equation_type is either 'inline' or 'display'
|
| 168 |
"""
|
|
|
|
| 169 |
equations = []
|
| 170 |
-
|
| 171 |
-
|
| 172 |
-
|
| 173 |
-
|
| 174 |
-
|
| 175 |
-
|
| 176 |
-
|
| 177 |
-
|
| 178 |
-
|
| 179 |
-
|
| 180 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 181 |
return equations
|
| 182 |
-
|
| 183 |
-
|
|
|
|
|
|
|
|
|
|
| 184 |
"""
|
| 185 |
-
|
| 186 |
-
|
| 187 |
-
Args:
|
| 188 |
-
latex_code: LaTeX code to validate
|
| 189 |
-
|
| 190 |
-
Returns:
|
| 191 |
-
Tuple of (is_valid, error_message)
|
| 192 |
"""
|
| 193 |
-
|
| 194 |
-
|
| 195 |
-
return False, "Unbalanced braces in LaTeX code"
|
| 196 |
|
| 197 |
-
|
| 198 |
-
|
| 199 |
-
|
| 200 |
-
|
| 201 |
-
|
| 202 |
-
|
| 203 |
-
|
| 204 |
-
|
| 205 |
-
|
| 206 |
-
|
| 207 |
-
|
| 208 |
-
|
| 209 |
-
|
| 210 |
-
|
| 211 |
-
|
| 212 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 213 |
def convert_latex_to_unicode(self, latex_code: str) -> str:
|
| 214 |
"""
|
| 215 |
-
|
| 216 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 217 |
|
| 218 |
-
|
| 219 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 220 |
|
| 221 |
-
|
| 222 |
-
|
| 223 |
-
|
| 224 |
-
|
| 225 |
-
|
| 226 |
-
|
| 227 |
-
|
| 228 |
-
|
| 229 |
-
|
| 230 |
-
|
| 231 |
-
|
| 232 |
-
|
| 233 |
-
|
| 234 |
-
|
| 235 |
-
|
| 236 |
-
|
| 237 |
-
|
| 238 |
-
|
| 239 |
-
|
| 240 |
-
|
| 241 |
-
r'\\neq': 'β ',
|
| 242 |
-
r'\\approx': 'β',
|
| 243 |
-
r'\\sum': 'β',
|
| 244 |
-
r'\\prod': 'β',
|
| 245 |
-
r'\\int': 'β«',
|
| 246 |
-
r'\\sqrt': 'β',
|
| 247 |
-
r'\\pm': 'Β±',
|
| 248 |
-
r'\\times': 'Γ',
|
| 249 |
-
r'\\div': 'Γ·',
|
| 250 |
}
|
| 251 |
-
|
| 252 |
-
|
| 253 |
-
|
| 254 |
-
|
| 255 |
-
|
| 256 |
-
|
|
|
|
|
|
|
| 257 |
|
| 258 |
-
|
| 259 |
-
|
| 260 |
-
|
| 261 |
-
|
| 262 |
-
|
| 263 |
-
|
| 264 |
-
|
| 265 |
-
|
| 266 |
-
|
| 267 |
-
|
| 268 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
optimized_latex_processor.py
|
| 3 |
+
|
| 4 |
+
Dependencies:
|
| 5 |
+
pip install pylatexenc latex2mathml
|
| 6 |
+
|
| 7 |
+
Optional (for more advanced features not used here):
|
| 8 |
+
pip install sympy
|
| 9 |
+
|
| 10 |
+
Functionality:
|
| 11 |
+
- sanitize Gemini output (strip ```latex``` fences safely)
|
| 12 |
+
- detect math heuristically and via parser
|
| 13 |
+
- extract inline/display math nodes using pylatexenc (MathNodes + Environments)
|
| 14 |
+
- validate LaTeX with parser + robust balanced-delimiters checks
|
| 15 |
+
- convert to MathML (latex2mathml)
|
| 16 |
+
- convert to Unicode with superscript/subscript support
|
| 17 |
+
"""
|
| 18 |
+
|
| 19 |
import re
|
| 20 |
+
from typing import List, Tuple, Dict, Any, Optional
|
| 21 |
|
| 22 |
+
# pylatexenc imports
|
| 23 |
+
from pylatexenc.latexwalker import LatexWalker, LatexMathNode, LatexEnvironmentNode, LatexNode, LatexWalkerParseError
|
| 24 |
+
from latex2mathml.converter import convert as latex2mathml_convert
|
| 25 |
+
|
| 26 |
+
|
| 27 |
+
class OptimizedLaTeXProcessor:
|
| 28 |
+
def __init__(self, enable_mathml: bool = True):
|
| 29 |
+
self.enable_mathml = enable_mathml
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 30 |
|
| 31 |
+
# 1. Basic Symbol Map
|
| 32 |
+
self.unicode_map = {
|
| 33 |
+
r'\alpha': 'Ξ±', r'\beta': 'Ξ²', r'\gamma': 'Ξ³', r'\delta': 'Ξ΄',
|
| 34 |
+
r'\epsilon': 'Ξ΅', r'\theta': 'ΞΈ', r'\lambda': 'Ξ»', r'\mu': 'ΞΌ',
|
| 35 |
+
r'\pi': 'Ο', r'\sigma': 'Ο', r'\phi': 'Ο', r'\omega': 'Ο',
|
| 36 |
+
r'\infty': 'β', r'\leq': 'β€', r'\geq': 'β₯', r'\neq': 'β ',
|
| 37 |
+
r'\approx': 'β', r'\sum': 'β', r'\prod': 'β', r'\int': 'β«',
|
| 38 |
+
r'\sqrt': 'β', r'\pm': 'Β±', r'\times': 'Γ', r'\div': 'Γ·',
|
| 39 |
+
r'\cdot': 'Β·', r'\rightarrow': 'β', r'\leftarrow': 'β',
|
| 40 |
+
}
|
| 41 |
+
|
| 42 |
+
# 2. Superscript/Subscript Maps
|
| 43 |
+
self.sup_map = str.maketrans("0123456789+-=()abcdefghijklmnopqrstuvwxyz",
|
| 44 |
+
"β°ΒΉΒ²Β³β΄β΅βΆβ·βΈβΉβΊβ»βΌβ½βΎα΅α΅αΆα΅α΅αΆ α΅Κ°β±Κ²α΅Λ‘α΅βΏα΅α΅qΚ³Λ’α΅α΅α΅Κ·Λ£ΚΈαΆ»")
|
| 45 |
+
self.sub_map = str.maketrans("0123456789+-=()aehijklmnoprstuvx",
|
| 46 |
+
"ββββββ
ββββββββββββα΅’β±Όββββββα΅£ββα΅€α΅₯β")
|
| 47 |
+
|
| 48 |
+
# 3. Regex patterns
|
| 49 |
+
self._re_unescaped_single_dollar = re.compile(r'(?<!\\)(?<!\$)\$(?!\$)')
|
| 50 |
+
self._heuristic_math_pat = re.compile(
|
| 51 |
+
r'(\\frac|\\sum|\\int|\\sqrt|\\alpha|\\beta|\\pi|\\infty|\$|\\\[|\\\]|\^|_|\b(sin|cos|tan|log|ln|lim)\b|[β«ββββ€β₯β Β±ΓΓ·])',
|
| 52 |
+
re.IGNORECASE
|
| 53 |
+
)
|
| 54 |
+
# Math environments to detect
|
| 55 |
+
self.math_environments = {
|
| 56 |
+
'equation', 'equation*', 'align', 'align*', 'gather', 'gather*',
|
| 57 |
+
'split', 'multline', 'flalign'
|
| 58 |
+
}
|
| 59 |
+
|
| 60 |
+
# ----------------------------
|
| 61 |
+
# Sanitization
|
| 62 |
+
# ----------------------------
|
| 63 |
+
def sanitize_input(self, text: str) -> str:
|
| 64 |
"""
|
| 65 |
+
Remove surrounding Markdown fences like ```latex``` or ``` that contain LaTeX,
|
| 66 |
+
but preserve the inner LaTeX exactly (do not mangle escaped dollars).
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 67 |
"""
|
| 68 |
+
def _fence_repl(m):
|
| 69 |
+
return m.group(1)
|
| 70 |
+
|
| 71 |
+
# Remove code fences with optional language specifier
|
| 72 |
+
text = re.sub(r'```(?:latex)?\n(.*?)```', _fence_repl, text, flags=re.DOTALL | re.IGNORECASE)
|
| 73 |
+
# Handle triple-backtick blocks without newline start
|
| 74 |
+
text = re.sub(r'```(.*?)```', _fence_repl, text, flags=re.DOTALL)
|
| 75 |
+
# Normalize CRLF -> LF
|
| 76 |
+
text = text.replace('\r\n', '\n')
|
| 77 |
+
return text
|
| 78 |
+
|
| 79 |
+
# ----------------------------
|
| 80 |
+
# Detection
|
| 81 |
+
# ----------------------------
|
| 82 |
+
def detect_mathematical_content(self, text: str) -> bool:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 83 |
"""
|
| 84 |
+
Cheap heuristic followed by parser attempt if heuristic triggered.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 85 |
"""
|
| 86 |
+
if not text or not text.strip():
|
| 87 |
+
return False
|
| 88 |
+
|
| 89 |
+
if self._heuristic_math_pat.search(text):
|
| 90 |
+
try:
|
| 91 |
+
walker = LatexWalker(text)
|
| 92 |
+
nodes, _, _ = walker.get_latex_nodes(pos=0)
|
| 93 |
+
# Check for MathNodes or Math Environments
|
| 94 |
+
for n in nodes:
|
| 95 |
+
if isinstance(n, LatexMathNode):
|
| 96 |
+
return True
|
| 97 |
+
if isinstance(n, LatexEnvironmentNode) and n.environmentname in self.math_environments:
|
| 98 |
+
return True
|
| 99 |
+
return True # Heuristic matched, no nodes found, return True just in case
|
| 100 |
+
except Exception:
|
| 101 |
+
# If parsing fails, heuristic matched, so we assume math is present
|
| 102 |
+
return True
|
| 103 |
+
|
| 104 |
+
return False
|
| 105 |
+
|
| 106 |
+
# ----------------------------
|
| 107 |
+
# Extraction
|
| 108 |
+
# ----------------------------
|
| 109 |
+
def extract_latex_equations(self, content: str) -> List[Dict[str, Any]]:
|
| 110 |
"""
|
| 111 |
+
Parse content and extract math nodes (inline $...$ and environments).
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 112 |
"""
|
| 113 |
+
sanitized = self.sanitize_input(content)
|
| 114 |
equations = []
|
| 115 |
+
|
| 116 |
+
try:
|
| 117 |
+
walker = LatexWalker(sanitized)
|
| 118 |
+
nodes, _, _ = walker.get_latex_nodes(pos=0)
|
| 119 |
+
except Exception:
|
| 120 |
+
# If parser fails entirely, fallback to regex for standard dollar delimiters
|
| 121 |
+
# Note: Regex won't reliably catch \begin{equation} blocks
|
| 122 |
+
for m in re.finditer(r'(?<!\\)\$\$(.*?)(?<!\\)\$\$', sanitized, flags=re.DOTALL):
|
| 123 |
+
equations.append({'type': 'display', 'latex': m.group(1).strip(), 'start_pos': m.start(), 'end_pos': m.end()})
|
| 124 |
+
for m in re.finditer(r'(?<!\\)(?<!\$)\$(?!\$)(.*?)(?<!\\)(?<!\$)\$(?!\$)', sanitized, flags=re.DOTALL):
|
| 125 |
+
equations.append({'type': 'inline', 'latex': m.group(1).strip(), 'start_pos': m.start(), 'end_pos': m.end()})
|
| 126 |
+
return equations
|
| 127 |
+
|
| 128 |
+
def walk_nodes(node_list: List[LatexNode]):
|
| 129 |
+
for node in node_list:
|
| 130 |
+
is_math_node = isinstance(node, LatexMathNode)
|
| 131 |
+
is_math_env = False
|
| 132 |
+
|
| 133 |
+
# Check for environments like equation, align
|
| 134 |
+
if isinstance(node, LatexEnvironmentNode):
|
| 135 |
+
if node.environmentname in self.math_environments:
|
| 136 |
+
is_math_env = True
|
| 137 |
+
|
| 138 |
+
if is_math_node or is_math_env:
|
| 139 |
+
latex_snip = node.latex_verbatim()
|
| 140 |
+
|
| 141 |
+
if is_math_env:
|
| 142 |
+
typ = 'display'
|
| 143 |
+
# For environments, we usually keep \begin{}...\end{}
|
| 144 |
+
# so converters know how to handle alignment.
|
| 145 |
+
inner_clean = latex_snip.strip()
|
| 146 |
+
else:
|
| 147 |
+
# Logic for standard LatexMathNode ($ or $$)
|
| 148 |
+
delim = getattr(node, 'delimiters', None)
|
| 149 |
+
displaytype = getattr(node, 'displaytype', None)
|
| 150 |
+
typ = 'display' if (delim == '$$' or displaytype == 'display') else 'inline'
|
| 151 |
+
|
| 152 |
+
# Strip outer delimiters for cleaner processing, unless it matches \[ \] pattern
|
| 153 |
+
# Standardizing on raw content is usually safer for converters
|
| 154 |
+
if latex_snip.startswith('$$') and latex_snip.endswith('$$'):
|
| 155 |
+
inner_clean = latex_snip[2:-2].strip()
|
| 156 |
+
elif latex_snip.startswith('$') and latex_snip.endswith('$'):
|
| 157 |
+
inner_clean = latex_snip[1:-1].strip()
|
| 158 |
+
elif latex_snip.startswith(r'\(') and latex_snip.endswith(r'\)'):
|
| 159 |
+
inner_clean = latex_snip[2:-2].strip()
|
| 160 |
+
elif latex_snip.startswith(r'\[') and latex_snip.endswith(r'\]'):
|
| 161 |
+
inner_clean = latex_snip[2:-2].strip()
|
| 162 |
+
typ = 'display'
|
| 163 |
+
else:
|
| 164 |
+
inner_clean = latex_snip
|
| 165 |
+
|
| 166 |
+
equations.append({
|
| 167 |
+
'type': typ,
|
| 168 |
+
'latex': inner_clean,
|
| 169 |
+
'start_pos': node.pos,
|
| 170 |
+
'end_pos': node.pos + node.len if hasattr(node, 'len') else None
|
| 171 |
+
})
|
| 172 |
+
else:
|
| 173 |
+
# Recursive search inside other nodes (e.g. bold text containing math)
|
| 174 |
+
if hasattr(node, 'nodelist') and node.nodelist:
|
| 175 |
+
walk_nodes(node.nodelist)
|
| 176 |
+
|
| 177 |
+
walk_nodes(nodes)
|
| 178 |
return equations
|
| 179 |
+
|
| 180 |
+
# ----------------------------
|
| 181 |
+
# Validation
|
| 182 |
+
# ----------------------------
|
| 183 |
+
def validate_latex(self, latex_code: str) -> Tuple[bool, Optional[str]]:
|
| 184 |
"""
|
| 185 |
+
Validate a single latex snippet.
|
| 186 |
+
Handles escaped braces correctly to avoid false negatives.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 187 |
"""
|
| 188 |
+
if latex_code is None:
|
| 189 |
+
return False, "Empty LaTeX snippet."
|
|
|
|
| 190 |
|
| 191 |
+
if not latex_code.strip():
|
| 192 |
+
return False, "Empty content."
|
| 193 |
+
|
| 194 |
+
# 1. Strip escaped characters (like \{, \}, \$) before counting structural delimiters
|
| 195 |
+
clean_code = re.sub(r'\\.', '', latex_code)
|
| 196 |
+
|
| 197 |
+
# 2. Check balanced delimiters on cleaned code
|
| 198 |
+
if clean_code.count('{') != clean_code.count('}'):
|
| 199 |
+
return False, "Unbalanced braces: { }"
|
| 200 |
+
if clean_code.count('[') != clean_code.count(']'):
|
| 201 |
+
return False, "Unbalanced brackets: [ ]"
|
| 202 |
+
|
| 203 |
+
# 3. Parser Check (on original code)
|
| 204 |
+
try:
|
| 205 |
+
# We wrap it in strict mode check
|
| 206 |
+
walker = LatexWalker(latex_code)
|
| 207 |
+
walker.get_latex_nodes(pos=0)
|
| 208 |
+
except Exception as e:
|
| 209 |
+
return False, f"Parser error: {str(e)}"
|
| 210 |
+
|
| 211 |
+
return True, None
|
| 212 |
+
|
| 213 |
+
# ----------------------------
|
| 214 |
+
# Conversions
|
| 215 |
+
# ----------------------------
|
| 216 |
+
def convert_latex_to_mathml(self, latex_code: str) -> Optional[str]:
|
| 217 |
+
if not self.enable_mathml:
|
| 218 |
+
return None
|
| 219 |
+
try:
|
| 220 |
+
return latex2mathml_convert(latex_code)
|
| 221 |
+
except Exception:
|
| 222 |
+
return None
|
| 223 |
+
|
| 224 |
def convert_latex_to_unicode(self, latex_code: str) -> str:
|
| 225 |
"""
|
| 226 |
+
Enhanced LaTeX -> Unicode mapping.
|
| 227 |
+
Includes fractions, superscripts, subscripts, and symbols.
|
| 228 |
+
"""
|
| 229 |
+
out = latex_code
|
| 230 |
+
|
| 231 |
+
# 1. Handle simple \frac{num}{den} -> (num/den)
|
| 232 |
+
def _frac_repl(m):
|
| 233 |
+
return f'({m.group(1).strip()}/{m.group(2).strip()})'
|
| 234 |
+
out = re.sub(r'\\frac\s*\{\s*([^{}]+?)\s*\}\s*\{\s*([^{}]+?)\s*\}', _frac_repl, out)
|
| 235 |
+
|
| 236 |
+
# 2. Superscripts (^)
|
| 237 |
+
# Handle ^{...}
|
| 238 |
+
out = re.sub(r'\^\{([a-zA-Z0-9+\-=()]+)\}', lambda m: m.group(1).translate(self.sup_map), out)
|
| 239 |
+
# Handle single char ^x
|
| 240 |
+
out = re.sub(r'\^([a-zA-Z0-9])', lambda m: m.group(1).translate(self.sup_map), out)
|
| 241 |
+
|
| 242 |
+
# 3. Subscripts (_)
|
| 243 |
+
# Handle _{...}
|
| 244 |
+
out = re.sub(r'_\{([a-zA-Z0-9+\-=()]+)\}', lambda m: m.group(1).translate(self.sub_map), out)
|
| 245 |
+
# Handle single char _x
|
| 246 |
+
out = re.sub(r'_([a-zA-Z0-9])', lambda m: m.group(1).translate(self.sub_map), out)
|
| 247 |
+
|
| 248 |
+
# 4. Symbol mapping
|
| 249 |
+
for k, v in self.unicode_map.items():
|
| 250 |
+
out = out.replace(k, v)
|
| 251 |
+
|
| 252 |
+
# 5. Cleanup remaining backslashes (simple commands like \text)
|
| 253 |
+
out = re.sub(r'\\([A-Za-z]+)', r'\1', out)
|
| 254 |
+
out = re.sub(r'\s+', ' ', out).strip()
|
| 255 |
|
| 256 |
+
return out
|
| 257 |
+
|
| 258 |
+
# ----------------------------
|
| 259 |
+
# Main Pipeline
|
| 260 |
+
# ----------------------------
|
| 261 |
+
def process_latex_content(self, content: str, convert_mathml: bool = True) -> Dict[str, Any]:
|
| 262 |
+
cleaned = self.sanitize_input(content)
|
| 263 |
+
equations = self.extract_latex_equations(cleaned)
|
| 264 |
+
|
| 265 |
+
enhanced_equations = []
|
| 266 |
+
for eq in equations:
|
| 267 |
+
latex_snip = eq['latex']
|
| 268 |
+
is_valid, error = self.validate_latex(latex_snip)
|
| 269 |
|
| 270 |
+
mathml = None
|
| 271 |
+
if is_valid and convert_mathml and self.enable_mathml:
|
| 272 |
+
mathml = self.convert_latex_to_mathml(latex_snip)
|
| 273 |
+
|
| 274 |
+
unicode_repr = self.convert_latex_to_unicode(latex_snip)
|
| 275 |
+
|
| 276 |
+
enhanced_equations.append({
|
| 277 |
+
'type': eq.get('type', 'inline'),
|
| 278 |
+
'latex': latex_snip,
|
| 279 |
+
'valid': is_valid,
|
| 280 |
+
'error': error,
|
| 281 |
+
'mathml': mathml,
|
| 282 |
+
'unicode': unicode_repr,
|
| 283 |
+
'start_pos': eq.get('start_pos'),
|
| 284 |
+
'end_pos': eq.get('end_pos')
|
| 285 |
+
})
|
| 286 |
+
|
| 287 |
+
return {
|
| 288 |
+
'cleaned_content': cleaned,
|
| 289 |
+
'equations': enhanced_equations
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 290 |
}
|
| 291 |
+
|
| 292 |
+
|
| 293 |
+
# ----------------------------
|
| 294 |
+
# Example usage
|
| 295 |
+
# ----------------------------
|
| 296 |
+
if __name__ == "__main__":
|
| 297 |
+
sample = r"""
|
| 298 |
+
Here is some text with inline math $E=mc^2$ and escaped dollar \$100.
|
| 299 |
|
| 300 |
+
A set definition with escaped braces (this caused bugs before):
|
| 301 |
+
$S = \{ x \in \mathbb{R} \mid x > 0 \}$
|
| 302 |
+
|
| 303 |
+
A display equation:
|
| 304 |
+
$$
|
| 305 |
+
\int_0^\infty x^2 e^{-x} \,dx = 2!
|
| 306 |
+
$$
|
| 307 |
+
|
| 308 |
+
An aligned environment:
|
| 309 |
+
\begin{align}
|
| 310 |
+
a &= b + c \\
|
| 311 |
+
d &= e + f
|
| 312 |
+
\end{align}
|
| 313 |
+
|
| 314 |
+
And a malformed example: $unbalanced { braces $
|
| 315 |
+
"""
|
| 316 |
+
|
| 317 |
+
proc = OptimizedLaTeXProcessor(enable_mathml=True)
|
| 318 |
+
result = proc.process_latex_content(sample)
|
| 319 |
+
|
| 320 |
+
print("--- CLEANED CONTENT (snippet) ---")
|
| 321 |
+
print(result['cleaned_content'][:100] + "...")
|
| 322 |
+
|
| 323 |
+
print("\n--- EQUATIONS FOUND ---")
|
| 324 |
+
for i, e in enumerate(result['equations'], 1):
|
| 325 |
+
print(f"\n#{i} Type: {e['type'].upper()}")
|
| 326 |
+
print(f" Raw: {e['latex']}")
|
| 327 |
+
print(f" Valid: {e['valid']} ({e['error'] if e['error'] else 'OK'})")
|
| 328 |
+
print(f" Unicode: {e['unicode']}")
|
| 329 |
+
if e['mathml']:
|
| 330 |
+
print(f" MathML: {e['mathml'][:60]}...")
|