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: enhance analyze.py with improved block content handling and add tests for PDF index processing
myhlolif6378c60686c
diff --git a/mineru/backend/analyze.py b/mineru/backend/analyze.pyindex b4beb496..48e7a061 100644--- a/mineru/backend/analyze.py+++ b/mineru/backend/analyze.py@@ -1082,13 +1082,13 @@ def _line_content_parts(line: Line) -> list[tuple[str, str]]:def _lines_to_block_content(lines: list[Line], block_type: str) -> str:- """将真实行折叠为统一 block content,保留代码换行并处理自然语言跨行连接。"""+ """将真实行折叠为统一 block content,保留目录/代码换行并处理自然语言跨行连接。"""content_lines = [parts for line in lines if (parts := _line_content_parts(line))]if not content_lines:return ""rendered_lines = [" ".join(content for _, content in parts) for parts in content_lines]- if block_type in CODE_CONTENT_BLOCK_TYPES:+ if block_type == BlockType.INDEX or block_type in CODE_CONTENT_BLOCK_TYPES:return "\n".join(rendered_lines).strip()text_for_language = "".join(@@ -2405,8 +2405,8 @@ if __name__ == "__main__":# 根据当前文件位置定位项目根目录,读取 demo 下的 PDF 和 Office 样例。project_root = os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))demo_file_paths = [- os.path.join(project_root, "demo", "office_docs", "docx_01.docx"),- os.path.join(project_root, "demo", "pdfs", "demo1.pdf"),+ # os.path.join(project_root, "demo", "office_docs", "docx_01.docx"),+ os.path.join(project_root, "demo", "pdfs", "NPU_开发环境部署_参考指南.pdf"),]for file_path in demo_file_paths:diff --git a/tests/unittest/test_pdf_analyze_block_content.py b/tests/unittest/test_pdf_analyze_block_content.pynew file mode 100644index 00000000..1de02159--- /dev/null+++ b/tests/unittest/test_pdf_analyze_block_content.py@@ -0,0 +1,132 @@+from __future__ import annotations++from pathlib import Path++import pytest++from mineru.backend import analyze+from mineru.model.flash import PdfModel+from mineru.types import BlockType, ContentType, Line, Span+from mineru.utils.pdf_document import PDFDocument+++_PROJECT_ROOT = Path(__file__).resolve().parents[2]+_NPU_PDF_PATH = _PROJECT_ROOT / "demo" / "pdfs" / "NPU_开发环境部署_参考指南.pdf"+++def _build_text_lines(*contents: str) -> list[Line]:+ """按输入文本构造稳定的多行文本结构,供 block content 拼接测试复用。"""+ return [+ Line(+ bbox=(0.0, float(line_idx), 100.0, float(line_idx + 1)),+ spans=[+ Span(+ type=ContentType.TEXT,+ bbox=(0.0, float(line_idx), 100.0, float(line_idx + 1)),+ content=content,+ )+ ],+ )+ for line_idx, content in enumerate(contents)+ ]+++def test_empty_index_content_preserves_grouped_line_breaks() -> None:+ """验证空目录块由组行结果回填时保留每个目录项的物理换行。"""+ block = {+ "type": BlockType.INDEX,+ "bbox": [0.1, 0.2, 0.9, 0.8],+ "angle": 0,+ "content": "",+ }++ analyze._apply_block_content_and_line_metadata(+ [block],+ {0: _build_text_lines("第一行", "第二行")},+ (100.0, 100.0),+ )++ assert block == {+ "type": BlockType.INDEX,+ "bbox": [0.1, 0.2, 0.9, 0.8],+ "angle": 0,+ "content": "第一行\n第二行",+ }+++def test_text_content_keeps_natural_paragraph_joining() -> None:+ """验证普通中文正文仍沿用自然段跨行连接规则,不受目录修复影响。"""+ content = analyze._lines_to_block_content(+ _build_text_lines("第一行", "第二行"),+ BlockType.TEXT,+ )++ assert content == "第一行第二行"++[email protected]("block_type", [BlockType.CODE, BlockType.ALGORITHM])+def test_code_content_keeps_existing_line_breaks(block_type: str) -> None:+ """验证 code/algorithm 原有的逐行拼接语义保持不变。"""+ content = analyze._lines_to_block_content(+ _build_text_lines("line one", "line two"),+ block_type,+ )++ assert content == "line one\nline two"+++def test_existing_index_content_is_not_overwritten() -> None:+ """验证 VLM 等上游已经提供的目录 content 不会被 PDF/OCR 组行结果覆盖。"""+ block = {+ "type": BlockType.INDEX,+ "bbox": [0.1, 0.2, 0.9, 0.8],+ "angle": 0,+ "content": "已有目录一\n已有目录二",+ }++ analyze._apply_block_content_and_line_metadata(+ [block],+ {0: _build_text_lines("回填目录一", "回填目录二")},+ (100.0, 100.0),+ )++ assert block["content"] == "已有目录一\n已有目录二"+ assert "_lines" not in block+++def test_npu_page_three_medium_index_matches_flash_line_structure() -> None:+ """验证真实 NPU 目录页的 Medium 回填结果与 Flash 一样保留 24 个目录行。"""+ document = PDFDocument(_NPU_PDF_PATH.read_bytes())+ try:+ page = document[2]+ page_size = tuple(float(value) for value in page.size)+ index_block = {+ "type": BlockType.INDEX,+ "bbox": [0.064, 0.17, 0.943, 0.689],+ "angle": 0,+ "content": "",+ }+ block_lines = analyze._group_page_spans_by_block(+ [index_block],+ analyze._build_pdf_text_line_spans(page),+ page_size,+ {BlockType.INDEX},+ )++ assert len(block_lines[0]) == 24+ analyze._apply_block_content_and_line_metadata(+ [index_block],+ block_lines,+ page_size,+ )++ flash_index_block = next(block for block in PdfModel().predict(document)[2] if block.get("type") == BlockType.INDEX)+ assert index_block["content"] == flash_index_block["content"]+ assert len(str(index_block["content"]).splitlines()) == 24+ assert str(index_block["content"]).splitlines()[0] == "1 前言 1"+ assert str(index_block["content"]).splitlines()[-1] == "5 附录: 19"+ assert index_block["type"] == BlockType.INDEX+ assert index_block["bbox"] == [0.064, 0.17, 0.943, 0.689]+ assert index_block["angle"] == 0+ finally:+ document.close()