dlp-lab / demo /src /normalize.py
alirezaaminzadeh's picture
Publish AriaDLP synthetic Persian/English redaction lab
eef4eed verified
Raw History Blame Contribute Delete
693 Bytes
from __future__ import annotations
import re
DIGIT_TABLE = str.maketrans("۰۱۲۳۴۵۶۷۸۹٠١٢٣٤٥٦٧٨٩", "01234567890123456789")
ZWNJ = "\u200c"
NBSP = "\u00a0"
HALF_SPACE_RE = re.compile(r"[\u200c\u200b\u00a0]")
def fold_digits(text: str) -> str:
return (text or "").translate(DIGIT_TABLE)
def squeeze_spaces(text: str) -> str:
return HALF_SPACE_RE.sub(" ", text or "")
def compact(text: str) -> str:
return fold_digits(squeeze_spaces(text or ""))
def only_digits(text: str) -> str:
return "".join(ch for ch in fold_digits(text or "") if ch.isdigit())
def strip_iban(text: str) -> str:
return re.sub(r"[\s\-]", "", fold_digits(text or "")).upper()