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
use crate::pipeline::Element;
/// Configuration for semantic chunking.
#[derive(Debug, Clone)]
pub struct SemanticChunkConfig {
/// Maximum tokens per chunk (approximate — uses word count as proxy).
pub max_tokens: usize,
/// Number of overlap tokens between consecutive chunks.
pub overlap_tokens: usize,
/// Whether to keep elements whole (don't split titles, tables, etc.).
pub respect_element_boundaries: bool,
}
impl Default for SemanticChunkConfig {
fn default() -> Self {
Self {
max_tokens: 512,
overlap_tokens: 50,
respect_element_boundaries: true,
}
}
}
impl SemanticChunkConfig {
/// Create config with specified max tokens.
pub fn new(max_tokens: usize) -> Self {
Self {
max_tokens,
..Default::default()
}
}
/// Set overlap tokens.
pub fn with_overlap(mut self, overlap: usize) -> Self {
self.overlap_tokens = overlap;
self
}
}
/// A semantic chunk: a group of elements with metadata.
#[derive(Debug, Clone)]
pub struct SemanticChunk {
elements: Vec<Element>,
oversized: bool,
}
impl SemanticChunk {
/// The elements in this chunk.
pub fn elements(&self) -> &[Element] {
&self.elements
}
/// Concatenated text of all elements.
pub fn text(&self) -> String {
self.elements
.iter()
.map(|e| e.display_text())
.collect::<Vec<_>>()
.join("\n")
}
/// Approximate token count (word count proxy).
pub fn token_estimate(&self) -> usize {
estimate_tokens(&self.text())
}
/// Page numbers spanned by this chunk.
pub fn page_numbers(&self) -> Vec<u32> {
let mut pages: Vec<u32> = self.elements.iter().map(|e| e.page()).collect();
pages.sort_unstable();
pages.dedup();
pages
}
/// Whether this chunk exceeds max_tokens (e.g., a large table).
pub fn is_oversized(&self) -> bool {
self.oversized
}
}
/// Semantic chunker that respects element boundaries.
pub struct SemanticChunker {
config: SemanticChunkConfig,
}
impl Default for SemanticChunker {
fn default() -> Self {
Self {
config: SemanticChunkConfig::default(),
}
}
}
impl SemanticChunker {
pub fn new(config: SemanticChunkConfig) -> Self {
Self { config }
}
/// Chunk a list of elements into semantic chunks.
pub fn chunk(&self, elements: &[Element]) -> Vec<SemanticChunk> {
if elements.is_empty() {
return Vec::new();
}
let mut chunks = Vec::new();
let mut current_elements: Vec<Element> = Vec::new();
let mut current_tokens = 0usize;
for element in elements {
let elem_tokens = element_token_count(element);
// Non-splittable elements (Table, Title, Header, Footer, Image)
if !is_splittable(element) {
// If adding this would overflow and we have content, flush first
if current_tokens > 0
&& current_tokens + elem_tokens > self.config.max_tokens
&& self.config.respect_element_boundaries
{
self.flush_chunk(
&mut chunks,
&mut current_elements,
&mut current_tokens,
false,
);
}
// If element alone exceeds max_tokens, it gets its own oversized chunk
if elem_tokens > self.config.max_tokens && current_elements.is_empty() {
chunks.push(SemanticChunk {
elements: vec![element.clone()],
oversized: true,
});
continue;
}
current_elements.push(element.clone());
current_tokens += elem_tokens;
continue;
}
// Splittable elements (Paragraph, ListItem, CodeBlock, KeyValue)
if current_tokens + elem_tokens <= self.config.max_tokens {
// Fits in current chunk
current_elements.push(element.clone());
current_tokens += elem_tokens;
} else if elem_tokens <= self.config.max_tokens {
// Doesn't fit but element itself is within limit — start new chunk
if !current_elements.is_empty() {
self.flush_chunk(
&mut chunks,
&mut current_elements,
&mut current_tokens,
false,
);
}
current_elements.push(element.clone());
current_tokens = elem_tokens;
} else {
// Element exceeds max_tokens — split by sentences
if !current_elements.is_empty() {
self.flush_chunk(
&mut chunks,
&mut current_elements,
&mut current_tokens,
false,
);
}
let sentences = split_sentences(element.text());
let meta = element.metadata().clone();
let mut sentence_buf = String::new();
let mut buf_tokens = 0;
for sentence in &sentences {
let s_tokens = estimate_tokens(sentence);
if buf_tokens + s_tokens > self.config.max_tokens && !sentence_buf.is_empty() {
chunks.push(make_paragraph_chunk(&sentence_buf, &meta));
sentence_buf.clear();
buf_tokens = 0;
}
if !sentence_buf.is_empty() {
sentence_buf.push(' ');
}
sentence_buf.push_str(sentence);
buf_tokens += s_tokens;
}
if !sentence_buf.is_empty() {
current_elements.push(Element::Paragraph(crate::pipeline::ElementData {
text: sentence_buf,
metadata: meta,
}));
current_tokens = buf_tokens;
}
}
}
// Flush remaining
if !current_elements.is_empty() {
chunks.push(SemanticChunk {
elements: current_elements,
oversized: false,
});
}
chunks
}
/// Flush current elements into a chunk and apply overlap if configured.
fn flush_chunk(
&self,
chunks: &mut Vec<SemanticChunk>,
current_elements: &mut Vec<Element>,
current_tokens: &mut usize,
oversized: bool,
) {
let flushed = std::mem::take(current_elements);
chunks.push(SemanticChunk {
elements: flushed.clone(),
oversized,
});
// Apply overlap: carry trailing elements from flushed chunk into the next
if self.config.overlap_tokens > 0 {
let mut overlap_tokens = 0usize;
let mut overlap_elements = Vec::new();
// Walk backwards through flushed elements to collect overlap
for elem in flushed.iter().rev() {
let t = element_token_count(elem);
if overlap_tokens + t > self.config.overlap_tokens && !overlap_elements.is_empty() {
break;
}
overlap_elements.push(elem.clone());
overlap_tokens += t;
}
overlap_elements.reverse();
*current_elements = overlap_elements;
*current_tokens = overlap_tokens;
} else {
*current_tokens = 0;
}
}
}
/// Whether an element can be split across chunks.
fn is_splittable(element: &Element) -> bool {
matches!(
element,
Element::Paragraph(_) | Element::ListItem(_) | Element::CodeBlock(_) | Element::KeyValue(_)
)
}
/// Approximate token count for an element.
fn element_token_count(element: &Element) -> usize {
estimate_tokens(&element.display_text())
}
/// Simple token estimator: word count (split by whitespace).
fn estimate_tokens(text: &str) -> usize {
text.split_whitespace().count()
}
/// Split text into sentences.
fn split_sentences(text: &str) -> Vec<String> {
let mut sentences = Vec::new();
let mut current = String::new();
for ch in text.chars() {
current.push(ch);
if (ch == '.' || ch == '!' || ch == '?') && !current.trim().is_empty() {
sentences.push(current.trim().to_string());
current.clear();
}
}
if !current.trim().is_empty() {
// Leftover without sentence terminator — append to last sentence or make new
if let Some(last) = sentences.last_mut() {
last.push(' ');
last.push_str(current.trim());
} else {
sentences.push(current.trim().to_string());
}
}
sentences
}
fn make_paragraph_chunk(text: &str, meta: &crate::pipeline::ElementMetadata) -> SemanticChunk {
SemanticChunk {
elements: vec![Element::Paragraph(crate::pipeline::ElementData {
text: text.to_string(),
metadata: meta.clone(),
})],
oversized: false,
}
}