Skip to content

Transforms complex documents like PDFs and Office docs into LLM-ready markdown/JSON for your Agentic workflows.

feat: add support for stacked table caption handling and improve caption fragment identification

myhloli834df284b2b6
diff --git a/mineru/utils/visual_magic_model_utils.py b/mineru/utils/visual_magic_model_utils.py
index ffc3544d..b216aa97 100644
--- a/mineru/utils/visual_magic_model_utils.py
+++ b/mineru/utils/visual_magic_model_utils.py
@@ -12,6 +12,11 @@ from mineru.utils.enum_class import BlockType
IMAGE_BLOCK_BODY = "image_block_body"
GENERIC_CHILD_TYPES = (BlockType.CAPTION, BlockType.FOOTNOTE)
INLINE_CAPTION_FRAGMENT_TYPES = {BlockType.TEXT, BlockType.FOOTNOTE}
+STACKED_TABLE_CAPTION_CLUSTER_TYPES = {
+ BlockType.CAPTION,
+ BlockType.TEXT,
+ BlockType.FOOTNOTE,
+}
VISUAL_RELATION_IGNORED_TYPES = {
BlockType.HEADER,
BlockType.FOOTER,
@@ -120,6 +125,115 @@ def fallback_inline_caption_fragments(blocks, visual_main_types):
# fallback 后该块已是视觉 caption 片段,不再参与正文跨块合并。
block.pop("merge_prev", None)
+ fallback_stacked_table_caption_fragments(blocks, visual_main_types)
+
+
+def fallback_stacked_table_caption_fragments(blocks, visual_main_types):
+ """将 table 上方紧贴标题簇里的 text/footnote 片段兜底为 caption。"""
+ table_main_types = get_table_main_types(visual_main_types)
+ if not table_main_types:
+ return
+
+ for table_block in blocks:
+ if table_block.get("type") not in table_main_types:
+ continue
+
+ caption_cluster = find_stacked_table_caption_cluster(table_block, blocks)
+ if not caption_cluster:
+ continue
+
+ last_caption_pos = find_last_caption_position(caption_cluster)
+ if last_caption_pos is None:
+ continue
+
+ for block in caption_cluster[last_caption_pos + 1:]:
+ if (
+ block.get("type") in INLINE_CAPTION_FRAGMENT_TYPES
+ and is_single_line_caption_fragment(block)
+ ):
+ block["type"] = BlockType.CAPTION
+ # fallback 后该块已是视觉 caption 片段,不再参与正文跨块合并。
+ block.pop("merge_prev", None)
+
+
+def get_table_main_types(visual_main_types):
+ """根据调用方传入的视觉主体类型,找出 table 对应的主体类型。"""
+ if isinstance(visual_main_types, dict):
+ return {
+ block_type
+ for block_type, visual_type in visual_main_types.items()
+ if visual_type == BlockType.TABLE
+ }
+
+ main_types = set(visual_main_types)
+ return main_types & {BlockType.TABLE, BlockType.TABLE_BODY}
+
+
+def find_stacked_table_caption_cluster(table_block, blocks):
+ """按几何位置收集紧贴 table 上方的 caption/text/footnote 标题簇。"""
+ table_bbox = table_block["bbox"]
+ table_top = table_bbox[1]
+ above_candidates = [
+ block
+ for block in blocks
+ if block is not table_block
+ and block.get("type") in STACKED_TABLE_CAPTION_CLUSTER_TYPES
+ and block["bbox"][3] <= table_top
+ and is_horizontally_near_table(block, table_block)
+ ]
+ if not above_candidates:
+ return []
+
+ caption_cluster = []
+ next_top = table_top
+ max_child_height = 1
+ for block in sorted(
+ above_candidates,
+ key=lambda x: (x["bbox"][1], x["index"]),
+ reverse=True,
+ ):
+ block_height = max(block["bbox"][3] - block["bbox"][1], 1)
+ max_allowed_gap = stacked_caption_max_gap(max(max_child_height, block_height))
+ vertical_gap = next_top - block["bbox"][3]
+ if not 0 <= vertical_gap <= max_allowed_gap:
+ break
+
+ caption_cluster.append(block)
+ next_top = block["bbox"][1]
+ max_child_height = max(max_child_height, block_height)
+
+ return list(reversed(caption_cluster))
+
+
+def find_last_caption_position(caption_cluster):
+ """定位标题簇里的最后一个 caption,避免吸收上一张表的尾注。"""
+ for pos in range(len(caption_cluster) - 1, -1, -1):
+ if caption_cluster[pos].get("type") == BlockType.CAPTION:
+ return pos
+ return None
+
+
+def is_horizontally_near_table(block, table_block):
+ """判断标题簇候选块是否横向落在 table 范围附近。"""
+ table_bbox = table_block["bbox"]
+ block_bbox = block["bbox"]
+ table_width = max(table_bbox[2] - table_bbox[0], 1)
+ tolerance = max(12, table_width * 0.03)
+ return not (
+ block_bbox[2] < table_bbox[0] - tolerance
+ or block_bbox[0] > table_bbox[2] + tolerance
+ )
+
+
+def is_single_line_caption_fragment(block):
+ """判断待兜底片段是否是单行块,避免吞掉多行正文。"""
+ return len(block.get("lines") or [None]) <= 1
+
+
+def stacked_caption_max_gap(block_height):
+ """计算堆叠标题簇允许的最大纵向间距。"""
+ return max(12, block_height * 1.5)
+
def find_previous_effective_block(ordered_blocks, pos):
"""向前查找参与视觉关系判断的有效块,跳过页眉页脚等外围块。"""