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 refining font compatibility checks and improving lane continuation logic
myhlolie0e34a3731b1
diff --git a/mineru/backend/flash/pdf_extractor.py b/mineru/backend/flash/pdf_extractor.pyindex 9aeac1dc..2790c533 100644--- a/mineru/backend/flash/pdf_extractor.py+++ b/mineru/backend/flash/pdf_extractor.py@@ -1425,7 +1425,7 @@ def _is_full_width_inline_heading(return (next_line.semantic_type is Noneand next_uses_body_font- and 0.85 <= next_height / profile.body_height <= 1.15+ and 0.75 <= next_height / profile.body_height <= 1.15and (next_bbox[2] - next_bbox[0]) >= 0.75 * lane_widthand _effective_text_row_gap(rows[index], rows[index + 1])<= profile.regular_gap + 0.25 * profile.body_height@@ -1561,15 +1561,23 @@ def _line_near_visual_container(def _title_fonts_compatible(first: _LineItem, second: _LineItem) -> bool:- """检查两行标题字体是否兼容;缺少高覆盖字体信息时允许几何兜底。"""+ """检查标题字体和字重是否兼容;低字体覆盖率时仍保留可靠字重证据。"""- return not (+ font_conflicts = (first.font_signature is not Noneand second.font_signature is not Noneand first.font_coverage >= 0.75and second.font_coverage >= 0.75and first.font_signature != second.font_signature)+ weight_conflicts = (+ first.dominant_font_weight is not None+ and second.dominant_font_weight is not None+ and abs(first.dominant_font_weight - second.dominant_font_weight) >= 100.0+ and max(first.dominant_font_weight, second.dominant_font_weight)+ >= 1.15 * min(first.dominant_font_weight, second.dominant_font_weight)+ )+ return not (font_conflicts or weight_conflicts)def _expand_paragraph_title_neighbors(@@ -4009,7 +4017,7 @@ def _build_hanging_indent_group_map("""检查相邻行的净空和几何障碍是否允许组成同一缩进序列。"""effective_gap = _effective_text_row_gap(previous, current)- if not -0.6 * median_height <= effective_gap <= 1.2 * median_height:+ if not -0.6 * median_height <= effective_gap <= 1.3 * median_height:return Falseif _connection_crosses_table(previous[0].bbox,@@ -4387,9 +4395,107 @@ def _infer_text_lanes(is_span=True,))+ _reattach_span_lane_continuations(lanes, median_height)return lanes+def _reattach_span_lane_continuations(+ lanes: list[_TextLane],+ median_height: float,+) -> None:+ """把紧随稳定跨栏多行之后的单栏宽短尾行迁回对应 span lane。"""++ regular_lanes = [lane for lane in lanes if not lane.is_span]+ span_lanes = [lane for lane in lanes if lane.is_span]+ if len(regular_lanes) < 2 or not span_lanes:+ return++ for span_lane in span_lanes:+ span_lane.lines.sort(+ key=lambda item: (item[1][1], item[1][0], item[0].source_index)+ )+ if len(span_lane.lines) < 2:+ continue+ previous, last = span_lane.lines[-2:]+ previous_height = _line_effective_height(*previous)+ last_height = _line_effective_height(*last)+ if (+ previous[0].semantic_type != last[0].semantic_type+ or abs(previous[1][0] - last[1][0]) > 0.75 * median_height+ or max(previous_height, last_height) / min(previous_height, last_height) > 1.35+ or not _title_fonts_compatible(previous[0], last[0])+ or not -0.25 * median_height+ <= _effective_text_row_gap(previous, last)+ <= 0.75 * median_height+ ):+ continue++ while True:+ candidates: list[+ tuple[+ float,+ float,+ _TextLane,+ tuple[_LineItem, BBox],+ ]+ ] = []+ for regular_lane in regular_lanes:+ for candidate in regular_lane.lines:+ candidate_line, candidate_bbox = candidate+ if (+ candidate_line.semantic_type != last[0].semantic_type+ or candidate_bbox[1] <= last[1][1]+ ):+ continue+ gap = _effective_text_row_gap(last, candidate)+ candidate_height = _line_effective_height(*candidate)+ if (+ not -0.25 * median_height <= gap <= 0.75 * median_height+ or abs(candidate_bbox[0] - last[1][0]) > 0.75 * median_height+ or max(last_height, candidate_height)+ / min(last_height, candidate_height)+ > 1.35+ or not _title_fonts_compatible(last[0], candidate_line)+ ):+ continue+ has_parallel_peer = any(+ other_line is not candidate_line+ and _bbox_axis_overlap_ratio(+ candidate_bbox,+ other_bbox,+ axis="y",+ )+ >= 0.5+ for lane in regular_lanes+ for other_line, other_bbox in lane.lines+ )+ if has_parallel_peer:+ continue+ candidates.append(+ (+ max(0.0, gap),+ candidate_bbox[1],+ regular_lane,+ candidate,+ )+ )+ if not candidates:+ break+ _gap, _top, regular_lane, candidate = min(+ candidates,+ key=lambda item: (item[0], item[1]),+ )+ regular_lane.lines.remove(candidate)+ span_lane.lines.append(candidate)+ span_lane.left = min(span_lane.left, candidate[1][0])+ span_lane.right = max(span_lane.right, candidate[1][2])+ span_lane.lines.sort(+ key=lambda item: (item[1][1], item[1][0], item[0].source_index)+ )+ last = candidate+ last_height = _line_effective_height(*last)++def _estimate_lane_gap(lane: _TextLane) -> tuple[float, float]:"""从栏带内兼容相邻行的较小间隙簇估计常规净空和 MAD。"""@@ -4493,9 +4599,15 @@ def _should_connect_text_rows(if vertical_gap > gap_limit:return False- next_indent = current_bbox[0] - lane.left- previous_fill = max(0.0, previous_bbox[2] - lane.left) / lane_widthterminal_previous = bool(re.search(r"[.!?。!?::;;][\]\)})】》”’'\"]*$", previous_line.text.rstrip()))+ if terminal_previous and vertical_gap > regular_gap + 0.5 * pair_height:+ return False++ # 局部版心可能比整栏推断边界更靠左,缩进需同时参考上一物理行。+ local_lane_left = min(lane.left, previous_bbox[0])+ local_lane_width = max(0.1, lane.right - local_lane_left)+ next_indent = current_bbox[0] - local_lane_left+ previous_fill = max(0.0, previous_bbox[2] - local_lane_left) / local_lane_widthif next_indent >= max(5.0, 0.65 * pair_height) and (previous_fill <= 0.8 or terminal_previous):return Falsediff --git a/tests/unittest/test_flash_pdf_extractor.py b/tests/unittest/test_flash_pdf_extractor.pyindex dbbbfe2e..a22bc839 100644--- a/tests/unittest/test_flash_pdf_extractor.py+++ b/tests/unittest/test_flash_pdf_extractor.py@@ -1029,6 +1029,79 @@ def test_complete_visual_row_promotes_number_and_demotes_inline_body() -> None:assert inline_row.text == "inline label inline body"+def test_low_coverage_mixed_weight_visual_row_demotes_inline_title() -> None:+ """验证低字体覆盖率的同一视觉行仍按字重冲突识别为行内粗体正文。"""++ body_font = ("Body", 0)+ lines = [+ _text_line(+ "body one",+ (0.0, 0.0, 100.0, 10.0),+ 0,+ font_signature=body_font,+ font_coverage=1.0,+ dominant_font_weight=400.0,+ ),+ _text_line(+ "body two",+ (0.0, 12.0, 100.0, 22.0),+ 1,+ font_signature=body_font,+ font_coverage=1.0,+ dominant_font_weight=400.0,+ ),+ _text_line(+ "body three",+ (0.0, 24.0, 100.0, 34.0),+ 2,+ font_signature=body_font,+ font_coverage=1.0,+ dominant_font_weight=400.0,+ ),+ _text_line(+ "inline label",+ (0.0, 50.0, 35.0, 62.0),+ 3,+ visual_row_id=30,+ split_from_row=True,+ font_signature=("Heading", 0),+ font_coverage=0.6,+ dominant_font_weight=700.0,+ ),+ _text_line(+ "inline body",+ (37.0, 50.0, 100.0, 62.0),+ 4,+ visual_row_id=30,+ run_index=1,+ split_from_row=True,+ font_signature=body_font,+ font_coverage=0.6,+ dominant_font_weight=400.0,+ ),+ _text_line(+ "body continuation",+ (0.0, 64.0, 100.0, 74.0),+ 5,+ font_signature=body_font,+ font_coverage=1.0,+ dominant_font_weight=400.0,+ ),+ ]++ pdf_extractor._classify_page_titles(+ lines,+ (100.0, 100.0),+ page_index=1,+ container_bboxes=[],+ )+ merged = pdf_extractor._merge_title_resolved_visual_rows(lines, (100.0, 100.0))++ inline_row = next(line for line in merged if line.visual_row_id == 30)+ assert inline_row.semantic_type is None+ assert inline_row.text == "inline label inline body"++def test_full_width_normal_height_inline_heading_merges_with_body_continuation() -> None:"""验证满栏正常字号粗体行降为正文,并只与其下方常规正文续接。"""@@ -1045,7 +1118,14 @@ def test_full_width_normal_height_inline_heading_merges_with_body_continuation()font_coverage=1.0,dominant_font_weight=700.0,),- _text_line("continuation one", (0.0, 52.0, 100.0, 62.0), 3, font_signature=body_font, font_coverage=1.0),+ _text_line(+ "continuation one",+ (0.0, 52.0, 100.0, 62.0),+ 3,+ effective_height=8.0,+ font_signature=body_font,+ font_coverage=1.0,+ ),_text_line("continuation two", (0.0, 64.0, 100.0, 74.0), 4, font_signature=body_font, font_coverage=1.0),_text_line("continuation three", (0.0, 76.0, 100.0, 86.0), 5, font_signature=body_font, font_coverage=1.0),]@@ -1249,6 +1329,78 @@ def test_hanging_indent_groups_neutral_entries_and_ignores_centered_heading() ->]+def test_hanging_indent_accepts_reference_spacing_and_italic_tail() -> None:+ """验证 1.25 倍行高的参考文献间距不会拆掉前一条斜体尾行。"""++ body_font = ("Body", 0)+ lines = [+ _text_line(+ "Alpha begins",+ (0.0, 0.0, 100.0, 10.0),+ 0,+ font_signature=body_font,+ font_coverage=1.0,+ ),+ _text_line(+ "alpha continues",+ (15.0, 10.0, 100.0, 20.0),+ 1,+ font_signature=body_font,+ font_coverage=1.0,+ ),+ _text_line(+ "alpha italic tail",+ (15.0, 20.0, 75.0, 30.0),+ 2,+ font_signature=("BodyItalic", 1),+ font_coverage=1.0,+ ),+ _text_line(+ "Beta begins",+ (0.0, 42.5, 100.0, 52.5),+ 3,+ font_signature=body_font,+ font_coverage=1.0,+ ),+ _text_line(+ "beta continues",+ (15.0, 52.5, 100.0, 62.5),+ 4,+ font_signature=body_font,+ font_coverage=1.0,+ ),+ _text_line(+ "Gamma begins",+ (0.0, 75.0, 100.0, 85.0),+ 5,+ font_signature=body_font,+ font_coverage=1.0,+ ),+ _text_line(+ "gamma continues",+ (15.0, 85.0, 100.0, 95.0),+ 6,+ font_signature=body_font,+ font_coverage=1.0,+ ),+ ]+ lane = pdf_extractor._TextLane(+ left=0.0,+ right=100.0,+ lines=[(line, line.bbox) for line in lines],+ )++ group_map = pdf_extractor._build_hanging_indent_group_map(lane, [], [])+ blocks = pdf_extractor._build_text_blocks(lines, [], (120.0, 120.0))++ assert [group_map[index] for index in range(7)] == [0, 0, 0, 1, 1, 2, 2]+ assert [block["content"] for block in blocks] == [+ "Alpha begins alpha continues alpha italic tail",+ "Beta begins beta continues",+ "Gamma begins gamma continues",+ ]++def test_first_line_indent_and_large_gap_do_not_form_hanging_indent_groups() -> None:"""验证普通首行缩进及跨越大间距的行不会误触发悬挂缩进模式。"""@@ -1299,12 +1451,12 @@ def test_twelve_point_gutter_keeps_two_text_lanes_and_paragraphs_separate() -> Ndef test_cross_column_caption_tail_stays_in_span_lane_and_one_text_block() -> None:- """验证同时覆盖双栏的短 caption 尾行不会按中心点落回左栏。"""+ """验证仅占单栏宽的短 caption 尾行仍回收到连续跨栏 caption。"""lines: list[pdf_extractor._LineItem] = [_text_line("caption line one", (0.0, 10.0, 200.0, 20.0), 0),_text_line("caption line two", (0.0, 22.0, 200.0, 32.0), 1),- _text_line("caption tail", (0.0, 34.0, 160.0, 44.0), 2),+ _text_line("caption tail", (0.0, 34.0, 70.0, 44.0), 2),_text_line("ordinary left short line", (0.0, 70.0, 70.0, 80.0), 3),]for row_index, top in enumerate((100.0, 112.0, 124.0, 136.0, 148.0)):@@ -1372,6 +1524,58 @@ def test_slight_bbox_overlap_contributes_to_gap_estimate_and_separates_caption())+def test_local_previous_left_edge_exposes_first_line_indent() -> None:+ """验证局部版心左移后仍能识别下一行相对前一物理行的首行缩进。"""++ previous = _text_line(+ "previous paragraph without punctuation",+ (0.0, 0.0, 80.0, 10.0),+ 0,+ )+ current = _text_line(+ "Indented new paragraph",+ (10.0, 12.0, 100.0, 22.0),+ 1,+ )+ lane = pdf_extractor._TextLane(+ left=20.0,+ right=100.0,+ lines=[(previous, previous.bbox), (current, current.bbox)],+ )++ assert not pdf_extractor._should_connect_text_rows(+ (previous, previous.bbox),+ (current, current.bbox),+ lane,+ 2.0,+ 0.0,+ [],+ [],+ )+++def test_terminal_full_lane_row_breaks_after_abnormal_clearance() -> None:+ """验证满栏句末行后的净空超过常规间距半行高时强制另起段落。"""++ previous = _text_line("Figure caption ends.", (0.0, 0.0, 100.0, 10.0), 0)+ current = _text_line("new paragraph fills the lane", (0.0, 17.0, 100.0, 27.0), 1)+ lane = pdf_extractor._TextLane(+ left=0.0,+ right=100.0,+ lines=[(previous, previous.bbox), (current, current.bbox)],+ )++ assert not pdf_extractor._should_connect_text_rows(+ (previous, previous.bbox),+ (current, current.bbox),+ lane,+ 1.0,+ 0.0,+ [],+ [],+ )++def test_effective_height_connects_body_line_after_tall_math_glyph() -> None:"""验证高数学字形拉长原始 bbox 时仍按有效行高连接下一正文行。"""@@ -2145,6 +2349,18 @@ def test_demo3_keeps_tables_and_covers_every_native_source_line() -> None:0,0,]+ assert [len(page) for page in model_list] == [+ 23,+ 15,+ 13,+ 21,+ 19,+ 16,+ 16,+ 15,+ 17,+ 18,+ ]page7_tables = [block for block in model_list[6] if block["type"] == "table"]page7_table4 = next(block for block in page7_tables if "Number of parameters" in block["content"]@@ -2154,10 +2370,10 @@ def test_demo3_keeps_tables_and_covers_every_native_source_line() -> None:for block in model_list[6]if block["content"] == "Table 4: Model size comparison.")- page7_inline_title = next(+ page7_inline_body = next(blockfor block in model_list[6]- if block["content"] == "Row, Column, & Global Positional IDs."+ if block["content"].startswith("Row, Column, & Global Positional IDs."))page9_conclusion = next(block@@ -2181,7 +2397,8 @@ def test_demo3_keeps_tables_and_covers_every_native_source_line() -> None:)assert page7_table4_caption["content"] not in page7_table4["content"]assert page7_table4["bbox"][3] < page7_table4_caption["bbox"][1]- assert page7_inline_title["type"] == "paragraph_title"+ assert page7_inline_body["type"] == "text"+ assert "With TAPASBASE" in page7_inline_body["content"]assert "To tackle this" in page9_conclusion["content"]assert "Acknowledgments" not in page9_conclusion["content"]assert "Cong Yu. 2021. TURL:" in page10_first_reference["content"]@@ -2216,12 +2433,31 @@ def test_demo3_pages1_and2_fix_title_front_matter_and_embedding_formula() -> Nonreleased_code = next(block for block in page1 if "TABLEFORMER.md" in block["content"])+ introduction = next(+ block for block in page1 if block["content"].startswith("Recently, semi-structured")+ )+ nutshell = next(+ block for block in page1 if block["content"].startswith("In a nutshell")+ )+ figure_caption = next(+ block for block in page1 if block["content"].startswith("Figure 1:")+ )+ tables_body = next(+ block for block in page1 if block["content"].startswith("tables or rows")+ )assert title["type"] == "doc_title"assert len(front_matter) == len(front_matter_contents)assert all(block["type"] == "text" for block in front_matter)assert next(block for block in page1 if block["content"] == "Abstract")["type"] == "paragraph_title"assert released_code["type"] == "text"+ assert introduction["content"].endswith("(Eisenschlos et al., 2021; Liu et al., 2021).")+ assert nutshell["type"] == "text"+ assert nutshell["content"].endswith("by serializing")+ assert figure_caption["type"] == "text"+ assert figure_caption["content"].endswith("both questions.")+ assert "tables or rows" not in figure_caption["content"]+ assert tables_body["type"] == "text"section_title = next(block for block in page2 if block["content"].startswith("2 Preliminaries:")@@ -2252,6 +2488,66 @@ def test_demo3_pages1_and2_fix_title_front_matter_and_embedding_formula() -> Nonassert "Let the layer input" in as_model_blocks[0]["content"]+def test_demo3_pages6_7_and10_fix_caption_inline_titles_and_reference_tail() -> None:+ """验证跨栏 caption、行内粗体正文与参考文献尾行均保持正确归属。"""++ model_list = _native_model_list("demo3.pdf")+ page6 = model_list[5]+ page7 = model_list[6]+ page10 = model_list[9]++ table2_caption = next(+ block for block in page6 if block["content"].startswith("Table 2:")+ )+ assert table2_caption["type"] == "text"+ assert "Median of 5 independent runs are reported." in table2_caption["content"]+ assert table2_caption["content"].endswith("not reported in the original paper.")+ assert sum(+ "not reported in the original paper." in block["content"]+ for block in page6+ ) == 1++ attention_bias = next(+ block for block in page7 if block["content"].startswith("Attention Bias Scaling.")+ )+ positional_ids = next(+ block+ for block in page7+ if block["content"].startswith("Row, Column, & Global Positional IDs.")+ )+ formula6 = next(+ block+ for block in page7+ if block["type"] == "equation" and "(6)" in block["content"]+ )+ assert attention_bias["type"] == "text"+ assert attention_bias["content"].endswith("attention score by:")+ assert positional_ids["type"] == "text"+ assert "With TAPASBASE" in positional_ids["content"]+ assert formula6["content"].splitlines()[-1] == "(6)"+ assert not [+ block+ for block in page7+ if block["type"] == "paragraph_title"+ and block["content"].startswith(+ ("Attention Bias Scaling.", "Row, Column, & Global Positional IDs.")+ )+ ]++ ying_reference = next(+ block for block in page10 if block["content"].startswith("Chengxuan Ying")+ )+ yu_reference = next(+ block for block in page10 if block["content"].startswith("Tao Yu")+ )+ assert ying_reference["type"] == yu_reference["type"] == "text"+ assert ying_reference["content"].endswith("arXiv:2106.05234.")+ assert "Tao Yu" not in ying_reference["content"]+ assert not [+ block for block in page10 if block["content"] == "arXiv:2106.05234."+ ]++def test_demo3_page3_form_image_formulas_titles_and_inline_body_are_whole() -> None:"""验证第三页大 Form、caption、公式、标题及行内粗体都按整体输出。"""