kreuzberg 4.4.6

High-performance document intelligence library for Rust. Extract text, metadata, and structured data from PDFs, Office documents, images, and 88+ formats with async/sync APIs.
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
552
553
554
555
556
557
558
//! Paragraph building from lines using vertical gaps and formatting changes.

use super::constants::{
    FONT_SIZE_CHANGE_THRESHOLD, FULL_LINE_FRACTION, LEFT_INDENT_CHANGE_THRESHOLD, MAX_LIST_ITEM_LINES,
    PARAGRAPH_GAP_MULTIPLIER,
};
use super::types::{PdfLine, PdfParagraph};

/// Group lines into paragraphs based on vertical gaps, font size changes, and indentation.
///
/// Short-line detection: when a line doesn't extend to the right margin, it indicates
/// intentionally positioned text (CVs, addresses, data entries) rather than word-wrapped
/// flowing text. Each short line becomes its own paragraph to preserve the document's
/// visual structure in the markdown output.
pub(super) fn lines_to_paragraphs(lines: Vec<PdfLine>) -> Vec<PdfParagraph> {
    if lines.is_empty() {
        return Vec::new();
    }

    if lines.len() == 1 {
        return vec![finalize_paragraph(lines)];
    }

    // Compute baseline line spacing for paragraph break detection.
    let avg_font_size = lines.iter().map(|l| l.dominant_font_size).sum::<f32>() / lines.len() as f32;

    let mut spacings: Vec<f32> = Vec::new();
    for pair in lines.windows(2) {
        let gap = (pair[1].baseline_y - pair[0].baseline_y).abs();
        if gap > avg_font_size * 0.4 {
            spacings.push(gap);
        }
    }

    let base_spacing = if spacings.is_empty() {
        avg_font_size
    } else {
        spacings.sort_by(|a, b| a.total_cmp(b));
        // Use 25th percentile (Q1) for robustness against outlier-tight spacings
        // from superscripts/subscripts, while staying conservative enough to work
        // with small sample sizes (unlike median which can pick a gap spacing).
        spacings[spacings.len() / 4]
    };

    let paragraph_gap_threshold = base_spacing * PARAGRAPH_GAP_MULTIPLIER;

    // Compute max right edge for short-line detection.
    // In flowing text, lines extend to the right margin (word-wrapped).
    // In positioned text (CVs, addresses), lines end where the content ends.
    let max_right_edge = lines
        .iter()
        .filter_map(|l| l.segments.last().map(|s| s.x + s.width))
        .fold(0.0_f32, f32::max);

    // Page-level positioned text detection:
    // If fewer than 30% of lines reach the right margin, this page has
    // predominantly positioned text (CVs, addresses, data entries).
    // Only then do we split short lines into separate paragraphs.
    let is_positioned_text_page = if max_right_edge > 0.0 {
        let full_line_count = lines
            .iter()
            .filter(|l| {
                l.segments
                    .last()
                    .is_some_and(|s| s.x + s.width >= max_right_edge * FULL_LINE_FRACTION)
            })
            .count();
        full_line_count * 100 < lines.len() * 30 // < 30% full-width
    } else {
        false
    };

    let mut paragraphs: Vec<PdfParagraph> = Vec::new();
    let mut current_lines: Vec<PdfLine> = vec![lines[0].clone()];

    for line in lines.into_iter().skip(1) {
        let prev = current_lines.last().unwrap();

        let vertical_gap = (line.baseline_y - prev.baseline_y).abs();
        let font_size_change = (line.dominant_font_size - prev.dominant_font_size).abs();

        let prev_left = prev.segments.first().map(|s| s.x).unwrap_or(0.0);
        let curr_left = line.segments.first().map(|s| s.x).unwrap_or(0.0);
        let indent_change = (curr_left - prev_left).abs();

        let has_significant_gap = vertical_gap > paragraph_gap_threshold;
        let has_some_gap = vertical_gap > base_spacing * 0.8;
        let has_font_change = font_size_change > FONT_SIZE_CHANGE_THRESHOLD;
        let has_indent_change = indent_change > LEFT_INDENT_CHANGE_THRESHOLD;

        // Force paragraph break if next line starts with a list prefix
        let next_starts_with_list = line
            .segments
            .first()
            .and_then(|s| s.text.split_whitespace().next())
            .map(is_list_prefix)
            .unwrap_or(false);

        // Short-line detection for positioned text pages only.
        // When the page is predominantly short lines (< 30% full-width), each
        // short line that's followed by a gap indicates a separate entry.
        let prev_is_short_on_positioned_page = is_positioned_text_page && {
            let prev_right = prev.segments.last().map(|s| s.x + s.width).unwrap_or(0.0);
            prev_right < max_right_edge * FULL_LINE_FRACTION
        };

        let is_paragraph_break = has_significant_gap
            || (has_some_gap && (has_font_change || has_indent_change))
            || next_starts_with_list
            || (prev_is_short_on_positioned_page && has_some_gap);

        if is_paragraph_break {
            paragraphs.push(finalize_paragraph(current_lines));
            current_lines = vec![line];
        } else {
            current_lines.push(line);
        }
    }

    if !current_lines.is_empty() {
        paragraphs.push(finalize_paragraph(current_lines));
    }

    paragraphs
}

