julienne 0.1.0

Range-preserving Rust text chunkers for retrieval and embedding pipelines
Documentation
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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
use std::sync::Arc;

use crate::character::validate_chunk_config;
use crate::chunk::{measured_spans, TextChunk, TextChunkIter, TextSpan};
use crate::error::ChunkError;
use crate::sizing::{CharSizer, ChunkConfig, ChunkSizer, FunctionSizer};

/// Default sentence delimiters.
const DEFAULT_DELIMITERS: &[&str] = &[". ", "! ", "? ", "\n"];

/// A text splitter that respects sentence boundaries.
///
/// Splits text into sentences using configurable delimiters, then greedily
/// packs consecutive sentences into chunks that fit within `chunk_size`.
///
/// Inspired by Chonkie's sentence chunking approach.
#[derive(Clone)]
pub struct SentenceChunker<S = CharSizer> {
    pub(crate) config: ChunkConfig<S>,
    pub(crate) min_sentences_per_chunk: usize,
    pub(crate) delimiters: Vec<String>,
    pub(crate) min_characters_per_sentence: usize,
    pub(crate) strip_whitespace: bool,
    length_fn: crate::LengthFn,
}

impl SentenceChunker<CharSizer> {
    pub fn new(chunk_size: usize, chunk_overlap: usize) -> Self {
        Self {
            config: ChunkConfig::new(chunk_size, chunk_overlap, CharSizer),
            min_sentences_per_chunk: 1,
            delimiters: DEFAULT_DELIMITERS.iter().map(|s| s.to_string()).collect(),
            min_characters_per_sentence: 12,
            strip_whitespace: true,
            length_fn: Arc::new(crate::char_len),
        }
    }

    pub fn builder() -> SentenceChunkerBuilder<CharSizer> {
        SentenceChunkerBuilder::default()
    }
}

impl<S> SentenceChunker<S>
where
    S: ChunkSizer,
{
    pub fn split_text(&self, text: &str) -> Vec<String> {
        self.chunks(text)
            .map(|chunk| chunk.text.to_string())
            .collect()
    }

    pub fn chunks<'a>(&'a self, text: &'a str) -> impl Iterator<Item = TextChunk<'a>> + 'a {
        let len_fn = self.length_fn.as_ref();
        TextChunkIter::new(
            text,
            measured_spans(text, self.chunk_spans(text, len_fn).into_iter(), len_fn),
        )
    }

    pub fn split_chunks<'a>(&'a self, text: &'a str) -> Vec<TextChunk<'a>> {
        self.chunks(text).collect()
    }

    fn chunk_spans(&self, text: &str, len_fn: &dyn Fn(&str) -> usize) -> Vec<TextSpan> {
        if text.is_empty() {
            return Vec::new();
        }

        let mut sentences = split_into_sentence_spans(text, &self.delimiters);

        sentences = merge_short_sentence_spans(text, sentences, self.min_characters_per_sentence);

        if sentences.is_empty() {
            return Vec::new();
        }

        let mut chunks: Vec<TextSpan> = Vec::new();
        let mut current_sentences: Vec<usize> = Vec::new(); // indices into sentences
        let mut current_len: usize = 0;

        for (i, sentence) in sentences.iter().enumerate() {
            let s_len = len_fn(sentence.text(text));

            if current_sentences.is_empty() {
                current_sentences.push(i);
                current_len = s_len;
                continue;
            }

            // Would adding this sentence exceed chunk_size?
            if current_len + s_len > self.config.chunk_size {
                // Best effort: avoid emitting tiny chunks when configured to keep
                // at least N sentences per chunk.
                if current_sentences.len() < self.min_sentences_per_chunk {
                    current_sentences.push(i);
                    current_len += s_len;
                    continue;
                }

                // Emit current chunk
                if let Some(chunk) = self.join_sentence_spans(text, &sentences, &current_sentences)
                {
                    chunks.push(chunk);
                }

                // Handle overlap: backtrack from the end of the previous chunk
                current_sentences.clear();
                current_len = 0;

                if self.config.chunk_overlap > 0 && i > 0 {
                    // Walk backwards from the last sentence in the previous chunk
                    let prev_end = i; // exclusive
                    let mut overlap_start = prev_end;
                    let mut overlap_len: usize = 0;

                    while overlap_start > 0 {
                        let candidate = overlap_start - 1;
                        let candidate_len = len_fn(sentences[candidate].text(text));
                        if overlap_len + candidate_len > self.config.chunk_overlap {
                            break;
                        }
                        overlap_len += candidate_len;
                        overlap_start = candidate;
                    }

                    for j in overlap_start..prev_end {
                        current_sentences.push(j);
                    }
                    current_len = overlap_len;
                }

                current_sentences.push(i);
                current_len += s_len;
            } else {
                current_sentences.push(i);
                current_len += s_len;
            }
        }

        // Emit final chunk
        if !current_sentences.is_empty() {
            if let Some(chunk) = self.join_sentence_spans(text, &sentences, &current_sentences) {
                chunks.push(chunk);
            }
        }

        chunks
    }

    fn join_sentence_spans(
        &self,
        input: &str,
        sentences: &[TextSpan],
        indices: &[usize],
    ) -> Option<TextSpan> {
        let start = sentences[*indices.first()?].start;
        let end = sentences[*indices.last()?].end;
        let span = TextSpan::new(start, end);
        if self.strip_whitespace {
            span.trim(input)
        } else {
            Some(span)
        }
    }
}

