Skip to content

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

fix(deepdoc): resolve conflicting caption labels by confidence (#21415)

jay77721914bcb0307f0
diff --git a/internal/deepdoc/parser/pdf/table/table_annotate.go b/internal/deepdoc/parser/pdf/table/table_annotate.go
index dcb50998fe..eb2bdfed2e 100644
--- a/internal/deepdoc/parser/pdf/table/table_annotate.go
+++ b/internal/deepdoc/parser/pdf/table/table_annotate.go
@@ -199,24 +199,22 @@ func cleanupLayouts(regs []annRegion, boxes []pdf.TextBox) []annRegion {
return regs
}
-// nmsDLARegions applies per-class non-maximum suppression to raw DLA regions,
-// mirroring Python's layout model postprocess (layout_recognizer.py:246,
-// operators.py:667 nms with iou_thresh). For each label, detections are sorted
-// by confidence descending; the top one is kept and any other same-label
-// detection whose IoU (using the +1 overlap convention from operators.py:685)
-// exceeds iouThresh is suppressed. This runs on the raw, pre-scale detections
-// just as Python's postprocess does, before cleanup/annotation.
-//
-// It is idempotent with a server-side NMS: applying it again to already-suppressed
-// boxes yields the same set, so it safely converges Go to Python regardless of
-// where suppression happens upstream.
+// nmsDLARegions suppresses overlapping raw DLA regions within each label.
+// Figure and table captions share a group so competing labels for the same
+// region are resolved by confidence before annotation's type priority applies.
+// The highest-confidence detection wins when IoU exceeds iouThresh, using
+// the +1 overlap convention from Python's operators.py:685-688.
func nmsDLARegions(regions []pdf.DLARegion, iouThresh float64) []pdf.DLARegion {
if len(regions) == 0 {
return regions
}
byLabel := map[string][]int{}
for i, r := range regions {
- byLabel[r.Label] = append(byLabel[r.Label], i)
+ label := r.Label
+ if label == pdf.DLALabelTableCaption {
+ label = pdf.DLALabelFigureCaption
+ }
+ byLabel[label] = append(byLabel[label], i)
}
suppressed := make([]bool, len(regions))
for _, idxs := range byLabel {
@@ -294,10 +292,10 @@ func nmsIoU(a, b pdf.DLARegion) float64 {
// top-to-bottom (sort_Y_firstly), and figure and equation regions receive
// separate geometry namespaces (figure-N /
// equation-N) so they never collide.
-// FilteredDLARegions returns the DLA regions after per-class NMS, the
+// FilteredDLARegions returns the DLA regions after NMS, the
// confidence filter, Y-sort, and cleanup — i.e. the exact set fed to
// annotation. It mirrors Python's page_layout (layout_recognizer.py:84-100):
-// - nmsDLARegions(0.45) == layout model postprocess (operators.py:667)
+// - nmsDLARegions(0.45), with figure/table captions competing by confidence
// - keep if score >= 0.4 OR type not garbage == layout_recognizer.py:97
// - sortYFirstly == layout_recognizer.py:99 (sort_Y_firstly)
// - cleanupLayouts == layout_recognizer.py:100 (layouts_cleanup)
diff --git a/internal/deepdoc/parser/pdf/table/table_annotate_test.go b/internal/deepdoc/parser/pdf/table/table_annotate_test.go
index 0b440681a4..dbc01b983f 100644
--- a/internal/deepdoc/parser/pdf/table/table_annotate_test.go
+++ b/internal/deepdoc/parser/pdf/table/table_annotate_test.go
@@ -3,6 +3,7 @@ package table
import (
"context"
"image"
+ "strings"
"testing"
pdf "ragflow/internal/deepdoc/parser/pdf/type"
@@ -65,6 +66,73 @@ func TestAnnotateBoxLayouts_PriorityOverMaxArea(t *testing.T) {
}
}
+func TestAnnotateBoxLayoutsCaptionConfidence(t *testing.T) {
+ for _, tc := range []struct {
+ name string
+ figureScore, tableScore float64
+ shift float64
+ want string
+ }{
+ {"table_wins", 0.217, 0.5509, 0, pdf.DLALabelTableCaption},
+ {"figure_wins", 0.9, 0.55, 0, pdf.DLALabelFigureCaption},
+ {"table_wins_with_shift", 0.217, 0.5509, 0.5, pdf.DLALabelTableCaption},
+ } {
+ t.Run(tc.name, func(t *testing.T) {
+ for _, reverse := range []bool{false, true} {
+ regions := []pdf.DLARegion{
+ {X0: 0, Y0: 0, X1: 100, Y1: 10, Label: pdf.DLALabelFigureCaption, Confidence: tc.figureScore},
+ {X0: tc.shift, Y0: tc.shift, X1: 100 + tc.shift, Y1: 10 + tc.shift, Label: pdf.DLALabelTableCaption, Confidence: tc.tableScore},
+ }
+ if reverse {
+ regions[0], regions[1] = regions[1], regions[0]
+ }
+ boxes := AnnotateBoxLayouts([]pdf.TextBox{{Text: "Caption", X0: 0, X1: 100, Top: 0, Bottom: 10}}, regions, 1, 0)
+ if len(boxes) != 1 || boxes[0].LayoutType != tc.want {
+ t.Fatalf("reverse=%v: caption boxes = %+v, want layout %q", reverse, boxes, tc.want)
+ }
+ }
+ })
+ }
+}
+
+func TestAnnotateBoxLayoutsDistinctCaptions(t *testing.T) {
+ boxes := []pdf.TextBox{
+ {Text: "Figure caption", X0: 0, X1: 100, Top: 0, Bottom: 10},
+ {Text: "Table caption", X0: 120, X1: 220, Top: 0, Bottom: 10},
+ }
+ regions := []pdf.DLARegion{
+ {X0: 0, Y0: 0, X1: 100, Y1: 10, Label: pdf.DLALabelFigureCaption, Confidence: 0.217},
+ {X0: 120, Y0: 0, X1: 220, Y1: 10, Label: pdf.DLALabelTableCaption, Confidence: 0.5509},
+ }
+ got := AnnotateBoxLayouts(boxes, regions, 1, 0)
+ if len(got) != 2 || got[0].LayoutType != pdf.DLALabelFigureCaption || got[1].LayoutType != pdf.DLALabelTableCaption {
+ t.Fatalf("distinct captions lost their labels: %+v", got)
+ }
+}
+
+func TestAnnotateBoxLayoutsTableCaptionMerges(t *testing.T) {
+ // an0590scv140.pdf, page 6: both caption labels refer to the same region.
+ caption := pdf.TextBox{Text: "表1. HT24LC02A主要规格", X0: 277.33, X1: 389, Top: 657, Bottom: 668.33}
+ regions := []pdf.DLARegion{
+ {X0: 832.37, Y0: 1969.45, X1: 1165.44, Y1: 2004.01, Label: pdf.DLALabelFigureCaption, Confidence: 0.217},
+ {X0: 832.37, Y0: 1969.45, X1: 1165.44, Y1: 2004.01, Label: pdf.DLALabelTableCaption, Confidence: 0.5509},
+ }
+ boxes := AnnotateBoxLayouts([]pdf.TextBox{caption}, regions, 3, 2526)
+ if len(boxes) != 1 {
+ t.Fatalf("caption boxes = %+v, want one caption", boxes)
+ }
+ sections := MergeCaptions([]pdf.Section{
+ {Text: "<table><tr><td>HT24LC02A</td></tr></table>", LayoutType: pdf.LayoutTypeTable,
+ Positions: []pdf.Position{{PageNumbers: []int{5}, Left: 180.99, Right: 466.79, Top: 570.77, Bottom: 650.92}}},
+ {Text: boxes[0].Text, LayoutType: boxes[0].LayoutType,
+ Positions: []pdf.Position{{PageNumbers: []int{5}, Left: caption.X0, Right: caption.X1, Top: caption.Top, Bottom: caption.Bottom}}},
+ })
+ if len(sections) != 1 || sections[0].LayoutType != pdf.LayoutTypeTable ||
+ !strings.Contains(sections[0].Text, "<caption>表1. HT24LC02A主要规格</caption>") {
+ t.Fatalf("table caption remained separate from its table: %+v", sections)
+ }
+}
+
func TestAnnotateBoxLayouts_OverlapThreshold(t *testing.T) {
// Region overlaps only 30% of box — below 0.4 threshold — should NOT match.
boxes := []pdf.TextBox{{X0: 0, X1: 100, Top: 0, Bottom: 50}}