Spaces:
Sleeping
Sleeping
File size: 7,642 Bytes
89c54bf | 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 | """SMS/text preprocessing for the BERT classifier (stdlib only)."""
from __future__ import annotations
import re
import unicodedata
from typing import Optional
_CHAR_REPLACEMENTS: dict[str, str] = {}
def _add(chars: str, replacement: str) -> None:
for ch in chars:
_CHAR_REPLACEMENTS[ch] = replacement
_add("—–‐‑‒―−", "-")
_add("„‟«»\u201c\u201d", '"')
_CHAR_REPLACEMENTS["\u201c"] = '"'
_CHAR_REPLACEMENTS["\u201d"] = '"'
_add("‚‛′‵\u2018\u2019", "'")
_CHAR_REPLACEMENTS["\u2018"] = "'"
_CHAR_REPLACEMENTS["\u2019"] = "'"
_CHAR_REPLACEMENTS["…"] = "..."
_CHAR_REPLACEMENTS["‥"] = ".."
_CHAR_REPLACEMENTS["․"] = "."
_add("•◦●○▪▫∙⁌⁍⁃", "*")
_add("\u00a0\u2000\u2001\u2002\u2003\u2004\u2005\u2006\u2007\u2008\u2009\u200a\u200b\u200c\u200d\u202f\u205f\u3000", " ")
_CHAR_REPLACEMENTS["\u200b"] = ""
_CHAR_REPLACEMENTS["\u200c"] = ""
_CHAR_REPLACEMENTS["\u200d"] = ""
_CHAR_REPLACEMENTS["×"] = "*"
_CHAR_REPLACEMENTS["÷"] = "/"
_CHAR_REPLACEMENTS["±"] = "+/-"
_CHAR_REPLACEMENTS["≈"] = "~"
_CHAR_REPLACEMENTS["≠"] = "!="
_CHAR_REPLACEMENTS["≤"] = "<="
_CHAR_REPLACEMENTS["≥"] = ">="
_CHAR_REPLACEMENTS["∞"] = "infinite"
_CHAR_REPLACEMENTS["→"] = "->"
_CHAR_REPLACEMENTS["←"] = "<-"
_CHAR_REPLACEMENTS["↑"] = "^"
_CHAR_REPLACEMENTS["↓"] = "v"
_CHAR_REPLACEMENTS["↔"] = "<->"
_CHAR_REPLACEMENTS["©"] = "(c)"
_CHAR_REPLACEMENTS["®"] = "(R)"
_CHAR_REPLACEMENTS["™"] = "TM"
_CHAR_REPLACEMENTS["°"] = "degree"
_CHAR_REPLACEMENTS["‰"] = "0/00"
_CHAR_REPLACEMENTS["‱"] = "0/000"
_CHAR_REPLACEMENTS["†"] = "+"
_CHAR_REPLACEMENTS["‡"] = "++"
_CHAR_REPLACEMENTS["§"] = "S"
_CHAR_REPLACEMENTS["¶"] = "P"
_CHAR_REPLACEMENTS["‹"] = "<"
_CHAR_REPLACEMENTS["›"] = ">"
_CHAR_REPLACEMENTS.update(
{
"½": "1/2",
"¼": "1/4",
"¾": "3/4",
"⅓": "1/3",
"⅔": "2/3",
"⅕": "1/5",
"⅖": "2/5",
"⅗": "3/5",
"⅘": "4/5",
"⅙": "1/6",
"⅚": "5/6",
"⅛": "1/8",
"⅜": "3/8",
"⅝": "5/8",
"⅞": "7/8",
"⅐": "1/7",
"⅑": "1/9",
"⅒": "1/10",
}
)
_CHAR_REPLACEMENTS.update(
{
"¹": "^1",
"²": "^2",
"³": "^3",
"⁴": "^4",
"⁵": "^5",
"⁶": "^6",
"⁷": "^7",
"⁸": "^8",
"⁹": "^9",
"⁰": "^0",
"⁺": "^+",
"⁻": "^-",
"⁼": "^=",
"⁽": "^(",
"⁾": "^)",
}
)
_CHAR_REPLACEMENTS.update(
{
"₁": "_1",
"₂": "_2",
"₃": "_3",
"₄": "_4",
"₅": "_5",
"₆": "_6",
"₇": "_7",
"₈": "_8",
"₉": "_9",
"₀": "_0",
"₊": "_+",
"₋": "_-",
"₌": "_=",
"₍": "_(",
"₎": "_)",
}
)
for _i in range(10):
_CHAR_REPLACEMENTS[chr(0xFF10 + _i)] = str(_i)
for _i in range(26):
_CHAR_REPLACEMENTS[chr(0xFF21 + _i)] = chr(ord("A") + _i)
_CHAR_REPLACEMENTS[chr(0xFF41 + _i)] = chr(ord("a") + _i)
_CHAR_REPLACEMENTS.update(
{
"!": "!",
""": '"',
"#": "#",
"$": "$",
"%": "%",
"&": "&",
"'": "'",
"(": "(",
")": ")",
"*": "*",
"+": "+",
",": ",",
"-": "-",
".": ".",
"/": "/",
":": ":",
";": ";",
"<": "<",
"=": "=",
">": ">",
"?": "?",
"@": "@",
"[": "[",
"\": "\\",
"]": "]",
"^": "^",
"_": "_",
"`": "`",
"{": "{",
"|": "|",
"}": "}",
"~": "~",
}
)
for _i in range(12):
n = str(_i + 1)
_CHAR_REPLACEMENTS[chr(0x2160 + _i)] = n
_CHAR_REPLACEMENTS[chr(0x2170 + _i)] = n
_CHAR_REPLACEMENTS["Ⅼ"] = "50"
_CHAR_REPLACEMENTS["Ⅽ"] = "100"
_CHAR_REPLACEMENTS["Ⅾ"] = "500"
_CHAR_REPLACEMENTS["Ⅿ"] = "1000"
_CHAR_REPLACEMENTS["ⅼ"] = "50"
_CHAR_REPLACEMENTS["ⅽ"] = "100"
_CHAR_REPLACEMENTS["ⅾ"] = "500"
_CHAR_REPLACEMENTS["ⅿ"] = "1000"
for _i in range(26):
_CHAR_REPLACEMENTS[chr(0x1D00C + _i)] = chr(ord("a") + _i)
_DESLOPIFY_TABLE = str.maketrans(_CHAR_REPLACEMENTS)
STATIC_EMAIL = "a@b.com"
STATIC_URL = "www.a.com"
_EMAIL_RE = re.compile(r"[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}")
_URL_RE = re.compile(r"https?://[^\s]+|www\.[^\s]+", flags=re.IGNORECASE)
ORG_CANDIDATE_SEP = " [SEP] "
_CURRENCY_ISO_CODES: tuple[str, ...] = (
"AED",
"BDT",
"CNY",
"EUR",
"GBP",
"INR",
"JPY",
"KRW",
"LKR",
"NPR",
"PKR",
"SAR",
"USD",
)
_CURRENCY_CODE_ALT = "|".join(sorted(_CURRENCY_ISO_CODES, key=len, reverse=True))
_CURRENCY_CODE_RE = re.compile(rf"(?i)(?:{_CURRENCY_CODE_ALT})(?![A-Za-z])")
_CURRENCY_RUPEE_WORDS_RE = re.compile(r"(?i)(?<![A-Za-z])(?:rupees|rupee)(?![A-Za-z])")
_CURRENCY_RS_RE = re.compile(r"(?i)(?<![A-Za-z])rs(?![A-Za-z])")
_CURRENCY_SYM_RE = re.compile(r"[€£¢¥₹₽₿₩](?![A-Za-z])")
_CURRENCY_DUP_DOLLAR_RE = re.compile(r"\${2,}")
def normalize_currency_markers(text: str) -> str:
"""Map common ISO codes and currency symbols to '$' when not followed by [A-Za-z]."""
if not text:
return text
text = _CURRENCY_CODE_RE.sub("$", text)
text = _CURRENCY_RUPEE_WORDS_RE.sub("$", text)
text = _CURRENCY_RS_RE.sub("$", text)
text = _CURRENCY_SYM_RE.sub("$", text)
text = _CURRENCY_DUP_DOLLAR_RE.sub("$", text)
return text
def deslopify(text: str) -> str:
if not text:
return text
return text.translate(_DESLOPIFY_TABLE)
def filter_ascii(text: str) -> str:
if not text:
return text
return "".join(ch for ch in text if (32 <= ord(ch) <= 126) or ch in "\t\n\f\r")
def normalize_terminal_punctuation(text: str) -> str:
if not text:
return ""
stripped = text.strip()
if not stripped:
return ""
last = stripped[-1]
if unicodedata.category(last)[0] != "P":
return f"{stripped}."
return stripped
def digits_to_ones(text: str) -> str:
return "".join("1" if c.isdigit() else c for c in text)
def mask_emails(text: str) -> str:
return _EMAIL_RE.sub(STATIC_EMAIL, text)
def mask_urls(text: str) -> str:
return _URL_RE.sub(STATIC_URL, text)
def normalize_freeform_text(text: str) -> str:
text = mask_emails(text)
text = mask_urls(text)
return digits_to_ones(text)
def preprocess_text(raw_text: str) -> str:
if not raw_text:
return ""
text = deslopify(raw_text)
text = normalize_currency_markers(text)
text = filter_ascii(text)
text = normalize_terminal_punctuation(text)
return normalize_freeform_text(text)
def preprocess_batch(raw_texts: list[str]) -> list[str]:
return [preprocess_text(t) for t in raw_texts]
def preprocess_for_model(
raw_text: str,
org_candidates: Optional[list[str]] = None,
*,
org_sep: str = ORG_CANDIDATE_SEP,
tokenizer_sep: str = " [SEP] ",
) -> str:
text = preprocess_text(raw_text)
if not org_candidates:
return text
return f"{org_sep.join(org_candidates)}{tokenizer_sep}{text}"
if __name__ == "__main__":
_samples = (
"Rs 100",
"RS.500",
"inr100",
"Rs1",
"Rsuper",
"INRing",
"₹500",
"₩1000",
"99 Rs",
"50 rupees",
"pay 1 rupee",
)
for s in _samples:
out = preprocess_text(s)
print(ascii(s), "->", ascii(out))
|