#[derive(Clone)]
pub struct SentenceChunkerBuilder<S = CharSizer> {
    inner: SentenceChunker<S>,
}

impl Default for SentenceChunkerBuilder<CharSizer> {
    fn default() -> Self {
        Self {
            inner: SentenceChunker::new(1000, 200),
        }
    }
}

impl<S> SentenceChunkerBuilder<S>
where
    S: ChunkSizer,
{
    pub fn chunk_size(mut self, chunk_size: usize) -> Self {
        self.inner.config.chunk_size = chunk_size;
        self
    }

    pub fn chunk_overlap(mut self, chunk_overlap: usize) -> Self {
        self.inner.config.chunk_overlap = chunk_overlap;
        self
    }

    pub fn min_sentences_per_chunk(mut self, min_sentences_per_chunk: usize) -> Self {
        self.inner.min_sentences_per_chunk = min_sentences_per_chunk;
        self
    }

    pub fn delimiters(mut self, delimiters: impl IntoIterator<Item = impl Into<String>>) -> Self {
        self.inner.delimiters = delimiters.into_iter().map(Into::into).collect();
        self
    }

    pub fn min_characters_per_sentence(mut self, min_characters_per_sentence: usize) -> Self {
        self.inner.min_characters_per_sentence = min_characters_per_sentence;
        self
    }

    pub fn strip_whitespace(mut self, strip_whitespace: bool) -> Self {
        self.inner.strip_whitespace = strip_whitespace;
        self
    }

    pub fn sizer<T>(self, sizer: T) -> SentenceChunkerBuilder<T>
    where
        T: ChunkSizer,
    {
        let inner = self.inner;
        let length_sizer = sizer.clone();
        SentenceChunkerBuilder {
            inner: SentenceChunker {
                config: ChunkConfig::new(
                    inner.config.chunk_size,
                    inner.config.chunk_overlap,
                    sizer,
                ),
                min_sentences_per_chunk: inner.min_sentences_per_chunk,
                delimiters: inner.delimiters,
                min_characters_per_sentence: inner.min_characters_per_sentence,
                strip_whitespace: inner.strip_whitespace,
                length_fn: Arc::new(move |value: &str| length_sizer.size(value)),
            },
        }
    }

    pub fn length_fn(self, length_fn: crate::LengthFn) -> SentenceChunkerBuilder<FunctionSizer> {
        self.sizer(FunctionSizer::new(length_fn))
    }

    pub fn build(self) -> Result<SentenceChunker<S>, ChunkError> {
        validate_chunk_config(
            self.inner.config.chunk_size,
            self.inner.config.chunk_overlap,
        )?;
        if self.inner.delimiters.is_empty() {
            return Err(ChunkError::invalid_configuration(
                "sentence chunker requires at least one delimiter",
            ));
        }
        if self.inner.min_sentences_per_chunk == 0 {
            return Err(ChunkError::invalid_configuration(
                "min_sentences_per_chunk must be greater than zero",
            ));
        }
        Ok(self.inner)
    }
}

