oar-ocr-core 0.6.3

Core types and predictors for oar-ocr
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
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
//! Table OCR box splitting utilities.
//!
//! This module provides functionality to split OCR boxes that span multiple table cells,
//! which improves table recognition accuracy for complex tables with rowspan/colspan.
//!
//! ## Problem
//!
//! When processing complex tables, OCR text boxes sometimes span multiple cells. Without
//! splitting, the entire text is assigned to a single cell, causing incorrect table structure.
//!
//! ## Solution
//!
//! This module detects cross-cell OCR boxes and splits them at cell boundaries:
//! 1. Detect OCR boxes that overlap with multiple cells (using IoA)
//! 2. Determine split boundaries based on cell edges
//! 3. Split the bounding box and distribute text proportionally
//!
//! ## Reference
//!
//! This implementation is inspired by PaddleX's `split_ocr_bboxes_by_table_cells()`.

use crate::domain::structure::TableCell;
use crate::domain::text_region::TextRegion;
use crate::processors::BoundingBox;
use std::sync::Arc;

/// Configuration for OCR box splitting.
#[derive(Debug, Clone)]
pub struct SplitConfig {
    /// Minimum overlap ratio (IoA) for considering a cell as affected.
    /// An OCR box is considered to overlap a cell if IoA > this threshold.
    pub min_overlap_ratio: f32,

    /// Minimum number of cells an OCR box must span to be considered for splitting.
    /// Boxes spanning fewer cells are not split.
    pub min_cells_to_split: usize,

    /// Whether to split horizontally (along x-axis) when OCR box spans columns.
    pub split_horizontal: bool,

    /// Whether to split vertically (along y-axis) when OCR box spans rows.
    pub split_vertical: bool,
}

impl Default for SplitConfig {
    fn default() -> Self {
        Self {
            min_overlap_ratio: 0.05, // Low threshold to catch partial overlaps
            min_cells_to_split: 2,
            split_horizontal: true,
            split_vertical: true,
        }
    }
}

/// Detection result for a cross-cell OCR box.
#[derive(Debug, Clone)]
pub struct CrossCellDetection {
    /// Index of the OCR box in the original array.
    pub ocr_index: usize,

    /// Indices of cells that this OCR box overlaps with.
    pub affected_cell_indices: Vec<usize>,

    /// Split boundaries - x-coordinates for horizontal splits.
    pub x_boundaries: Vec<f32>,

    /// Split boundaries - y-coordinates for vertical splits.
    pub y_boundaries: Vec<f32>,

    /// Split direction: true for horizontal (column-wise), false for vertical (row-wise).
    pub is_horizontal_split: bool,
}

/// A segment resulting from splitting an OCR box.
#[derive(Debug, Clone)]
pub struct SplitSegment {
    /// The bounding box of this segment.
    pub bbox: BoundingBox,

    /// The text content assigned to this segment.
    pub text: String,

    /// Index of the cell this segment belongs to.
    pub cell_index: usize,
}

/// Result of splitting a single OCR box.
#[derive(Debug, Clone)]
pub struct SplitOcrResult {
    /// Original OCR box bounding box.
    pub original_bbox: BoundingBox,

    /// Original text content.
    pub original_text: String,

    /// Confidence score from original OCR.
    pub confidence: Option<f32>,

    /// Resulting segments after splitting.
    pub segments: Vec<SplitSegment>,
}

