|
| 1 | +//! Split markdown text into overlapping chunks by line, targeting a token count. |
| 2 | +//! |
| 3 | +//! Tokens are approximated as whitespace-split words. |
| 4 | +
|
| 5 | +/// A chunk produced by the chunker. |
| 6 | +#[derive(Debug, Clone, PartialEq, Eq)] |
| 7 | +pub struct Chunk { |
| 8 | + pub text: String, |
| 9 | + pub start_line: usize, |
| 10 | + pub end_line: usize, |
| 11 | +} |
| 12 | + |
| 13 | +/// Split `text` into chunks of approximately `chunk_size` tokens with `overlap` token overlap. |
| 14 | +/// |
| 15 | +/// Lines are never split mid-line. Each chunk records its 1-based start and end line numbers. |
| 16 | +pub fn chunk_markdown(text: &str, chunk_size: usize, overlap: usize) -> Vec<Chunk> { |
| 17 | + if text.is_empty() || chunk_size == 0 { |
| 18 | + return vec![]; |
| 19 | + } |
| 20 | + |
| 21 | + let lines: Vec<&str> = text.lines().collect(); |
| 22 | + if lines.is_empty() { |
| 23 | + return vec![]; |
| 24 | + } |
| 25 | + |
| 26 | + let line_tokens: Vec<usize> = lines |
| 27 | + .iter() |
| 28 | + .map(|l| l.split_whitespace().count().max(1)) // empty lines count as 1 token |
| 29 | + .collect(); |
| 30 | + |
| 31 | + let mut chunks = Vec::new(); |
| 32 | + let mut start = 0; |
| 33 | + |
| 34 | + while start < lines.len() { |
| 35 | + let mut end = start; |
| 36 | + let mut tokens = 0; |
| 37 | + |
| 38 | + // Accumulate lines until we reach chunk_size tokens |
| 39 | + while end < lines.len() && tokens + line_tokens[end] <= chunk_size { |
| 40 | + tokens += line_tokens[end]; |
| 41 | + end += 1; |
| 42 | + } |
| 43 | + |
| 44 | + // If we couldn't fit even one line, take it anyway |
| 45 | + if end == start { |
| 46 | + end = start + 1; |
| 47 | + } |
| 48 | + |
| 49 | + let chunk_text: String = lines[start..end].join("\n"); |
| 50 | + chunks.push(Chunk { |
| 51 | + text: chunk_text, |
| 52 | + start_line: start + 1, // 1-based |
| 53 | + end_line: end, // 1-based inclusive |
| 54 | + }); |
| 55 | + |
| 56 | + if end >= lines.len() { |
| 57 | + break; |
| 58 | + } |
| 59 | + |
| 60 | + // Move start forward, keeping `overlap` tokens of context |
| 61 | + let mut overlap_tokens = 0; |
| 62 | + let mut new_start = end; |
| 63 | + while new_start > start && overlap_tokens < overlap { |
| 64 | + new_start -= 1; |
| 65 | + overlap_tokens += line_tokens[new_start]; |
| 66 | + } |
| 67 | + |
| 68 | + // Ensure progress |
| 69 | + if new_start <= start { |
| 70 | + new_start = start + 1; |
| 71 | + } |
| 72 | + start = new_start; |
| 73 | + } |
| 74 | + |
| 75 | + chunks |
| 76 | +} |
| 77 | + |
| 78 | +#[allow(clippy::unwrap_used, clippy::expect_used)] |
| 79 | +#[cfg(test)] |
| 80 | +mod tests { |
| 81 | + use super::*; |
| 82 | + |
| 83 | + #[test] |
| 84 | + fn test_empty() { |
| 85 | + assert!(chunk_markdown("", 400, 80).is_empty()); |
| 86 | + } |
| 87 | + |
| 88 | + #[test] |
| 89 | + fn test_single_small_chunk() { |
| 90 | + let text = "hello world\nfoo bar"; |
| 91 | + let chunks = chunk_markdown(text, 400, 80); |
| 92 | + assert_eq!(chunks.len(), 1); |
| 93 | + assert_eq!(chunks[0].start_line, 1); |
| 94 | + assert_eq!(chunks[0].end_line, 2); |
| 95 | + assert_eq!(chunks[0].text, text); |
| 96 | + } |
| 97 | + |
| 98 | + #[test] |
| 99 | + fn test_multiple_chunks_with_overlap() { |
| 100 | + // Create text with ~10 tokens per line, 5 lines = 50 tokens |
| 101 | + let lines: Vec<String> = (0..10) |
| 102 | + .map(|i| format!("line {} has several words in it here now ok", i)) |
| 103 | + .collect(); |
| 104 | + let text = lines.join("\n"); |
| 105 | + |
| 106 | + let chunks = chunk_markdown(&text, 20, 5); |
| 107 | + assert!(chunks.len() > 1); |
| 108 | + |
| 109 | + // Verify overlap: last lines of chunk N should overlap with first lines of chunk N+1 |
| 110 | + for i in 0..chunks.len() - 1 { |
| 111 | + assert!( |
| 112 | + chunks[i + 1].start_line <= chunks[i].end_line, |
| 113 | + "chunk {} end_line {} should overlap with chunk {} start_line {}", |
| 114 | + i, |
| 115 | + chunks[i].end_line, |
| 116 | + i + 1, |
| 117 | + chunks[i + 1].start_line |
| 118 | + ); |
| 119 | + } |
| 120 | + |
| 121 | + // Verify all lines are covered |
| 122 | + assert_eq!(chunks[0].start_line, 1); |
| 123 | + assert_eq!(chunks.last().unwrap().end_line, 10); |
| 124 | + } |
| 125 | + |
| 126 | + #[test] |
| 127 | + fn test_line_numbers_are_1_based() { |
| 128 | + let text = "a\nb\nc"; |
| 129 | + let chunks = chunk_markdown(text, 1, 0); |
| 130 | + assert_eq!(chunks[0].start_line, 1); |
| 131 | + assert_eq!(chunks[0].end_line, 1); |
| 132 | + } |
| 133 | + |
| 134 | + #[test] |
| 135 | + fn test_zero_chunk_size() { |
| 136 | + assert!(chunk_markdown("hello", 0, 0).is_empty()); |
| 137 | + } |
| 138 | +} |
0 commit comments