/// Build a PdfParagraph from a set of lines.
fn finalize_paragraph(lines: Vec<PdfLine>) -> PdfParagraph {
    let dominant_font_size = super::lines::most_frequent_font_size(lines.iter().map(|l| l.dominant_font_size));

    let bold_count = lines.iter().filter(|l| l.is_bold).count();
    let majority = lines.len().div_ceil(2);

    // Detect list items: first segment of first line starts with bullet or number prefix
    let first_text = lines
        .first()
        .and_then(|l| l.segments.first())
        .map(|s| s.text.as_str())
        .unwrap_or("");
    let first_word = first_text.split_whitespace().next().unwrap_or("");
    let is_list_item = lines.len() <= MAX_LIST_ITEM_LINES && is_list_prefix(first_word);

    // Detect code blocks: all lines must be monospace (and there must be at least one line)
    let is_code_block = !lines.is_empty() && lines.iter().all(|l| l.is_monospace);

    PdfParagraph {
        dominant_font_size,
        heading_level: None,
        is_bold: bold_count >= majority,
        is_list_item,
        is_code_block,
        lines,
    }
}

/// Merge consecutive body-text paragraphs that are continuations of the same logical paragraph.
///
/// Two consecutive paragraphs are merged if:
/// - Both are body text (no heading_level, not is_list_item)
/// - The first paragraph doesn't end with sentence-ending punctuation
/// - Font sizes are within 2pt of each other
pub(super) fn merge_continuation_paragraphs(paragraphs: &mut Vec<PdfParagraph>) {
    if paragraphs.len() < 2 {
        return;
    }

    let mut i = 0;
    while i + 1 < paragraphs.len() {
        let should_merge = {
            let current = &paragraphs[i];
            let next = &paragraphs[i + 1];

            // Both must be body text
            current.heading_level.is_none()
                && next.heading_level.is_none()
                && !current.is_list_item
                && !next.is_list_item
                // Font sizes close enough
                && (current.dominant_font_size - next.dominant_font_size).abs() < 2.0
                // Current paragraph doesn't end with sentence-ending punctuation
                && !ends_with_sentence_terminator(current)
        };

        if should_merge {
            let next = paragraphs.remove(i + 1);
            paragraphs[i].lines.extend(next.lines);
        } else {
            i += 1;
        }
    }
}

/// Check if a paragraph's last line ends with sentence-terminating punctuation.
fn ends_with_sentence_terminator(para: &PdfParagraph) -> bool {
    let last_text = para
        .lines
        .last()
        .and_then(|l| l.segments.last())
        .map(|s| s.text.trim_end())
        .unwrap_or("");
    matches!(last_text.chars().last(), Some('.' | '?' | '!' | ':' | ';'))
}