/// Split text into sentences by scanning for delimiters.
/// The delimiter is attached to the **preceding** sentence.
#[cfg(test)]
fn split_into_sentences(text: &str, delimiters: &[String]) -> Vec<String> {
    let mut sentences: Vec<String> = Vec::new();
    let mut remaining = text;

    while !remaining.is_empty() {
        // Find the earliest delimiter match
        let mut earliest_pos: Option<usize> = None;
        let mut earliest_delim_len: usize = 0;

        for delim in delimiters {
            if let Some(pos) = remaining.find(delim.as_str()) {
                match earliest_pos {
                    None => {
                        earliest_pos = Some(pos);
                        earliest_delim_len = delim.len();
                    }
                    Some(ep) => {
                        if pos < ep {
                            earliest_pos = Some(pos);
                            earliest_delim_len = delim.len();
                        }
                    }
                }
            }
        }

        match earliest_pos {
            Some(pos) => {
                let end = pos + earliest_delim_len;
                let sentence = &remaining[..end];
                if !sentence.is_empty() {
                    sentences.push(sentence.to_string());
                }
                remaining = &remaining[end..];
            }
            None => {
                // No more delimiters found — remainder is the last sentence
                if !remaining.is_empty() {
                    sentences.push(remaining.to_string());
                }
                break;
            }
        }
    }

    sentences
}

fn split_into_sentence_spans(text: &str, delimiters: &[String]) -> Vec<TextSpan> {
    let mut sentences = Vec::new();
    let mut start = 0usize;

    while start < text.len() {
        let remaining = &text[start..];
        let mut earliest_pos: Option<usize> = None;
        let mut earliest_delim_len: usize = 0;

        for delim in delimiters {
            if let Some(pos) = remaining.find(delim.as_str()) {
                if earliest_pos.is_none_or(|current| pos < current) {
                    earliest_pos = Some(pos);
                    earliest_delim_len = delim.len();
                }
            }
        }

        match earliest_pos {
            Some(pos) => {
                let end = start + pos + earliest_delim_len;
                if start < end {
                    sentences.push(TextSpan::new(start, end));
                }
                start = end;
            }
            None => {
                sentences.push(TextSpan::new(start, text.len()));
                break;
            }
        }
    }

    sentences
}

/// Merge sentences shorter than `min_chars` with the following sentence.
#[cfg(test)]
fn merge_short_sentences(sentences: Vec<String>, min_chars: usize) -> Vec<String> {
    if sentences.is_empty() {
        return sentences;
    }

    let mut result: Vec<String> = Vec::new();
    let mut buffer = String::new();

    for sentence in sentences {
        buffer.push_str(&sentence);
        if buffer.chars().count() >= min_chars {
            result.push(buffer);
            buffer = String::new();
        }
    }

    // Remaining buffer: merge with last sentence or push as-is
    if !buffer.is_empty() {
        if let Some(last) = result.last_mut() {
            last.push_str(&buffer);
        } else {
            result.push(buffer);
        }
    }

    result
}

