Transforms complex documents like PDFs and Office docs into LLM-ready markdown/JSON for your Agentic workflows.
Python ai4sciencedocument-analysisdocxextract-datalayout-analysisocrparserpdfpdf-converterpdf-extractor-llmpdf-extractor-pretrainpdf-extractor-ragpdf-parserpptxpythonxlsx
feat: update text merging logic to allow crossing visual root and page decoration blocks
myhloliba715156c974
diff --git a/mineru/backend/postprocess/paragraphs.py b/mineru/backend/postprocess/paragraphs.pyindex c2521f62..cdd3e0a5 100644--- a/mineru/backend/postprocess/paragraphs.py+++ b/mineru/backend/postprocess/paragraphs.py@@ -16,12 +16,17 @@ TEXT_MERGE_BARRIER_TYPES = {*SECTION_MERGE_BARRIER_TYPES,BlockType.LIST,}-# 文本段落合并只允许跨过这些视觉根块,避免 ref_text/phonetic 等语义块被当作透明块。+# 文本段落合并允许跨过视觉根块和页面装饰块,其他语义块仍会阻断候选查找。TEXT_MERGE_TRANSPARENT_TYPES = {BlockType.IMAGE,BlockType.TABLE,BlockType.CHART,BlockType.CODE,+ BlockType.HEADER,+ BlockType.FOOTER,+ BlockType.PAGE_NUMBER,+ BlockType.PAGE_FOOTNOTE,+ BlockType.ASIDE_TEXT,}VERTICAL_LINE_HEIGHT_TO_WIDTH_RATIO_THRESHOLD = 2VERTICAL_LINE_IN_BLOCK_THRESHOLD = 0.8@@ -133,7 +138,7 @@ def _find_previous_text_block(ordered_blocks: list[OrderedBlock],current_index: int,) -> OrderedBlock | None:- """向前查找 text,视觉根块可跨过,结构或其他语义块会阻断查找。"""+ """向前查找 text,视觉根块和页面装饰块可跨过,其他语义块会阻断查找。"""for previous_index in range(current_index - 1, -1, -1):previous_block = ordered_blocks[previous_index][2]previous_type = previous_block.get("type")diff --git a/tests/unittest/test_flash_pdf_samples.py b/tests/unittest/test_flash_pdf_samples.pyindex 30177585..9bb01fbf 100644--- a/tests/unittest/test_flash_pdf_samples.py+++ b/tests/unittest/test_flash_pdf_samples.py@@ -7,6 +7,7 @@ from typing import Anyimport unicodedatafrom mineru.backend.postprocess.page_blocks import process_page_blocks+from mineru.backend.postprocess.pages import model_list_to_pagesfrom mineru.model.flash import PdfModelfrom mineru.model.flash.native_pdf import (formulas,@@ -186,6 +187,13 @@ def test_demo1_keeps_five_real_tables_without_formula_false_positive() -> None:assert sum(block["type"] == "equation" for page in model_list for block in page) == 7assert sum(block["type"] == "caption" for page in model_list for block in page) == 10assert sum(block["type"] == "footnote" for page in model_list for block in page) == 5+ middle_pages = model_list_to_pages(model_list)+ page3_continuation = next(+ block+ for block in middle_pages[2].blocks+ if block.type == "text" and isinstance(block.content, str) and block.content.startswith("were to")+ )+ assert page3_continuation.continues_prev is Trueassert [block["bbox"] for block in model_list[5] if block["type"] == "table"] == [[0.087, 0.153, 0.922, 0.333],[0.087, 0.697, 0.922, 0.876],@@ -1428,6 +1436,13 @@ def test_frozen_soil_reference_tails_remain_single_text_blocks() -> None:"""验证中文论文双语图注独立标记,正文图引用和参考文献保持 text。"""model_list = _auto_model_list("中文论文.pdf")+ middle_pages = model_list_to_pages(model_list)+ page2_continuation = next(+ block+ for block in middle_pages[1].blocks+ if block.type == "text" and isinstance(block.content, str) and block.content.startswith("预测。")+ )+ assert page2_continuation.continues_prev is Truepage = model_list[8]captions = [block for page_blocks in model_list for block in page_blocks if block["type"] == "caption"]diff --git a/tests/unittest/test_postprocess_paragraphs.py b/tests/unittest/test_postprocess_paragraphs.pyindex 8a98396a..7fd1fda2 100644--- a/tests/unittest/test_postprocess_paragraphs.py+++ b/tests/unittest/test_postprocess_paragraphs.py@@ -114,6 +114,11 @@ def test_merge_para_text_blocks_requires_consecutive_cross_page_indices([(BlockType.IMAGE, True),(BlockType.CODE, True),+ (BlockType.HEADER, True),+ (BlockType.FOOTER, True),+ (BlockType.PAGE_NUMBER, True),+ (BlockType.PAGE_FOOTNOTE, True),+ (BlockType.ASIDE_TEXT, True),(BlockType.DOC_TITLE, False),(BlockType.LIST, False),(BlockType.REF_TEXT, False),@@ -123,7 +128,7 @@ def test_merge_para_text_blocks_respects_transparent_and_barrier_types(middle_type: str,expected_continuation: bool,) -> None:- """验证视觉根块可跨过,标题、列表和其他语义块会阻断 text 查找。"""+ """验证视觉根块和页面装饰块可跨过,其他语义块会阻断 text 查找。"""previous_block, current_block = _horizontal_pair()middle_block = {"index": 1,@@ -139,6 +144,70 @@ def test_merge_para_text_blocks_respects_transparent_and_barrier_types(assert current_block.get("continues_prev", False) is expected_continuation+def test_merge_para_text_blocks_ignores_cross_page_decorations() -> None:+ """验证跨页正文会跳过前页尾部和后页头部的全部页面装饰块。"""+ previous_block, current_block = _horizontal_pair()+ current_block["index"] = 3+ pages = [+ {+ "page_idx": 0,+ "blocks": [+ previous_block,+ {"index": 1, "type": BlockType.FOOTER, "content": "footer"},+ {"index": 2, "type": BlockType.PAGE_FOOTNOTE, "content": "page footnote"},+ ],+ },+ {+ "page_idx": 1,+ "blocks": [+ {"index": 0, "type": BlockType.HEADER, "content": "header"},+ {"index": 1, "type": BlockType.PAGE_NUMBER, "content": "2"},+ {"index": 2, "type": BlockType.ASIDE_TEXT, "content": "aside"},+ current_block,+ ],+ },+ ]++ merge_para_text_blocks(pages)++ assert current_block["continues_prev"] is True+ assert previous_block["content"] == "previous continuation"+ assert current_block["content"] == "current continuation"+ assert "lines" not in previous_block+ assert "lines" not in current_block+++ "barrier_type",+ [BlockType.DOC_TITLE, BlockType.PARAGRAPH_TITLE, BlockType.EQUATION, BlockType.LIST],+)+def test_merge_para_text_blocks_keeps_cross_page_semantic_barriers(barrier_type: str) -> None:+ """验证页面装饰块透明后,标题、公式和列表仍会阻断跨页正文连接。"""+ previous_block, current_block = _horizontal_pair()+ current_block["index"] = 2+ pages = [+ {+ "page_idx": 0,+ "blocks": [+ previous_block,+ {"index": 1, "type": BlockType.PAGE_FOOTNOTE, "content": "page footnote"},+ ],+ },+ {+ "page_idx": 1,+ "blocks": [+ {"index": 0, "type": BlockType.HEADER, "content": "header"},+ {"index": 1, "type": barrier_type, "content": "barrier"},+ current_block,+ ],+ },+ ]++ merge_para_text_blocks(pages)++ assert "continues_prev" not in current_block++def test_can_auto_merge_horizontal_text_blocks_rejects_paragraph_boundaries() -> None:"""验证横排规则会拒绝终止标点、特殊段首以及不满足几何条件的候选。"""previous_block, current_block = _horizontal_pair()