/// Check if text looks like a list item prefix.
fn is_list_prefix(text: &str) -> bool {
    let trimmed = text.trim();
    // Bullet characters: hyphen, asterisk, bullet, en dash, em dash
    if matches!(trimmed, "-" | "*" | "\u{2022}" | "\u{2013}" | "\u{2014}") {
        return true;
    }
    let bytes = trimmed.as_bytes();
    if bytes.is_empty() {
        return false;
    }
    // Numbered: 1. 2) etc.
    let digit_end = bytes.iter().position(|&b| !b.is_ascii_digit()).unwrap_or(bytes.len());
    if digit_end > 0 && digit_end < bytes.len() {
        let suffix = bytes[digit_end];
        if suffix == b'.' || suffix == b')' {
            return true;
        }
    }
    // Alphabetic: a. b) A. B) (single letter + period/paren)
    if bytes.len() == 2 && bytes[0].is_ascii_alphabetic() && (bytes[1] == b'.' || bytes[1] == b')') {
        return true;
    }
    // Roman numerals: i. ii. iii. iv. v. vi. I. II. III. IV. V. VI. etc.
    if trimmed.ends_with('.') || trimmed.ends_with(')') {
        let prefix = &trimmed[..trimmed.len() - 1];
        if is_roman_numeral(prefix) {
            return true;
        }
    }
    false
}

/// Check if text is a roman numeral (i-xii or I-XII range).
fn is_roman_numeral(s: &str) -> bool {
    if s.is_empty() {
        return false;
    }
    let lower = s.to_ascii_lowercase();
    matches!(
        lower.as_str(),
        "i" | "ii" | "iii" | "iv" | "v" | "vi" | "vii" | "viii" | "ix" | "x" | "xi" | "xii"
    )
}

#[cfg(test)]
mod tests {
    use crate::pdf::hierarchy::SegmentData;

    use super::*;

    fn plain_segment(text: &str, x: f32, baseline_y: f32, width: f32, font_size: f32) -> SegmentData {
        SegmentData {
            text: text.to_string(),
            x,
            y: baseline_y,
            width,
            height: font_size,
            font_size,
            is_bold: false,
            is_italic: false,
            is_monospace: false,
            baseline_y,
        }
    }

    fn make_line(segments: Vec<SegmentData>, baseline_y: f32, font_size: f32) -> PdfLine {
        PdfLine {
            segments,
            baseline_y,
            dominant_font_size: font_size,
            is_bold: false,
            is_monospace: false,
        }
    }

    #[test]
    fn test_lines_to_paragraphs_single_line() {
        let lines = vec![make_line(
            vec![plain_segment("Hello world", 10.0, 700.0, 80.0, 12.0)],
            700.0,
            12.0,
        )];
        let paragraphs = lines_to_paragraphs(lines);
        assert_eq!(paragraphs.len(), 1);
    }

    #[test]
    fn test_lines_to_paragraphs_gap_detection() {
        // Full-width lines (490pt wide from x=10) followed by a big gap
        let lines = vec![
            make_line(
                vec![plain_segment("Para 1 line one", 10.0, 700.0, 490.0, 12.0)],
                700.0,
                12.0,
            ),
            make_line(
                vec![plain_segment("Still para 1", 10.0, 686.0, 490.0, 12.0)],
                686.0,
                12.0,
            ),
            // Big gap
            make_line(vec![plain_segment("Para 2", 10.0, 640.0, 490.0, 12.0)], 640.0, 12.0),
        ];
        let paragraphs = lines_to_paragraphs(lines);
        assert_eq!(paragraphs.len(), 2);
    }

    #[test]
    fn test_lines_to_paragraphs_empty() {
        let paragraphs = lines_to_paragraphs(vec![]);
        assert!(paragraphs.is_empty());
    }

