Fixed-Size Text Chunking with Overlap
Splitting long documents into smaller, overlapping chunks is a foundational step in building Retrieval-Augmented Generation (RAG) applications.
Tasks
Implement the function fixed_size_chunking(text, chunk_size, overlap_size) to:
- Slice the input string
textinto substrings of lengthchunk_sizecharacters. - Slide the window forward by a step size equal to
chunk_size - overlap_size. - Ensure that if
overlap_sizeis greater than or equal tochunk_size, aValueErroris raised. - Ensure that if
chunk_sizeis less than or equal to0, aValueErroris raised. - Handle empty strings by returning an empty list
[].
Example
fixed_size_chunking("abcdefgh", 4, 2)
# Returns: ["abcd", "cdef", "efgh"]