rs-chunks 0.1.0

Fast, high-fidelity document chunking for RAG — a pure-Rust engine covering 36 file formats (Office, OpenDocument, PDF, email, ebooks, notebooks, and more).
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
/// Semantic chunker for Markdown.
///
/// Groups typed blocks (paragraphs, lists) by topic continuity using ten
/// distinct signals, in strict priority order.  Code blocks and tables are
/// always emitted as standalone chunks because they are structurally atomic.
/// Headings are always standalone and reset the heading-path context.
///
/// Each output chunk carries rich metadata: full heading breadcrumb, every
/// distinct merge reason that fired, the dominant reason, block-type
/// inventory, keyword density, and per-chunk position.
use serde_json::json;
use std::collections::{HashMap, HashSet};

use crate::shared::{
    ci_starts_with, CAUSE_EFFECT_STARTS, CONTRAST_CONTINUATION, ELABORATION_STARTS, EXAMPLE_STARTS,
    MAX_SEMANTIC_CHARS, REFERENCE_STARTS, TRANSITION_BREAKS,
};
use super::common::{
    current_section_heading, current_section_level, extract_heading_text, has_keyword_overlap,
    heading_level, heading_path_strings, parse_markdown_blocks, strip_block_content,
    tokenize_keywords, update_heading_stack, ChunkRecordInput, ContentType, MdBlockType,
};

// ── Signal word tables ────────────────────────────────────────────────────────

// Signal word tables — imported from crate::shared (single source of truth).
// See shared.rs for the full definitions and comments on each signal.

// ── Internal accumulator ──────────────────────────────────────────────────────

struct SemanticPart {
    content: String,
    block_type: MdBlockType,
    merge_reason: &'static str,
}

struct SemanticAccum {
    parts: Vec<SemanticPart>,
    section_heading: Option<String>,
    heading_path: Vec<String>,
    section_level: u8,
    /// Accumulated significant keywords across all merged blocks.
    keywords: HashSet<String>,
    /// Total character count of joined content.
    char_count: usize,
    /// Whether the last emitted paragraph ends with a question mark.
    ends_with_question: bool,
    /// Whether the last emitted paragraph is a short definition-like line
    /// (≤ 80 chars, ends with ':').  Signals the next block may expand it.
    ends_with_definition_label: bool,
}

impl SemanticAccum {
    fn new(
        first_content: String,
        first_type: MdBlockType,
        section_heading: Option<String>,
        heading_path: Vec<String>,
        section_level: u8,
        keywords: HashSet<String>,
    ) -> Self {
        let char_count = first_content.len();
        let ends_with_question = first_content.trim_end().ends_with('?');
        let ends_with_definition_label =
            first_content.len() <= 80 && first_content.trim_end().ends_with(':');
        SemanticAccum {
            parts: vec![SemanticPart {
                content: first_content,
                block_type: first_type,
                merge_reason: "initial",
            }],
            section_heading,
            heading_path,
            section_level,
            keywords,
            char_count,
            ends_with_question,
            ends_with_definition_label,
        }
    }

    fn append(&mut self, content: String, block_type: MdBlockType, reason: &'static str) {
        self.char_count += content.len() + 2;
        self.ends_with_question = content.trim_end().ends_with('?');
        self.ends_with_definition_label = content.len() <= 80 && content.trim_end().ends_with(':');
        self.keywords.extend(tokenize_keywords(&content));
        self.parts.push(SemanticPart {
            content,
            block_type,
            merge_reason: reason,
        });
    }

    fn joined_content(&self) -> String {
        self.parts
            .iter()
            .map(|p| p.content.as_str())
            .collect::<Vec<_>>()
            .join("\n\n")
    }
}

// ── Merge decision ────────────────────────────────────────────────────────────

