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 text processing by adding compact text section detection for small font candidates
myhlolia18a16129e3c
diff --git a/mineru/backend/flash/native_pdf/titles.py b/mineru/backend/flash/native_pdf/titles.pyindex 8df764f5..dd0149ef 100644--- a/mineru/backend/flash/native_pdf/titles.py+++ b/mineru/backend/flash/native_pdf/titles.py@@ -300,11 +300,28 @@ def _classify_paragraph_titles_in_lane(and not _title_fonts_compatible(rows[index - 1][0], line)):continue- if height_ratio < 0.9 and not inside_front_matter and not _has_following_body_row(- rows,- index,- lane_width,- profile,+ compact_text_section = (+ height_ratio < 0.9+ and centered+ and width_ratio <= 0.7+ and gap_above >= 0.35+ and gap_below >= 0.2+ and _has_following_compact_text_section(+ rows,+ index,+ lane_width,+ )+ )+ if (+ height_ratio < 0.9+ and not inside_front_matter+ and not _has_following_body_row(+ rows,+ index,+ lane_width,+ profile,+ )+ and not compact_text_section):continueif width_ratio >= 0.9 and height_ratio < 1.15 and not (@@ -495,6 +512,36 @@ def _has_following_body_row(return False+def _has_following_compact_text_section(+ rows: list[tuple[_LineItem, BBox]],+ index: int,+ lane_width: float,+) -> bool:+ """检查小字号候选后是否紧接三行同尺度的宽文本区段。"""++ candidate = rows[index]+ candidate_height = _line_effective_height(*candidate)+ following_rows = rows[index + 1 : index + 4]+ if len(following_rows) < 3:+ return False++ previous = candidate+ for current in following_rows:+ line, bbox = current+ height_ratio = _line_effective_height(line, bbox) / candidate_height+ width_ratio = (bbox[2] - bbox[0]) / lane_width+ gap = _effective_text_row_gap(previous, current)+ if (+ line.semantic_type is not None+ or not 0.75 <= height_ratio <= 1.25+ or width_ratio < 0.45+ or not -0.25 * candidate_height <= gap <= 0.75 * candidate_height+ ):+ return False+ previous = current+ return True++def _unify_visual_row_title_types(lane: _TextLane,selected_indices: set[int],@@ -651,4 +698,3 @@ def _expand_paragraph_title_neighbors(candidate_line.semantic_type = "paragraph_title"selected_indices.add(candidate_index)pending.append(candidate_index)-diff --git a/tests/unittest/test_flash_pdf_samples.py b/tests/unittest/test_flash_pdf_samples.pyindex db13acdc..ddaede4e 100644--- a/tests/unittest/test_flash_pdf_samples.py+++ b/tests/unittest/test_flash_pdf_samples.py@@ -203,11 +203,14 @@ def test_demo2_pages2_to6_restore_paragraphs_formulas_and_reading_order() -> Nonassert results.endswith("methods that operate on pairs of images.")assert improvements.endswith("has the effect")- page5_contents = [block["content"] for block in model_list[4]]+ page5 = model_list[4]+ page5_contents = [block["content"] for block in page5]optimal_feedback = next(content for content in page5_contents if content.startswith("The optimal value"))page5_references = [content for content in page5_contents if content.startswith("[")]+ references_title = next(block for block in page5 if block["content"] == "REFERENCES")assert "noise ranging between ±0 to ±40" in optimal_feedbackassert optimal_feedback.endswith("temporal stereo matching is used.")+ assert references_title["type"] == "paragraph_title"assert [content.partition("]")[0] + "]" for content in page5_references] == [f"[{number}]" for number in range(1, 6)]@@ -677,4 +680,3 @@ def test_demo3_pages4_and5_fix_lists_formula_titles_italics_and_footnotes() -> Nassert final_footnote["type"] == "page_footnote"assert "3By perturbation" not in final_bullet["content"]-diff --git a/tests/unittest/test_flash_pdf_titles.py b/tests/unittest/test_flash_pdf_titles.pyindex 167509ca..15e5c8e8 100644--- a/tests/unittest/test_flash_pdf_titles.py+++ b/tests/unittest/test_flash_pdf_titles.py@@ -83,6 +83,86 @@ def test_centered_smaller_paragraph_title_uses_layout_contrast_only() -> None:assert lines[2].semantic_type == "paragraph_title"+def test_centered_smaller_paragraph_title_accepts_compact_text_section() -> None:+ """验证小字号居中标题可由紧随其后的连续小字号正文区段支撑。"""++ body_font = ("Body", 0)+ lines = [+ _text_line(+ f"body {index}",+ (0.0, 12.0 * index, 100.0, 12.0 * index + 10.0),+ index,+ font_signature=body_font,+ font_coverage=1.0,+ )+ for index in range(5)+ ]+ lines.extend(+ [+ _text_line(+ "neutral label",+ (30.0, 70.0, 70.0, 78.0),+ 5,+ effective_height=8.0,+ font_signature=body_font,+ font_coverage=1.0,+ ),+ _text_line("compact row one", (0.0, 84.0, 100.0, 92.0), 6, effective_height=8.0),+ _text_line("compact row two", (0.0, 94.0, 100.0, 102.0), 7, effective_height=8.0),+ _text_line("compact row three", (0.0, 104.0, 100.0, 112.0), 8, effective_height=8.0),+ ]+ )++ titles._classify_page_titles(lines, (100.0, 140.0), page_index=1, container_bboxes=[])++ assert lines[5].semantic_type == "paragraph_title"++[email protected]("failure_mode", ["too_few", "narrow", "large_gap"])+def test_centered_smaller_paragraph_title_rejects_incomplete_compact_section(+ failure_mode: str,+) -> None:+ """验证不足三行、行宽不足或行距中断的小字号区段不能放宽标题。"""++ body_font = ("Body", 0)+ lines = [+ _text_line(+ f"body {index}",+ (0.0, 12.0 * index, 100.0, 12.0 * index + 10.0),+ index,+ font_signature=body_font,+ font_coverage=1.0,+ )+ for index in range(5)+ ]+ compact_rows = [+ _text_line("compact row one", (0.0, 84.0, 100.0, 92.0), 6, effective_height=8.0),+ _text_line("compact row two", (0.0, 94.0, 100.0, 102.0), 7, effective_height=8.0),+ _text_line("compact row three", (0.0, 104.0, 100.0, 112.0), 8, effective_height=8.0),+ ]+ if failure_mode == "too_few":+ compact_rows.pop()+ elif failure_mode == "narrow":+ compact_rows[-1].bbox = (0.0, 104.0, 40.0, 112.0)+ else:+ compact_rows[-1].bbox = (0.0, 114.0, 100.0, 122.0)+ lines.append(+ _text_line(+ "neutral label",+ (30.0, 70.0, 70.0, 78.0),+ 5,+ effective_height=8.0,+ font_signature=body_font,+ font_coverage=1.0,+ )+ )+ lines.extend(compact_rows)++ titles._classify_page_titles(lines, (100.0, 140.0), page_index=1, container_bboxes=[])++ assert lines[5].semantic_type is None++def test_multiline_document_title_does_not_absorb_author_line() -> None:"""验证首页两行大字号标题合并为文档标题,较小作者行保持普通文本。"""@@ -472,6 +552,7 @@ def test_paragraph_title_detector_does_not_read_line_text() -> None:titles._is_near_full_mixed_inline_row,titles._is_full_width_inline_heading,titles._has_following_body_row,+ titles._has_following_compact_text_section,titles._unify_visual_row_title_types,titles._protect_front_matter_title_types,titles._infer_front_matter_boundary,@@ -484,4 +565,3 @@ def test_paragraph_title_detector_does_not_read_line_text() -> None:assert ".text" not in source-