46 lines
1.3 KiB
Python
46 lines
1.3 KiB
Python
import re
|
||
import unicodedata
|
||
|
||
# Normalize Arabic lookalikes to Persian
|
||
_ARABIC_TO_PERSIAN = str.maketrans({
|
||
'ي': 'ی', # Arabic ya → Persian ya
|
||
'ك': 'ک', # Arabic kaf → Persian kaf
|
||
'ى': 'ی', # Alef maqsura → Persian ya
|
||
})
|
||
|
||
_PUPPY_RE = re.compile(r'puppy|پاپی|پاپي', re.IGNORECASE | re.UNICODE)
|
||
|
||
# Rough threshold: if >15% of chars are Arabic/Persian script → Persian
|
||
_PERSIAN_BLOCK = re.compile(r'[-ۿ]')
|
||
|
||
|
||
def normalize(text: str) -> str:
|
||
text = unicodedata.normalize('NFC', text)
|
||
text = text.translate(_ARABIC_TO_PERSIAN)
|
||
text = text.replace('', '').replace('', '') # ZWNJ / ZWJ
|
||
return text
|
||
|
||
|
||
def is_puppy_mentioned(text: str) -> bool:
|
||
if not text:
|
||
return False
|
||
return bool(_PUPPY_RE.search(normalize(text)))
|
||
|
||
|
||
def detect_language(text: str) -> str:
|
||
if not text:
|
||
return 'en'
|
||
persian_count = len(_PERSIAN_BLOCK.findall(text))
|
||
return 'fa' if persian_count > len(text) * 0.15 else 'en'
|
||
|
||
|
||
def strip_puppy_prefix(text: str) -> str:
|
||
"""Remove 'Puppy/پاپی' name prefix from a command-style message."""
|
||
cleaned = re.sub(
|
||
r'^[\s]*(puppy|پاپی|پاپي)[،,:\s]+',
|
||
'',
|
||
normalize(text),
|
||
flags=re.IGNORECASE | re.UNICODE,
|
||
)
|
||
return cleaned.strip() or text.strip()
|