/// Detects OCR boxes that span multiple table cells.
///
/// This function analyzes each OCR box to determine if it overlaps with multiple cells
/// based on the IoA (Intersection over Area of OCR box) metric.
///
/// # Arguments
///
/// * `text_regions` - Slice of text regions from OCR.
/// * `cells` - Slice of table cells.
/// * `config` - Configuration for the detection.
///
/// # Returns
///
/// Vector of `CrossCellDetection` results for OCR boxes that span multiple cells.
pub fn detect_cross_cell_ocr_boxes(
    text_regions: &[TextRegion],
    cells: &[TableCell],
    config: &SplitConfig,
) -> Vec<CrossCellDetection> {
    let mut detections = Vec::new();

    if cells.is_empty() || text_regions.is_empty() {
        return detections;
    }

    for (ocr_idx, region) in text_regions.iter().enumerate() {
        // Skip regions without text
        if region.text.is_none() {
            continue;
        }

        let ocr_bbox = &region.bounding_box;
        let ocr_area = calculate_bbox_area(ocr_bbox);

        if ocr_area <= 0.0 {
            continue;
        }

        // Find all cells that this OCR box overlaps with
        let mut overlapping_cells: Vec<(usize, f32)> = Vec::new();

        for (cell_idx, cell) in cells.iter().enumerate() {
            let inter_area = ocr_bbox.intersection_area(&cell.bbox);
            let ioa = inter_area / ocr_area; // IoA = intersection / OCR area

            if ioa > config.min_overlap_ratio {
                overlapping_cells.push((cell_idx, ioa));
            }
        }

        // Only consider splitting if OCR box spans multiple cells
        if overlapping_cells.len() >= config.min_cells_to_split {
            // Sort by cell index to maintain consistent ordering
            overlapping_cells.sort_by_key(|(idx, _)| *idx);

            let affected_cell_indices: Vec<usize> =
                overlapping_cells.iter().map(|(idx, _)| *idx).collect();

            // Determine split direction and boundaries
            let (x_boundaries, y_boundaries, is_horizontal) =
                compute_split_boundaries(ocr_bbox, &affected_cell_indices, cells, config);

            // Only add detection if we have valid split boundaries
            if !x_boundaries.is_empty() || !y_boundaries.is_empty() {
                detections.push(CrossCellDetection {
                    ocr_index: ocr_idx,
                    affected_cell_indices,
                    x_boundaries,
                    y_boundaries,
                    is_horizontal_split: is_horizontal,
                });
            }
        }
    }

    detections
}

/// Computes split boundaries based on cell edges.
///
/// Returns (x_boundaries, y_boundaries, is_horizontal_split).
fn compute_split_boundaries(
    ocr_bbox: &BoundingBox,
    cell_indices: &[usize],
    cells: &[TableCell],
    config: &SplitConfig,
) -> (Vec<f32>, Vec<f32>, bool) {
    if cell_indices.is_empty() {
        return (Vec::new(), Vec::new(), true);
    }

    // Collect all cell boundaries within the OCR box range
    let mut x_edges: Vec<f32> = Vec::new();
    let mut y_edges: Vec<f32> = Vec::new();

    let ocr_x_min = ocr_bbox.x_min();
    let ocr_x_max = ocr_bbox.x_max();
    let ocr_y_min = ocr_bbox.y_min();
    let ocr_y_max = ocr_bbox.y_max();

    for &cell_idx in cell_indices {
        let cell = &cells[cell_idx];

        // Collect vertical boundaries (x edges) for horizontal splitting
        if config.split_horizontal {
            let cell_x_min = cell.bbox.x_min();
            let cell_x_max = cell.bbox.x_max();

            // Add left edge if it's inside the OCR box
            if cell_x_min > ocr_x_min && cell_x_min < ocr_x_max {
                x_edges.push(cell_x_min);
            }
            // Add right edge if it's inside the OCR box
            if cell_x_max > ocr_x_min && cell_x_max < ocr_x_max {
                x_edges.push(cell_x_max);
            }
        }

        // Collect horizontal boundaries (y edges) for vertical splitting
        if config.split_vertical {
            let cell_y_min = cell.bbox.y_min();
            let cell_y_max = cell.bbox.y_max();

            // Add top edge if it's inside the OCR box
            if cell_y_min > ocr_y_min && cell_y_min < ocr_y_max {
                y_edges.push(cell_y_min);
            }
            // Add bottom edge if it's inside the OCR box
            if cell_y_max > ocr_y_min && cell_y_max < ocr_y_max {
                y_edges.push(cell_y_max);
            }
        }
    }

    // Remove duplicates and sort
    x_edges.sort_by(|a, b| a.partial_cmp(b).unwrap_or(std::cmp::Ordering::Equal));
    x_edges.dedup_by(|a, b| (*a - *b).abs() < 1.0); // Remove edges within 1 pixel

    y_edges.sort_by(|a, b| a.partial_cmp(b).unwrap_or(std::cmp::Ordering::Equal));
    y_edges.dedup_by(|a, b| (*a - *b).abs() < 1.0);

    // Determine primary split direction based on which has more boundaries
    // or based on the OCR box aspect ratio
    let ocr_width = ocr_x_max - ocr_x_min;
    let ocr_height = ocr_y_max - ocr_y_min;

    let is_horizontal = if !x_edges.is_empty() && !y_edges.is_empty() {
        // Both directions have boundaries - prefer horizontal for wide boxes
        ocr_width >= ocr_height
    } else {
        !x_edges.is_empty()
    };

    // Return only the relevant boundaries based on split direction
    if is_horizontal {
        (x_edges, Vec::new(), true)
    } else {
        (Vec::new(), y_edges, false)
    }
}

