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
fix: promote non-initial document titles from confirmed paragraph title candidates
myhlolid4520571ca24
diff --git a/mineru/model/flash/pdf/pipeline.py b/mineru/model/flash/pdf/pipeline.pyindex b10769a0..0c4fdd8c 100644--- a/mineru/model/flash/pdf/pipeline.py+++ b/mineru/model/flash/pdf/pipeline.py@@ -101,11 +101,11 @@ from .auxiliary_text import (from .titles import (_classify_body_height_section_titles,_classify_explicit_section_titles,- _classify_secondary_document_title_bands,_classify_document_structural_titles,_classify_page_titles,_infer_document_body_profile,_infer_document_title_profile,+ _promote_noninitial_document_title_band,)from .text_blocks import (_build_text_blocks,@@ -381,14 +381,11 @@ def _analyze_native_document(legacy_body_profile=document_body_profile,document_title_profile=document_title_profile,)- _classify_secondary_document_title_bands(- prepared_pages,- canonical_body_profile,- )finalized_pages = [_finalize_prepared_page(prepared,page_index,+ canonical_body_profile=canonical_body_profile,document_body_profile=document_body_profile,document_title_profile=document_title_profile,)@@ -723,6 +720,7 @@ def _finalize_prepared_page(prepared: _PreparedPage,page_index: int,*,+ canonical_body_profile: _DocumentBodyProfile | None = None,document_body_profile: _DocumentBodyProfile | None = None,document_title_profile: _DocumentTitleProfile | None = None,) -> list[dict[str, Any]]:@@ -811,6 +809,16 @@ def _finalize_prepared_page(document_body_profile=document_body_profile,document_title_profile=document_title_profile,)+ _promote_noninitial_document_title_band(+ remaining_lines,+ prepared.page_size,+ page_index=page_index,+ container_bboxes=title_container_bboxes,+ document_body_profile=(canonical_body_profile or document_body_profile),+ title_candidate_source_indices={+ line.source_index for line in remaining_lines if line.semantic_type == "paragraph_title"+ },+ )remaining_lines = _merge_title_resolved_visual_rows(remaining_lines,prepared.page_size,diff --git a/mineru/model/flash/pdf/titles.py b/mineru/model/flash/pdf/titles.pyindex ea6a1b21..0ecd1636 100644--- a/mineru/model/flash/pdf/titles.py+++ b/mineru/model/flash/pdf/titles.py@@ -483,13 +483,6 @@ def _classify_page_titles()if document_body_profile is not None and document_body_profile.has_style_scale_repairs:_demote_non_structural_anomaly_titles(line_geometry)- if page_index == 0:- _demote_front_matter_non_document_titles(- line_geometry,- local_page_height,- document_title_bottom,- document_body_profile=document_body_profile,- )def _demote_non_structural_anomaly_titles(@@ -716,161 +709,153 @@ def _classify_document_structural_titles(line.style_scale_repaired = True-def _classify_secondary_document_title_bands(- prepared_pages: list[_PreparedPage],+def _promote_noninitial_document_title_band(+ lines: list[_LineItem],+ page_size: tuple[float, float],+ *,+ page_index: int,+ container_bboxes: list[BBox],document_body_profile: _DocumentBodyProfile | None,+ title_candidate_source_indices: set[int],) -> None:- """用标题、作者、单位和宽摘要的连续版式识别非首页双语标题带。"""+ """把非首页中已确认且显著大于正文的最强段落标题带升为文档标题。"""- if document_body_profile is None:+ if page_index == 0 or document_body_profile is None:returnbody_height = max(0.1, document_body_profile.body_height)- for prepared in prepared_pages[1:]:- for angle in sorted(- {line.angle for line in prepared.remaining_lines if line.semantic_type in {None, "paragraph_title"}}- ):- geometry = sorted(- [- (- line,- _rotate_bbox_to_upright(- line.bbox,- prepared.page_size,- angle,- ),- )- for line in prepared.remaining_lines- if line.angle == angle and line.semantic_type in {None, "paragraph_title"}- ],- key=lambda item: (- item[1][1],- item[1][0],- item[0].source_index,- ),+ page_candidates: list[+ tuple[+ tuple[float, float, int, float],+ list[tuple[_LineItem, BBox]],+ ]+ ] = []+ for angle in sorted(+ {+ line.angle+ for line in lines+ if line.source_index in title_candidate_source_indices+ and line.semantic_type in {None, "paragraph_title"}+ and not line.title_suppressed+ }+ ):+ geometry = sorted(+ [+ (+ line,+ _rotate_bbox_to_upright(+ line.bbox,+ page_size,+ angle,+ ),+ )+ for line in lines+ if line.angle == angle+ and line.source_index in title_candidate_source_indices+ and line.semantic_type in {None, "paragraph_title"}+ and not line.title_suppressed+ ],+ key=lambda item: (+ item[1][1],+ item[1][0],+ item[0].source_index,+ ),+ )+ if not geometry:+ continue+ local_page_width = page_size[1] if angle in {90, 270} else page_size[0]+ local_page_height = page_size[0] if angle in {90, 270} else page_size[1]+ local_containers = [+ _rotate_bbox_to_upright(+ bbox,+ page_size,+ angle,)- if len(geometry) < 5:+ for bbox in container_bboxes+ ]+ for index, (line, bbox) in enumerate(geometry):+ line_scale = _line_effective_height(line, bbox)+ width_ratio = (bbox[2] - bbox[0]) / max(+ 0.1,+ local_page_width,+ )+ centered = abs(_bbox_center_x(bbox) - 0.5 * local_page_width) <= 0.08 * local_page_width+ if (+ line_scale < 1.25 * body_height+ or not 0.45 <= width_ratio <= 0.85+ or not centered+ or not 0.12 * local_page_height <= _bbox_center_y(bbox) <= 0.75 * local_page_height+ or _line_inside_visual_container(+ bbox,+ local_containers,+ )+ ):continue- local_page_width = prepared.page_size[1] if angle in {90, 270} else prepared.page_size[0]- local_page_height = prepared.page_size[0] if angle in {90, 270} else prepared.page_size[1]- local_containers = [- _rotate_bbox_to_upright(- block["bbox"],- prepared.page_size,- angle,++ title_members = [(line, bbox)]+ cursor = index + 1+ while cursor < len(geometry):+ candidate_line, candidate_bbox = geometry[cursor]+ candidate_scale = _line_effective_height(+ candidate_line,+ candidate_bbox,)- for block in prepared.fixed_blocks- if isinstance(block.get("bbox"), (list, tuple))- ]- consumed_sources: set[int] = set()- for index, (line, bbox) in enumerate(geometry):- if line.source_index in consumed_sources:- continue- line_scale = _line_effective_height(line, bbox)- width_ratio = (bbox[2] - bbox[0]) / max(- 0.1,- local_page_width,+ vertical_gap = max(+ 0.0,+ candidate_bbox[1] - title_members[-1][1][3],)- centered = abs(_bbox_center_x(bbox) - 0.5 * local_page_width) <= 0.08 * local_page_widthif (- line_scale < 1.2 * body_height- or not 0.45 <= width_ratio <= 0.85- or not centered- or not 0.12 * local_page_height <= _bbox_center_y(bbox) <= 0.75 * local_page_height+ candidate_scale < 1.2 * body_height+ or vertical_gap+ > 1.5+ * max(+ line_scale,+ candidate_scale,+ )+ or abs(_bbox_center_x(candidate_bbox) - 0.5 * local_page_width) > 0.1 * local_page_width+ or candidate_bbox[2] - candidate_bbox[0] > 0.85 * local_page_widthor _line_inside_visual_container(- bbox,+ candidate_bbox,local_containers,)- ):- continue-- title_members = [(line, bbox)]- cursor = index + 1- while cursor < len(geometry):- candidate_line, candidate_bbox = geometry[cursor]- candidate_scale = _line_effective_height(+ or not _title_fonts_compatible(+ title_members[-1][0],candidate_line,- candidate_bbox,- )- vertical_gap = max(- 0.0,- candidate_bbox[1] - title_members[-1][1][3],- )- if (- candidate_scale < 1.15 * body_height- or vertical_gap- > 1.5- * max(- line_scale,- candidate_scale,- )- or abs(_bbox_center_x(candidate_bbox) - 0.5 * local_page_width) > 0.1 * local_page_width- or candidate_bbox[2] - candidate_bbox[0] > 0.85 * local_page_width- or not _title_fonts_compatible(- title_members[-1][0],- candidate_line,- )- ):- break- title_members.append(- (candidate_line, candidate_bbox),)- cursor += 1+ ):+ break+ title_members.append(+ (candidate_line, candidate_bbox),+ )+ cursor += 1- followers = geometry[cursor:]- if len(followers) < 3:- continue- matched_metadata: list[tuple[_LineItem, BBox]] | None = None- for metadata_count in (2, 3):- if len(followers) <= metadata_count:- continue- metadata = followers[:metadata_count]- if any(- not 0.2 * local_page_width <= metadata_bbox[2] - metadata_bbox[0] <= 0.7 * local_page_width- or abs(_bbox_center_x(metadata_bbox) - 0.5 * local_page_width) > 0.12 * local_page_width- or _line_effective_height(- metadata_line,- metadata_bbox,- )- > line_scale- for metadata_line, metadata_bbox in metadata- ):- continue- metadata_gaps = [- max(- 0.0,- current[1][1] - previous[1][3],- )- for previous, current in zip(- metadata,- metadata[1:],- )- ]- _abstract_line, abstract_bbox = followers[metadata_count]- title_to_metadata_gap = max(- 0.0,- metadata[0][1][1] - title_members[-1][1][3],- )- abstract_gap = max(- 0.0,- abstract_bbox[1] - metadata[-1][1][3],- )- if (- title_to_metadata_gap <= 4.5 * body_height- and all(gap <= 2.0 * body_height for gap in metadata_gaps)- and abstract_gap <= 4.0 * body_height- and abstract_bbox[2] - abstract_bbox[0] >= 0.7 * local_page_width- and abstract_bbox[0] <= 0.15 * local_page_width- and abstract_bbox[2] >= 0.85 * local_page_width- ):- matched_metadata = metadata- break- if matched_metadata is None:- continue- for title_line, _title_bbox in title_members:- title_line.semantic_type = "doc_title"- consumed_sources.add(title_line.source_index)- for metadata_line, _metadata_bbox in matched_metadata:- metadata_line.title_suppressed = True+ title_scales = [+ _line_effective_height(title_line, title_bbox)+ for title_line, title_bbox in title_members+ ]+ title_bbox = _bbox_union_many(+ [member_bbox for _member_line, member_bbox in title_members],+ )+ page_candidates.append(+ (+ (+ statistics.median(title_scales) / body_height,+ sum(member_bbox[2] - member_bbox[0] for _member_line, member_bbox in title_members)+ / local_page_width,+ len(title_members),+ -_bbox_center_y(title_bbox) / local_page_height,+ ),+ title_members,+ )+ )++ if not page_candidates:+ return+ _score, title_members = max(+ page_candidates,+ key=lambda item: item[0],+ )+ for title_line, _title_bbox in title_members:+ title_line.semantic_type = "doc_title"def _canonical_title_style_key(@@ -1875,26 +1860,6 @@ def _classify_additional_document_title_bands(return max(selected_bottoms, default=document_title_bottom)-def _demote_front_matter_non_document_titles(- line_geometry: list[tuple[_LineItem, BBox]],- local_page_height: float,- document_title_bottom: float | None,- *,- document_body_profile: _DocumentBodyProfile | None,-) -> None:- """把首页最后标题带后的作者与单位行降回正文。"""-- if document_title_bottom is None or document_body_profile is None or not document_body_profile.has_style_scale_repairs:- return- boundary = max(- document_title_bottom + 2.5 * document_body_profile.body_height,- 0.46 * local_page_height,- )- for line, bbox in line_geometry:- if line.semantic_type == "paragraph_title" and _bbox_center_y(bbox) <= boundary:- line.semantic_type = None--def _classify_cross_lane_centered_section_titles(line_geometry: list[tuple[_LineItem, BBox]],lanes: list[_TextLane],diff --git a/tests/fixtures/flash_layout_block_expectations.json b/tests/fixtures/flash_layout_block_expectations.jsonindex a94db921..b384dcbf 100644--- a/tests/fixtures/flash_layout_block_expectations.json+++ b/tests/fixtures/flash_layout_block_expectations.json@@ -289,9 +289,9 @@"header": 19,"image": 5,"page_footnote": 1,- "paragraph_title": 13,+ "paragraph_title": 14,"table": 5,- "text": 46+ "text": 45},"exact_typed_text": [{"page_index": 0, "type": "paragraph_title", "text": "引言"},@@ -314,6 +314,7 @@{"page_index": 3, "type": "paragraph_title", "text": "2.5逆温垂直结构特征分析"},{"page_index": 3, "type": "paragraph_title", "text": "3结论与讨论"},{"page_index": 4, "type": "caption", "text": "表519:00多层逆温次数分布"},+ {"page_index": 4, "type": "paragraph_title", "text": "参考文献"},{"page_index": 4, "type": "doc_title", "text": "CharacteristicAnalysisofTemperatureInversioninLower-LevelAtmosphereoverChongqingBasedonL-BandRadarData"}],"same_block_groups": [diff --git a/tests/unittest/test_flash_pdf_samples.py b/tests/unittest/test_flash_pdf_samples.pyindex f66353a0..c8feb52d 100644--- a/tests/unittest/test_flash_pdf_samples.py+++ b/tests/unittest/test_flash_pdf_samples.py@@ -482,9 +482,9 @@ def test_explicit_pdf_fixtures_keep_expected_txt_block_inventory() -> None:"header": 19,"image": 5,"page_footnote": 1,- "paragraph_title": 13,+ "paragraph_title": 14,"table": 5,- "text": 46,+ "text": 45,}),),diff --git a/tests/unittest/test_flash_pdf_titles.py b/tests/unittest/test_flash_pdf_titles.pyindex d0b386e1..64a43b65 100644--- a/tests/unittest/test_flash_pdf_titles.py+++ b/tests/unittest/test_flash_pdf_titles.py@@ -124,8 +124,8 @@ def test_document_regular_fonts_only_use_body_height_band() -> None:assert profile.regular_fonts == frozenset({body_font})-def test_secondary_document_title_requires_author_affiliation_and_wide_body() -> None:- """验证非首页大标题只有在作者、单位和宽摘要结构完整时才成为 doc_title。"""+def test_noninitial_document_title_requires_paragraph_title_candidate() -> None:+ """验证非首页文档标题只能从已确认的段落标题候选升档。"""title_font = ("Title", 0)body_font = ("Body", 0)@@ -145,6 +145,8 @@ def test_secondary_document_title_requires_author_affiliation_and_wide_body() ->font_signature=title_font,font_coverage=1.0,)+ title_first.semantic_type = "paragraph_title"+ title_second.semantic_type = "paragraph_title"author = _text_line("Author One, Author Two",(20.0, 65.0, 80.0, 75.0),@@ -169,11 +171,90 @@ def test_secondary_document_title_requires_author_affiliation_and_wide_body() ->font_signature=body_font,font_coverage=1.0,)+ profile = titles._DocumentBodyProfile(+ body_height=10.0,+ body_weight=400.0,+ regular_fonts=frozenset({body_font}),+ )++ titles._promote_noninitial_document_title_band(+ [title_first, title_second, author, affiliation, abstract],+ (100.0, 150.0),+ page_index=1,+ container_bboxes=[],+ document_body_profile=profile,+ title_candidate_source_indices={0, 1},+ )++ assert title_first.semantic_type == "doc_title"+ assert title_second.semantic_type == "doc_title"+ assert all(line.semantic_type is None for line in (author, affiliation, abstract))++ ordinary_large_line = _text_line(+ "large centered non-title",+ (15.0, 20.0, 85.0, 38.0),+ 10,+ effective_height=18.0,+ font_signature=title_font,+ font_coverage=1.0,+ )+ titles._promote_noninitial_document_title_band(+ [ordinary_large_line],+ (100.0, 150.0),+ page_index=2,+ container_bboxes=[],+ document_body_profile=profile,+ title_candidate_source_indices=set(),+ )+ assert ordinary_large_line.semantic_type is None+++def test_noninitial_document_titles_support_multiple_articles_without_metadata() -> None:+ """验证杂志中的多篇文章可在各自起始页仅凭稳定标题版式成为 doc_title。"""++ title_font = ("Title", 0)+ body_font = ("Body", 0)+ first_article_title = _text_line(+ "First article title",+ (15.0, 20.0, 85.0, 36.0),+ 0,+ effective_height=16.0,+ font_signature=title_font,+ font_coverage=1.0,+ )+ first_article_title_tail = _text_line(+ "continued title",+ (20.0, 37.0, 80.0, 53.0),+ 1,+ effective_height=16.0,+ font_signature=title_font,+ font_coverage=1.0,+ )+ second_article_title = _text_line(+ "Second article title",+ (18.0, 28.0, 82.0, 44.0),+ 0,+ effective_height=16.0,+ font_signature=title_font,+ font_coverage=1.0,+ )+ later_section = _text_line(+ "Centered section heading",+ (25.0, 85.0, 75.0, 98.0),+ 2,+ effective_height=13.0,+ font_signature=title_font,+ font_coverage=1.0,+ )+ first_article_title.semantic_type = "paragraph_title"+ first_article_title_tail.semantic_type = "paragraph_title"+ second_article_title.semantic_type = "paragraph_title"+ later_section.semantic_type = "paragraph_title"pages = [_prepared_text_page(_text_line(- "ordinary first page body",- (0.0, 20.0, 100.0, 30.0),+ "magazine cover",+ (5.0, 20.0, 95.0, 30.0),0,effective_height=10.0,font_signature=body_font,@@ -182,11 +263,29 @@ def test_secondary_document_title_requires_author_affiliation_and_wide_body() ->page_size=(100.0, 150.0),),_prepared_text_page(- title_first,- title_second,- author,- affiliation,- abstract,+ first_article_title,+ first_article_title_tail,+ _text_line(+ "first article body",+ (5.0, 70.0, 95.0, 80.0),+ 2,+ effective_height=10.0,+ font_signature=body_font,+ font_coverage=1.0,+ ),+ page_size=(100.0, 150.0),+ ),+ _prepared_text_page(+ second_article_title,+ _text_line(+ "second article body",+ (5.0, 55.0, 95.0, 65.0),+ 1,+ effective_height=10.0,+ font_signature=body_font,+ font_coverage=1.0,+ ),+ later_section,page_size=(100.0, 150.0),),]@@ -196,14 +295,27 @@ def test_secondary_document_title_requires_author_affiliation_and_wide_body() ->regular_fonts=frozenset({body_font}),)- titles._classify_secondary_document_title_bands(- pages,- profile,+ titles._promote_noninitial_document_title_band(+ pages[1].remaining_lines,+ pages[1].page_size,+ page_index=1,+ container_bboxes=[],+ document_body_profile=profile,+ title_candidate_source_indices={0, 1},+ )+ titles._promote_noninitial_document_title_band(+ pages[2].remaining_lines,+ pages[2].page_size,+ page_index=2,+ container_bboxes=[],+ document_body_profile=profile,+ title_candidate_source_indices={0, 2},)- assert title_first.semantic_type == "doc_title"- assert title_second.semantic_type == "doc_title"- assert all(line.semantic_type is None for line in (author, affiliation, abstract))+ assert first_article_title.semantic_type == "doc_title"+ assert first_article_title_tail.semantic_type == "doc_title"+ assert second_article_title.semantic_type == "doc_title"+ assert later_section.semantic_type == "paragraph_title"def test_document_body_profile_prefers_width_support_over_footer_page_count() -> None: