Split text into chunks using the specified strategy. - `chunk_size`: maximum number of **characters** per chunk. - `overlap`: number of characters shared between consecutive chunks. - `strategy`: splitting strategy. Returns a deterministic sequence of chunks. Same input + params → same output.
(
text: &str,
chunk_size: usize,
overlap: usize,
strategy: ChunkStrategy,
)
| 80 | /// |
| 81 | /// Returns a deterministic sequence of chunks. Same input + params → same output. |
| 82 | pub fn chunk_text( |
| 83 | text: &str, |
| 84 | chunk_size: usize, |
| 85 | overlap: usize, |
| 86 | strategy: ChunkStrategy, |
| 87 | ) -> Result<Vec<TextChunk>, ChunkError> { |
| 88 | if chunk_size == 0 { |
| 89 | return Err(ChunkError::InvalidChunkSize); |
| 90 | } |
| 91 | if overlap >= chunk_size { |
| 92 | return Err(ChunkError::OverlapTooLarge); |
| 93 | } |
| 94 | if text.is_empty() { |
| 95 | return Ok(Vec::new()); |
| 96 | } |
| 97 | |
| 98 | match strategy { |
| 99 | ChunkStrategy::Character => chunk_by_characters(text, chunk_size, overlap), |
| 100 | ChunkStrategy::Sentence => chunk_by_sentences(text, chunk_size, overlap), |
| 101 | ChunkStrategy::Paragraph => chunk_by_paragraphs(text, chunk_size, overlap), |
| 102 | } |
| 103 | } |
| 104 | |
| 105 | /// Character-based splitting: advance by `chunk_size - overlap` chars each step. |
| 106 | fn chunk_by_characters( |