/// Splits an OCR box at cell boundaries and distributes text proportionally.
///
/// # Arguments
///
/// * `region` - The text region to split.
/// * `detection` - The cross-cell detection result.
/// * `cells` - Slice of table cells.
///
/// # Returns
///
/// `SplitOcrResult` containing the original box info and resulting segments.
pub fn split_ocr_box_at_cell_boundaries(
    region: &TextRegion,
    detection: &CrossCellDetection,
    cells: &[TableCell],
) -> SplitOcrResult {
    let original_bbox = region.bounding_box.clone();
    let original_text = region
        .text
        .as_ref()
        .map(|s| s.to_string())
        .unwrap_or_default();
    let confidence = region.confidence;

    if original_text.is_empty() || detection.affected_cell_indices.is_empty() {
        return SplitOcrResult {
            original_bbox,
            original_text,
            confidence,
            segments: Vec::new(),
        };
    }

    let segments = if detection.is_horizontal_split && !detection.x_boundaries.is_empty() {
        split_horizontally(
            &original_bbox,
            &original_text,
            &detection.x_boundaries,
            &detection.affected_cell_indices,
            cells,
        )
    } else if !detection.y_boundaries.is_empty() {
        split_vertically(
            &original_bbox,
            &original_text,
            &detection.y_boundaries,
            &detection.affected_cell_indices,
            cells,
        )
    } else {
        // Fallback: assign to first affected cell
        vec![SplitSegment {
            bbox: original_bbox.clone(),
            text: original_text.clone(),
            cell_index: detection.affected_cell_indices[0],
        }]
    };

    SplitOcrResult {
        original_bbox,
        original_text,
        confidence,
        segments,
    }
}

/// Splits a bounding box horizontally (along x-axis) and distributes text.
fn split_horizontally(
    ocr_bbox: &BoundingBox,
    text: &str,
    x_boundaries: &[f32],
    cell_indices: &[usize],
    cells: &[TableCell],
) -> Vec<SplitSegment> {
    let mut segments = Vec::new();

    let ocr_x_min = ocr_bbox.x_min();
    let ocr_x_max = ocr_bbox.x_max();
    let ocr_y_min = ocr_bbox.y_min();
    let ocr_y_max = ocr_bbox.y_max();
    let ocr_width = ocr_x_max - ocr_x_min;

    if ocr_width <= 0.0 {
        return segments;
    }

    // Build segment x-ranges from boundaries
    let mut x_ranges: Vec<(f32, f32)> = Vec::new();
    let mut prev_x = ocr_x_min;

    for &boundary_x in x_boundaries {
        if boundary_x > prev_x && boundary_x < ocr_x_max {
            x_ranges.push((prev_x, boundary_x));
            prev_x = boundary_x;
        }
    }
    // Add final segment
    if prev_x < ocr_x_max {
        x_ranges.push((prev_x, ocr_x_max));
    }

    if x_ranges.is_empty() {
        return segments;
    }

    // Calculate width ratios for text distribution
    let total_width: f32 = x_ranges.iter().map(|(x1, x2)| x2 - x1).sum();
    let ratios: Vec<f32> = x_ranges
        .iter()
        .map(|(x1, x2)| (x2 - x1) / total_width)
        .collect();

    // Split text by ratios
    let text_parts = split_text_by_ratio(text, &ratios);

    // Create segments and assign to cells
    for ((x1, x2), text_part) in x_ranges.iter().zip(text_parts.iter()) {
        let segment_bbox = BoundingBox::from_coords(*x1, ocr_y_min, *x2, ocr_y_max);

        // Find the best matching cell for this segment
        let cell_index = find_best_matching_cell(&segment_bbox, cell_indices, cells);

        segments.push(SplitSegment {
            bbox: segment_bbox,
            text: text_part.clone(),
            cell_index,
        });
    }

    segments
}

