Skip to content

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

fix: merge full-width Flash span continuations

myhloli48f2fc3d463b
diff --git a/mineru/model/flash/pdf/text_blocks.py b/mineru/model/flash/pdf/text_blocks.py
index 918f1c56..f13f9952 100644
--- a/mineru/model/flash/pdf/text_blocks.py
+++ b/mineru/model/flash/pdf/text_blocks.py
@@ -1424,20 +1424,13 @@ def _merge_paragraph_formula_context_blocks(
ordered_group,
)
local_rows = [
- bbox
- for bbox in merged.get("_local_line_bboxes", [])
- if isinstance(bbox, (list, tuple))
- and len(bbox) == 4
+ bbox for bbox in merged.get("_local_line_bboxes", []) if isinstance(bbox, (list, tuple)) and len(bbox) == 4
]
maximum_width = max(
(bbox[2] - bbox[0] for bbox in local_rows),
default=0.0,
)
- body_rows = [
- bbox
- for bbox in local_rows
- if bbox[2] - bbox[0] >= 0.75 * maximum_width
- ]
+ body_rows = [bbox for bbox in local_rows if bbox[2] - bbox[0] >= 0.75 * maximum_width]
if len(body_rows) >= 2:
# 复杂分式可能比正文左缘多探出少量 glyph;公开框按重复满行边界稳定收口。
merged_bbox = merged["bbox"]
@@ -2678,7 +2671,7 @@ def _merge_list_intro_text_components(
def _merge_unterminated_text_components(
blocks: list[dict[str, Any]],
) -> list[dict[str, Any]]:
- """合并同栏、满行且前块没有终止标点的紧邻正文组件。"""
+ """合并普通栏未终止正文,以及满足严格结构约束的满宽 span 正文。"""
output = list(blocks)
terminal_re = re.compile(
@@ -2721,6 +2714,44 @@ def _merge_unterminated_text_components(
],
)
second_interval = _component_lane_interval(second)
+ first_declared_interval = _component_declared_lane_interval(
+ first,
+ )
+ second_declared_interval = _component_declared_lane_interval(
+ second,
+ )
+ row_pair_height = statistics.median(
+ [
+ max(
+ 0.1,
+ first_rows[-1][3] - first_rows[-1][1],
+ ),
+ max(
+ 0.1,
+ second_rows[0][3] - second_rows[0][1],
+ ),
+ ],
+ )
+ span_connection_height = max(
+ pair_height,
+ row_pair_height,
+ )
+ span_tolerance = 0.75 * span_connection_height
+ span_pair = (
+ first.get("_lane_is_span") is True
+ and second.get("_lane_is_span") is True
+ and int(first.get("angle", 0) or 0) % 360 == int(second.get("angle", 0) or 0) % 360
+ and first_declared_interval is not None
+ and second_declared_interval is not None
+ and abs(
+ first_declared_interval[0] - second_declared_interval[0],
+ )
+ <= span_tolerance
+ and abs(
+ first_declared_interval[1] - second_declared_interval[1],
+ )
+ <= span_tolerance
+ )
first_content = str(first.get("content") or "")
second_content = str(second.get("content") or "")
single_numbered_tail = (
@@ -2742,14 +2773,14 @@ def _merge_unterminated_text_components(
second_content,
)
is not None
- and second_interval is not None
- and second_rows[0][2] - second_rows[0][0] >= 0.5 * (second_interval[1] - second_interval[0])
+ and (reference_interval := (second_declared_interval if span_pair else second_interval)) is not None
+ and second_rows[0][2] - second_rows[0][0] >= 0.5 * (reference_interval[1] - reference_interval[0])
)
if (
second.get("_protected_hard_break_before") is True
- or (second.get("_hard_break_before") is True and not narrow_continuation)
+ or (second.get("_hard_break_before") is True and (span_pair or not narrow_continuation))
or second.get("_leading_emphasis_start") is True
- or (starts_wide_label and not aligned_short_tail)
+ or (starts_wide_label and (span_pair or not aligned_short_tail))
or first.get("_hanging_indent_group") is not None
or second.get("_hanging_indent_group") is not None
or _FIGURE_CAPTION_MARKER_RE.match(
@@ -2757,7 +2788,8 @@ def _merge_unterminated_text_components(
)
is not None
or (
- not single_numbered_tail
+ not span_pair
+ and not single_numbered_tail
and not url_continuation
and terminal_re.search(
first_content.rstrip(),
@@ -2767,15 +2799,21 @@ def _merge_unterminated_text_components(
):
continue
interval = _component_lane_interval(first)
- if interval is None or not _components_share_lane_role(
+ if span_pair:
+ interval = first_declared_interval
+ elif interval is None or not _components_share_lane_role(
first,
second,
pair_height,
):
continue
+ if interval is None:
+ continue
lane_width = interval[1] - interval[0]
vertical_gap = second_rows[0][1] - first_rows[-1][3]
- minimum_first_fill = 0.5 if single_numbered_tail else 0.65
+ minimum_first_fill = 0.8 if span_pair else 0.5 if single_numbered_tail else 0.65
+ minimum_second_fill = 0.8 if span_pair else 0.65
+ connection_height = span_connection_height if span_pair else pair_height
first_reference_width = (
max(row[2] - row[0] for row in first_rows) if narrow_continuation else first_rows[-1][2] - first_rows[-1][0]
)
@@ -2790,14 +2828,17 @@ def _merge_unterminated_text_components(
)
if (
first_reference_width < minimum_first_fill * lane_width
- or (not narrow_continuation and second_rows[0][2] - second_rows[0][0] < 0.65 * lane_width)
- or not -pair_height <= vertical_gap <= 1.5 * pair_height
+ or (
+ (span_pair or not narrow_continuation)
+ and second_rows[0][2] - second_rows[0][0] < minimum_second_fill * lane_width
+ )
+ or not -connection_height <= vertical_gap <= 1.5 * connection_height
or fonts_conflict
or _component_connection_skips_block(
output,
first_index,
second_index,
- pair_height,
+ connection_height,
)
):
continue
@@ -2813,13 +2854,11 @@ def _merge_unterminated_text_components(
output.pop(second_index)
-def _component_lane_interval(
+def _component_declared_lane_interval(
block: dict[str, Any],
) -> tuple[float, float] | None:
- """读取普通文本组件所属的有效非跨栏栏带区间。"""
+ """读取组件声明的有效栏带区间,不区分普通栏或跨栏。"""
- if block.get("_lane_is_span") is not False:
- return None
interval = block.get("_lane_interval")
if (
not isinstance(interval, (list, tuple))
@@ -2831,6 +2870,16 @@ def _component_lane_interval(
return (left, right) if right > left else None
+def _component_lane_interval(
+ block: dict[str, Any],
+) -> tuple[float, float] | None:
+ """读取普通文本组件所属的有效非跨栏栏带区间。"""
+
+ if block.get("_lane_is_span") is not False:
+ return None
+ return _component_declared_lane_interval(block)
+
+
def _component_reference_width(
block: dict[str, Any],
local_page_width: float,
diff --git a/tests/fixtures/flash_layout_block_expectations.json b/tests/fixtures/flash_layout_block_expectations.json
index 197ca359..a94db921 100644
--- a/tests/fixtures/flash_layout_block_expectations.json
+++ b/tests/fixtures/flash_layout_block_expectations.json
@@ -237,7 +237,7 @@
"page_footnote": 4,
"page_number": 3,
"paragraph_title": 6,
- "text": 59
+ "text": 56
},
"exact_typed_text": [
{"page_index": 0, "type": "header", "text": "DOI:10.16270/j.cnki.slgc.2013.03.029"},
@@ -252,6 +252,18 @@
{"page_index": 3, "type": "caption", "text": "Fig.3ManagementprocessofintegratedProjectbetweenAPSandERP"},
{"page_index": 3, "type": "paragraph_title", "text": "【参考文献】"}
],
+ "same_block_groups": [
+ {
+ "page_index": 0,
+ "type": "text",
+ "fragments": [
+ "Abstract: Advanced planning and scheduling",
+ "based on the Theory of Constraint",
+ "huge advantage in production planning",
+ "important role in the production planning"
+ ]
+ }
+ ],
"different_block_groups": [
{
"page_index": 0,
@@ -260,6 +272,10 @@
{
"page_index": 0,
"fragments": ["1APS与ERP简介", "1.1ERP系统的弱点"]
+ },
+ {
+ "page_index": 0,
+ "fragments": ["Abstract: Advanced planning and scheduling", "Keywords: APS"]
}
]
},
diff --git a/tests/fixtures/flash_layout_geometry_manifest.json b/tests/fixtures/flash_layout_geometry_manifest.json
index 60a81fbc..e6f12443 100644
--- a/tests/fixtures/flash_layout_geometry_manifest.json
+++ b/tests/fixtures/flash_layout_geometry_manifest.json
@@ -15318,8 +15318,8 @@
"pages": [
{
"page_index": 0,
- "fingerprint": "10c7003ad02f6b2a0b0ec73d8fb9842e3856f7f1077e6a672c1218453e9c6716",
- "bbox_fingerprint": "9825313d730f19b1f29a7ad60c4f194d03a905c01cb5e79234bbd338cd97ddad",
+ "fingerprint": "691868af009d91fbdc4d43127d66717d83f64405f551a4c7896ba7d704926558",
+ "bbox_fingerprint": "31bdbbc64e58ffec31bfefc9ad228d9c0738d05fffcb505f127c0e83c8174b1f",
"approved": true,
"blocks": [
{
@@ -15408,82 +15408,67 @@
"text_sha256": "ce93751a458c21be8d8216511c0429f8af22ba37ce0fa9ba142084a9d769b159"
},
{
- "id": "3fb6a95e84aebba221f0",
+ "id": "f0149b89a9f7c551d32c",
"type": "text",
- "text_sha256": "6135cb595b6a44ac0b1f83e777b9c1368e19ae7e7cc0bc2516383fcab98d79c9"
+ "text_sha256": "cd24d0f95423fb38a98b24bdebd8766a31340893f52854165072c8d81edcb2b5"
},
{
- "id": "f73bf87339147e1ef87c",
- "type": "text",
- "text_sha256": "d35691561d144fe1bc705dbac69076558ce6534cfb2d0c20ee87d218df25e6ff"
- },
- {
- "id": "15887426ff716593f86b",
- "type": "text",
- "text_sha256": "f2a30116729cecbbcbb2abf85a89913cc831b94a313b9d98ac619511e7e2e169"
- },
- {
- "id": "7e2e522eee79bd24dc32",
- "type": "text",
- "text_sha256": "c404a4505d40de06f329a449caca810b7a0270028b8dcfe55f84ecfa50110618"
- },
- {
- "id": "e3609c52c44e8881f4fb",
+ "id": "d0f1628cdd0df25c8ec3",
"type": "text",
"text_sha256": "57062170fafa9aab8a02b74224e8d7bc3916cd3095dd9322441fc464d8c05a84"
},
{
- "id": "effc3a46f93f3f6ffd51",
+ "id": "de141528f1a108d2bab0",
"type": "text",
"text_sha256": "dd579f9dca995144e66410bbfac2372f24ef8841e91da1403edf0d2b68d90c54"
},
{
- "id": "f7e7603dab3beef3d8f0",
+ "id": "f008886cb36f19777c76",
"type": "text",
"text_sha256": "0bd40e5187027c20e261b0ba8265f0cda183f52e682ef8325bfcdec5c5667ff4"
},
{
- "id": "9cfa8afb25140150657d",
+ "id": "de7e553848f43b60e710",
"type": "text",
"text_sha256": "b26cfda16bb1ca373cf38e89540cc173b1d27dc5ce947a35cf508e35355dfc70"
},
{
- "id": "517097a84ba8c2ffff5d",
+ "id": "d5dc353196caaf835150",
"type": "paragraph_title",
"text_sha256": "43a5aff3a2b04e2ecf77e8b43f58ce277864cafdec454c19c6871e2042753c86"
},
{
- "id": "f331f94651da579cd233",
+ "id": "1dda13cb55e399c0afec",
"type": "paragraph_title",
"text_sha256": "9b01688e04e6c70a9aefae712c86c27355533e7d386e3d56af317a24275e3cf2"
},
{
- "id": "ecf882ab2edd975315c3",
+ "id": "0d4fd99611fb9903fef1",
"type": "text",
"text_sha256": "0a42360909fdaad4c7c1813fc48a69a1f56af9041b6535fcbcdfef8c7b9f1396"
},
{
- "id": "fac81474a35e1a75e35c",
+ "id": "435acdd2a3669bb32006",
"type": "text",
"text_sha256": "bfe8c8675d7dc0a8092ef36834a2af3f02badf8d2a94be38aa7750eab2e8b841"
},
{
- "id": "49342749d23c81795626",
+ "id": "8f6218245404117288d5",
"type": "page_footnote",
"text_sha256": "636a4aa82543baa6ee109629389cf59fef90e87dd73479ae5b967e565663447c"
},
{
- "id": "64f577c813b5bdd15cb5",
+ "id": "f076a77c0d7ad5ef7d51",
"type": "page_footnote",
"text_sha256": "7c2894bb02606f5a375cd9d5743772ce00e971169d8250ec6a6feb16dd9c89cd"
},
{
- "id": "2088afa4f1a8f18edf80",
+ "id": "b6ccf0f923dd6ec8d7c4",
"type": "page_footnote",
"text_sha256": "fe406fb44b94810275c0c16ad5e27d0e123e1bc7a0db5dcb172b868969169c35"
},
{
- "id": "933af638553ce75d4637",
+ "id": "df4b1a1fae0c2c3b89c9",
"type": "page_footnote",
"text_sha256": "33da632aca31786f893397be4ba3bccb87571d9ed026c0f9c8af85800819e404"
}
diff --git a/tests/unittest/test_flash_pdf_samples.py b/tests/unittest/test_flash_pdf_samples.py
index 55095b49..b305cfec 100644
--- a/tests/unittest/test_flash_pdf_samples.py
+++ b/tests/unittest/test_flash_pdf_samples.py
@@ -469,7 +469,7 @@ def test_explicit_pdf_fixtures_keep_expected_txt_block_inventory() -> None:
"page_footnote": 4,
"page_number": 3,
"paragraph_title": 6,
- "text": 59,
+ "text": 56,
}
),
),
diff --git a/tests/unittest/test_flash_pdf_text_blocks.py b/tests/unittest/test_flash_pdf_text_blocks.py
index e300e92a..60c51150 100644
--- a/tests/unittest/test_flash_pdf_text_blocks.py
+++ b/tests/unittest/test_flash_pdf_text_blocks.py
@@ -18,6 +18,32 @@ from _flash_pdf_test_utils import (
)
+def _span_text_block(
+ content: str,
+ bbox: tuple[float, float, float, float],
+) -> dict[str, object]:
+ """构造全宽 span 正文回并测试使用的最小内部文本块。"""
+
+ return {
+ "type": "text",
+ "bbox": bbox,
+ "angle": 0,
+ "content": content,
+ "_visual_row_ids": set(),
+ "_single_run_row_id": None,
+ "_local_line_bboxes": [bbox],
+ "_line_heights": [4.0],
+ "_font_signatures": {("Body", 0)},
+ "_inline_math_regions": [],
+ "_lane_interval": (0.0, 100.0),
+ "_lane_is_span": True,
+ "_hard_break_before": False,
+ "_protected_hard_break_before": False,
+ "_hanging_indent_group": None,
+ "_leading_emphasis_start": False,
+ }
+
+
def test_hanging_indent_groups_neutral_entries_and_ignores_centered_heading() -> None:
"""验证不含序号的重复悬挂缩进逐条分组,且居中标题不参与条目。"""
@@ -155,6 +181,118 @@ def test_paragraph_formula_context_merges_dense_same_lane_text() -> None:
]
+def test_full_width_span_text_merges_without_terminal_punctuation_dependency() -> None:
+ """验证同栏满宽 span 正文可跨句号连续回并,并沿用英文断词恢复。"""
+
+ blocks = [
+ _span_text_block(
+ "Abstract first sentence.",
+ (0.0, 0.0, 90.0, 10.0),
+ ),
+ _span_text_block(
+ "Continuation remains in the same paragraph.",
+ (0.0, 16.0, 90.0, 26.0),
+ ),
+ _span_text_block(
+ "Another impor-",
+ (0.0, 32.0, 90.0, 42.0),
+ ),
+ _span_text_block(
+ "tant detail",
+ (0.0, 48.0, 90.0, 58.0),
+ ),
+ {
+ **_span_text_block(
+ "Keywords: separate metadata",
+ (0.0, 64.0, 90.0, 74.0),
+ ),
+ "_lane_is_span": False,
+ "_hard_break_before": True,
+ "_protected_hard_break_before": True,
+ },
+ ]
+
+ merged = text_blocks._merge_unterminated_text_components(
+ blocks,
+ )
+
+ assert [block["content"] for block in merged] == [
+ "Abstract first sentence. Continuation remains in the same paragraph. Another important detail",
+ "Keywords: separate metadata",
+ ]
+
+
+ "barrier",
+ [
+ "hard_break",
+ "protected_break",
+ "leading_emphasis",
+ "font_conflict",
+ "narrow_row",
+ "different_lane",
+ "different_angle",
+ "labelled_metadata",
+ "intervening_block",
+ "ordinary_lane_terminal",
+ ],
+)
+def test_full_width_span_text_respects_structural_barriers(
+ barrier: str,
+) -> None:
+ """验证 span 回并仍受硬边界、栏带、字体、宽度和阻隔块约束。"""
+
+ first = _span_text_block(
+ "First sentence.",
+ (0.0, 0.0, 90.0, 10.0),
+ )
+ second = _span_text_block(
+ "Second sentence",
+ (0.0, 16.0, 90.0, 26.0),
+ )
+ blocks = [first, second]
+ if barrier == "hard_break":
+ second["_hard_break_before"] = True
+ elif barrier == "protected_break":
+ second["_protected_hard_break_before"] = True
+ elif barrier == "leading_emphasis":
+ second["_leading_emphasis_start"] = True
+ elif barrier == "font_conflict":
+ second["_font_signatures"] = {("Other", 0)}
+ elif barrier == "narrow_row":
+ second["bbox"] = (0.0, 16.0, 79.0, 26.0)
+ second["_local_line_bboxes"] = [second["bbox"]]
+ elif barrier == "different_lane":
+ second["_lane_interval"] = (0.0, 120.0)
+ elif barrier == "different_angle":
+ second["angle"] = 90
+ elif barrier == "labelled_metadata":
+ second["content"] = "Keywords: separate metadata"
+ elif barrier == "intervening_block":
+ blocks.insert(
+ 1,
+ {
+ **_span_text_block(
+ "visual barrier",
+ (0.0, 11.0, 90.0, 15.0),
+ ),
+ "type": "caption",
+ },
+ )
+ elif barrier == "ordinary_lane_terminal":
+ first["_lane_is_span"] = False
+ second["_lane_is_span"] = False
+
+ merged = text_blocks._merge_unterminated_text_components(
+ blocks,
+ )
+
+ assert not any(
+ "First sentence." in str(block.get("content") or "") and "Second sentence" in str(block.get("content") or "")
+ for block in merged
+ )
+
+
def test_hanging_indent_accepts_reference_spacing_and_italic_tail() -> None:
"""验证 1.25 倍行高的参考文献间距不会拆掉前一条斜体尾行。"""