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: normalize xhigh VLM block types and enhance line metadata handling
myhloli4a202d8b9c57
diff --git a/mineru/backend/analyze.py b/mineru/backend/analyze.pyindex acadfb70..5ca3e941 100644--- a/mineru/backend/analyze.py+++ b/mineru/backend/analyze.py@@ -93,6 +93,18 @@ CODE_CONTENT_BLOCK_TYPES = {BlockType.CODE,BlockType.ALGORITHM,}+LINE_METADATA_BLOCK_TYPES = {+ BlockType.TEXT,+ BlockType.CAPTION,+ BlockType.FOOTNOTE,+}+VLM_VISUAL_ANNOTATION_TYPE_MAP = {+ BlockType.TABLE_CAPTION: BlockType.CAPTION,+ BlockType.IMAGE_CAPTION: BlockType.CAPTION,+ BlockType.CODE_CAPTION: BlockType.CAPTION,+ BlockType.TABLE_FOOTNOTE: BlockType.FOOTNOTE,+ BlockType.IMAGE_FOOTNOTE: BlockType.FOOTNOTE,+}MODEL_JSON_VISUAL_BLOCK_TYPES = {BlockType.IMAGE,BlockType.CHART,@@ -389,6 +401,17 @@ def _build_vl_style_layout_blocks(return blocks_list+def _normalize_xhigh_vlm_blocks(model_list: list[list[dict[str, Any]]]) -> None:+ """归一化 xhigh VLM 注释类型,并移除正文块的 merge_prev。"""+ for page_model_list in model_list:+ for block in page_model_list:+ if block.get("type") == BlockType.TEXT:+ block.pop("merge_prev", None)+ normalized_type = VLM_VISUAL_ANNOTATION_TYPE_MAP.get(block.get("type"))+ if normalized_type is not None:+ block["type"] = normalized_type++def _build_formula_inputs(images_layout_res: list[list[dict[str, Any]]]) -> list[list[dict[str, Any]]]:"""构造完整 MFD/MFR 输入,保留全部行内和行间公式框。"""formula_inputs = []@@ -1144,10 +1167,10 @@ def _apply_block_content_and_line_metadata(block_lines: dict[int, list[Line]],page_size: tuple[float, float],) -> None:- """将组行结果回填到 block,并只为 TEXT 保存行框。"""+ """将组行结果回填到 block,并为正文、视觉标题和视觉脚注保存行框。"""for block_item in page_model_list:block_type = str(block_item.get("type") or block_item.get("label") or "")- if block_type == BlockType.TEXT:+ if block_type in LINE_METADATA_BLOCK_TYPES:block_item["_lines"] = []else:block_item.pop("_lines", None)@@ -1160,7 +1183,7 @@ def _apply_block_content_and_line_metadata(if not has_nonempty_content:block_item["content"] = _lines_to_block_content(lines, block_type)- if block_type == BlockType.TEXT:+ if block_type in LINE_METADATA_BLOCK_TYPES:block_item["_lines"] = _build_ocr_det_line_items(lines, page_size)@@ -2508,6 +2531,7 @@ def doc_analyze(not_extract_list=NOT_EXTRACT_TYPES,image_analysis=image_analysis,)+ _normalize_xhigh_vlm_blocks(window_model_list)_apply_layout_title_split(window_model_list,images_layout_res,@@ -2529,6 +2553,7 @@ def doc_analyze(images=images_pil_list,image_analysis=image_analysis,)+ _normalize_xhigh_vlm_blocks(window_model_list)_apply_layout_title_split(window_model_list,images_layout_res,diff --git a/mineru/backend/magic_model.py b/mineru/backend/magic_model.pyindex 178a897b..07a10502 100644--- a/mineru/backend/magic_model.py+++ b/mineru/backend/magic_model.py@@ -9,10 +9,10 @@ from mineru.backend.utils.visual_magic_model_utils import (code_content_clean,isolated_formula_clean,clean_content,- fallback_inline_caption_fragments,- fallback_leading_table_continuation_captions,VISUAL_MAIN_TYPES,regroup_visual_blocks,+ fallback_inline_caption_fragments,+ fallback_leading_table_continuation_captions,)from mineru.types import BlockTypefrom mineru.utils.guess_suffix_or_lang import guess_language_by_text@@ -36,7 +36,6 @@ class MagicModel:if block_info.get("bbox"):is_block_has_bbox = True- guess_lang = Nonecode_block_sub_type = Noneblock_type = block_info.get("type", "")block_content = block_info.get("content", "")@@ -77,6 +76,7 @@ class MagicModel:block = block_infoblock["type"] = block_typeblock["content"] = block_content+ block["index"] = indexif code_block_sub_type:block["sub_type"] = code_block_sub_typeblocks.append(block)@@ -112,7 +112,6 @@ class MagicModel:self.list_blocks = fix_office_list_blocks(self.list_blocks)self.index_blocks = fix_office_index_blocks(self.index_blocks)-visual_groups, unmatched_child_blocks = regroup_visual_blocks(blocks)self.image_blocks = visual_groups[BlockType.IMAGE]self.table_blocks = visual_groups[BlockType.TABLE]diff --git a/mineru/backend/utils/visual_magic_model_utils.py b/mineru/backend/utils/visual_magic_model_utils.pyindex 960cfb54..eedac7bd 100644--- a/mineru/backend/utils/visual_magic_model_utils.py+++ b/mineru/backend/utils/visual_magic_model_utils.py@@ -3,7 +3,7 @@ from __future__ import annotationsimport refrom copy import deepcopy-from typing import Any+from typing import Any, castfrom ...types import BBox, Block, BlockTypefrom .boxbase import bbox_center_distance, bbox_distance@@ -54,6 +54,45 @@ VISUAL_TYPE_MAPPING = {},}+BlockLike = Block | dict[str, Any]+++def _get_block_field(block: BlockLike, field_name: str, default: Any = None) -> Any:+ """统一读取 Block 属性或 dict 字段。"""+ if isinstance(block, dict):+ return block.get(field_name, default)+ return getattr(block, field_name, default)+++def _set_block_field(block: BlockLike, field_name: str, value: Any) -> None:+ """统一回写 Block 属性或 dict 字段。"""+ if isinstance(block, dict):+ block[field_name] = value+ else:+ setattr(block, field_name, value)+++def _block_index(block: BlockLike) -> int:+ """读取 block 顺序索引,缺失时沿用原有的零值排序语义。"""+ return int(_get_block_field(block, "index", 0) or 0)+++def _block_type(block: BlockLike) -> str:+ """读取 block 类型。"""+ return str(_get_block_field(block, "type", "") or "")+++def _block_bbox(block: BlockLike) -> BBox:+ """读取参与视觉关系判断的 block bbox。"""+ return cast(BBox, _get_block_field(block, "bbox"))+++def _block_line_items(block: BlockLike) -> list[Any]:+ """读取行级元数据,dict 使用 _lines,Block 使用 lines。"""+ if isinstance(block, dict):+ return list(block.get("_lines") or [])+ return list(block.lines or [])+def isolated_formula_clean(txt: str) -> str:latex = txt[:]@@ -98,33 +137,42 @@ def clean_content(content: str | None) -> str | None:return content-def fallback_inline_caption_fragments(blocks: list[Block], visual_main_types: dict[str, str] | set[str]) -> None:+def fallback_inline_caption_fragments(+ blocks: list[BlockLike],+ visual_main_types: dict[str, str] | set[str],+) -> None:"""将紧贴视觉主体上方的同行 text/footnote 片段兜底为通用 caption。"""if len(blocks) < 3:returnmain_types = set(visual_main_types)- ordered_blocks = sorted(blocks, key=lambda x: x.index or 0)+ ordered_blocks = sorted(blocks, key=_block_index)for pos, block in enumerate(ordered_blocks):- if block.type not in INLINE_CAPTION_FRAGMENT_TYPES:+ if _block_type(block) not in INLINE_CAPTION_FRAGMENT_TYPES:continueprevious_block = find_previous_effective_block(ordered_blocks, pos)next_block = find_next_effective_block(ordered_blocks, pos)- if not (previous_block and next_block and previous_block.type == BlockType.CAPTION and next_block.type in main_types):+ if not (+ previous_block+ and next_block+ and _block_type(previous_block) == BlockType.CAPTION+ and _block_type(next_block) in main_types+ ):continueif not is_inline_caption_fragment(previous_block, block, next_block):continue- block.type = BlockType.CAPTION- # fallback 后该块已是视觉 caption 片段,不再参与正文跨块合并。- block.merge_prev = False+ _set_block_field(block, "type", BlockType.CAPTION)fallback_stacked_table_caption_fragments(blocks, visual_main_types)-def fallback_leading_table_continuation_captions(blocks: list[Block], visual_main_types: dict[Any, Any] | set[Any]) -> None:+def fallback_leading_table_continuation_captions(+ blocks: list[BlockLike],+ visual_main_types: dict[Any, Any] | set[Any],+) -> None:"""将页首紧贴表格的续表文本兜底为通用 caption。该规则只处理页面有效块开头的 text,避免正文中出现“续表”时被误挂。@@ -136,7 +184,9 @@ def fallback_leading_table_continuation_captions(blocks: list[Block], visual_maireturneffective_blocks = [- block for block in sorted(blocks, key=lambda x: x.index or 0) if block.type not in VISUAL_RELATION_IGNORED_TYPES+ block+ for block in sorted(blocks, key=_block_index)+ if _block_type(block) not in VISUAL_RELATION_IGNORED_TYPES]if len(effective_blocks) < 2:return@@ -154,30 +204,29 @@ def fallback_leading_table_continuation_captions(blocks: list[Block], visual_maireturntable_block = effective_blocks[cursor]- if table_block.type not in table_main_types:+ if _block_type(table_block) not in table_main_types:returnif not _is_leading_continuation_cluster_near_table(leading_blocks, table_block):returnfor block in leading_blocks:- block.type = BlockType.CAPTION- # fallback 后该块已是视觉 caption,不再参与正文跨块合并。- block.merge_prev = False+ _set_block_field(block, "type", BlockType.CAPTION)-def _is_leading_continuation_text_block(block: Block) -> bool:+def _is_leading_continuation_text_block(block: BlockLike) -> bool:"""判断页首候选块是否是单行续表文本。"""return (- block.type in INLINE_CAPTION_FRAGMENT_TYPES+ _block_type(block) in INLINE_CAPTION_FRAGMENT_TYPESand is_single_line_caption_fragment(block)and is_table_continuation_text(_block_text_content(block)))-def _block_text_content(block: Block) -> str:+def _block_text_content(block: BlockLike) -> str:"""提取视觉块中的可见文本,用于续表 marker 判断。"""- return "".join(span.content for line in block.lines for span in line.spans)+ content = _get_block_field(block, "content", "")+ return content if isinstance(content, str) else ""def is_transparent_visual_relation_block(block: Block) -> bool:@@ -191,36 +240,43 @@ def is_transparent_visual_relation_block(block: Block) -> bool:return not _block_text_content(block).strip()-def _is_leading_continuation_cluster_near_table(leading_blocks: list[Block], table_block: Block) -> bool:+def _is_leading_continuation_cluster_near_table(+ leading_blocks: list[BlockLike],+ table_block: BlockLike,+) -> bool:"""判断页首续表文本簇是否与后续 table 在几何上相邻。"""- next_top = table_block.bbox[1]+ next_top = _block_bbox(table_block)[1]max_child_height = 1for 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]+ block_bbox = _block_bbox(block)+ 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]+ next_top = block_bbox[1]max_child_height = max(max_child_height, block_height)return True-def fallback_stacked_table_caption_fragments(blocks: list[Block], visual_main_types: dict[str, str] | set[str]) -> None:+def fallback_stacked_table_caption_fragments(+ blocks: list[BlockLike],+ visual_main_types: dict[str, str] | set[str],+) -> None:"""将 table 上方紧贴标题簇里的 text/footnote 片段兜底为 caption。"""table_main_types = get_table_main_types(visual_main_types)if not table_main_types:returnfor table_block in blocks:- if table_block.type not in table_main_types:+ if _block_type(table_block) not in table_main_types:continuecaption_cluster = find_stacked_table_caption_cluster(table_block, blocks)@@ -232,10 +288,8 @@ def fallback_stacked_table_caption_fragments(blocks: list[Block], visual_main_tycontinuefor block in caption_cluster[last_caption_pos + 1 :]:- if block.type in INLINE_CAPTION_FRAGMENT_TYPES and is_single_line_caption_fragment(block):- block.type = BlockType.CAPTION- # fallback 后该块已是视觉 caption 片段,不再参与正文跨块合并。- block.merge_prev = False+ if _block_type(block) in INLINE_CAPTION_FRAGMENT_TYPES and is_single_line_caption_fragment(block):+ _set_block_field(block, "type", BlockType.CAPTION)def get_table_main_types(visual_main_types: dict[str, str] | set[str]) -> set[str]:@@ -247,16 +301,19 @@ def get_table_main_types(visual_main_types: dict[str, str] | set[str]) -> set[streturn main_types & {BlockType.TABLE, BlockType.TABLE_BODY}-def find_stacked_table_caption_cluster(table_block: Block, blocks: list[Block]) -> list[Block]:+def find_stacked_table_caption_cluster(+ table_block: BlockLike,+ blocks: list[BlockLike],+) -> list[BlockLike]:"""按几何位置收集紧贴 table 上方的 caption/text/footnote 标题簇。"""- table_bbox = table_block.bbox+ table_bbox = _block_bbox(table_block)table_top = table_bbox[1]above_candidates = [blockfor block in blocksif block is not table_block- and block.type in STACKED_TABLE_CAPTION_CLUSTER_TYPES- and block.bbox[3] <= table_top+ and _block_type(block) in STACKED_TABLE_CAPTION_CLUSTER_TYPES+ and _block_bbox(block)[3] <= table_topand is_horizontally_near_table(block, table_block)]if not above_candidates:@@ -267,42 +324,43 @@ def find_stacked_table_caption_cluster(table_block: Block, blocks: list[Block])max_child_height = 1for block in sorted(above_candidates,- key=lambda x: (x.bbox[1], x.index),+ key=lambda x: (_block_bbox(x)[1], _block_index(x)),reverse=True,):- block_height = max(block.bbox[3] - block.bbox[1], 1)+ block_bbox = _block_bbox(block)+ 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]+ vertical_gap = next_top - block_bbox[3]if not 0 <= vertical_gap <= max_allowed_gap:breakcaption_cluster.append(block)- next_top = block.bbox[1]+ 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: list[Block]) -> int | None:+def find_last_caption_position(caption_cluster: list[BlockLike]) -> int | None:"""定位标题簇里的最后一个 caption,避免吸收上一张表的尾注。"""for pos in range(len(caption_cluster) - 1, -1, -1):- if caption_cluster[pos].type == BlockType.CAPTION:+ if _block_type(caption_cluster[pos]) == BlockType.CAPTION:return posreturn None-def is_horizontally_near_table(block: Block, table_block: Block) -> bool:+def is_horizontally_near_table(block: BlockLike, table_block: BlockLike) -> bool:"""判断标题簇候选块是否横向落在 table 范围附近。"""- table_bbox = table_block.bbox- block_bbox = block.bbox+ table_bbox = _block_bbox(table_block)+ block_bbox = _block_bbox(block)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: Block) -> bool:+def is_single_line_caption_fragment(block: BlockLike) -> bool:"""判断待兜底片段是否是单行块,避免吞掉多行正文。"""- return len(block.lines or [None]) <= 1+ return len(_block_line_items(block) or [None]) <= 1def stacked_caption_max_gap(block_height: float) -> float:@@ -310,29 +368,33 @@ def stacked_caption_max_gap(block_height: float) -> float:return max(12, block_height * 1.5)-def find_previous_effective_block(ordered_blocks: list[Block], pos: int) -> Block | None:+def find_previous_effective_block(ordered_blocks: list[BlockLike], pos: int) -> BlockLike | None:"""向前查找参与视觉关系判断的有效块,跳过页眉页脚等外围块。"""for index in range(pos - 1, -1, -1):block = ordered_blocks[index]- if block.type not in VISUAL_RELATION_IGNORED_TYPES:+ if _block_type(block) not in VISUAL_RELATION_IGNORED_TYPES:return blockreturn None-def find_next_effective_block(ordered_blocks: list[Block], pos: int) -> Block | None:+def find_next_effective_block(ordered_blocks: list[BlockLike], pos: int) -> BlockLike | None:"""向后查找参与视觉关系判断的有效块,跳过页眉页脚等外围块。"""for index in range(pos + 1, len(ordered_blocks)):block = ordered_blocks[index]- if block.type not in VISUAL_RELATION_IGNORED_TYPES:+ if _block_type(block) not in VISUAL_RELATION_IGNORED_TYPES:return blockreturn None-def is_inline_caption_fragment(previous_caption: Block, text_block: Block, next_visual: Block) -> bool:+def is_inline_caption_fragment(+ previous_caption: BlockLike,+ text_block: BlockLike,+ next_visual: BlockLike,+) -> bool:"""判断当前块是否是前一 caption 的同行补充片段。"""- caption_bbox = previous_caption.bbox- text_bbox = text_block.bbox- visual_bbox = next_visual.bbox+ caption_bbox = _block_bbox(previous_caption)+ text_bbox = _block_bbox(text_block)+ visual_bbox = _block_bbox(next_visual)caption_height = max(caption_bbox[3] - caption_bbox[1], 1)text_height = max(text_bbox[3] - text_bbox[1], 1)diff --git a/tests/unittest/test_pdf_analyze_block_content.py b/tests/unittest/test_pdf_analyze_block_content.pyindex 9e08543b..15b28f0b 100644--- a/tests/unittest/test_pdf_analyze_block_content.py+++ b/tests/unittest/test_pdf_analyze_block_content.py@@ -60,6 +60,85 @@ def _build_post_ocr_page_block_lines(return page_block_lines_list, spans+def test_xhigh_vlm_blocks_normalize_visual_annotation_types() -> None:+ """验证 xhigh VLM 的细分视觉标题和脚注会统一成通用类型。"""+ page_blocks = [+ {"type": BlockType.TABLE_CAPTION, "content": "table caption"},+ {"type": BlockType.IMAGE_CAPTION, "content": "image caption"},+ {"type": BlockType.CODE_CAPTION, "content": "code caption"},+ {"type": BlockType.TABLE_FOOTNOTE, "content": "table footnote"},+ {"type": BlockType.IMAGE_FOOTNOTE, "content": "image footnote"},+ {"type": BlockType.TEXT, "content": "text"},+ ]++ analyze._normalize_xhigh_vlm_blocks([page_blocks])++ assert [block["type"] for block in page_blocks] == [+ BlockType.CAPTION,+ BlockType.CAPTION,+ BlockType.CAPTION,+ BlockType.FOOTNOTE,+ BlockType.FOOTNOTE,+ BlockType.TEXT,+ ]+ assert [block["content"] for block in page_blocks] == [+ "table caption",+ "image caption",+ "code caption",+ "table footnote",+ "image footnote",+ "text",+ ]+++def test_xhigh_vlm_blocks_remove_merge_prev_from_all_text_blocks() -> None:+ """验证 xhigh VLM 正文无论 merge_prev 取值如何都移除该字段。"""+ page_blocks = [+ {"type": BlockType.TEXT, "content": "merge", "merge_prev": True},+ {"type": BlockType.TEXT, "content": "do not merge", "merge_prev": False},+ {"type": BlockType.TEXT, "content": "without hint"},+ {"type": BlockType.CAPTION, "content": "caption"},+ ]++ analyze._normalize_xhigh_vlm_blocks([page_blocks])++ assert all("merge_prev" not in block for block in page_blocks if block["type"] == BlockType.TEXT)+ assert page_blocks[-1] == {"type": BlockType.CAPTION, "content": "caption"}+++def test_caption_and_footnote_keep_normalized_line_metadata() -> None:+ """验证通用 caption/footnote 与 text 一样保留归一化 _lines。"""+ assert analyze.LINE_METADATA_BLOCK_TYPES == {+ BlockType.TEXT,+ BlockType.CAPTION,+ BlockType.FOOTNOTE,+ }+ text_block = {"type": BlockType.TEXT, "content": "text"}+ caption_block = {"type": BlockType.CAPTION, "content": "caption"}+ footnote_block = {"type": BlockType.FOOTNOTE, "content": "footnote"}+ unrelated_block = {"type": BlockType.TITLE, "content": "title", "_lines": [{"bbox": [0, 0, 1, 1]}]}+ page_blocks = [text_block, caption_block, footnote_block, unrelated_block]++ analyze._apply_block_content_and_line_metadata(+ page_blocks,+ {+ 0: _build_text_lines("text"),+ 1: _build_text_lines("caption line 1", "caption line 2"),+ 2: _build_text_lines("footnote"),+ 3: _build_text_lines("title"),+ },+ (200.0, 100.0),+ )++ assert text_block["_lines"] == [{"bbox": [0.0, 0.0, 0.5, 0.01]}]+ assert caption_block["_lines"] == [+ {"bbox": [0.0, 0.0, 0.5, 0.01]},+ {"bbox": [0.0, 0.01, 0.5, 0.02]},+ ]+ assert footnote_block["_lines"] == [{"bbox": [0.0, 0.0, 0.5, 0.01]}]+ assert "_lines" not in unrelated_block++def test_window_post_ocr_batches_all_pages_once() -> None:"""验证窗口内多页待识别 span 合并为一次 OCR-rec,并按原顺序回填。"""page_block_lines_list, spans = _build_post_ocr_page_block_lines((11, 12), (21, 22))diff --git a/tests/unittest/test_visual_magic_model_utils.py b/tests/unittest/test_visual_magic_model_utils.pynew file mode 100644index 00000000..042d44f4--- /dev/null+++ b/tests/unittest/test_visual_magic_model_utils.py@@ -0,0 +1,145 @@+from __future__ import annotations++from mineru.backend.utils.visual_magic_model_utils import (+ VISUAL_MAIN_TYPES,+ fallback_inline_caption_fragments,+ fallback_leading_table_continuation_captions,+)+from mineru.types import Block, BlockType, Line+++def _line_metadata(count: int) -> list[dict[str, list[float]]]:+ """构造仅包含 bbox 的 dict 行级元数据。"""+ return [{"bbox": [0.0, float(index), 1.0, float(index + 1)]} for index in range(count)]+++def test_dict_inline_caption_fragment_uses_common_fields() -> None:+ """验证 dict 同行标题片段可原地改成通用 caption。"""+ companion = {+ "index": 1,+ "type": BlockType.TEXT,+ "bbox": [40.0, 0.0, 80.0, 10.0],+ "content": "continued caption",+ "_lines": _line_metadata(1),+ }+ blocks = [+ {+ "index": 0,+ "type": BlockType.CAPTION,+ "bbox": [0.0, 0.0, 40.0, 10.0],+ "content": "Table 1",+ "_lines": _line_metadata(1),+ },+ companion,+ {+ "index": 2,+ "type": BlockType.TABLE_BODY,+ "bbox": [0.0, 15.0, 80.0, 60.0],+ "content": "",+ },+ ]++ fallback_inline_caption_fragments(blocks, VISUAL_MAIN_TYPES)++ assert companion["type"] == BlockType.CAPTION+ assert "merge_prev" not in companion+++def test_dict_stacked_caption_fragment_uses_line_metadata() -> None:+ """验证堆叠标题片段根据 dict 的 _lines 区分单行和多行。"""+ single_line = {+ "index": 1,+ "type": BlockType.TEXT,+ "bbox": [0.0, 12.0, 80.0, 22.0],+ "content": "single line",+ "_lines": _line_metadata(1),+ }+ blocks = [+ {+ "index": 0,+ "type": BlockType.CAPTION,+ "bbox": [0.0, 0.0, 80.0, 10.0],+ "content": "Table 1",+ "_lines": _line_metadata(1),+ },+ single_line,+ {+ "index": 2,+ "type": BlockType.TABLE_BODY,+ "bbox": [0.0, 25.0, 80.0, 60.0],+ "content": "",+ },+ ]++ fallback_inline_caption_fragments(blocks, VISUAL_MAIN_TYPES)++ assert single_line["type"] == BlockType.CAPTION+ single_line["type"] = BlockType.TEXT+ single_line["_lines"] = _line_metadata(2)++ fallback_inline_caption_fragments(blocks, VISUAL_MAIN_TYPES)++ assert single_line["type"] == BlockType.TEXT+++def test_dict_leading_table_continuation_reads_top_level_content() -> None:+ """验证 dict 页首续表使用顶层 content 和 _lines 完成判断。"""+ continuation = {+ "index": 0,+ "type": BlockType.TEXT,+ "bbox": [0.0, 0.0, 80.0, 10.0],+ "content": "Table 1 (continued)",+ "_lines": _line_metadata(1),+ "merge_prev": True,+ }+ blocks = [+ continuation,+ {+ "index": 1,+ "type": BlockType.TABLE_BODY,+ "bbox": [0.0, 15.0, 80.0, 60.0],+ "content": "",+ },+ ]++ fallback_leading_table_continuation_captions(blocks, VISUAL_MAIN_TYPES)++ assert continuation["type"] == BlockType.CAPTION+ assert continuation["merge_prev"] is True+++def test_block_class_fallbacks_keep_attribute_access_compatibility() -> None:+ """验证两个 fallback 仍兼容使用属性和 Line 的 Block class。"""+ inline_companion = Block(+ index=1,+ type=BlockType.TEXT,+ bbox=(40.0, 0.0, 80.0, 10.0),+ lines=[Line(bbox=(40.0, 0.0, 80.0, 10.0))],+ content="continued caption",+ merge_prev=True,+ )+ inline_blocks = [+ Block(index=0, type=BlockType.CAPTION, bbox=(0.0, 0.0, 40.0, 10.0), content="Table 1"),+ inline_companion,+ Block(index=2, type=BlockType.TABLE_BODY, bbox=(0.0, 15.0, 80.0, 60.0)),+ ]+ fallback_inline_caption_fragments(inline_blocks, VISUAL_MAIN_TYPES)++ continuation = Block(+ index=0,+ type=BlockType.TEXT,+ bbox=(0.0, 0.0, 80.0, 10.0),+ lines=[Line(bbox=(0.0, 0.0, 80.0, 10.0))],+ content="Table 1 (continued)",+ merge_prev=True,+ )+ continuation_blocks = [+ continuation,+ Block(index=1, type=BlockType.TABLE_BODY, bbox=(0.0, 15.0, 80.0, 60.0)),+ ]+ fallback_leading_table_continuation_captions(continuation_blocks, VISUAL_MAIN_TYPES)++ assert inline_companion.type == BlockType.CAPTION+ assert inline_companion.merge_prev is True+ assert continuation.type == BlockType.CAPTION+ assert continuation.merge_prev is True