Add text chunking and Persian language helpers

This commit is contained in:
arsam
2026-09-18 22:41:11 +03:30
parent 07f518f76b
commit 3857bd8ded
2 changed files with 71 additions and 0 deletions
+45
View File
@@ -0,0 +1,45 @@
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()