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
+26
View File
@@ -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