Skip to content

Transforms complex documents like PDFs and Office docs into LLM-ready markdown/JSON for your Agentic workflows.

feat: enhance graphic body tail preservation by implementing logic to prevent absorption of trailing lines by graphics

myhloli41dfa2c34e4a
diff --git a/mineru/backend/flash/native_pdf/graphics.py b/mineru/backend/flash/native_pdf/graphics.py
index fe2cb664..6f083d28 100644
--- a/mineru/backend/flash/native_pdf/graphics.py
+++ b/mineru/backend/flash/native_pdf/graphics.py
@@ -299,10 +299,16 @@ def _build_graphic_like_blocks(
candidates,
median_height,
)
+ protected_body_tail_indices = _graphic_body_tail_line_indices_to_preserve(
+ lines,
+ candidates,
+ lanes,
+ median_height,
+ )
for row_lines in row_groups.values():
if any(
- line.source_index in protected_caption_indices
+ line.source_index in protected_caption_indices | protected_body_tail_indices
for line in row_lines
):
continue
@@ -710,6 +716,82 @@ def _graphic_caption_line_indices_to_preserve(
return protected
+def _graphic_body_tail_line_indices_to_preserve(
+ lines: list[_LineItem],
+ candidates: list[_GraphicCandidate],
+ lanes: list[_TextLane],
+ median_height: float,
+) -> set[int]:
+ """保护贴近图形上沿但延续上方满栏正文排版的短尾行。"""
+
+ protected: set[int] = set()
+ horizontal_lines = [line for line in lines if line.angle == 0]
+ for tail in horizontal_lines:
+ lane_index = _graphic_lane_index(tail.bbox, lanes)
+ lane = lanes[lane_index]
+ lane_width = max(0.1, lane.right - lane.left)
+ tail_width = tail.bbox[2] - tail.bbox[0]
+ if tail_width > 0.5 * lane_width:
+ continue
+
+ matching_candidates = [
+ candidate
+ for candidate in candidates
+ if (candidate.lane_index < 0 or candidate.lane_index == lane_index)
+ and tail.bbox[3] <= candidate.core_bbox[1] + 0.25 * median_height
+ and _is_graphic_label_member(
+ tail,
+ candidate.core_bbox,
+ median_height,
+ margin_scale=candidate.label_margin_scale,
+ )
+ ]
+ if not matching_candidates:
+ continue
+
+ tail_height = _line_effective_height(tail, tail.bbox)
+ for previous in horizontal_lines:
+ if previous.source_index == tail.source_index:
+ continue
+ if _graphic_lane_index(previous.bbox, lanes) != lane_index:
+ continue
+ vertical_gap = tail.bbox[1] - previous.bbox[3]
+ if not -0.25 * median_height <= vertical_gap <= 0.75 * median_height:
+ continue
+ if abs(previous.bbox[0] - tail.bbox[0]) > 0.75 * median_height:
+ continue
+
+ previous_width = previous.bbox[2] - previous.bbox[0]
+ if (
+ previous_width < 0.75 * lane_width
+ or lane.right - previous.bbox[2] > median_height
+ or previous_width < 1.5 * tail_width
+ ):
+ continue
+ previous_height = _line_effective_height(previous, previous.bbox)
+ if max(previous_height, tail_height) > 1.25 * min(previous_height, tail_height):
+ continue
+ if (
+ previous.font_signature is not None
+ and tail.font_signature is not None
+ and previous.font_signature != tail.font_signature
+ ):
+ continue
+ if any(
+ _is_graphic_label_member(
+ previous,
+ candidate.core_bbox,
+ median_height,
+ margin_scale=candidate.label_margin_scale,
+ )
+ for candidate in matching_candidates
+ ):
+ continue
+ protected.add(tail.source_index)
+ break
+ return protected
+
+
def _detect_strong_graphic_bboxes(source: _PageSource) -> list[BBox]:
"""仅按复杂 Path、容器尺度与成对坐标轴识别高置信图形核心。"""
diff --git a/tests/unittest/test_flash_pdf_samples.py b/tests/unittest/test_flash_pdf_samples.py
index 7e2d172a..8f8cee1f 100644
--- a/tests/unittest/test_flash_pdf_samples.py
+++ b/tests/unittest/test_flash_pdf_samples.py
@@ -313,7 +313,7 @@ def test_demo2_container_claims_are_pairwise_disjoint() -> None:
def test_demo2_page4_groups_five_graphics_and_keeps_table1() -> None:
- """验证 demo2 第四页五个图形区域分别聚合,Table 1 继续优先输出为 table。"""
+ """验证 demo2 第四页正文尾行不被图形吸收,五个图形和 Table 1 均保持完整。"""
page = _native_model_list("demo2.pdf")[3]
table_blocks = [block for block in page if block["type"] == "table"]
@@ -328,6 +328,14 @@ def test_demo2_page4_groups_five_graphics_and_keeps_table1() -> None:
assert len({id(block) for block in graphic_blocks}) == 5
assert "Frame 90" in graphic_blocks[0]["content"]
assert all("Figure" not in block["content"] for block in graphic_blocks)
+ body_tail_block = next(
+ block
+ for block in page
+ if block["content"].startswith("of the synthetic stereo scene")
+ )
+ assert body_tail_block["type"] == "text"
+ assert body_tail_block["content"].endswith("discontinuity map.")
+ assert "discontinuity map." not in graphic_blocks[0]["content"]
def test_demo2_table_captions_and_numeric_footnotes_are_not_text_blocks() -> None: