diff --git a/src/ai_typewriter/math_format.py b/src/ai_typewriter/math_format.py index 2d4c9c2..f02ef4b 100644 --- a/src/ai_typewriter/math_format.py +++ b/src/ai_typewriter/math_format.py @@ -9,36 +9,14 @@ GREEK_AND_SYMBOLS = { r"\gamma": "γ", r"\delta": "δ", r"\epsilon": "ε", - r"\varepsilon": "ε", - r"\zeta": "ζ", - r"\eta": "η", r"\theta": "θ", - r"\vartheta": "ϑ", - r"\iota": "ι", - r"\kappa": "κ", r"\lambda": "λ", r"\mu": "μ", - r"\nu": "ν", - r"\xi": "ξ", r"\pi": "π", - r"\rho": "ρ", r"\sigma": "σ", - r"\tau": "τ", - r"\upsilon": "υ", r"\phi": "φ", - r"\varphi": "φ", - r"\chi": "χ", - r"\psi": "ψ", r"\omega": "ω", - r"\Gamma": "Γ", r"\Delta": "Δ", - r"\Theta": "Θ", - r"\Lambda": "Λ", - r"\Xi": "Ξ", - r"\Pi": "Π", - r"\Sigma": "Σ", - r"\Phi": "Φ", - r"\Psi": "Ψ", r"\Omega": "Ω", r"\infty": "∞", r"\leq": "≤", @@ -51,31 +29,11 @@ GREEK_AND_SYMBOLS = { r"\times": "×", r"\cdot": "·", r"\pm": "±", - r"\mp": "∓", r"\to": "→", r"\rightarrow": "→", - r"\leftarrow": "←", - r"\Rightarrow": "⇒", - r"\Leftrightarrow": "⇔", - r"\forall": "∀", - r"\exists": "∃", - r"\in": "∈", - r"\notin": "∉", - r"\subset": "⊂", - r"\subseteq": "⊆", - r"\cup": "∪", - r"\cap": "∩", - r"\emptyset": "∅", - r"\mathbb{R}": "ℝ", - r"\mathbb{C}": "ℂ", - r"\mathbb{N}": "ℕ", - r"\mathbb{Z}": "ℤ", - r"\mathbb{Q}": "ℚ", r"\int": "∫", r"\sum": "∑", - r"\prod": "∏", - r"\partial": "∂", - r"\nabla": "∇", + r"\sqrt": "√", } SUPERSCRIPT = str.maketrans({ @@ -94,18 +52,9 @@ SUBSCRIPT = str.maketrans({ "s": "ₛ", "t": "ₜ", "u": "ᵤ", "v": "ᵥ", "x": "ₓ", }) -_GROUP_PATTERN = re.compile(r"([_^])\(([^()]+)\)|([_^])\{([^{}]+)\}|([_^])([A-Za-z0-9+\-=])") -_MATH_START_PATTERN = re.compile( - r"(\\[A-Za-z]+|[A-Za-zΑ-Ωα-ω]+(?:[_^](?:\([^()]+\)|\{[^{}]+\}|[A-Za-z0-9+\-=]))+|[∫∑∞≤≥≠≈α-ωΑ-Ω]|=)" -) -_MATH_TOKEN_PATTERN = re.compile( - r"\s*(?:" - r"\\[A-Za-z]+(?:\([^()]+\)|\{[^{}]+\})?" - r"|[A-Za-zΑ-Ωα-ω0-9]+(?:[_^](?:\([^()]+\)|\{[^{}]+\}|[A-Za-z0-9+\-=]))*" - r"|[=+\-*/×·≤≥≠≈<>]" - r"|[(){}]" - r")" -) +_SCRIPT_PATTERN = re.compile(r"([_^])\(([^()]+)\)|([_^])\{([^{}]+)\}|([_^])([A-Za-z0-9+\-=])") +EQUATION_OPEN = "[EQ]" +EQUATION_CLOSE = "[/EQ]" @dataclass(frozen=True) @@ -127,75 +76,54 @@ def _replace_script(match: re.Match[str]) -> str: def format_math_text(text: str, mode: str = "plain") -> str: - r"""Convert common UnicodeMath/LaTeX-like linear math to Unicode plain text. - - This is meant for Word normal text insertion. It makes expressions such as - z_1, x^2, \alpha and \infty render as z₁, x², α and ∞ without requiring the - user to manually open Word's equation editor for every expression. - """ + """Optional legacy text-only math formatting for non-Word targets.""" if mode == "plain": return text if mode not in {"unicode", "unicode_math"}: raise ValueError("math_text_format doit être 'plain', 'unicode' ou 'word_equation'") formatted = text - # Replace longer commands first so \subseteq wins before \subset. for command, replacement in sorted(GREEK_AND_SYMBOLS.items(), key=lambda item: len(item[0]), reverse=True): formatted = formatted.replace(command, replacement) - formatted = re.sub(r"\\vec\(([^()]+)\)", lambda match: match.group(1) + "⃗", formatted) - formatted = formatted.replace(r"\sqrt", "√") previous = None while previous != formatted: previous = formatted - formatted = _GROUP_PATTERN.sub(_replace_script, formatted) + formatted = _SCRIPT_PATTERN.sub(_replace_script, formatted) return formatted -def _consume_math_sequence(text: str, start: int) -> int: - end = start - while end < len(text): - if text[end] in "\n.,;:!?": - break - match = _MATH_TOKEN_PATTERN.match(text, end) - if match is None: - break - end = match.end() - return end - - def split_word_equation_segments(text: str) -> list[MathSegment]: - r"""Split text into normal text and Word UnicodeMath equation insertions. + """Split explicit [EQ]...[/EQ] blocks into Word equation actions. - Equation segments keep Word's linear UnicodeMath syntax (z_1, x^2, \alpha), - because Word converts that syntax inside the Alt+= equation editor. + No automatic math guessing is done here: the model decides where an equation + starts and ends by returning [EQ] before the expression and [/EQ] after it. """ segments: list[MathSegment] = [] cursor = 0 + while cursor < len(text): - match = _MATH_START_PATTERN.search(text, cursor) - if match is None: + start = text.find(EQUATION_OPEN, cursor) + if start == -1: if cursor < len(text): segments.append(MathSegment("text", text[cursor:])) break - start = match.start() if start > cursor: segments.append(MathSegment("text", text[cursor:start])) - end = _consume_math_sequence(text, start) - expression = text[start:end].strip() - leading = text[start : start + len(text[start:end]) - len(text[start:end].lstrip())] - trailing = text[start:end][len(text[start:end].rstrip()):] - if leading: - segments.append(MathSegment("text", leading)) + expression_start = start + len(EQUATION_OPEN) + end = text.find(EQUATION_CLOSE, expression_start) + if end == -1: + expression = text[expression_start:].strip() + cursor = len(text) + else: + expression = text[expression_start:end].strip() + cursor = end + len(EQUATION_CLOSE) + if expression: segments.append(MathSegment("equation", expression)) - if trailing: - segments.append(MathSegment("text", trailing)) - cursor = end - # Merge adjacent text segments for cleaner action counts. merged: list[MathSegment] = [] for segment in segments: if merged and segment.kind == "text" and merged[-1].kind == "text":