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: implement fallback for leading table continuation captions
myhlolid285f7a8c699
diff --git a/mineru/backend/hybrid/hybrid_magic_model.py b/mineru/backend/hybrid/hybrid_magic_model.pyindex c0300d3c..096e15bc 100644--- a/mineru/backend/hybrid/hybrid_magic_model.py+++ b/mineru/backend/hybrid/hybrid_magic_model.py@@ -16,6 +16,7 @@ from mineru.utils.visual_magic_model_utils import (clean_content,code_content_clean,fallback_inline_caption_fragments,+ fallback_leading_table_continuation_captions,isolated_formula_clean,regroup_visual_blocks,)@@ -301,6 +302,7 @@ class MagicModel:blocks.append(block)fallback_inline_caption_fragments(blocks, VISUAL_MAIN_TYPES)+ fallback_leading_table_continuation_captions(blocks, VISUAL_MAIN_TYPES)self.image_blocks = []self.table_blocks = []diff --git a/mineru/backend/pipeline/pipeline_magic_model.py b/mineru/backend/pipeline/pipeline_magic_model.pyindex 59c5360f..ba105c0c 100644--- a/mineru/backend/pipeline/pipeline_magic_model.py+++ b/mineru/backend/pipeline/pipeline_magic_model.py@@ -12,6 +12,7 @@ from mineru.utils.span_block_fix import merge_spans_to_vertical_line, vertical_lfrom mineru.utils.span_pre_proc import SpanBlockMatcher, txt_spans_extractfrom mineru.utils.visual_magic_model_utils import (fallback_inline_caption_fragments,+ fallback_leading_table_continuation_captions,find_best_visual_parent,)@@ -121,6 +122,7 @@ class MagicModel:self.page_blocks.sort(key=lambda x: x["index"])self.__build_page_blocks()fallback_inline_caption_fragments(self.page_blocks, self.VISUAL_MAIN_TYPES)+ fallback_leading_table_continuation_captions(self.page_blocks, self.VISUAL_MAIN_TYPES)self.__classify_visual_blocks()self.__build_return_blocks()diff --git a/mineru/backend/vlm/vlm_magic_model.py b/mineru/backend/vlm/vlm_magic_model.pyindex 20f294b7..b05e3759 100644--- a/mineru/backend/vlm/vlm_magic_model.py+++ b/mineru/backend/vlm/vlm_magic_model.py@@ -13,6 +13,7 @@ from mineru.utils.visual_magic_model_utils import (clean_content,code_content_clean,fallback_inline_caption_fragments,+ fallback_leading_table_continuation_captions,isolated_formula_clean,regroup_visual_blocks,)@@ -230,6 +231,7 @@ class MagicModel:blocks.append(block)fallback_inline_caption_fragments(blocks, VISUAL_MAIN_TYPES)+ fallback_leading_table_continuation_captions(blocks, VISUAL_MAIN_TYPES)self.image_blocks = []self.table_blocks = []diff --git a/mineru/utils/table_continuation.py b/mineru/utils/table_continuation.pynew file mode 100644index 00000000..6a534639--- /dev/null+++ b/mineru/utils/table_continuation.py@@ -0,0 +1,47 @@+# Copyright (c) Opendatalab. All rights reserved.++from mineru.utils.char_utils import full_to_half+++CONTINUATION_END_MARKERS = [+ "(续)",+ "(续表)",+ "(续上表)",+ "(continued)",+ "(cont.)",+ "(cont’d)",+ "(…continued)",+ "continued",+ "续表",+]++CONTINUATION_INLINE_MARKERS = [+ "(continued)",+]+++def is_table_continuation_text(text: str) -> bool:+ """判断文本是否表达续表语义,供表格归组和跨页合并共同复用。"""+ continuation_text = full_to_half((text or "").strip()).lower()+ if not continuation_text:+ return False++ return (+ any(+ _matches_continuation_end_marker(continuation_text, marker.lower())+ for marker in CONTINUATION_END_MARKERS+ )+ or any(marker.lower() in continuation_text for marker in CONTINUATION_INLINE_MARKERS)+ )+++def _matches_continuation_end_marker(text: str, marker: str) -> bool:+ """判断续表后缀是否按词边界命中,避免 discontinued 误命中 continued。"""+ if not text.endswith(marker):+ return False++ if marker == "continued":+ marker_start = len(text) - len(marker)+ return marker_start == 0 or not text[marker_start - 1].isalpha()++ return Truediff --git a/mineru/utils/table_merge.py b/mineru/utils/table_merge.pyindex 6efb5386..828f7ac8 100644--- a/mineru/utils/table_merge.py+++ b/mineru/utils/table_merge.py@@ -8,23 +8,9 @@ from bs4 import BeautifulSoupfrom mineru.backend.vlm.vlm_middle_json_mkcontent import merge_para_with_textfrom mineru.utils.char_utils import full_to_halffrom mineru.utils.enum_class import BlockType, SplitFlag+from mineru.utils.table_continuation import is_table_continuation_text-CONTINUATION_END_MARKERS = [- "(续)",- "(续表)",- "(续上表)",- "(continued)",- "(cont.)",- "(cont’d)",- "(…continued)",- "续表",-]--CONTINUATION_INLINE_MARKERS = [- "(continued)",-]-MAX_HEADER_ROWS = 5@@ -209,11 +195,7 @@ def _find_table_body_span(table_block):def _is_continuation_caption(caption_block) -> bool:"""判断 caption 文本是否带有续表标记。"""- caption_text = full_to_half(merge_para_with_text(caption_block).strip()).lower()- return (- any(caption_text.endswith(marker.lower()) for marker in CONTINUATION_END_MARKERS)- or any(marker.lower() in caption_text for marker in CONTINUATION_INLINE_MARKERS)- )+ return is_table_continuation_text(merge_para_with_text(caption_block))def _is_post_table_non_continuation_caption(table_block, caption_block) -> bool:@@ -571,7 +553,10 @@ def _detect_table_headers_visual(for row_idx in range(min_rows):row1 = front_rows1[row_idx]row2 = front_rows2[row_idx]- if row1.normalized_texts == row2.normalized_texts and row1.effective_cols == row2.effective_cols:+ # OCR 识别表头时可能丢失 colspan/rowspan,这里用渲染段数约束视觉一致性。+ rendered_segments1 = calculate_row_rendered_segments(state1.rows, row_idx)+ rendered_segments2 = calculate_row_rendered_segments(state2.rows, row_idx)+ if row1.normalized_texts == row2.normalized_texts and rendered_segments1 == rendered_segments2:header_rows += 1header_texts.append(list(row1.display_texts))else:diff --git a/mineru/utils/visual_magic_model_utils.py b/mineru/utils/visual_magic_model_utils.pyindex b216aa97..7bf699ad 100644--- a/mineru/utils/visual_magic_model_utils.py+++ b/mineru/utils/visual_magic_model_utils.py@@ -7,6 +7,7 @@ from mineru.utils.boxbase import (calculate_overlap_area_in_bbox1_area_ratio,)from mineru.utils.enum_class import BlockType+from mineru.utils.table_continuation import is_table_continuation_textIMAGE_BLOCK_BODY = "image_block_body"@@ -128,6 +129,89 @@ def fallback_inline_caption_fragments(blocks, visual_main_types):fallback_stacked_table_caption_fragments(blocks, visual_main_types)+def fallback_leading_table_continuation_captions(blocks, visual_main_types):+ """将页首紧贴表格的续表文本兜底为通用 caption。++ 该规则只处理页面有效块开头的 text,避免正文中出现“续表”时被误挂。+ 后续 regroup_visual_blocks() 会根据表格主体类型将通用 caption 落成+ table_caption 子块。+ """+ table_main_types = get_table_main_types(visual_main_types)+ if not table_main_types:+ return++ effective_blocks = [+ block for block in sorted(blocks, key=lambda x: x["index"])+ if block.get("type") not in VISUAL_RELATION_IGNORED_TYPES+ ]+ if len(effective_blocks) < 2:+ return++ leading_blocks = []+ cursor = 0+ while cursor < len(effective_blocks):+ block = effective_blocks[cursor]+ if not _is_leading_continuation_text_block(block):+ break+ leading_blocks.append(block)+ cursor += 1++ if not leading_blocks or cursor >= len(effective_blocks):+ return++ table_block = effective_blocks[cursor]+ if table_block.get("type") not in table_main_types:+ return++ if not _is_leading_continuation_cluster_near_table(leading_blocks, table_block):+ return++ for block in leading_blocks:+ block["type"] = BlockType.CAPTION+ # fallback 后该块已是视觉 caption,不再参与正文跨块合并。+ block.pop("merge_prev", None)+++def _is_leading_continuation_text_block(block):+ """判断页首候选块是否是单行续表文本。"""+ return (+ block.get("type") in INLINE_CAPTION_FRAGMENT_TYPES+ and is_single_line_caption_fragment(block)+ and is_table_continuation_text(_block_text_content(block))+ )+++def _block_text_content(block):+ """提取视觉块中的可见文本,用于续表 marker 判断。"""+ return "".join(+ span.get("content", "")+ for line in block.get("lines", [])+ for span in line.get("spans", [])+ )+++def _is_leading_continuation_cluster_near_table(leading_blocks, table_block):+ """判断页首续表文本簇是否与后续 table 在几何上相邻。"""+ next_top = table_block["bbox"][1]+ max_child_height = 1++ for block in reversed(leading_blocks):+ if not is_horizontally_near_table(block, table_block):+ return False++ block_height = max(block["bbox"][3] - block["bbox"][1], 1)+ vertical_gap = next_top - block["bbox"][3]+ max_gap = stacked_caption_max_gap(max(max_child_height, block_height))+ max_overlap = max(2, block_height * 0.3)+ if vertical_gap > max_gap or vertical_gap < -max_overlap:+ return False++ next_top = block["bbox"][1]+ max_child_height = max(max_child_height, block_height)++ return True++def fallback_stacked_table_caption_fragments(blocks, visual_main_types):"""将 table 上方紧贴标题簇里的 text/footnote 片段兜底为 caption。"""table_main_types = get_table_main_types(visual_main_types)