    #[test]
    fn test_list_item_detection() {
        let lines = vec![make_line(
            vec![plain_segment("- Item text", 10.0, 700.0, 80.0, 12.0)],
            700.0,
            12.0,
        )];
        let paragraphs = lines_to_paragraphs(lines);
        assert_eq!(paragraphs.len(), 1);
        assert!(paragraphs[0].is_list_item);
    }

    #[test]
    fn test_numbered_list_detection() {
        let lines = vec![make_line(
            vec![plain_segment("1. First item", 10.0, 700.0, 80.0, 12.0)],
            700.0,
            12.0,
        )];
        let paragraphs = lines_to_paragraphs(lines);
        assert!(paragraphs[0].is_list_item);
    }

    #[test]
    fn test_not_list_item() {
        let lines = vec![make_line(
            vec![plain_segment("Normal text", 10.0, 700.0, 80.0, 12.0)],
            700.0,
            12.0,
        )];
        let paragraphs = lines_to_paragraphs(lines);
        assert!(!paragraphs[0].is_list_item);
    }

    // ── Short-line paragraph splitting tests (issue #431) ──

    #[test]
    fn test_short_lines_split_into_separate_paragraphs() {
        // Simulates a CV/address block: short lines with uniform spacing.
        // Each line should become its own paragraph.
        // Max right edge = 500 (from the widest line).
        let lines = vec![
            make_line(
                vec![plain_segment("Max Mustermann", 50.0, 700.0, 120.0, 12.0)],
                700.0,
                12.0,
            ),
            make_line(
                vec![plain_segment("Musterstraße 1", 50.0, 686.0, 110.0, 12.0)],
                686.0,
                12.0,
            ),
            make_line(
                vec![plain_segment("12345 Musterstadt", 50.0, 672.0, 130.0, 12.0)],
                672.0,
                12.0,
            ),
            // Add a full-width line to establish right margin (like page footer)
            make_line(
                vec![plain_segment("Full width reference line", 50.0, 600.0, 450.0, 12.0)],
                600.0,
                12.0,
            ),
        ];
        let paragraphs = lines_to_paragraphs(lines);
        // The 3 short CV lines should each be separate paragraphs,
        // plus the full-width line (4 total)
        assert_eq!(paragraphs.len(), 4);
        assert_eq!(paragraphs[0].lines[0].segments[0].text, "Max Mustermann");
        assert_eq!(paragraphs[1].lines[0].segments[0].text, "Musterstraße 1");
        assert_eq!(paragraphs[2].lines[0].segments[0].text, "12345 Musterstadt");
    }

    #[test]
    fn test_full_width_lines_grouped_as_paragraph() {
        // Flowing text: full-width lines should be grouped together.
        // Max right edge = 500 (x=10 + width=490).
        let lines = vec![
            make_line(
                vec![plain_segment(
                    "The quick brown fox jumps over",
                    10.0,
                    700.0,
                    490.0,
                    12.0,
                )],
                700.0,
                12.0,
            ),
            make_line(
                vec![plain_segment("the lazy dog and continues on", 10.0, 686.0, 490.0, 12.0)],
                686.0,
                12.0,
            ),
            make_line(
                vec![plain_segment("to the end.", 10.0, 672.0, 100.0, 12.0)],
                672.0,
                12.0,
            ),
        ];
        let paragraphs = lines_to_paragraphs(lines);
        // Full-width lines are flowing text → should be one paragraph
        assert_eq!(paragraphs.len(), 1);
        assert_eq!(paragraphs[0].lines.len(), 3);
    }