/// Splits a bounding box vertically (along y-axis) and distributes text.
fn split_vertically(
    ocr_bbox: &BoundingBox,
    text: &str,
    y_boundaries: &[f32],
    cell_indices: &[usize],
    cells: &[TableCell],
) -> Vec<SplitSegment> {
    let mut segments = Vec::new();

    let ocr_x_min = ocr_bbox.x_min();
    let ocr_x_max = ocr_bbox.x_max();
    let ocr_y_min = ocr_bbox.y_min();
    let ocr_y_max = ocr_bbox.y_max();
    let ocr_height = ocr_y_max - ocr_y_min;

    if ocr_height <= 0.0 {
        return segments;
    }

    // Build segment y-ranges from boundaries
    let mut y_ranges: Vec<(f32, f32)> = Vec::new();
    let mut prev_y = ocr_y_min;

    for &boundary_y in y_boundaries {
        if boundary_y > prev_y && boundary_y < ocr_y_max {
            y_ranges.push((prev_y, boundary_y));
            prev_y = boundary_y;
        }
    }
    // Add final segment
    if prev_y < ocr_y_max {
        y_ranges.push((prev_y, ocr_y_max));
    }

    if y_ranges.is_empty() {
        return segments;
    }

    // For vertical splits, try to split by lines first
    let lines: Vec<&str> = text.lines().collect();

    if lines.len() >= y_ranges.len() {
        // Distribute lines across segments
        let lines_per_segment = lines.len() / y_ranges.len();
        let mut line_idx = 0;

        for (i, (y1, y2)) in y_ranges.iter().enumerate() {
            let segment_bbox = BoundingBox::from_coords(ocr_x_min, *y1, ocr_x_max, *y2);

            // Calculate how many lines this segment gets
            let num_lines = if i == y_ranges.len() - 1 {
                lines.len() - line_idx // Last segment gets remaining lines
            } else {
                lines_per_segment
            };

            let segment_text: String = lines[line_idx..line_idx + num_lines].join("\n");
            line_idx += num_lines;

            let cell_index = find_best_matching_cell(&segment_bbox, cell_indices, cells);

            segments.push(SplitSegment {
                bbox: segment_bbox,
                text: segment_text,
                cell_index,
            });
        }
    } else {
        // Fall back to ratio-based splitting
        let total_height: f32 = y_ranges.iter().map(|(y1, y2)| y2 - y1).sum();
        let ratios: Vec<f32> = y_ranges
            .iter()
            .map(|(y1, y2)| (y2 - y1) / total_height)
            .collect();

        let text_parts = split_text_by_ratio(text, &ratios);

        for ((y1, y2), text_part) in y_ranges.iter().zip(text_parts.iter()) {
            let segment_bbox = BoundingBox::from_coords(ocr_x_min, *y1, ocr_x_max, *y2);
            let cell_index = find_best_matching_cell(&segment_bbox, cell_indices, cells);

            segments.push(SplitSegment {
                bbox: segment_bbox,
                text: text_part.clone(),
                cell_index,
            });
        }
    }

    segments
}

/// Finds the best matching cell for a segment bbox.
fn find_best_matching_cell(
    segment_bbox: &BoundingBox,
    candidate_indices: &[usize],
    cells: &[TableCell],
) -> usize {
    let mut best_cell_idx = candidate_indices.first().copied().unwrap_or(0);
    let mut best_iou = 0.0f32;

    for &cell_idx in candidate_indices {
        if cell_idx >= cells.len() {
            continue;
        }

        let iou = segment_bbox.iou(&cells[cell_idx].bbox);
        if iou > best_iou {
            best_iou = iou;
            best_cell_idx = cell_idx;
        }
    }

    best_cell_idx
}

