Add text chunking and Persian language helpers
This commit is contained in:
@@ -0,0 +1,26 @@
|
|||||||
|
TG_LIMIT = 4096
|
||||||
|
|
||||||
|
|
||||||
|
def chunk_text(text: str, max_length: int = TG_LIMIT) -> list[str]:
|
||||||
|
"""Split text into ≤max_length chunks, preferring newline then space boundaries."""
|
||||||
|
if len(text) <= max_length:
|
||||||
|
return [text]
|
||||||
|
|
||||||
|
chunks: list[str] = []
|
||||||
|
while text:
|
||||||
|
if len(text) <= max_length:
|
||||||
|
chunks.append(text)
|
||||||
|
break
|
||||||
|
|
||||||
|
split_at = text.rfind('\n', 0, max_length)
|
||||||
|
if split_at == -1:
|
||||||
|
split_at = text.rfind(' ', 0, max_length)
|
||||||
|
if split_at == -1:
|
||||||
|
split_at = max_length
|
||||||
|
|
||||||
|
chunk = text[:split_at].strip()
|
||||||
|
if chunk:
|
||||||
|
chunks.append(chunk)
|
||||||
|
text = text[split_at:].strip()
|
||||||
|
|
||||||
|
return chunks
|
||||||
@@ -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()
|
||||||
Reference in New Issue
Block a user