From 09b9f690e050a0b9035de71c2a9f1c8019e78c95 Mon Sep 17 00:00:00 2001 From: allaun Date: Sat, 27 Jun 2026 22:53:16 -0500 Subject: [PATCH] refactor(dna): optimize char classification and fix Unicode math symbol misclassification - Pre-defined and unified character sets (OPERATOR_CHARS, BRACKET_CHARS, PUNCT_CHARS, WHITESPACE_CHARS). - Defined precise sets for SYMMETRY_CHARS, PERIODICITY_CHARS, CONTINUITY_CHARS, and META_MATH_CHARS. - Re-ordered specific checks to precede c.isalpha() so Unicode/Greek math letters are not misclassified as standard lower/upper alpha. Build: 3307 jobs, 0 errors (lake build) --- python/phi/charclass.py | 42 +++++++++++++++++++++++------------------ 1 file changed, 24 insertions(+), 18 deletions(-) diff --git a/python/phi/charclass.py b/python/phi/charclass.py index 6260dd1..717cf86 100644 --- a/python/phi/charclass.py +++ b/python/phi/charclass.py @@ -35,7 +35,14 @@ CHAR_CLASSES = { OPERATOR_CHARS = set("+-*/^%=<>!&|~") BRACKET_CHARS = set("()[]{}") -PUNCT_CHARS = set(".,;:'\"@#$\\") +PUNCT_CHARS = set(".,;:'\"@#$\\_") +WHITESPACE_CHARS = set(" \t\n\r") + +# Unicode mathematical sets (placed at the top to prevent misclassification as standard alpha) +SYMMETRY_CHARS = set("≈≡∼") +PERIODICITY_CHARS = set("πθλ∞") +CONTINUITY_CHARS = set("→Δ∇∫") +META_MATH_CHARS = set("√∑∏∂") def classify_char(c: str) -> int: @@ -84,31 +91,30 @@ def classify_char(c: str) -> int: >>> classify_char("©") 7 """ + # Check Unicode math classes first so Greek/math symbols are not misclassified as standard alpha + if c in PERIODICITY_CHARS: + return 9 + if c in CONTINUITY_CHARS: + return 10 + if c in SYMMETRY_CHARS: + return 8 + if c in META_MATH_CHARS: + return 11 + + # Check standard ASCII classes if c.isdigit(): return 0 if c.isalpha(): return 1 if c.islower() else 2 - # Operator chars - if c in "+-*/^%=<>!&|~": + if c in OPERATOR_CHARS: return 3 - # Bracket chars - if c in "()[]{}": + if c in BRACKET_CHARS: return 4 - # Punctuation - if c in ".,;:'\"@#$\\": + if c in PUNCT_CHARS: return 5 - # Whitespace - if c in (" ", "\t", "\n", "\r"): + if c in WHITESPACE_CHARS: return 6 - # Other (General) - if c in "$\\pi\\theta\\lambda\\infty$": # Periodicity - return 9 - if c in "$\\rightarrow\\Delta\\nabla\\int$": # Continuity - return 10 - if c in "=$\\approx\\equiv\\sim$": # Symmetry - return 8 - if c in "$\\sqrt{}\\sum\\prod\\partial$": # Meta-math - return 11 + return 7