whFalsa / app /utils /phone.py
codeBOKER's picture
feat: LLM-driven driver message cleanup for phone numbers and names
bb9b63f
Raw
History Blame Contribute Delete
648 Bytes
import re
_PHONE_RE = re.compile(
r"(?:\+|00)\d[\d\s\-\.\(\)]{6,18}\d"
)
_LOCAL_PHONE_RE = re.compile(
r"(?<!\d)"
r"(?:\d[\d\s\-\.\(\)]{7,18}\d)"
r"(?!\d)"
)
def count_digits(text: str) -> int:
return sum(1 for c in text if c.isdigit())
def strip_phone_numbers(text: str, replacement: str = "[رقم الهاتف]") -> str:
if not text:
return text
def _replace(match: re.Match) -> str:
if count_digits(match.group()) >= 7:
return replacement
return match.group()
result = _PHONE_RE.sub(_replace, text)
result = _LOCAL_PHONE_RE.sub(_replace, result)
return result