Spaces:
Running on Zero
Running on Zero
Download core/lang.py from MustafaHussein/te-assistant: direct link, hf CLI and curl.
- Browser
- Download file 1.66 kB
-
https://huggingface.co/spaces/MustafaHussein/te-assistant/resolve/main/core/lang.py
- Command line
-
hf download hf://spaces/MustafaHussein/te-assistant/core/lang.py
-
curl -L -o lang.py https://huggingface.co/spaces/MustafaHussein/te-assistant/resolve/main/core/lang.py
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 | |