fn merge_short_sentence_spans(
    input: &str,
    sentences: Vec<TextSpan>,
    min_chars: usize,
) -> Vec<TextSpan> {
    if sentences.is_empty() {
        return sentences;
    }

    let mut result: Vec<TextSpan> = Vec::new();
    let mut buffer_start: Option<usize> = None;
    let mut buffer_end = 0usize;

    for sentence in sentences {
        let start = buffer_start.unwrap_or(sentence.start);
        buffer_start = Some(start);
        buffer_end = sentence.end;
        let buffer = TextSpan::new(start, buffer_end);
        if buffer.text(input).chars().count() >= min_chars {
            result.push(buffer);
            buffer_start = None;
        }
    }

    if let Some(start) = buffer_start {
        let buffer = TextSpan::new(start, buffer_end);
        if let Some(last) = result.last_mut() {
            last.end = buffer.end;
        } else {
            result.push(buffer);
        }
    }

    result
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_sentence_chunker_basic() {
        let chunker = SentenceChunker {
            config: crate::sizing::ChunkConfig::new(30, 0, crate::sizing::CharSizer),
            min_sentences_per_chunk: 1,
            min_characters_per_sentence: 1,
            ..SentenceChunker::new(30, 0)
        };
        let result = chunker.split_text("Hello world. How are you? I am fine. Thank you.");
        // Sentences: ["Hello world. ", "How are you? ", "I am fine. ", "Thank you."]
        // Sizes: 13, 13, 10, 10
        // Chunk 1: "Hello world. How are you?" (26 chars) — fits
        // Can't add "I am fine. " (10) → 36 > 30
        // Chunk 2: "I am fine. Thank you." (21 chars)
        assert_eq!(result.len(), 2);
        assert!(result[0].contains("Hello world."));
        assert!(result[1].contains("Thank you."));
    }

    #[test]
    fn test_sentence_chunker_no_delimiters() {
        let chunker = SentenceChunker::new(100, 0);
        let result = chunker.split_text("No delimiters in this text");
        assert_eq!(result, vec!["No delimiters in this text"]);
    }

    #[test]
    fn test_sentence_chunker_empty() {
        let chunker = SentenceChunker::new(100, 0);
        let result = chunker.split_text("");
        assert!(result.is_empty());
    }

    #[test]
    fn test_sentence_chunker_delimiter_at_end() {
        let chunker = SentenceChunker {
            min_sentences_per_chunk: 1,
            min_characters_per_sentence: 1,
            ..SentenceChunker::new(100, 0)
        };
        let result = chunker.split_text("Hello world. ");
        assert_eq!(result, vec!["Hello world."]);
    }

    #[test]
    fn test_sentence_chunker_min_chars_filtering() {
        let chunker = SentenceChunker {
            config: crate::sizing::ChunkConfig::new(100, 0, crate::sizing::CharSizer),
            min_sentences_per_chunk: 1,
            min_characters_per_sentence: 15,
            ..SentenceChunker::new(100, 0)
        };
        // "Hi. " is 4 chars — below min_characters_per_sentence=15
        // Should be merged with "How are you doing today? "
        let result = chunker.split_text("Hi. How are you doing today? Fine thanks.");
        // "Hi. How are you doing today? " merged because "Hi. " < 15 chars
        assert_eq!(result.len(), 1);
    }

    #[test]
    fn test_sentence_chunker_overlap() {
        let chunker = SentenceChunker {
            config: crate::sizing::ChunkConfig::new(60, 35, crate::sizing::CharSizer),
            min_sentences_per_chunk: 1,
            min_characters_per_sentence: 1,
            ..SentenceChunker::new(60, 35)
        };
        let text = "Schemas define structure. Vectorizers create embeddings. Workers process pending rows. Queries retrieve semantic context.";
        let result = chunker.split_text(text);
        assert!(
            result.len() >= 2,
            "Expected multiple chunks, got {:?}",
            result
        );
        assert!(
            result[0].contains("Vectorizers create embeddings.")
                && result[1].contains("Vectorizers create embeddings."),
            "Expected overlap sentence to appear in adjacent chunks: {:?}",
            result
        );
    }

    #[test]
    fn test_split_into_sentences() {
        let delimiters: Vec<String> = vec![". ", "! ", "? "]
            .into_iter()
            .map(String::from)
            .collect();
        let result = split_into_sentences("Hello world. How are you? Fine! Thanks.", &delimiters);
        assert_eq!(
            result,
            vec!["Hello world. ", "How are you? ", "Fine! ", "Thanks."]
        );
    }

    #[test]
    fn test_merge_short_sentences() {
        let sentences = vec![
            "Hi. ".to_string(),
            "How are you? ".to_string(),
            "Good. ".to_string(),
        ];
        let result = merge_short_sentences(sentences, 10);
        // "Hi. " (4) < 10, merged with "How are you? " → "Hi. How are you? " (18) >= 10, emitted
        // "Good. " (6) < 10, remains in buffer, merged with last
        assert_eq!(result, vec!["Hi. How are you? Good. "]);
    }

    #[test]
    fn test_sentence_chunker_min_sentences_per_chunk() {
        let chunker = SentenceChunker {
            config: crate::sizing::ChunkConfig::new(7, 0, crate::sizing::CharSizer),
            min_sentences_per_chunk: 2,
            min_characters_per_sentence: 1,
            ..SentenceChunker::new(7, 0)
        };
        let result = chunker.split_text("A. B. C. D.");
        assert_eq!(result, vec!["A. B.", "C. D."]);
    }

    #[test]
    fn test_sentence_chunker_min_sentences_best_effort() {
        let chunker = SentenceChunker {
            config: crate::sizing::ChunkConfig::new(7, 0, crate::sizing::CharSizer),
            min_sentences_per_chunk: 2,
            min_characters_per_sentence: 1,
            ..SentenceChunker::new(7, 0)
        };
        let result = chunker.split_text("AAAA. BBBB. CCCC.");
        assert_eq!(result.len(), 2);
        assert!(result[0].contains("AAAA."));
        assert!(result[0].contains("BBBB."));
    }
}