Spaces:
Running
Running
File size: 8,267 Bytes
e7c7270 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 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 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 | from __future__ import annotations
import re
import unicodedata
from typing import Dict
SYMBOL_REPLACEMENTS: Dict[str, str] = {
# Equality / comparison
"=": "=",
"β ": " !=", # keep spaced form easy to regex
"β": " approx ",
"~": " approx ",
"β‘": " equivalent ",
"β": " = ",
":=": " = ",
">": " > ",
"<": " < ",
"β₯": " >= ",
"β€": " <= ",
"βͺ": " << ",
"β«": " >> ",
# Arithmetic operators
"+": " + ",
"β": " - ",
"β": " - ",
"β": " - ",
"-": " - ",
"β": " - ",
"Β±": " plus_minus ",
"β": " minus_plus ",
"*": " * ",
"Γ": " * ",
"β
": " * ",
"Β·": " * ",
"Γ·": " / ",
"/": " / ",
"β": " / ",
"β": " / ",
# Brackets / grouping
"[": "(",
"]": ")",
"{": " { ",
"}": " } ",
"β": " floor(",
"β": ")",
"β": " ceil(",
"β": ")",
# Powers / roots
"^": "^",
"Β²": "^2",
"Β³": "^3",
"β΄": "^4",
"β΅": "^5",
"βΆ": "^6",
"β·": "^7",
"βΈ": "^8",
"βΉ": "^9",
"β°": "^0",
"ΒΉ": "^1",
"β": " sqrt ",
"β": " cbrt ",
"β": " fourth_root ",
# Percent / rates
"%": " percent ",
"β°": " permille ",
"β±": " permyriad ",
# Geometry
"β ": " angle ",
"β": " right_angle ",
"Β°": " degrees ",
"β²": " prime ",
"β³": " double_prime ",
"β₯": " perpendicular ",
"β₯": " parallel ",
"β
": " congruent ",
"Ξ": " triangle ",
"β³": " triangle ",
"Ο": " pi ",
# Algebra / calculus-ish
"β": " infinity ",
"β": " proportional_to ",
"β": " delta ",
"β": " sum ",
"β": " product ",
"β«": " integral ",
# Probability / sets
"β©": " intersection ",
"βͺ": " union ",
"β": " subseteq ",
"β": " subset ",
"β": " in ",
"β": " not_in ",
"β
": " empty_set ",
"|": " | ",
# Common OCR / typography junk
"β": '"',
"β": '"',
"β": "'",
"β": "'",
"β¦": "...",
"\u00a0": " ", # non-breaking space
}
TEXT_REPLACEMENTS: Dict[str, str] = {
# Verbal math phrases -> more parseable forms
"divided by": " / ",
"multiplied by": " * ",
"times": " * ",
"plus": " + ",
"minus": " - ",
"equals": " = ",
"is equal to": " = ",
"is greater than or equal to": " >= ",
"is less than or equal to": " <= ",
"greater than or equal to": " >= ",
"less than or equal to": " <= ",
"greater than": " > ",
"less than": " < ",
"not equal to": " != ",
"approximately equal to": " approx ",
"approx equal to": " approx ",
"squared": "^2",
"cubed": "^3",
"square root of": " sqrt ",
"cube root of": " cbrt ",
"to the power of": "^",
"raised to the power of": "^",
"percent": " percent ",
"per cent": " percent ",
"percentage": " percent ",
"remainder when": " remainder ",
"is divisible by": " divisible_by ",
"divisible by": " divisible_by ",
"is a multiple of": " multiple_of ",
"multiple of": " multiple_of ",
"factor of": " factor_of ",
"prime number": " prime ",
"consecutive integers": " consecutive_integers ",
"positive integer": " positive_integer ",
"negative integer": " negative_integer ",
"at least": " >= ",
"at most": " <= ",
"no more than": " <= ",
"no less than": " >= ",
"more than": " > ",
"fewer than": " < ",
"probability of": " probability ",
"mean": " mean ",
"average": " average ",
"median": " median ",
"mode": " mode ",
"standard deviation": " standard_deviation ",
"variance": " variance ",
"perimeter": " perimeter ",
"area": " area ",
"volume": " volume ",
"circumference": " circumference ",
"radius": " radius ",
"diameter": " diameter ",
"ratio of": " ratio ",
"ratio": " ratio ",
"proportion": " proportion ",
"sum of": " sum ",
"difference between": " difference ",
"product of": " product ",
"quotient of": " quotient ",
}
UNICODE_FRACTIONS: Dict[str, str] = {
"Β½": "1/2",
"β
": "1/3",
"β
": "2/3",
"ΒΌ": "1/4",
"ΒΎ": "3/4",
"β
": "1/5",
"β
": "2/5",
"β
": "3/5",
"β
": "4/5",
"β
": "1/6",
"β
": "5/6",
"β
": "1/7",
"β
": "1/8",
"β
": "3/8",
"β
": "5/8",
"β
": "7/8",
"β
": "1/9",
"β
": "1/10",
}
SUPERSCRIPT_MAP: Dict[str, str] = {
"β°": "0",
"ΒΉ": "1",
"Β²": "2",
"Β³": "3",
"β΄": "4",
"β΅": "5",
"βΆ": "6",
"β·": "7",
"βΈ": "8",
"βΉ": "9",
"βΊ": "+",
"β»": "-",
}
SUBSCRIPT_MAP: Dict[str, str] = {
"β": "0",
"β": "1",
"β": "2",
"β": "3",
"β": "4",
"β
": "5",
"β": "6",
"β": "7",
"β": "8",
"β": "9",
"β": "+",
"β": "-",
}
def _replace_unicode_fractions(text: str) -> str:
for k, v in UNICODE_FRACTIONS.items():
text = text.replace(k, v)
return text
def _replace_superscripts_and_subscripts(text: str) -> str:
out = []
i = 0
while i < len(text):
ch = text[i]
if ch in SUPERSCRIPT_MAP:
digits = []
while i < len(text) and text[i] in SUPERSCRIPT_MAP:
digits.append(SUPERSCRIPT_MAP[text[i]])
i += 1
out.append("^" + "".join(digits))
continue
if ch in SUBSCRIPT_MAP:
digits = []
while i < len(text) and text[i] in SUBSCRIPT_MAP:
digits.append(SUBSCRIPT_MAP[text[i]])
i += 1
out.append("_" + "".join(digits))
continue
out.append(ch)
i += 1
return "".join(out)
def _replace_symbol_chars(text: str) -> str:
for k, v in SYMBOL_REPLACEMENTS.items():
text = text.replace(k, v)
return text
def _replace_text_phrases(text: str) -> str:
# longest first so "greater than or equal to" is replaced before "greater than"
for k in sorted(TEXT_REPLACEMENTS.keys(), key=len, reverse=True):
text = re.sub(rf"\b{re.escape(k)}\b", TEXT_REPLACEMENTS[k], text, flags=re.I)
return text
def _normalize_roots(text: str) -> str:
# "sqrt 9" -> "sqrt(9)"
text = re.sub(r"\bsqrt\s+([a-z0-9\(\)\/\+\-\*\.]+)", r"sqrt(\1)", text, flags=re.I)
text = re.sub(r"\bcbrt\s+([a-z0-9\(\)\/\+\-\*\.]+)", r"cbrt(\1)", text, flags=re.I)
return text
def _normalize_percent_expressions(text: str) -> str:
# "25 percent of 80" -> "(25/100) * 80"
text = re.sub(
r"(\d+(?:\.\d+)?)\s*percent\s+of\s+(\d+(?:\.\d+)?)",
r"(\1/100) * \2",
text,
flags=re.I,
)
# "x percent" -> "(x/100)"
text = re.sub(
r"(\d+(?:\.\d+)?)\s*percent\b",
r"(\1/100)",
text,
flags=re.I,
)
# per-mille
text = re.sub(
r"(\d+(?:\.\d+)?)\s*permille\b",
r"(\1/1000)",
text,
flags=re.I,
)
return text
def _normalize_multiplication_spacing(text: str) -> str:
# 5x -> 5*x
text = re.sub(r"(\d)([a-zA-Z])", r"\1*\2", text)
# )x -> )*x
text = re.sub(r"(\))([a-zA-Z0-9])", r"\1*\2", text)
# x( -> x*(
text = re.sub(r"([a-zA-Z0-9])(\()", r"\1*\2", text)
return text
def normalize_math_text(text: str) -> str:
if not text:
return ""
text = unicodedata.normalize("NFKC", text)
text = _replace_unicode_fractions(text)
text = _replace_superscripts_and_subscripts(text)
text = _replace_symbol_chars(text)
text = _replace_text_phrases(text)
text = _normalize_roots(text)
text = _normalize_percent_expressions(text)
text = _normalize_multiplication_spacing(text)
# normalize repeated spaces
text = re.sub(r"\s+", " ", text).strip()
return text
def normalize_for_solver(text: str) -> str:
text = normalize_math_text(text)
# make some solver-oriented aliases
text = text.replace("pi", "3.141592653589793")
text = text.replace("approx", "~")
return text
def normalize_for_parser(text: str) -> str:
text = normalize_math_text(text)
# keep semantic tokens for router/parser
return text |