Skip to content

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

feat: enhance image footnote classification by adding deferred processing based on document body height

myhlolib7c710ebeac6
diff --git a/mineru/backend/flash/native_pdf/auxiliary_text.py b/mineru/backend/flash/native_pdf/auxiliary_text.py
index 797bcd7a..ab4459a8 100644
--- a/mineru/backend/flash/native_pdf/auxiliary_text.py
+++ b/mineru/backend/flash/native_pdf/auxiliary_text.py
@@ -128,6 +128,8 @@ def _classify_image_footnotes(
table_bboxes: list[BBox],
drawing_lines: list[_AxisLine],
page_size: tuple[float, float],
+ *,
+ reference_body_height: float | None = None,
) -> None:
"""用图片、下缘长横线和紧凑小字的联合关系识别图表脚注。"""
@@ -160,17 +162,21 @@ def _classify_image_footnotes(
)
if not line_geometry:
return
- body_samples = [
- _line_effective_height(line, bbox)
- for line, bbox in line_geometry
- if bbox[2] - bbox[0] >= 0.2 * local_page_width
- ]
- if not body_samples:
+ if reference_body_height is not None and reference_body_height > 0:
+ # 图片占主导的稀疏页可能只剩图注和脚注,延迟复核时改用全文正文尺度。
+ body_height = max(0.1, reference_body_height)
+ else:
body_samples = [
_line_effective_height(line, bbox)
for line, bbox in line_geometry
+ if bbox[2] - bbox[0] >= 0.2 * local_page_width
]
- body_height = max(0.1, statistics.median(body_samples))
+ if not body_samples:
+ body_samples = [
+ _line_effective_height(line, bbox)
+ for line, bbox in line_geometry
+ ]
+ body_height = max(0.1, statistics.median(body_samples))
local_images = [
_rotate_bbox_to_upright(bbox, page_size, dominant_angle)
for bbox in image_bboxes
@@ -233,6 +239,29 @@ def _classify_image_footnotes(
line.semantic_type = "footnote"
+def _classify_deferred_image_footnotes(
+ prepared_pages: list[_PreparedPage],
+ body_height: float,
+) -> None:
+ """在全文正文尺度确定后,仅重试仍未分类的图片脚注候选。"""
+
+ if body_height <= 0:
+ return
+ for prepared in prepared_pages:
+ _classify_image_footnotes(
+ prepared.remaining_lines,
+ [
+ block["bbox"]
+ for block in prepared.fixed_blocks
+ if block.get("type") == "image"
+ ],
+ prepared.table_bboxes,
+ prepared.drawing_lines,
+ prepared.page_size,
+ reference_body_height=body_height,
+ )
+
+
def _image_footnote_members(
line_geometry: list[tuple[_LineItem, BBox]],
rule_bbox: BBox,
diff --git a/mineru/backend/flash/native_pdf/line_layout.py b/mineru/backend/flash/native_pdf/line_layout.py
index 3a00912a..48893ce7 100644
--- a/mineru/backend/flash/native_pdf/line_layout.py
+++ b/mineru/backend/flash/native_pdf/line_layout.py
@@ -929,7 +929,12 @@ def _should_connect_text_rows(
local_lane_width = max(0.1, lane.right - local_lane_left)
next_indent = current_bbox[0] - local_lane_left
previous_fill = max(0.0, previous_bbox[2] - local_lane_left) / local_lane_width
- if next_indent >= max(5.0, 0.65 * pair_height) and (previous_fill <= 0.8 or terminal_previous):
+ if (
+ next_indent >= max(5.0, 0.65 * pair_height)
+ and (previous_fill <= 0.8 or terminal_previous)
+ and not safe_short_tail
+ ):
+ # 已确认的同左缘短尾优先于栏左缘缩进,避免参考文献冒号后的末行被切断。
return False
abnormal_gap = vertical_gap > regular_gap + max(0.25 * pair_height, 3.0 * gap_mad)
diff --git a/mineru/backend/flash/native_pdf/pipeline.py b/mineru/backend/flash/native_pdf/pipeline.py
index d359d5af..b0dcbe82 100644
--- a/mineru/backend/flash/native_pdf/pipeline.py
+++ b/mineru/backend/flash/native_pdf/pipeline.py
@@ -61,6 +61,7 @@ from .formulas import (
)
from .code_blocks import _build_code_blocks
from .auxiliary_text import (
+ _classify_deferred_image_footnotes,
_classify_isolated_first_page_footer,
_classify_page_number_outer_companions,
_classify_page_auxiliary_text,
@@ -70,6 +71,7 @@ from .auxiliary_text import (
_classify_single_page_compound_headers,
)
from .titles import (
+ _classify_body_height_section_titles,
_classify_page_titles,
_infer_document_body_profile,
_infer_document_title_profile,
@@ -207,6 +209,11 @@ def _analyze_native_document(pdf_doc: PDFDocument) -> list[list[dict[str, Any]]]
_classify_page_number_outer_companions(prepared_pages)
_classify_isolated_first_page_footer(prepared_pages)
document_body_profile = _infer_document_body_profile(prepared_pages)
+ if document_body_profile is not None:
+ _classify_deferred_image_footnotes(
+ prepared_pages,
+ document_body_profile.body_height,
+ )
document_title_profile = _infer_document_title_profile(
prepared_pages,
document_body_profile,
@@ -398,6 +405,12 @@ def _finalize_prepared_page(
for block in prepared.fixed_blocks
if not isinstance(block.get("_inline_visual_row_id"), int)
]
+ _classify_body_height_section_titles(
+ remaining_lines,
+ prepared.page_size,
+ container_bboxes=title_container_bboxes,
+ document_body_profile=document_body_profile,
+ )
_classify_page_titles(
remaining_lines,
prepared.page_size,
diff --git a/mineru/backend/flash/native_pdf/titles.py b/mineru/backend/flash/native_pdf/titles.py
index 43b883b1..a80e10ad 100644
--- a/mineru/backend/flash/native_pdf/titles.py
+++ b/mineru/backend/flash/native_pdf/titles.py
@@ -185,11 +185,18 @@ def _infer_document_body_profile(
height_clusters.append([(height, page_index, normalized_width)])
else:
target.append((height, page_index, normalized_width))
+ cross_page_clusters = [
+ cluster
+ for cluster in height_clusters
+ if len({item[1] for item in cluster}) >= 2
+ ]
+ eligible_clusters = cross_page_clusters or height_clusters
body_cluster = max(
- height_clusters,
+ eligible_clusters,
key=lambda cluster: (
- len({item[1] for item in cluster}),
sum(item[2] for item in cluster),
+ len({item[1] for item in cluster}),
+ len(cluster),
),
)
body_height = statistics.median(item[0] for item in body_cluster)
@@ -232,6 +239,166 @@ def _infer_document_body_profile(
)
+def _classify_body_height_section_titles(
+ lines: list[_LineItem],
+ page_size: tuple[float, float],
+ *,
+ container_bboxes: list[BBox],
+ document_body_profile: _DocumentBodyProfile | None,
+) -> None:
+ """用重复的短行加正文组结构识别与正文同字号的独立章节标题。"""
+
+ if document_body_profile is None:
+ return
+ body_height = document_body_profile.body_height
+ if body_height <= 0:
+ return
+
+ for angle in sorted({line.angle for line in lines if line.semantic_type is None}):
+ line_geometry = sorted(
+ [
+ (line, _rotate_bbox_to_upright(line.bbox, page_size, angle))
+ for line in lines
+ if line.angle == angle and line.semantic_type is None
+ ],
+ key=lambda item: (item[1][1], item[1][0], item[0].source_index),
+ )
+ if len(line_geometry) < 8:
+ continue
+ median_height = statistics.median(
+ _line_effective_height(line, bbox)
+ for line, bbox in line_geometry
+ )
+ local_page_width = page_size[1] if angle in {90, 270} else page_size[0]
+ local_page_height = page_size[0] if angle in {90, 270} else page_size[1]
+ lanes = _infer_text_lanes(
+ line_geometry,
+ local_page_width,
+ median_height,
+ )
+ lane_by_source: dict[int, _TextLane] = {}
+ regular_gaps: list[float] = []
+ for lane in lanes:
+ lane.lines.sort(
+ key=lambda item: (item[1][1], item[1][0], item[0].source_index)
+ )
+ regular_gap, _gap_mad = _estimate_lane_gap(lane)
+ regular_gaps.append(regular_gap)
+ for line, _bbox in lane.lines:
+ lane_by_source[line.source_index] = lane
+ if not lane_by_source:
+ continue
+
+ page_regular_gap = (
+ statistics.median(regular_gaps)
+ if regular_gaps
+ else 0.2 * body_height
+ )
+ physical_gaps = _build_physical_title_gap_map(line_geometry)
+ local_container_bboxes = [
+ _rotate_bbox_to_upright(bbox, page_size, angle)
+ for bbox in container_bboxes
+ ]
+ candidates: list[tuple[_LineItem, BBox, _TextLane]] = []
+ for line, bbox in line_geometry:
+ lane = lane_by_source.get(line.source_index)
+ if lane is None:
+ continue
+ line_height = _line_effective_height(line, bbox)
+ lane_width = max(0.1, lane.right - lane.left)
+ if not 0.9 <= line_height / body_height <= 1.1:
+ continue
+ if bbox[2] - bbox[0] > 0.22 * lane_width:
+ continue
+ if abs(bbox[0] - lane.left) > 0.75 * body_height:
+ continue
+ if _line_inside_visual_container(bbox, local_container_bboxes):
+ continue
+
+ followers = _body_height_section_followers(
+ line,
+ bbox,
+ line_geometry,
+ lane_by_source,
+ body_height,
+ )
+ if len(followers) < 3:
+ continue
+ gap_above = physical_gaps.get(line.source_index, (None, None))[0]
+ starts_body = (
+ gap_above is None
+ and bbox[1] <= 0.2 * local_page_height
+ )
+ has_extra_gap = (
+ gap_above is not None
+ and gap_above - page_regular_gap >= 0.75 * body_height
+ )
+ has_full_width_follower = any(
+ follower_bbox[2] - follower_bbox[0]
+ >= 0.75
+ * max(
+ 0.1,
+ lane_by_source[follower_line.source_index].right
+ - lane_by_source[follower_line.source_index].left,
+ )
+ for follower_line, follower_bbox in followers
+ if follower_line.source_index in lane_by_source
+ )
+ if starts_body or has_extra_gap or has_full_width_follower:
+ candidates.append((line, bbox, lane))
+
+ for line, bbox, _lane in candidates:
+ compatible_count = sum(
+ 1
+ for peer_line, peer_bbox, _peer_lane in candidates
+ if abs(peer_bbox[0] - bbox[0]) <= body_height
+ and 0.9
+ <= _line_effective_height(peer_line, peer_bbox)
+ / _line_effective_height(line, bbox)
+ <= 1.1
+ and _title_fonts_compatible(line, peer_line)
+ )
+ if compatible_count >= 2:
+ # 只标记结构锚点本身,避免普通正文被标题邻行扩展再次吞入。
+ line.semantic_type = "paragraph_title"
+
+
+def _body_height_section_followers(
+ candidate_line: _LineItem,
+ candidate_bbox: BBox,
+ line_geometry: list[tuple[_LineItem, BBox]],
+ lane_by_source: dict[int, _TextLane],
+ body_height: float,
+) -> list[tuple[_LineItem, BBox]]:
+ """返回短标题后方同锚点、同正文尺度且行距稳定的前三行。"""
+
+ followers: list[tuple[_LineItem, BBox]] = []
+ previous_top = candidate_bbox[1]
+ for line, bbox in line_geometry:
+ if line is candidate_line or bbox[1] <= candidate_bbox[1] + 0.4 * body_height:
+ continue
+ if not (
+ candidate_bbox[0] - 0.75 * body_height
+ <= bbox[0]
+ <= candidate_bbox[0] + 1.5 * body_height
+ ):
+ continue
+ top_pitch = bbox[1] - previous_top
+ if top_pitch < 0.5 * body_height:
+ continue
+ if top_pitch > 1.8 * body_height:
+ break
+ if not 0.9 <= _line_effective_height(line, bbox) / body_height <= 1.1:
+ break
+ if line.source_index not in lane_by_source:
+ break
+ followers.append((line, bbox))
+ previous_top = bbox[1]
+ if len(followers) == 3:
+ break
+ return followers
+
+
def _infer_document_title_profile(
prepared_pages: list[_PreparedPage],
document_body_profile: _DocumentBodyProfile | None,
diff --git a/tests/unittest/test_flash_pdf_auxiliary_text.py b/tests/unittest/test_flash_pdf_auxiliary_text.py
index bca3c540..e43b412f 100644
--- a/tests/unittest/test_flash_pdf_auxiliary_text.py
+++ b/tests/unittest/test_flash_pdf_auxiliary_text.py
@@ -96,7 +96,56 @@ def test_image_footnote_requires_image_rule_and_smaller_text() -> None:
assert page.page_footnote_groups == []
[email protected]("missing_evidence", ["image", "rule", "small_text"])
+def test_sparse_image_footnotes_retry_with_document_body_height() -> None:
+ """验证稀疏图片页可用全文正文尺度恢复两个被自身字号污染的图表脚注。"""
+
+ caption = _text_line(
+ "chart caption",
+ (50.0, 40.0, 300.0, 48.0),
+ 0,
+ effective_height=8.0,
+ )
+ notes = [
+ _text_line(
+ "left source",
+ (50.0, 255.0, 300.0, 262.0),
+ 1,
+ effective_height=7.0,
+ ),
+ _text_line(
+ "right source",
+ (550.0, 255.0, 800.0, 262.0),
+ 2,
+ effective_height=7.0,
+ ),
+ ]
+ page = _prepared_text_page(caption, *notes, page_size=(1000.0, 500.0))
+ page.fixed_blocks = [
+ {"type": "image", "bbox": bbox, "angle": 0, "content": ""}
+ for bbox in [
+ (50.0, 80.0, 450.0, 250.0),
+ (550.0, 80.0, 950.0, 250.0),
+ ]
+ ]
+ page.drawing_lines = [
+ models._AxisLine((50.0, 252.0, 450.0, 253.0), 1.0, "horizontal"),
+ models._AxisLine((550.0, 252.0, 950.0, 253.0), 1.0, "horizontal"),
+ ]
+
+ auxiliary_text._classify_page_auxiliary_text(page)
+
+ assert [line.semantic_type for line in notes] == [None, None]
+
+ auxiliary_text._classify_deferred_image_footnotes([page], body_height=10.0)
+
+ assert [line.semantic_type for line in notes] == ["footnote", "footnote"]
+ assert page.page_footnote_groups == []
+
+
+ "missing_evidence",
+ ["image", "rule", "small_text", "table_rule"],
+)
def test_image_footnote_rejects_incomplete_visual_evidence(
missing_evidence: str,
) -> None:
@@ -131,8 +180,11 @@ def test_image_footnote_rejects_incomplete_visual_evidence(
page.drawing_lines = [
models._AxisLine((50.0, 318.0, 300.0, 319.0), 1.0, "horizontal")
]
+ if missing_evidence == "table_rule":
+ page.table_bboxes = [(40.0, 300.0, 310.0, 340.0)]
auxiliary_text._classify_page_auxiliary_text(page)
+ auxiliary_text._classify_deferred_image_footnotes([page], body_height=10.0)
assert note.semantic_type is None
@@ -574,6 +626,7 @@ def test_auxiliary_text_classifiers_do_not_read_line_text() -> None:
auxiliary_text._classify_aside_text,
auxiliary_text._geometric_text_support_by_angle,
auxiliary_text._classify_image_footnotes,
+ auxiliary_text._classify_deferred_image_footnotes,
auxiliary_text._image_footnote_members,
auxiliary_text._classify_page_footnotes,
auxiliary_text._augment_footnote_groups_with_edge_markers,
diff --git a/tests/unittest/test_flash_pdf_text_blocks.py b/tests/unittest/test_flash_pdf_text_blocks.py
index 92e6b927..066633e4 100644
--- a/tests/unittest/test_flash_pdf_text_blocks.py
+++ b/tests/unittest/test_flash_pdf_text_blocks.py
@@ -1632,6 +1632,52 @@ def test_exaggerated_bbox_short_tail_stays_with_full_width_previous_row() -> Non
)
+def test_indented_reference_colon_keeps_safe_short_tail_with_abnormal_height() -> None:
+ """验证参考文献续行相对编号左缘缩进时,冒号后的异常高短尾仍保持同块。"""
+
+ previous = _text_line(
+ "full continuation:",
+ (25.0, 0.0, 100.0, 10.0),
+ 0,
+ effective_height=9.0,
+ )
+ current = _text_line(
+ "short tall tail",
+ (25.0, 11.0, 50.0, 29.0),
+ 1,
+ effective_height=18.6,
+ )
+ next_entry = _text_line(
+ "[10] next entry",
+ (0.0, 24.0, 100.0, 34.0),
+ 2,
+ effective_height=10.0,
+ )
+ lane = models._TextLane(
+ left=0.0,
+ right=100.0,
+ lines=[
+ (previous, previous.bbox),
+ (current, current.bbox),
+ (next_entry, next_entry.bbox),
+ ],
+ )
+
+ assert line_layout._should_connect_text_rows(
+ (previous, previous.bbox),
+ (current, current.bbox),
+ lane,
+ regular_gap=1.0,
+ gap_mad=0.1,
+ table_bboxes=[],
+ axis_lines=[],
+ )
+ assert text_blocks._starts_structural_reference_entry(
+ (current, current.bbox),
+ (next_entry, next_entry.bbox),
+ )
+
+
def test_indented_list_row_can_return_to_lane_left_for_short_tail() -> None:
"""验证列表首行轻度缩进时,回到栏左边的同字体短尾行仍可续接。"""
diff --git a/tests/unittest/test_flash_pdf_titles.py b/tests/unittest/test_flash_pdf_titles.py
index 1f5b0b11..b6684fe9 100644
--- a/tests/unittest/test_flash_pdf_titles.py
+++ b/tests/unittest/test_flash_pdf_titles.py
@@ -93,6 +93,94 @@ def test_document_regular_fonts_only_use_body_height_band() -> None:
assert profile.regular_fonts == frozenset({body_font})
+def test_document_body_profile_prefers_width_support_over_footer_page_count() -> None:
+ """验证窄页脚覆盖更多页面时,累计行宽更大的正文高度仍优先。"""
+
+ body_font = ("Body", 0)
+ footer_font = ("Footer", 0)
+ pages = []
+ for page_index in range(4):
+ lines = [
+ _text_line(
+ f"footer-{page_index}",
+ (0.0, 90.0, 10.0, 98.0),
+ 100,
+ effective_height=8.0,
+ font_signature=footer_font,
+ font_coverage=1.0,
+ )
+ ]
+ if page_index < 3:
+ lines.extend(
+ _text_line(
+ f"body-{page_index}-{row_index}",
+ (0.0, 10.0 + 12.0 * row_index, 100.0, 20.0 + 12.0 * row_index),
+ row_index,
+ effective_height=10.0,
+ font_signature=body_font,
+ font_coverage=1.0,
+ )
+ for row_index in range(4)
+ )
+ pages.append(_prepared_text_page(*lines))
+
+ profile = titles._infer_document_body_profile(pages)
+
+ assert profile is not None
+ assert profile.body_height == pytest.approx(10.0)
+ assert profile.regular_fonts == frozenset({body_font})
+
+
+def test_body_height_section_titles_mark_only_repeated_short_anchors() -> None:
+ """验证同字号短章节锚点独立成标题,明细、联系方式和版权行保持正文。"""
+
+ body_font = ("Body", 0)
+ lines = [
+ _text_line("first section", (0.0, 10.0, 20.0, 20.0), 0),
+ _text_line("wide body one", (0.0, 24.0, 100.0, 34.0), 1),
+ _text_line("wide body two", (0.0, 36.0, 100.0, 46.0), 2),
+ _text_line("wide body three", (0.0, 48.0, 100.0, 58.0), 3),
+ _text_line("second section", (0.0, 72.0, 20.0, 82.0), 4),
+ _text_line("compact row one", (0.0, 84.0, 60.0, 94.0), 5),
+ _text_line("compact row two", (0.0, 96.0, 60.0, 106.0), 6),
+ _text_line("compact row three", (0.0, 108.0, 60.0, 118.0), 7),
+ _text_line("boxed label", (0.0, 132.0, 20.0, 142.0), 8),
+ _text_line("boxed row one", (0.0, 144.0, 100.0, 154.0), 9),
+ _text_line("boxed row two", (0.0, 156.0, 100.0, 166.0), 10),
+ _text_line("boxed row three", (0.0, 168.0, 100.0, 178.0), 11),
+ _text_line("contact label", (0.0, 192.0, 20.0, 202.0), 12),
+ _text_line("small address", (0.0, 204.0, 60.0, 212.0), 13, effective_height=8.0),
+ _text_line("small phone", (0.0, 214.0, 60.0, 222.0), 14, effective_height=8.0),
+ _text_line("small email", (0.0, 224.0, 60.0, 232.0), 15, effective_height=8.0),
+ _text_line("copyright", (0.0, 250.0, 20.0, 260.0), 16),
+ ]
+ for line in lines:
+ line.font_signature = body_font
+ line.font_coverage = 1.0
+ line.dominant_font_weight = 250.0
+
+ titles._classify_body_height_section_titles(
+ lines,
+ (100.0, 280.0),
+ container_bboxes=[(0.0, 128.0, 100.0, 180.0)],
+ document_body_profile=titles._DocumentBodyProfile(
+ body_height=10.0,
+ body_weight=250.0,
+ regular_fonts=frozenset({body_font}),
+ ),
+ )
+
+ assert [lines[index].semantic_type for index in (0, 4)] == [
+ "paragraph_title",
+ "paragraph_title",
+ ]
+ assert all(
+ line.semantic_type is None
+ for index, line in enumerate(lines)
+ if index not in {0, 4}
+ )
+
+
def test_physical_title_gap_ignores_disjoint_column_and_keeps_overlapping_row() -> None:
"""验证物理邻行只在水平投影相交时参与标题留白,避免另一栏正文压缩间距。"""
@@ -894,6 +982,8 @@ def test_paragraph_title_detector_does_not_read_line_text() -> None:
for function in (
titles._classify_page_titles,
titles._infer_document_body_profile,
+ titles._classify_body_height_section_titles,
+ titles._body_height_section_followers,
titles._infer_document_title_profile,
titles._title_profile_seed_matches_cluster,
titles._title_profile_alignment,