/// Returns `Some(reason)` if `clean` should be merged into `accum`, or `None`
/// if it must start a new chunk.
///
/// Signals are evaluated in strict priority order:
///   1. transition_break      — hard stop, always new chunk
///   2. reference_continuity  — pronoun/demonstrative continuing same subject
///   3. elaboration           — "furthermore", "additionally", etc.
///   4. example               — "for example", "e.g.", etc.
///   5. cause_effect          — "therefore", "as a result", etc.
///   6. contrast_continuation — "although", "despite", etc. (same topic)
///   7. question_answer       — previous paragraph was a question
///   8. definition_expansion  — previous was a short label ending with ':'
///   9. short_paragraph       — absorb prose ≤ 80 chars
///  10. list_continuation     — list shares keywords with preceding prose
///  11. keyword_overlap       — at least one shared significant keyword
fn decide_merge(
    clean: &str,
    block_type: MdBlockType,
    accum: &SemanticAccum,
    max_chars: usize,
) -> Option<&'static str> {
    // Hard size limit — never exceed regardless of signal.
    if accum.char_count + clean.len() + 2 > max_chars {
        return None;
    }

    let t = clean.trim_start();

    // 1. Transition break: topic has genuinely shifted.
    if TRANSITION_BREAKS.iter().any(|s| ci_starts_with(t, s)) {
        return None;
    }

    // 2. Reference continuity.
    if REFERENCE_STARTS.iter().any(|s| ci_starts_with(t, s)) {
        return Some("reference_continuity");
    }

    // 3. Elaboration.
    if ELABORATION_STARTS.iter().any(|s| ci_starts_with(t, s)) {
        return Some("elaboration");
    }

    // 4. Example.
    if EXAMPLE_STARTS.iter().any(|s| ci_starts_with(t, s)) {
        return Some("example");
    }

    // 5. Cause / effect.
    if CAUSE_EFFECT_STARTS.iter().any(|s| ci_starts_with(t, s)) {
        return Some("cause_effect");
    }

    // 6. In-clause contrast (same topic, opposite framing).
    if CONTRAST_CONTINUATION.iter().any(|s| ci_starts_with(t, s)) {
        return Some("contrast_continuation");
    }

    // 7. Question → answer pair.
    if accum.ends_with_question {
        return Some("question_answer");
    }

    // 8. Definition label expansion: prev was "Term:" and this explains it.
    if accum.ends_with_definition_label && clean.len() > 60 {
        return Some("definition_expansion");
    }

    // 9. Short paragraph — absorb rather than emit a tiny standalone chunk.
    if clean.len() <= 80 {
        return Some("short_paragraph");
    }

    // 10+11. Keyword-based signals — compute keywords once, reuse for both checks.
    let bkw = tokenize_keywords(clean);
    if matches!(block_type, MdBlockType::List) {
        if has_keyword_overlap(&accum.keywords, &bkw) {
            return Some("list_continuation");
        }
    }

    // 11. Keyword overlap — paragraph shares at least one significant term.
    if has_keyword_overlap(&accum.keywords, &bkw) {
        return Some("keyword_overlap");
    }

    None
}

// ── Finalise an accumulator into a ChunkRecordInput ───────────────────────────