/// Splits text into parts based on given ratios.
///
/// This function attempts to split text at word boundaries when possible,
/// distributing characters roughly according to the specified ratios.
///
/// # Arguments
///
/// * `text` - The text to split.
/// * `ratios` - Slice of ratios (should sum to ~1.0).
///
/// # Returns
///
/// Vector of text parts, one for each ratio.
pub fn split_text_by_ratio(text: &str, ratios: &[f32]) -> Vec<String> {
    if ratios.is_empty() {
        return vec![text.to_string()];
    }

    if ratios.len() == 1 {
        return vec![text.to_string()];
    }

    let chars: Vec<char> = text.chars().collect();
    let total_chars = chars.len();

    if total_chars == 0 {
        return ratios.iter().map(|_| String::new()).collect();
    }

    // Normalize ratios
    let total_ratio: f32 = ratios.iter().sum();
    let normalized_ratios: Vec<f32> = if total_ratio > 0.0 {
        ratios.iter().map(|r| r / total_ratio).collect()
    } else {
        let equal = 1.0 / ratios.len() as f32;
        vec![equal; ratios.len()]
    };

    let mut result = Vec::with_capacity(ratios.len());
    let mut start_idx = 0;

    for (i, ratio) in normalized_ratios.iter().enumerate() {
        let chars_for_segment = if i == normalized_ratios.len() - 1 {
            // Last segment gets remaining characters
            total_chars - start_idx
        } else {
            (total_chars as f32 * ratio).round() as usize
        };

        let end_idx = (start_idx + chars_for_segment).min(total_chars);

        // Try to find a word boundary near the split point
        let adjusted_end_idx = if end_idx < total_chars && end_idx > start_idx {
            find_word_boundary(&chars, start_idx, end_idx)
        } else {
            end_idx
        };

        let segment: String = chars[start_idx..adjusted_end_idx].iter().collect();
        result.push(segment.trim().to_string());

        start_idx = adjusted_end_idx;
    }

    // Handle any remaining characters
    if start_idx < total_chars && !result.is_empty() {
        let remaining: String = chars[start_idx..].iter().collect();
        if let Some(last) = result.last_mut()
            && !remaining.trim().is_empty()
        {
            last.push_str(remaining.trim());
        }
    }

    result
}

/// Finds a suitable word boundary near the target split point.
fn find_word_boundary(chars: &[char], start: usize, target_end: usize) -> usize {
    // Search within a small window around the target
    let window = 5.min(target_end - start);

    // Look for space or punctuation near the target
    for offset in 0..window {
        let check_idx = target_end.saturating_sub(offset);
        if check_idx > start
            && check_idx < chars.len()
            && (chars[check_idx].is_whitespace()
                || chars[check_idx] == ','
                || chars[check_idx] == '.')
        {
            return check_idx + 1;
        }
    }

    // No word boundary found, use original target
    target_end
}

/// Calculates the area of a bounding box.
fn calculate_bbox_area(bbox: &BoundingBox) -> f32 {
    let width = bbox.x_max() - bbox.x_min();
    let height = bbox.y_max() - bbox.y_min();
    (width * height).max(0.0)
}

