Skip to content

RAGFlow is an agent-native database: a Retrieval-Augmented Generation (RAG) engine that grew into context infrastructure.

fix(deepdoc): collapse overlapping OCR boxes before Go table cell-fill (#18507)

Wangshue3def573d487
diff --git a/internal/deepdoc/parser/pdf/table_extract.go b/internal/deepdoc/parser/pdf/table_extract.go
index e6ad5cb628..d7ff1656e2 100644
--- a/internal/deepdoc/parser/pdf/table_extract.go
+++ b/internal/deepdoc/parser/pdf/table_extract.go
@@ -6,6 +6,7 @@ import (
"log/slog"
"math"
+ lyt "ragflow/internal/deepdoc/parser/pdf/layout"
tbl "ragflow/internal/deepdoc/parser/pdf/table"
pdf "ragflow/internal/deepdoc/parser/pdf/type"
util "ragflow/internal/deepdoc/parser/pdf/util"
@@ -116,12 +117,24 @@ func (p *Parser) processOneTable(ctx context.Context, pageImg image.Image, boxes
if firstCellTop == 1e9 {
firstCellTop = cells[0].Y0
}
- boxInCrop = make([]pdf.TextBox, 0, len(tm.BoxIdx))
+ tableBoxes := make([]pdf.TextBox, 0, len(tm.BoxIdx))
for _, idx := range tm.BoxIdx {
b := boxes[idx]
if b.Bottom*scale-cropOffY < firstCellTop {
continue
}
+ tableBoxes = append(tableBoxes, b)
+ }
+ // Collapse overlapping/adjacent OCR boxes before cell-fill, mirroring
+ // Python's pipeline order: _naive_vertical_merge runs before
+ // construct_table, so overlapping boxes are merged before they reach
+ // cell assignment. Go runs table cell-fill per-page (here), before the
+ // document-wide vertical merge in buildLayout, so it must run its own
+ // collapse on this table's box subset first or overlapping OCR boxes
+ // duplicate text across cells.
+ tableBoxes = lyt.NaiveVerticalMerge(tableBoxes, nil, nil, nil)
+ boxInCrop = make([]pdf.TextBox, 0, len(tableBoxes))
+ for _, b := range tableBoxes {
boxInCrop = append(boxInCrop, tbl.BoxToCropSpace(b, scale, cropOffX, cropOffY))
}
}
diff --git a/internal/deepdoc/parser/pdf/table_extract_test.go b/internal/deepdoc/parser/pdf/table_extract_test.go
index 58e60b4b13..ab0587e4c3 100644
--- a/internal/deepdoc/parser/pdf/table_extract_test.go
+++ b/internal/deepdoc/parser/pdf/table_extract_test.go
@@ -225,3 +225,74 @@ func TestProcessOneTable_NoPerCellOCR(t *testing.T) {
})
}
}
+
+// twoRowTableBuilder groups DetectCells' output into one row per cell
+// (single column), so a test can control exactly which TSR row band each
+// cell occupies.
+type twoRowTableBuilder struct {
+ cells []pdf.TSRCell
+}
+
+func (b *twoRowTableBuilder) Name() string { return "tworow" }
+
+func (b *twoRowTableBuilder) DetectCells(_ context.Context, _ image.Image) ([]pdf.TSRCell, error) {
+ return append([]pdf.TSRCell(nil), b.cells...), nil
+}
+
+func (b *twoRowTableBuilder) GroupCells(cells []pdf.TSRCell) [][]pdf.TSRCell {
+ rows := make([][]pdf.TSRCell, len(cells))
+ for i, c := range cells {
+ rows[i] = []pdf.TSRCell{c}
+ }
+ return rows
+}
+
+// TestProcessOneTable_CollapsesOverlappingBoxesBeforeCellFill verifies that
+// two overlapping OCR boxes over the same table region (one text box and a
+// nested duplicate detection covering part of the same text, e.g. "Alpha
+// Beta" and "Beta") are collapsed before cell assignment, matching Python's
+// pipeline order (_naive_vertical_merge runs before construct_table).
+// Without the collapse, the two boxes independently pick their own
+// best-matching row, spreading the duplicated word across two different
+// cells instead of landing once in a single row.
+func TestProcessOneTable_CollapsesOverlappingBoxesBeforeCellFill(t *testing.T) {
+ cfg := pdf.DefaultParserConfig()
+ p := NewParser(cfg)
+
+ pageImg := image.NewRGBA(image.Rect(0, 0, 600, 600))
+ // PDF-point space. Box2 ("Beta") is a nested duplicate OCR detection
+ // overlapping the tail of box1 ("Alpha Beta"): by itself it best-matches
+ // the second TSR row, while box1 alone best-matches the first.
+ boxes := []pdf.TextBox{
+ {X0: 0, X1: 50, Top: 5, Bottom: 25, Text: "Alpha Beta"},
+ {X0: 20, X1: 50, Top: 18.33, Bottom: 28.33, Text: "Beta"},
+ }
+ match := tbl.TableMatch{
+ Region: pdf.DLARegion{X0: 0, Y0: 0, X1: 600, Y1: 600, Label: pdf.LayoutTypeTable},
+ BoxIdx: []int{0, 1},
+ }
+ // TSR cells in crop-pixel space (scale = DlaScale = 3): row0 y=[0,60],
+ // row1 y=[60,120], single column x=[0,150].
+ builder := &twoRowTableBuilder{
+ cells: []pdf.TSRCell{
+ {X0: 0, Y0: 0, X1: 150, Y1: 60, Label: "table row"},
+ {X0: 0, Y0: 60, X1: 150, Y1: 120, Label: "table row"},
+ },
+ }
+
+ item := p.processOneTable(context.Background(), pageImg, boxes, 0, &orientationScoringDoc{}, builder, match, pdf.DlaScale)
+ if len(item.Grid) != 2 || len(item.Grid[0]) != 1 || len(item.Grid[1]) != 1 {
+ t.Fatalf("grid shape = %v, want 2x1", item.Grid)
+ }
+
+ row0, row1 := item.Grid[0][0].Text, item.Grid[1][0].Text
+ nonEmpty := 0
+ for _, text := range []string{row0, row1} {
+ if text != "" {
+ nonEmpty++
+ }
+ }
+ if nonEmpty != 1 {
+ t.Fatalf("nonEmpty cells = %d (row0=%q row1=%q), want exactly 1: overlapping boxes must merge into a single cell assignment instead of spreading duplicated text across rows", nonEmpty, row0, row1)
+ }
+}