fn finalize(
    accum: SemanticAccum,
    chunk_index: usize,
    total_input_blocks: usize,
) -> ChunkRecordInput {
    let content = accum.joined_content();

    // Unique block types present in this chunk.
    let mut block_types: Vec<&'static str> = Vec::new();
    for part in &accum.parts {
        let t = match part.block_type {
            MdBlockType::Paragraph => "paragraph",
            MdBlockType::List => "list",
            MdBlockType::Code => "code_block",
            MdBlockType::Table => "table",
            MdBlockType::Heading => "heading",
        };
        if !block_types.contains(&t) {
            block_types.push(t);
        }
    }

    // All distinct merge reasons (excluding "initial").
    let mut merge_reasons: Vec<&'static str> = Vec::new();
    for part in &accum.parts {
        if part.merge_reason != "initial" && !merge_reasons.contains(&part.merge_reason) {
            merge_reasons.push(part.merge_reason);
        }
    }

    // Primary reason = the one that fired most often.
    let primary_merge_reason: &'static str = if accum.parts.len() <= 1 {
        "initial"
    } else {
        let mut counts: HashMap<&'static str, usize> = HashMap::new();
        for part in &accum.parts {
            if part.merge_reason != "initial" {
                *counts.entry(part.merge_reason).or_default() += 1;
            }
        }
        // Sort by (count desc, key asc) for determinism when counts are tied.
        let mut reason_vec: Vec<(&'static str, usize)> = counts.into_iter().collect();
        reason_vec.sort_by(|a, b| b.1.cmp(&a.1).then(a.0.cmp(&b.0)));
        reason_vec.first().map(|(r, _)| *r).unwrap_or("keyword_overlap")
    };

    let has_list = accum
        .parts
        .iter()
        .any(|p| matches!(p.block_type, MdBlockType::List));

    let paragraph_count = accum
        .parts
        .iter()
        .filter(|p| matches!(p.block_type, MdBlockType::Paragraph | MdBlockType::List))
        .count();

    // Average block length (chars) before joining.
    let avg_block_length = if accum.parts.is_empty() {
        0
    } else {
        accum.parts.iter().map(|p| p.content.len()).sum::<usize>() / accum.parts.len()
    };

    // Keyword density = unique significant keywords / total word count.
    let total_words = content.split_whitespace().count().max(1);
    let keyword_density =
        (accum.keywords.len() as f64 / total_words as f64 * 1000.0).round() / 1000.0;

    let metadata = json!({
        "section_heading":         accum.section_heading,
        "heading_path":            accum.heading_path,
        "section_level":           accum.section_level,
        "paragraph_count":         paragraph_count,
        "block_types":             block_types,
        "merge_reasons":           merge_reasons,
        "primary_merge_reason":    primary_merge_reason,
        "has_list":                has_list,
        "keyword_density":         keyword_density,
        "avg_block_length":        avg_block_length,
        "chunk_index":             chunk_index,
        "document_metadata": {
            "source_type":         "md",
            "total_input_blocks":  total_input_blocks,
        }
    });

    ChunkRecordInput {
        content_type: ContentType::Semantic,
        content,
        metadata,
    }
}

// ── Core algorithm ────────────────────────────────────────────────────────────

