Files
ragflow/internal/parser/parser/pdf_postprocess_test.go

144 lines
4.5 KiB
Go
Raw Normal View History

package parser
import (
"testing"
deepdoctype "ragflow/internal/deepdoc/parser/type"
)
func makePDFSection(text, layout string, page int, left, right, top, bottom float64) deepdoctype.Section {
return deepdoctype.Section{
Text: text,
LayoutType: layout,
Positions: []deepdoctype.Position{{
PageNumbers: []int{page},
Left: left,
Right: right,
Top: top,
Bottom: bottom,
}},
}
}
func TestApplyPDFPostProcess_NormalizesLayoutTypes(t *testing.T) {
result := &deepdoctype.ParseResult{
Sections: []deepdoctype.Section{
{Text: "a", LayoutType: ""},
{Text: "b", LayoutType: " "},
{Text: "c", LayoutType: "table"},
{Text: "d", LayoutType: " figure "},
},
}
applyPDFPostProcess(result, pdfPostProcessOptions{})
want := []string{"text", "text", "table", "figure"}
for i, s := range result.Sections {
if s.LayoutType != want[i] {
t.Fatalf("Sections[%d].LayoutType = %q, want %q", i, s.LayoutType, want[i])
}
}
}
func TestApplyPDFPostProcess_AssignsDocTypeKeywords(t *testing.T) {
result := &deepdoctype.ParseResult{
Sections: []deepdoctype.Section{
{Text: "a", LayoutType: "table"},
{Text: "b", LayoutType: "figure"},
{Text: "c", LayoutType: "text"},
{Text: "d", LayoutType: "", Image: "abc"},
},
}
applyPDFPostProcess(result, pdfPostProcessOptions{})
feat(agent): Go ingestion pipeline progress mirroring and DeepDOC parser hardening (#16795) feat(ingestion): mirror Go pipeline progress into the document table; harden resume guards - pipeline: bind the owning document via WithDocumentID; after each TrackProgress event aggregate ingestion_task_log progress and mirror progress/run/progress_msg back into the document table, so GET /api/v1/datasets/{dataset_id}/documents reflects live Go pipeline progress without a bespoke endpoint. - canvas: extend the S3 resume guard to reject legacy no-op nodes (e.g. ExitLoop) so component_total equals the count of progress-reporting components and the aggregate percent can reach 100%. - runtime/canvas: route progress through TrackProgress; add interrupt test coverage (r3_interrupt_test.go). - dao/entity: add IngestionTask.DocumentID column and AggregateProgress support used by the mirror; IngestionTaskLog keeps a Checkpoint column alongside the progress fields. feat(deepdoc): cache DocAnalyzer inference results in Redis (1h TTL) - Redis-backed DocAnalyzerCache decorator over inference.Client; cache key = "ddoc:cache:<method>:" + sha256 of the JPEG-encoded image bytes (deterministic). - TTL = 1h; hits skip the inner HTTP call and return cached JSON; inner errors are not cached. refactor(deepdoc): align figure cropping with Python cropout + bounded page caches - CropSectionByDLA mirrors Python cropout: best-overlap DLA figure/equation region, fallback to section bbox per page, vertical concat on gray background. - sliding-window page-image cache bounds peak memory to the recent window instead of the whole PDF. - rename DLADebug -> DLARegions across parser/chunker/tests. refactor(parser): drop lib_type selector; align NewXxxParser with NewPDFParser - remove config["lib_type"] lookup and the libType param/field/switch from all nine constructors; surface the CGO-required error at ParseWithResult time instead of construction time; drop resolveLibType, its test, and the four lib_type constants. feat(utility): add a reusable workerpool for bounded concurrent execution - internal/utility/workerpool.go (+ tests). refactor: translate Chinese prose comments to English in non-harness Go files. chore: upgrade github.com/cloudwego/eino from v0.9.9 to v0.9.12.
2026-07-10 10:36:10 +08:00
// doc_type_kwd is derived from layout type only. A pre-set Image no
// longer reclassifies a section as "image" — cropping happens lazily
// at markdown serialization / chunk time (see pdf_parser_common.go).
want := []string{"table", "image", "text", "text"}
for i, s := range result.Sections {
if s.DocTypeKwd != want[i] {
t.Fatalf("Sections[%d].DocTypeKwd = %q, want %q", i, s.DocTypeKwd, want[i])
}
}
}
func TestApplyPDFPostProcess_FlattenMediaKeepsImagesButMarksText(t *testing.T) {
result := &deepdoctype.ParseResult{
Sections: []deepdoctype.Section{
{Text: "a", LayoutType: "figure", Image: "abc"},
{Text: "b", LayoutType: "table"},
},
}
applyPDFPostProcess(result, pdfPostProcessOptions{flattenMediaToText: true})
for i, s := range result.Sections {
if s.DocTypeKwd != "text" {
t.Fatalf("Sections[%d].DocTypeKwd = %q, want text", i, s.DocTypeKwd)
}
}
if got, want := result.Sections[0].Image, "abc"; got != want {
t.Fatalf("Sections[0].Image = %q, want %q", got, want)
}
}
func TestApplyPDFPostProcess_HeaderFooterFilteringIsOptional(t *testing.T) {
result := &deepdoctype.ParseResult{
Sections: []deepdoctype.Section{
{Text: "header", LayoutType: "header"},
{Text: "body", LayoutType: "text"},
},
}
applyPDFPostProcess(result, pdfPostProcessOptions{})
if len(result.Sections) != 2 {
t.Fatalf("len(Sections) = %d, want 2 when removeHeaderFooter is false", len(result.Sections))
}
applyPDFPostProcess(result, pdfPostProcessOptions{removeHeaderFooter: true})
if len(result.Sections) != 1 {
t.Fatalf("len(Sections) = %d, want 1 when removeHeaderFooter is true", len(result.Sections))
}
if got, want := result.Sections[0].Text, "body"; got != want {
t.Fatalf("remaining section = %q, want %q", got, want)
}
}
func TestApplyPDFPostProcess_RemoveTOCByOutlines(t *testing.T) {
result := &deepdoctype.ParseResult{
Sections: []deepdoctype.Section{
makePDFSection("目录", "text", 1, 50, 550, 100, 120),
makePDFSection("章节列表", "text", 2, 50, 550, 120, 140),
makePDFSection("正文", "text", 3, 50, 550, 100, 120),
},
Outlines: []deepdoctype.Outline{
{Title: "目录", Level: 0, PageNumber: 1},
{Title: "第一章", Level: 0, PageNumber: 3},
},
}
applyPDFPostProcess(result, pdfPostProcessOptions{removeTOC: true})
if len(result.Sections) != 1 {
t.Fatalf("len(Sections) = %d, want 1", len(result.Sections))
}
if got, want := result.Sections[0].Text, "正文"; got != want {
t.Fatalf("remaining section = %q, want %q", got, want)
}
}
func TestApplyPDFPostProcess_ReordersMultiColumnText(t *testing.T) {
cases := []struct {
name string
pageWidth float64
zoom float64
}{
{name: "unit zoom", pageWidth: 600, zoom: 1},
{name: "pre-normalized width", pageWidth: 200, zoom: 3},
}
for _, tc := range cases {
result := &deepdoctype.ParseResult{
Sections: []deepdoctype.Section{
makePDFSection("right", "text", 0, 100, 166, 100, 120),
makePDFSection("left", "text", 0, 10, 76, 100, 120),
},
}
applyPDFPostProcess(result, pdfPostProcessOptions{pageWidth: tc.pageWidth, zoom: tc.zoom, enableMultiColumn: true})
if got, want := result.Sections[0].Text, "left"; got != want {
t.Fatalf("%s: Sections[0].Text = %q, want %q", tc.name, got, want)
}
}
}