Spaces:
Paused
Paused
File size: 732 Bytes
5eb4857 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 | from typing import List
def chunk_text(text: str, chunk_size: int = 600, chunk_overlap: int = 100) -> List[str]:
"""
Simple recursive-style character splitter for text chunking.
Ensures chunks have the specified overlap for context preservation.
"""
if not text:
return []
chunks = []
start = 0
while start < len(text):
end = start + chunk_size
chunk = text[start:end]
chunks.append(chunk)
# Move start forward by chunk_size minus overlap
start += (chunk_size - chunk_overlap)
# Prevent infinite loop if overlap >= size
if chunk_overlap >= chunk_size:
start = end
return chunks
|