pub fn build_semantic_chunks(bytes: &[u8]) -> Result<Vec<ChunkRecordInput>, String> {
    let text = std::str::from_utf8(bytes)
        .map(|v| v.to_string())
        .unwrap_or_else(|_| String::from_utf8_lossy(bytes).to_string());

    if text.trim().is_empty() {
        return Err("Markdown file is empty after decoding".to_string());
    }

    let blocks = parse_markdown_blocks(&text);
    let total_input_blocks = blocks.len();

    let mut result: Vec<ChunkRecordInput> = Vec::new();
    let mut heading_stack: Vec<(u8, String)> = Vec::new();
    let mut accum: Option<SemanticAccum> = None;
    let mut chunk_index = 0usize;

    for block in blocks {
        match block.block_type {
            // ── Headings: flush current group, emit heading standalone ────────
            MdBlockType::Heading => {
                if let Some(a) = accum.take() {
                    result.push(finalize(a, chunk_index, total_input_blocks));
                    chunk_index += 1;
                }

                let level = heading_level(&block.content);
                let text = extract_heading_text(&block.content);
                update_heading_stack(&mut heading_stack, level, text.clone());

                // Emit the heading itself as a typed heading chunk.
                result.push(ChunkRecordInput {
                    content_type: ContentType::HeadingSection,
                    content: text.clone(),
                    metadata: json!({
                        "section_heading":      current_section_heading(&heading_stack[..heading_stack.len()-1]),
                        "heading_path":         heading_path_strings(&heading_stack),
                        "section_level":        level,
                        "paragraph_count":      0,
                        "block_types":          ["heading"],
                        "merge_reasons":        [],
                        "primary_merge_reason": "initial",
                        "has_list":             false,
                        "keyword_density":      0.0,
                        "avg_block_length":     text.len(),
                        "chunk_index":          chunk_index,
                        "document_metadata": {
                            "source_type":        "md",
                            "total_input_blocks": total_input_blocks,
                        }
                    }),
                });
                chunk_index += 1;
            }

            // ── Code blocks: always standalone ────────────────────────────────
            MdBlockType::Code => {
                if let Some(a) = accum.take() {
                    result.push(finalize(a, chunk_index, total_input_blocks));
                    chunk_index += 1;
                }
                let content = block.content.clone();
                result.push(ChunkRecordInput {
                    content_type: ContentType::CodeBlock,
                    content: content.clone(),
                    metadata: json!({
                        "section_heading":      current_section_heading(&heading_stack),
                        "heading_path":         heading_path_strings(&heading_stack),
                        "section_level":        current_section_level(&heading_stack),
                        "paragraph_count":      0,
                        "block_types":          ["code_block"],
                        "merge_reasons":        [],
                        "primary_merge_reason": "structural_boundary",
                        "has_list":             false,
                        "keyword_density":      0.0,
                        "avg_block_length":     content.len(),
                        "chunk_index":          chunk_index,
                        "document_metadata": {
                            "source_type":        "md",
                            "total_input_blocks": total_input_blocks,
                        }
                    }),
                });
                chunk_index += 1;
            }

            // ── Tables: always standalone ─────────────────────────────────────
            MdBlockType::Table => {
                if let Some(a) = accum.take() {
                    result.push(finalize(a, chunk_index, total_input_blocks));
                    chunk_index += 1;
                }
                let content = block.content.clone();
                result.push(ChunkRecordInput {
                    content_type: ContentType::Table,
                    content: content.clone(),
                    metadata: json!({
                        "section_heading":      current_section_heading(&heading_stack),
                        "heading_path":         heading_path_strings(&heading_stack),
                        "section_level":        current_section_level(&heading_stack),
                        "paragraph_count":      0,
                        "block_types":          ["table"],
                        "merge_reasons":        [],
                        "primary_merge_reason": "structural_boundary",
                        "has_list":             false,
                        "keyword_density":      0.0,
                        "avg_block_length":     content.len(),
                        "chunk_index":          chunk_index,
                        "document_metadata": {
                            "source_type":        "md",
                            "total_input_blocks": total_input_blocks,
                        }
                    }),
                });
                chunk_index += 1;
            }

            // ── Paragraphs and lists: apply full signal pipeline ───────────────
            MdBlockType::Paragraph | MdBlockType::List => {
                let clean = strip_block_content(
                    &block.content,
                    matches!(block.block_type, MdBlockType::List),
                );
                if clean.is_empty() {
                    continue;
                }

                match accum.as_mut() {
                    None => {
                        // Start the first accumulator for this section.
                        accum = Some(SemanticAccum::new(
                            clean.clone(),
                            block.block_type,
                            current_section_heading(&heading_stack),
                            heading_path_strings(&heading_stack),
                            current_section_level(&heading_stack),
                            tokenize_keywords(&clean),
                        ));
                    }
                    Some(a) => {
                        match decide_merge(&clean, block.block_type, a, MAX_SEMANTIC_CHARS) {
                            Some(reason) => {
                                a.append(clean, block.block_type, reason);
                            }
                            None => {
                                // Flush current, start fresh.
                                let finished = accum.take().unwrap();
                                result.push(finalize(finished, chunk_index, total_input_blocks));
                                chunk_index += 1;
                                accum = Some(SemanticAccum::new(
                                    clean.clone(),
                                    block.block_type,
                                    current_section_heading(&heading_stack),
                                    heading_path_strings(&heading_stack),
                                    current_section_level(&heading_stack),
                                    tokenize_keywords(&clean),
                                ));
                            }
                        }
                    }
                }
            }
        }
    }

    // Flush whatever remains.
    if let Some(a) = accum.take() {
        result.push(finalize(a, chunk_index, total_input_blocks));
    }

    if result.is_empty() {
        return Err("No chunks generated from Markdown document".to_string());
    }
    Ok(result)
}

// ── PyO3 entry point ──────────────────────────────────────────────────────────