Skip to content

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

feat: enhance formula processing by refining nearby fragment width logic and adding tests for visual row formula handling

myhlolica709bbaecbd
diff --git a/mineru/backend/flash/native_pdf/formulas.py b/mineru/backend/flash/native_pdf/formulas.py
index 26555adc..34215c1f 100644
--- a/mineru/backend/flash/native_pdf/formulas.py
+++ b/mineru/backend/flash/native_pdf/formulas.py
@@ -788,6 +788,12 @@ def _build_split_visual_row_formula_blocks(
[bbox for _member, bbox in body_members]
)
body_width = max(0.1, body_bbox[2] - body_bbox[0])
+ # 同行成员可能只是分式尾部;窄尾部不能压低外部公式片段的宽度容差,
+ # 否则会提前认领分母、右括号和编号,使左侧公式主体落回普通文本。
+ nearby_fragment_width_limit = max(
+ 0.65 * body_width,
+ 3.0 * median_height,
+ )
member_ids = {id(member) for member in members}
has_nearby_formula_fragment = False
for other in lines:
@@ -805,7 +811,7 @@ def _build_split_visual_row_formula_blocks(
)
if (
vertical_gap <= 0.75 * median_height
- and other_bbox[2] - other_bbox[0] <= 0.65 * body_width
+ and other_bbox[2] - other_bbox[0] <= nearby_fragment_width_limit
and max(
0.0,
max(other_bbox[0], body_bbox[0])
diff --git a/tests/unittest/test_flash_pdf_formulas.py b/tests/unittest/test_flash_pdf_formulas.py
index 75fb5482..e823d360 100644
--- a/tests/unittest/test_flash_pdf_formulas.py
+++ b/tests/unittest/test_flash_pdf_formulas.py
@@ -1236,6 +1236,137 @@ def test_split_visual_row_with_right_number_forms_one_equation() -> None:
assert {line.source_index for line in remaining} == {0, 4}
+def test_split_visual_row_formula_tail_defers_to_spatial_growth() -> None:
+ """验证分式尾部同行时延后认领,并由空间锚点收齐完整公式。"""
+
+ body_font = ("Body", 0)
+ math_font = ("Math", 0)
+ italic_font = ("MathItalic", 0)
+ bracket_font = ("Bracket", 0)
+ lines = [
+ _text_line(
+ "body above 1",
+ (0.0, 0.0, 100.0, 10.0),
+ 0,
+ effective_height=10.0,
+ font_signature=body_font,
+ font_coverage=1.0,
+ ),
+ _text_line(
+ "body above 2",
+ (0.0, 12.0, 100.0, 22.0),
+ 1,
+ effective_height=10.0,
+ font_signature=body_font,
+ font_coverage=1.0,
+ ),
+ _text_line(
+ "formula intro",
+ (0.0, 36.0, 100.0, 46.0),
+ 2,
+ effective_height=10.0,
+ font_signature=body_font,
+ font_coverage=1.0,
+ ),
+ _text_line(
+ "at = 1",
+ (10.0, 63.0, 28.0, 73.0),
+ 3,
+ effective_height=10.0,
+ font_signature=italic_font,
+ font_coverage=0.5,
+ ),
+ _text_line(
+ "2 ln",
+ (30.0, 67.0, 42.0, 77.0),
+ 4,
+ effective_height=10.0,
+ font_signature=math_font,
+ font_coverage=1.0,
+ ),
+ _text_line(
+ "( 1 - εt",
+ (45.0, 52.0, 65.0, 82.0),
+ 5,
+ effective_height=10.0,
+ font_signature=bracket_font,
+ font_coverage=0.2,
+ ),
+ _text_line(
+ "εt",
+ (58.0, 72.0, 62.0, 80.0),
+ 6,
+ visual_row_id=10,
+ run_index=0,
+ split_from_row=True,
+ effective_height=8.0,
+ font_signature=italic_font,
+ font_coverage=0.5,
+ ),
+ _text_line(
+ ")",
+ (67.0, 52.0, 70.0, 82.0),
+ 7,
+ visual_row_id=10,
+ run_index=1,
+ split_from_row=True,
+ effective_height=30.0,
+ font_signature=bracket_font,
+ font_coverage=1.0,
+ ),
+ _text_line(
+ "(3)",
+ (90.0, 67.0, 100.0, 77.0),
+ 8,
+ visual_row_id=10,
+ run_index=2,
+ split_from_row=True,
+ effective_height=10.0,
+ font_signature=body_font,
+ font_coverage=0.67,
+ ),
+ _text_line(
+ "body below 1",
+ (0.0, 86.0, 100.0, 96.0),
+ 9,
+ effective_height=10.0,
+ font_signature=body_font,
+ font_coverage=1.0,
+ ),
+ _text_line(
+ "body below 2",
+ (0.0, 98.0, 100.0, 108.0),
+ 10,
+ effective_height=10.0,
+ font_signature=body_font,
+ font_coverage=1.0,
+ ),
+ ]
+
+ early_blocks, early_claimed = formulas._build_split_visual_row_formula_blocks(
+ lines,
+ [],
+ (100.0, 120.0),
+ )
+ blocks, remaining = formulas._build_formula_like_blocks(
+ lines,
+ [],
+ (100.0, 120.0),
+ )
+
+ assert early_blocks == []
+ assert early_claimed == set()
+ assert blocks == [
+ {
+ "type": "equation",
+ "bbox": (10.0, 52.0, 100.0, 82.0),
+ "angle": 0,
+ "content": "at = 1 ( 1 - εt)\n2 ln\nεt\n(3)",
+ }
+ ]
+ assert {line.source_index for line in remaining} == {0, 1, 2, 9, 10}
+
+
def test_centered_low_body_font_line_forms_unnumbered_equation() -> None:
"""验证上下正文之间居中且低正文覆盖的数学字体行形成无编号公式。"""
diff --git a/tests/unittest/test_flash_pdf_samples.py b/tests/unittest/test_flash_pdf_samples.py
index 575940a5..ed8c6a6a 100644
--- a/tests/unittest/test_flash_pdf_samples.py
+++ b/tests/unittest/test_flash_pdf_samples.py
@@ -1332,6 +1332,38 @@ def test_npu_numbered_figure_captions_are_independent_annotations() -> None:
assert not [block for page in model_list for block in page if block["type"] == "footnote"]
+def test_frozen_soil_page3_formula3_remains_one_equation() -> None:
+ """验证中文论文第三页公式(3)完整归入单个公式块。"""
+
+ page = _txt_model_list("中文论文.pdf")[2]
+ equations = [block for block in page if block["type"] == "equation"]
+ formula3 = [
+ block
+ for block in equations
+ if "(3)" in str(block.get("content", ""))
+ ]
+
+ assert len(page) == 28
+ assert len(equations) == 4
+ assert len(formula3) == 1
+ assert formula3[0]["bbox"] == [0.653, 0.703, 0.898, 0.751]
+ assert all(
+ probe in str(formula3[0]["content"])
+ for probe in ("at = 1", "2 ln", "1 - εt", "εt", "(3)")
+ )
+ assert not [
+ block
+ for block in page
+ if block["type"] == "text"
+ and any(
+ probe in str(block.get("content", ""))
+ for probe in ("at = 1", "2 ln", "1 - εt")
+ )
+ ]
+ for marker in ("(1)", "(2)", "(3)", "(4)"):
+ assert sum(marker in str(block.get("content", "")) for block in equations) == 1
+
+
def test_frozen_soil_reference_tails_remain_single_text_blocks() -> None:
"""验证中文论文双语图注独立标记,正文图引用和参考文献保持 text。"""