    #[test]
    fn test_flowing_text_with_short_last_line_followed_by_new_paragraph() {
        // Paragraph 1: two full lines + short last line
        // Paragraph 2: starts after a big gap
        let lines = vec![
            make_line(
                vec![plain_segment(
                    "Flowing text line one that extends",
                    10.0,
                    700.0,
                    490.0,
                    12.0,
                )],
                700.0,
                12.0,
            ),
            make_line(
                vec![plain_segment(
                    "to the edge of the margin here",
                    10.0,
                    686.0,
                    490.0,
                    12.0,
                )],
                686.0,
                12.0,
            ),
            make_line(
                vec![plain_segment("short ending.", 10.0, 672.0, 100.0, 12.0)],
                672.0,
                12.0,
            ),
            // Big gap → new paragraph
            make_line(
                vec![plain_segment("New paragraph starts here", 10.0, 630.0, 490.0, 12.0)],
                630.0,
                12.0,
            ),
        ];
        let paragraphs = lines_to_paragraphs(lines);
        assert_eq!(paragraphs.len(), 2);
        assert_eq!(paragraphs[0].lines.len(), 3); // full + full + short ending
        assert_eq!(paragraphs[1].lines.len(), 1);
    }

    #[test]
    fn test_no_positional_data_no_short_line_detection() {
        // Structure tree path: all segments have x=0, width=0.
        // Short-line detection should not apply (no positional data).
        let lines = vec![
            make_line(vec![plain_segment("Line one", 0.0, 0.0, 0.0, 12.0)], 0.0, 12.0),
            make_line(vec![plain_segment("Line two", 0.0, 0.0, 0.0, 12.0)], 0.0, 12.0),
        ];
        let paragraphs = lines_to_paragraphs(lines);
        // Without positional data, lines are grouped into one paragraph
        assert_eq!(paragraphs.len(), 1);
    }

    #[test]
    fn test_merge_continuation_respects_sentence_terminators() {
        // Two paragraphs where the first ends with a period.
        // Should NOT be merged because the first ends with sentence-terminating punctuation.
        let mut paragraphs = vec![
            PdfParagraph {
                lines: vec![make_line(
                    vec![plain_segment("First paragraph ends here.", 10.0, 700.0, 490.0, 12.0)],
                    700.0,
                    12.0,
                )],
                dominant_font_size: 12.0,
                heading_level: None,
                is_bold: false,
                is_list_item: false,
                is_code_block: false,
            },
            PdfParagraph {
                lines: vec![make_line(
                    vec![plain_segment("Second paragraph starts", 10.0, 686.0, 490.0, 12.0)],
                    686.0,
                    12.0,
                )],
                dominant_font_size: 12.0,
                heading_level: None,
                is_bold: false,
                is_list_item: false,
                is_code_block: false,
            },
        ];
        merge_continuation_paragraphs(&mut paragraphs);
        // Should NOT merge: first paragraph ends with "."
        assert_eq!(paragraphs.len(), 2);
    }

    #[test]
    fn test_merge_continuation_merges_full_width_paragraphs() {
        // Two paragraphs where the first ends with a full-width line and no sentence terminator.
        // These should be merged (flowing text continuation).
        let mut paragraphs = vec![
            PdfParagraph {
                lines: vec![make_line(
                    vec![plain_segment(
                        "The quick brown fox jumps over the",
                        10.0,
                        700.0,
                        490.0,
                        12.0,
                    )],
                    700.0,
                    12.0,
                )],
                dominant_font_size: 12.0,
                heading_level: None,
                is_bold: false,
                is_list_item: false,
                is_code_block: false,
            },
            PdfParagraph {
                lines: vec![make_line(
                    vec![plain_segment("lazy dog and more text", 10.0, 686.0, 490.0, 12.0)],
                    686.0,
                    12.0,
                )],
                dominant_font_size: 12.0,
                heading_level: None,
                is_bold: false,
                is_list_item: false,
                is_code_block: false,
            },
        ];
        merge_continuation_paragraphs(&mut paragraphs);
        // Full-width lines without sentence terminator → should be merged
        assert_eq!(paragraphs.len(), 1);
        assert_eq!(paragraphs[0].lines.len(), 2);
    }
}