Skip to content

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

feat: implement classification for trailing footers in page footnotes and refine gap limit logic

myhloli46e5e76da20d
diff --git a/mineru/backend/flash/native_pdf/auxiliary_text.py b/mineru/backend/flash/native_pdf/auxiliary_text.py
index 5fbd5ca6..4bbe63fb 100644
--- a/mineru/backend/flash/native_pdf/auxiliary_text.py
+++ b/mineru/backend/flash/native_pdf/auxiliary_text.py
@@ -27,6 +27,7 @@ from .geometry import (
_bbox_center_x,
_bbox_center_y,
_bbox_intersects,
+ _bbox_union_many,
_clip_bbox,
_coerce_bbox,
_expand_bbox,
@@ -609,7 +610,10 @@ def _footnote_lane_members(
):
return set()
- continuation_gap_limit = max(1.25 * median_height, 0.01 * local_page_height)
+ continuation_gap_limit = _page_footnote_continuation_gap_limit(
+ median_height,
+ local_page_height,
+ )
members = [lane_lines[first_index]]
for current in lane_lines[first_index + 1 :]:
if lower_barrier_y is not None and current[1][1] >= lower_barrier_y:
@@ -620,6 +624,15 @@ def _footnote_lane_members(
return {line.source_index for line, _bbox in members}
+def _page_footnote_continuation_gap_limit(
+ reference_height: float,
+ local_page_height: float,
+) -> float:
+ """统一返回页脚注连续扩展允许的最大有效净空。"""
+
+ return max(1.25 * reference_height, 0.01 * local_page_height)
+
+
def _footnote_lane_width_reference(
lane: _TextLane,
lanes: list[_TextLane],
@@ -1242,6 +1255,225 @@ def _classify_single_page_compound_headers(pages: list[_PreparedPage]) -> None:
line.semantic_type = "header"
+def _classify_page_footnote_trailing_footers(
+ pages: list[_PreparedPage],
+) -> None:
+ """把任意页脚注投影下方、已越过续行边界的紧凑尾段标为页脚。"""
+
+ for page in pages:
+ line_by_source = {
+ line.source_index: line
+ for line in page.remaining_lines
+ }
+ ranked_groups: list[tuple[float, set[int]]] = []
+ for source_indices in page.page_footnote_groups:
+ anchor_lines = [
+ line_by_source[source_index]
+ for source_index in source_indices
+ if source_index in line_by_source
+ and line_by_source[source_index].semantic_type == "page_footnote"
+ ]
+ angles = {line.angle for line in anchor_lines}
+ if len(angles) != 1:
+ continue
+ angle = next(iter(angles))
+ local_bottom = max(
+ _rotate_bbox_to_upright(
+ line.bbox,
+ page.page_size,
+ angle,
+ )[3]
+ for line in anchor_lines
+ )
+ ranked_groups.append((local_bottom, source_indices))
+
+ # 优先处理页面最下方的脚注组,避免上方脚注跨过下方脚注寻找页脚。
+ for _local_bottom, source_indices in sorted(
+ ranked_groups,
+ key=lambda item: item[0],
+ reverse=True,
+ ):
+ for line in _page_footnote_trailing_footer_members(
+ page,
+ source_indices,
+ ):
+ line.semantic_type = "footer"
+
+
+def _page_footnote_trailing_footer_members(
+ page: _PreparedPage,
+ source_indices: set[int],
+) -> list[_LineItem]:
+ """返回脚注水平投影下方唯一、紧凑且小于正文尺度的页脚行。"""
+
+ line_by_source = {
+ line.source_index: line
+ for line in page.remaining_lines
+ }
+ anchor_lines = [
+ line_by_source[source_index]
+ for source_index in source_indices
+ if source_index in line_by_source
+ and line_by_source[source_index].semantic_type == "page_footnote"
+ ]
+ angles = {line.angle for line in anchor_lines}
+ if len(angles) != 1:
+ return []
+ angle = next(iter(angles))
+ local_page_size = (
+ (page.page_size[1], page.page_size[0])
+ if angle in {90, 270}
+ else page.page_size
+ )
+ local_page_width, local_page_height = local_page_size
+ if local_page_width <= 0 or local_page_height <= 0:
+ return []
+
+ anchor_geometry = [
+ (
+ line,
+ _rotate_bbox_to_upright(
+ line.bbox,
+ page.page_size,
+ angle,
+ ),
+ )
+ for line in anchor_lines
+ ]
+ anchor_bbox = _bbox_union_many([bbox for _line, bbox in anchor_geometry])
+ if anchor_bbox[3] < 0.75 * local_page_height:
+ return []
+
+ unresolved_geometry = [
+ (
+ line,
+ _rotate_bbox_to_upright(
+ line.bbox,
+ page.page_size,
+ angle,
+ ),
+ )
+ for line in page.remaining_lines
+ if line.semantic_type is None and line.angle == angle
+ ]
+ body_geometry = [
+ (line, bbox)
+ for line, bbox in unresolved_geometry
+ if bbox[3] <= anchor_bbox[1]
+ and bbox[2] - bbox[0] >= 0.3 * local_page_width
+ and 0.1 * local_page_height
+ <= _bbox_center_y(bbox)
+ <= 0.94 * local_page_height
+ and _bbox_axis_overlap_ratio(
+ anchor_bbox,
+ bbox,
+ axis="x",
+ )
+ >= 0.2
+ ]
+ if len(body_geometry) < 3:
+ return []
+ body_height = statistics.median(
+ _line_effective_height(line, bbox)
+ for line, bbox in body_geometry
+ )
+ if body_height <= 0:
+ return []
+
+ if any(
+ bbox[1] <= anchor_bbox[3] < bbox[3]
+ for _line, bbox in unresolved_geometry
+ ):
+ # 另一栏正文仍跨过脚注底边时,不能把其下方局部文本猜成全页页脚。
+ return []
+ trailing_geometry = sorted(
+ (
+ (line, bbox)
+ for line, bbox in unresolved_geometry
+ if bbox[1] > anchor_bbox[3]
+ ),
+ key=lambda item: (item[1][1], item[1][0], item[0].source_index),
+ )
+ if not 1 <= len(trailing_geometry) <= 3:
+ return []
+
+ first = trailing_geometry[0]
+ candidate_bbox = _bbox_union_many(
+ [bbox for _line, bbox in trailing_geometry]
+ )
+ if first[1][1] < 0.82 * local_page_height:
+ return []
+ projection_tolerance = 0.5 * body_height
+ if (
+ candidate_bbox[0] < anchor_bbox[0] - projection_tolerance
+ or candidate_bbox[2] > anchor_bbox[2] + projection_tolerance
+ or abs(candidate_bbox[0] - anchor_bbox[0]) > body_height
+ ):
+ return []
+ left_edges = [bbox[0] for _line, bbox in trailing_geometry]
+ if max(left_edges) - min(left_edges) > 0.75 * body_height:
+ return []
+ if candidate_bbox[2] - candidate_bbox[0] > 0.6 * local_page_width:
+ return []
+ if candidate_bbox[3] - candidate_bbox[1] > 4.0 * body_height:
+ return []
+ if any(
+ _line_effective_height(line, bbox) > 0.95 * body_height
+ for line, bbox in trailing_geometry
+ ):
+ return []
+
+ projecting_anchor_rows = [
+ item
+ for item in anchor_geometry
+ if _bbox_axis_overlap_ratio(
+ item[1],
+ candidate_bbox,
+ axis="x",
+ )
+ >= 0.2
+ ]
+ if not projecting_anchor_rows:
+ return []
+ anchor_last = max(
+ projecting_anchor_rows,
+ key=lambda item: (item[1][1], item[1][0], item[0].source_index),
+ )
+ continuation_gap_limit = _page_footnote_continuation_gap_limit(
+ body_height,
+ local_page_height,
+ )
+ first_gap = _effective_text_row_gap(anchor_last, first)
+ if not continuation_gap_limit < first_gap <= 3.0 * body_height:
+ return []
+ if any(
+ _effective_text_row_gap(previous, current) > continuation_gap_limit
+ for previous, current in zip(
+ trailing_geometry,
+ trailing_geometry[1:],
+ )
+ ):
+ return []
+
+ original_candidate_bbox = _bbox_union_many(
+ [line.bbox for line, _bbox in trailing_geometry]
+ )
+ if any(
+ container_bbox is not None
+ and _bbox_intersects(original_candidate_bbox, container_bbox)
+ for block in page.fixed_blocks
+ if (
+ container_bbox := _clip_bbox(
+ _coerce_bbox(block.get("bbox")),
+ page.page_size,
+ )
+ )
+ is not None
+ ):
+ return []
+ return [line for line, _bbox in trailing_geometry]
+
+
def _classify_isolated_first_page_footer(pages: list[_PreparedPage]) -> None:
"""用多页首页的极底位置、正文尺度和孤立净空补标唯一页脚。"""
diff --git a/mineru/backend/flash/native_pdf/pipeline.py b/mineru/backend/flash/native_pdf/pipeline.py
index 48e347a0..31e3f921 100644
--- a/mineru/backend/flash/native_pdf/pipeline.py
+++ b/mineru/backend/flash/native_pdf/pipeline.py
@@ -68,6 +68,7 @@ from .code_blocks import (
from .auxiliary_text import (
_classify_deferred_image_footnotes,
_classify_isolated_first_page_footer,
+ _classify_page_footnote_trailing_footers,
_classify_page_number_outer_companions,
_classify_page_auxiliary_text,
_classify_raw_page_marginals,
@@ -228,6 +229,7 @@ def _analyze_native_document(pdf_doc: PDFDocument) -> list[list[dict[str, Any]]]
_classify_rule_delimited_headers(prepared_pages)
_classify_rule_delimited_footers(prepared_pages)
_classify_page_number_outer_companions(prepared_pages)
+ _classify_page_footnote_trailing_footers(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:
diff --git a/tests/unittest/test_flash_pdf_auxiliary_text.py b/tests/unittest/test_flash_pdf_auxiliary_text.py
index 3e8cbd9c..cdab8977 100644
--- a/tests/unittest/test_flash_pdf_auxiliary_text.py
+++ b/tests/unittest/test_flash_pdf_auxiliary_text.py
@@ -58,6 +58,168 @@ def test_page_footnote_uses_separator_and_stops_before_distant_footer_text() ->
assert page.page_footnote_groups == [{3, 4, 5}]
+ ("page_index", "page_count"),
+ [(0, 1), (1, 3)],
+)
+def test_page_footnote_trailing_footer_applies_on_any_page(
+ page_index: int,
+ page_count: int,
+) -> None:
+ """验证单页或非首页脚注投影内、越过续行阈值的紧凑尾段可标为页脚。"""
+
+ body_lines = [
+ _text_line(
+ f"body {index}",
+ (100.0, 100.0 + 30.0 * index, 900.0, 110.0 + 30.0 * index),
+ index,
+ effective_height=10.0,
+ )
+ for index in range(3)
+ ]
+ footnote_lines = [
+ _text_line(
+ "note one",
+ (100.0, 770.0, 800.0, 778.0),
+ 3,
+ effective_height=8.0,
+ semantic_type="page_footnote",
+ ),
+ _text_line(
+ "note two",
+ (100.0, 790.0, 500.0, 798.0),
+ 4,
+ effective_height=8.0,
+ semantic_type="page_footnote",
+ ),
+ _text_line(
+ "note three",
+ (100.0, 810.0, 450.0, 818.0),
+ 5,
+ effective_height=8.0,
+ semantic_type="page_footnote",
+ ),
+ ]
+ footer_lines = [
+ _text_line(
+ "footer first",
+ (100.0, 834.0, 600.0, 842.0),
+ 6,
+ effective_height=8.0,
+ ),
+ _text_line(
+ "footer second",
+ (100.0, 846.0, 400.0, 854.0),
+ 7,
+ effective_height=8.0,
+ ),
+ ]
+ page = _prepared_text_page(
+ *body_lines,
+ *footnote_lines,
+ *footer_lines,
+ page_size=(1000.0, 1000.0),
+ )
+ page.page_footnote_groups = [{3, 4, 5}]
+
+ pages = [_prepared_text_page() for _index in range(page_count)]
+ pages[page_index] = page
+ auxiliary_text._classify_page_footnote_trailing_footers(pages)
+
+ assert [line.semantic_type for line in footnote_lines] == [
+ "page_footnote",
+ ] * 3
+ assert [line.semantic_type for line in footer_lines] == ["footer"] * 2
+
+
+ "case",
+ [
+ "other_lane",
+ "within_continuation",
+ "body_sized",
+ "split_tail",
+ "container_overlap",
+ "missing_anchor",
+ ],
+)
+def test_page_footnote_trailing_footer_rejects_weak_geometry(case: str) -> None:
+ """验证跨栏正文、弱分隔、正文尺度、多尾段和容器重叠均不能猜成页脚。"""
+
+ body_lines = [
+ _text_line(
+ f"body {index}",
+ (100.0, 100.0 + 30.0 * index, 900.0, 110.0 + 30.0 * index),
+ index,
+ effective_height=10.0,
+ )
+ for index in range(3)
+ ]
+ footnote_lines = [
+ _text_line(
+ f"note {index}",
+ (100.0, 770.0 + 20.0 * index, 500.0, 778.0 + 20.0 * index),
+ 3 + index,
+ effective_height=8.0,
+ semantic_type="page_footnote",
+ )
+ for index in range(3)
+ ]
+ first_top, second_top = (828.0, 840.0) if case == "within_continuation" else (834.0, 846.0)
+ if case == "split_tail":
+ second_top = 870.0
+ tail_height = 10.0 if case == "body_sized" else 8.0
+ tail_lines = [
+ _text_line(
+ "tail first",
+ (100.0, first_top, 600.0, first_top + tail_height),
+ 6,
+ effective_height=tail_height,
+ ),
+ _text_line(
+ "tail second",
+ (100.0, second_top, 400.0, second_top + tail_height),
+ 7,
+ effective_height=tail_height,
+ ),
+ ]
+ extra_lines = (
+ [
+ _text_line(
+ "right column body",
+ (700.0, 835.0, 950.0, 845.0),
+ 8,
+ effective_height=10.0,
+ )
+ ]
+ if case == "other_lane"
+ else []
+ )
+ page = _prepared_text_page(
+ *body_lines,
+ *footnote_lines,
+ *tail_lines,
+ *extra_lines,
+ page_size=(1000.0, 1000.0),
+ )
+ if case != "missing_anchor":
+ page.page_footnote_groups = [{3, 4, 5}]
+ if case == "container_overlap":
+ page.fixed_blocks = [
+ {
+ "type": "image",
+ "bbox": (80.0, 825.0, 620.0, 860.0),
+ "angle": 0,
+ "content": "",
+ }
+ ]
+
+ auxiliary_text._classify_page_footnote_trailing_footers([page])
+
+ assert all(line.semantic_type is None for line in tail_lines)
+ assert all(line.semantic_type is None for line in extra_lines)
+
+
def test_image_footnote_requires_image_rule_and_smaller_text() -> None:
"""验证图表脚注必须同时具备图片、下缘长横线和字号收缩证据。"""
@@ -629,6 +791,9 @@ def test_auxiliary_text_classifiers_do_not_read_line_text() -> None:
auxiliary_text._classify_deferred_image_footnotes,
auxiliary_text._image_footnote_members,
auxiliary_text._classify_page_footnotes,
+ auxiliary_text._classify_page_footnote_trailing_footers,
+ auxiliary_text._page_footnote_trailing_footer_members,
+ auxiliary_text._page_footnote_continuation_gap_limit,
auxiliary_text._augment_footnote_groups_with_edge_markers,
auxiliary_text._classify_rule_delimited_headers,
auxiliary_text._classify_page_number_outer_companions,
diff --git a/tests/unittest/test_flash_pdf_samples.py b/tests/unittest/test_flash_pdf_samples.py
index 41364505..e824faea 100644
--- a/tests/unittest/test_flash_pdf_samples.py
+++ b/tests/unittest/test_flash_pdf_samples.py
@@ -99,7 +99,7 @@ def _blocks_containing(
def test_all_demo_pdfs_keep_expected_txt_block_inventory() -> None:
- """验证 15 份 demo PDF 在显式 TXT 模式下仅新增既定表格注释块。"""
+ """验证 15 份 demo PDF 在显式 TXT 模式下保持预期块库存与页脚迁移。"""
pdf_names = sorted(
path.name
@@ -121,12 +121,12 @@ def test_all_demo_pdfs_keep_expected_txt_block_inventory() -> None:
assert sum(inventory.values()) == 2591
assert inventory == Counter(
{
- "text": 901,
+ "text": 900,
"image": 530,
"paragraph_title": 214,
"header": 180,
"page_number": 179,
- "footer": 176,
+ "footer": 177,
"table": 135,
"caption": 120,
"equation": 50,
@@ -141,7 +141,7 @@ def test_all_demo_pdfs_keep_expected_txt_block_inventory() -> None:
def test_demo1_keeps_five_real_tables_without_formula_false_positive() -> None:
- """验证 demo1 首页脚注、参考文献、公式与五个真实表格均保持正确。"""
+ """验证 demo1 首页脚注尾段页脚、参考文献、公式与五个真实表格均正确。"""
model_list = _native_model_list("demo1.pdf")
@@ -207,8 +207,10 @@ def test_demo1_keeps_five_real_tables_without_formula_false_positive() -> None:
copyright_block = next(
block for block in model_list[0] if block["content"].startswith("0022-1694/$")
)
- assert copyright_block["type"] == "text"
+ assert copyright_block["type"] == "footer"
+ assert copyright_block["bbox"] == [0.078, 0.87, 0.513, 0.894]
assert "doi:10.1016/j.jhydrol.2005.01.006" in copyright_block["content"]
+ assert model_list[0].index(page1_footnotes[0]) < model_list[0].index(copyright_block)
assert next(block for block in model_list[0] if block["content"] == "Abstract")["type"] == "paragraph_title"
assert next(block for block in model_list[6] if block["content"].startswith("4.2."))["type"] == "paragraph_title"
@@ -620,6 +622,12 @@ def test_demo3_auxiliary_text_types_match_real_page_geometry() -> None:
for block in model_list[6]
if block["type"] in {"aside_text", "page_footnote"}
]
+ assert not [
+ block
+ for page in model_list
+ for block in page
+ if block["type"] == "footer"
+ ]
def test_demo3_pages1_and2_fix_title_front_matter_and_embedding_formula() -> None:
@@ -893,6 +901,11 @@ def test_demo4_nct00083083_targeted_flash_regressions() -> None:
)["type"] == "text"
assert _blocks_containing(page1, "contributed equally")[0]["type"] == "page_footnote"
assert _blocks_containing(page1, "University of Edinburgh")[0]["type"] == "page_footnote"
+ assert [
+ block["bbox"]
+ for block in page1
+ if block["type"] == "footer"
+ ] == [[0.086, 0.934, 0.157, 0.957]]
abstract_sections = [
_blocks_containing(page1, opener)
@@ -1873,6 +1886,18 @@ def test_mixed_elements_pages_11_15_force_txt_regressions() -> None:
assert "pepsin. Isolated and everted esophagus" in figure6[0]["content"]
assert not [block for block in all_blocks if block["type"] == "footnote"]
assert sum(block["type"] == "caption" for block in all_blocks) == 7
+ page1_corresponding_author = _blocks_containing(
+ model_list[0],
+ "Corresponding author",
+ )
+ page1_right_column_tail = _blocks_containing(
+ model_list[0],
+ "The reflux of gastric juice was induced",
+ )
+ assert len(page1_corresponding_author) == len(page1_right_column_tail) == 1
+ assert page1_corresponding_author[0]["type"] == "page_footnote"
+ assert page1_right_column_tail[0]["type"] == "text"
+ assert not [block for block in model_list[0] if block["type"] == "footer"]
table_group = MagicModel(
model_list[3],