Skip to content

Commit 9936b98

Browse files
feat: add split_text method to utils
1 parent af2c524 commit 9936b98

1 file changed

Lines changed: 36 additions & 0 deletions

File tree

pyrogram/utils.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -617,6 +617,42 @@ def get_first_url(text):
617617
return f"{matches[0][0]}://{matches[0][1]}{matches[0][2]}" if matches else None
618618

619619

620+
def split_text(text: str, max_length: int = 4096) -> List[str]:
621+
"""Split text into chunks no longer than max_length"""
622+
if len(text) <= max_length:
623+
return [text]
624+
625+
chunks = []
626+
remaining = text
627+
628+
while remaining:
629+
if len(remaining) <= max_length:
630+
chunks.append(remaining)
631+
break
632+
633+
cut = remaining[:max_length]
634+
635+
last_newline = cut.rfind('\n')
636+
637+
if last_newline > 0:
638+
chunk = cut[:last_newline]
639+
remaining = remaining[last_newline + 1:]
640+
else:
641+
last_space = cut.rfind(' ')
642+
643+
if last_space > 0 and last_space > len(cut) // 2:
644+
chunk = cut[:last_space]
645+
remaining = remaining[last_space + 1:]
646+
else:
647+
chunk = cut
648+
remaining = remaining[max_length:]
649+
650+
if chunk:
651+
chunks.append(chunk)
652+
653+
return chunks
654+
655+
620656
def parse_text_with_entities(client, message: "raw.types.TextWithEntities", users):
621657
entities = types.List(
622658
filter(

0 commit comments

Comments
 (0)