import re def sanitize_latex_for_sympy(latex_str: str) -> str: """ מסיר פקודות עיצוב של LaTeX (כמו צבעים) כדי ש-SymPy יוכל לפענח את הביטוי. לדוגמה: הופך את \color{red}{5} ל- 5. """ if not latex_str: return latex_str # שלב 1: מחלץ את התוכן מתוך פקודות \color{...}{content} cleaned = re.sub(r'\\color\{.*?\}\{(.*?)\}', r'\1', latex_str) # שלב 2: מוחק פקודות \color{...} גלמודות שלא עוטפות כלום cleaned = re.sub(r'\\color\{.*?\}', '', cleaned) # שלב 3: מחיקת פקודות \text{} (SymPy שונא טקסט חופשי) cleaned = re.sub(r'\\text\{.*?\}', '', cleaned) # שלב 4: ניקוי סוגריים מסוג \left( \right) 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 # 1. Strip Hebrew Content including surrounding parentheses (e.g. (לכן C)) # This prevents leaving behind empty shells like "( C)" which crash SymPy. s = re.sub(r'\([^)]*[\u0590-\u05FF]+[^)]*\)', '', s) s = re.sub(r'[\u0590-\u05FF]+', '', s) # 2. Strip formatting tags (\text{} and \color{}) s = re.sub(r'\\text\{.*?\}', '', s) s = re.sub(r'\\color\{.*?\}', '', s) # 3. Replace Logical Arrows with Equals (to maintain equation structure) arrows = [r'\Rightarrow', r'\implies', r'\iff', r'\rightarrow'] for arrow in arrows: s = s.replace(arrow, '=') # --- 🚀 BEGIN HOTFIX V9.0.3: LATEX MULTIPLICATION --- s = s.replace(r'\cdot', '*').replace(r'\times', '*') # --- END HOTFIX --- # 4. Split multiple equations # SymPy cannot parse "x=0, y=9" as a single AST node. # We must split by comma and return a list of clean equations. # We also run the existing sanitize_latex_for_sympy on each part. expressions = [sanitize_latex_for_sympy(expr.strip()) for expr in s.split(',')] # Filter out empty strings that might have resulted from the split/regex return [e for e in expressions if e]