| import re |
|
|
| def sanitize_latex_for_sympy(latex_str: str) -> str: |
| """ |
| ืืกืืจ ืคืงืืืืช ืขืืฆืื ืฉื LaTeX (ืืื ืฆืืขืื) ืืื ืฉ-SymPy ืืืื ืืคืขื ื ืืช ืืืืืื. |
| ืืืืืื: ืืืคื ืืช \color{red}{5} ื- 5. |
| """ |
| if not latex_str: |
| return latex_str |
| |
| |
| cleaned = re.sub(r'\\color\{.*?\}\{(.*?)\}', r'\1', latex_str) |
| |
| |
| cleaned = re.sub(r'\\color\{.*?\}', '', cleaned) |
| |
| |
| cleaned = re.sub(r'\\text\{.*?\}', '', cleaned) |
| |
| |
| cleaned = cleaned.replace(r'\left', '').replace(r'\right', '') |
| |
| return cleaned |
|
|
| from typing import List |
|
|
| def aggressive_sympy_sanitizer(latex_str: str) -> List[str]: |
| """ |
| V9.0.1: Cleans dirty LaTeX generated by the LLM before feeding it to SymPy. |
| Splits comma-separated equations into a list of pure math strings. |
| """ |
| if not latex_str: |
| return [] |
| |
| s = latex_str |
| |
| |
| |
| s = re.sub(r'\([^)]*[\u0590-\u05FF]+[^)]*\)', '', s) |
| s = re.sub(r'[\u0590-\u05FF]+', '', s) |
| |
| |
| s = re.sub(r'\\text\{.*?\}', '', s) |
| s = re.sub(r'\\color\{.*?\}', '', s) |
| |
| |
| arrows = [r'\Rightarrow', r'\implies', r'\iff', r'\rightarrow'] |
| for arrow in arrows: |
| s = s.replace(arrow, '=') |
|
|
| |
| s = s.replace(r'\cdot', '*').replace(r'\times', '*') |
| |
| |
| |
| |
| |
| |
| expressions = [sanitize_latex_for_sympy(expr.strip()) for expr in s.split(',')] |
| |
| |
| return [e for e in expressions if e] |
|
|