BasicsRAGChunkingOpenAIAnthropicGoogle
Problem
Implement word_overlap_chunks(text, chunk_words, overlap) that splits text into whitespace-separated words and produces space-joined chunks of up to chunk_words words, advancing the window start by step = chunk_words - overlap words each iteration and stopping once the end of the word list is covered.
Why It Matters: Sliding-window overlap prevents RAG pipelines from slicing a key fact or entity reference right in half at a chunk boundary.
Function Signature
def word_overlap_chunks(text: str, chunk_words: int, overlap: int) -> list[str]:
"""Split text into overlapping word chunks."""
Sample Input & Output
Example 1: 6 Words With chunk_words=4, overlap=2
Input:
text = "one two three four five six", chunk_words = 4, overlap = 2
Expected Output:
["one two three four", "three four five six"]
Explanation: Window 1 covers indices 0..3; step is 4 - 2 = 2, so Window 2 covers indices 2..5 and stops at the end.
Example 2: Text Shorter Than chunk_words & Empty Text
Input:
word_overlap_chunks("hello world", 5, 1) | word_overlap_chunks(" ", 4, 1)
Expected Output:
["hello world"] | []
Explanation: Short text emits a single chunk without repeating; whitespace-only text returns [].
Constraints & Edge Cases
0 <= overlap < chunk_words.- Return a list of space-joined chunk strings.
- Guaranteed
0 <= overlap < chunk_wordssostep = chunk_words - overlap >= 1. - Stop immediately once
start + chunk_words >= len(words)to avoid emitting redundant trailing sub-chunks.
Constraints & Evaluation Criteria
- `0 <= overlap < chunk_words`.
- Return a list of space-joined chunk strings.
Hints
Learn the Concept First
Python 3⌘/Ctrl+Enter to Run · ⌘/Ctrl+Shift+Enter to Submit
Loading Code Editor...
+1 Hidden Submit Case
Input / Scenario
'a b c d e', 3, 1
Expected Output / Criteria
['a b c', 'c d e']