te-assistant / core /lang.py
MustafaHussein's picture
Add Telecom Egypt assistant (int8 ASR only)
7c19e41
Raw History Blame Contribute Delete
1.66 kB
"""Script-based language detection + Egyptian-dialect markers (no model needed)."""
import re
_AR = re.compile(r"[؀-ۿݐ-ݿࢠ-ࣿﭐ-﷿ﹰ-]")
_LAT = re.compile(r"[A-Za-z]")
_DIAC = re.compile(r"[ً-ْٰـ]")
_AR_WORD = re.compile(r"[؀-ۿ]+")
# Words that are common in Egyptian colloquial and rare in Modern Standard Arabic.
# Compared after normalize_ar() (alef/yaa unified, diacritics removed).
EGYPTIAN_MARKERS = {
"ازاي", "عايز", "عايزه", "عايزة", "عاوز", "عاوزه", "عاوزة", "دلوقتي", "دلوقت",
"ليه", "فين", "كده", "كدا", "ايه", "ده", "دي", "دول", "مش", "بتاع", "بتاعي",
"بتاعت", "عشان", "علشان", "لسه", "برضو", "اللي", "هاعمل", "هعمل", "ممكن",
}
_STRONG = EGYPTIAN_MARKERS - {"ممكن", "اللي", "ليه"} # "strong" = enough on their own
def normalize_ar(text: str) -> str:
text = _DIAC.sub("", text)
text = re.sub("[أإآٱ]", "ا", text)
return text.replace("ى", "ي")
def detect_language(text: str) -> str:
"""Return 'ar' or 'en' by dominant script."""
ar = len(_AR.findall(text or ""))
lat = len(_LAT.findall(text or ""))
if ar == 0 and lat == 0:
return "en"
return "ar" if ar >= lat else "en"
def is_egyptian(text: str) -> bool:
"""Heuristic: True if the Arabic text contains Egyptian colloquial markers."""
if detect_language(text) != "ar":
return False
words = _AR_WORD.findall(normalize_ar(text))
hits = [w for w in words if w in EGYPTIAN_MARKERS]
strong = [w for w in hits if w in _STRONG]
return bool(strong) or len(hits) >= 2