1008. Recursive Text Splitter (RAG Chunker)
Hard · String
Split a long text into chunks for embedding. Each chunk has length ≤ N characters. Prefer splitting at sentence boundaries (period followed by a space); fall back to word boundaries (space) if no period works; fall back to a hard char split as a last resort.
This is what LangChain's RecursiveCharacterTextSplitter does in spirit.
Input (stdin, JSON): { "text": "First sentence. Second one. Third here.", "max_len": 25 }
Output (stdout): JSON list of chunk strings.
Example output for above: ["First sentence.", "Second one. Third here."]
Constraints: • No chunk exceeds max_len. • Whitespace at chunk boundaries trimmed. • Always return at least one chunk if text is non-empty.
Examples
Example 1
Input: {"text":"First sentence. Second one. Third here.","max_len":25}
Output: ["First sentence.", "Second one. Third here."]Constraints
- text length 1..10000.
- max_len 10..2000.