Files

46 lines
1.3 KiB
Python
Raw Permalink Blame History

This file contains invisible Unicode characters
This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
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()