/// Creates expanded OCR results after splitting cross-cell boxes.
///
/// This is a convenience function that takes the original text regions,
/// detects cross-cell boxes, splits them, and returns an expanded list
/// of text regions suitable for table stitching.
///
/// # Arguments
///
/// * `text_regions` - Original text regions from OCR.
/// * `cells` - Table cells.
/// * `config` - Optional split configuration.
///
/// # Returns
///
/// A tuple of (expanded_regions, processed_indices) where:
/// - expanded_regions: New text regions after splitting
/// - processed_indices: Indices of original regions that were split
pub fn create_expanded_ocr_for_table(
    text_regions: &[TextRegion],
    cells: &[TableCell],
    config: Option<&SplitConfig>,
) -> (Vec<TextRegion>, std::collections::HashSet<usize>) {
    let default_config = SplitConfig::default();
    let config = config.unwrap_or(&default_config);

    let detections = detect_cross_cell_ocr_boxes(text_regions, cells, config);

    let mut expanded_regions = Vec::new();
    let mut processed_indices = std::collections::HashSet::new();

    for detection in &detections {
        processed_indices.insert(detection.ocr_index);

        let region = &text_regions[detection.ocr_index];
        let split_result = split_ocr_box_at_cell_boundaries(region, detection, cells);

        for segment in split_result.segments {
            if !segment.text.is_empty() {
                let new_region = TextRegion::with_recognition(
                    segment.bbox,
                    Some(Arc::from(segment.text.as_str())),
                    split_result.confidence,
                );
                expanded_regions.push(new_region);
            }
        }
    }

    (expanded_regions, processed_indices)
}

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

    fn make_region(x1: f32, y1: f32, x2: f32, y2: f32, text: &str) -> TextRegion {
        TextRegion::with_recognition(
            BoundingBox::from_coords(x1, y1, x2, y2),
            Some(Arc::from(text)),
            Some(0.9),
        )
    }

    fn make_cell(x1: f32, y1: f32, x2: f32, y2: f32) -> TableCell {
        TableCell::new(BoundingBox::from_coords(x1, y1, x2, y2), 0.9)
    }

    #[test]
    fn test_detect_no_cross_cell_ocr() {
        // OCR box fully inside one cell
        let regions = vec![make_region(10.0, 10.0, 90.0, 40.0, "Hello World")];

        let cells = vec![
            make_cell(0.0, 0.0, 100.0, 50.0),
            make_cell(100.0, 0.0, 200.0, 50.0),
        ];

        let config = SplitConfig::default();
        let detections = detect_cross_cell_ocr_boxes(&regions, &cells, &config);

        assert!(
            detections.is_empty(),
            "Should not detect cross-cell for box fully inside one cell"
        );
    }

    #[test]
    fn test_detect_cross_cell_horizontal() {
        // OCR box spans two cells horizontally
        let regions = vec![make_region(50.0, 10.0, 150.0, 40.0, "Header Text")];

        let cells = vec![
            make_cell(0.0, 0.0, 100.0, 50.0),
            make_cell(100.0, 0.0, 200.0, 50.0),
        ];

        let config = SplitConfig::default();
        let detections = detect_cross_cell_ocr_boxes(&regions, &cells, &config);

        assert_eq!(detections.len(), 1, "Should detect one cross-cell OCR box");
        assert_eq!(detections[0].affected_cell_indices.len(), 2);
        assert!(detections[0].is_horizontal_split);
    }

    #[test]
    fn test_split_text_by_ratio_equal() {
        let text = "ABCDEFGHIJ";
        let ratios = vec![0.5, 0.5];

        let parts = split_text_by_ratio(text, &ratios);

        assert_eq!(parts.len(), 2);
        // Total should be original length
        let total_len: usize = parts.iter().map(|s| s.len()).sum();
        assert_eq!(total_len, text.len());
    }

    #[test]
    fn test_split_text_by_ratio_unequal() {
        let text = "Hello World";
        let ratios = vec![0.3, 0.7];

        let parts = split_text_by_ratio(text, &ratios);

        assert_eq!(parts.len(), 2);
        // Parts should be non-empty
        assert!(!parts[0].is_empty() || !parts[1].is_empty());
    }

    #[test]
    fn test_split_text_empty() {
        let text = "";
        let ratios = vec![0.5, 0.5];

        let parts = split_text_by_ratio(text, &ratios);

        assert_eq!(parts.len(), 2);
        assert!(parts[0].is_empty());
        assert!(parts[1].is_empty());
    }

    #[test]
    fn test_split_ocr_box_horizontal() {
        let region = make_region(50.0, 10.0, 150.0, 40.0, "Col1 Col2");

        let cells = vec![
            make_cell(0.0, 0.0, 100.0, 50.0),
            make_cell(100.0, 0.0, 200.0, 50.0),
        ];

        let detection = CrossCellDetection {
            ocr_index: 0,
            affected_cell_indices: vec![0, 1],
            x_boundaries: vec![100.0],
            y_boundaries: Vec::new(),
            is_horizontal_split: true,
        };

        let result = split_ocr_box_at_cell_boundaries(&region, &detection, &cells);

        assert_eq!(result.segments.len(), 2, "Should produce 2 segments");

        // Verify segment bboxes don't overlap
        let seg1_x_max = result.segments[0].bbox.x_max();
        let seg2_x_min = result.segments[1].bbox.x_min();
        assert!(
            seg1_x_max <= seg2_x_min + 1.0,
            "Segments should not overlap"
        );
    }

    #[test]
    fn test_create_expanded_ocr_for_table() {
        let regions = vec![
            make_region(10.0, 10.0, 90.0, 40.0, "Cell1 Only"), // Inside cell 0
            make_region(50.0, 10.0, 150.0, 40.0, "Across Cells"), // Spans cells 0 and 1
        ];

        let cells = vec![
            make_cell(0.0, 0.0, 100.0, 50.0),
            make_cell(100.0, 0.0, 200.0, 50.0),
        ];

        let config = SplitConfig::default();
        let (expanded, processed) = create_expanded_ocr_for_table(&regions, &cells, Some(&config));

        // Second region should be split
        assert!(processed.contains(&1));
        assert!(!processed.contains(&0));

        // Should have created new regions from the split
        assert!(!expanded.is_empty());
    }
}