Skip to content

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

fix: enhance inline typography detection and improve paragraph title handling

myhloli3c938cb7126b
diff --git a/mineru/model/flash/pdf/auxiliary_text.py b/mineru/model/flash/pdf/auxiliary_text.py
index b6d56734..a651af45 100644
--- a/mineru/model/flash/pdf/auxiliary_text.py
+++ b/mineru/model/flash/pdf/auxiliary_text.py
@@ -567,10 +567,24 @@ def _footnote_lane_members(
rule_bbox[0] < lane.left
and lane.left - rule_bbox[0] <= 2.25 * median_height
)
- if not strict_left_alignment and not relaxed_left_alignment:
+ rule_center_y = _bbox_center_y(rule_bbox)
+ centered_short_alignment = (
+ not lane.is_span
+ and rule_center_y >= 0.7 * local_page_height
+ and 0.35 * lane_width <= rule_width <= 0.7 * lane_width
+ and abs(
+ _bbox_center_x(rule_bbox)
+ - 0.5 * (lane.left + lane.right)
+ )
+ <= 0.08 * lane_width
+ )
+ if (
+ not strict_left_alignment
+ and not relaxed_left_alignment
+ and not centered_short_alignment
+ ):
return set()
- rule_center_y = _bbox_center_y(rule_bbox)
is_regular_short_rule = (
rule_center_y >= 0.7 * local_page_height
and rule_width <= 0.65 * lane_width
@@ -582,7 +596,11 @@ def _footnote_lane_members(
and 0.65 * lane_width <= rule_width <= 1.05 * lane_width
and abs(rule_bbox[2] - lane.right) <= endpoint_tolerance
)
- if not is_regular_short_rule and not is_column_width_rule:
+ if (
+ not is_regular_short_rule
+ and not is_column_width_rule
+ and not centered_short_alignment
+ ):
return set()
# 首行采用较宽的 3.5% 页高窗口;命中后仅按紧凑的连续净空向下扩展。
@@ -650,6 +668,40 @@ def _footnote_lane_members(
or horizontal_overlap / max(0.1, rule_width) < 0.8
):
return set()
+ if centered_short_alignment:
+ reference_height = max(
+ median_height,
+ page_median_height or 0.0,
+ )
+ projecting_rows_above = []
+ for _line, bbox in lane_lines[:first_index]:
+ overlap = max(
+ 0.0,
+ min(rule_bbox[2], bbox[2])
+ - max(rule_bbox[0], bbox[0]),
+ )
+ row_width = max(0.1, bbox[2] - bbox[0])
+ if (
+ bbox[1] < rule_bbox[1]
+ and overlap
+ >= 0.2 * min(rule_width, row_width)
+ ):
+ projecting_rows_above.append(bbox)
+ if any(
+ bbox[3]
+ > rule_bbox[1] - 0.75 * reference_height
+ for bbox in projecting_rows_above
+ ):
+ # 分式横线位于公式成员之间;真正的脚注分隔线上方应保留正文净空。
+ return set()
+ member_height = statistics.median(
+ _line_effective_height(*member) for member in members
+ )
+ if (
+ len(members) < 2
+ or member_height > 0.9 * reference_height
+ ):
+ return set()
return {line.source_index for line, _bbox in members}
@@ -746,7 +798,7 @@ def _classify_rule_delimited_headers(pages: list[_PreparedPage]) -> None:
]
if not candidates:
continue
- separator = max(candidates, key=lambda item: _bbox_center_y(item.bbox))
+ separator = min(candidates, key=lambda item: _bbox_center_y(item.bbox))
separator_y = _bbox_center_y(separator.bbox)
local_lines = [
(
diff --git a/mineru/model/flash/pdf/formulas.py b/mineru/model/flash/pdf/formulas.py
index 8de407ba..29fa412b 100644
--- a/mineru/model/flash/pdf/formulas.py
+++ b/mineru/model/flash/pdf/formulas.py
@@ -16,6 +16,7 @@ from .document import PDFPathInfo
from ....utils.text import build_tagged_formula_content
from .models import (
+ _AxisLine,
_FormulaAnchor,
_LineItem,
_PageSource,
@@ -35,6 +36,7 @@ from .geometry import (
_coerce_bbox,
_expand_bbox,
_rotate_bbox_to_upright,
+ _transform_axis_lines,
)
from .native_text import _sanitize_pdf_control_text
from .line_layout import (
@@ -514,6 +516,8 @@ def _build_formula_like_blocks(
lines: list[_LineItem],
table_bboxes: list[BBox],
page_size: tuple[float, float],
+ *,
+ drawing_lines: list[_AxisLine] | None = None,
) -> tuple[list[dict[str, Any]], list[_LineItem]]:
"""仅依据栏带、右侧短锚点和空间连通关系聚合公式状区域。"""
@@ -534,6 +538,15 @@ def _build_formula_like_blocks(
median_height = statistics.median(effective_heights) if effective_heights else 1.0
local_page_width = page_size[1] if angle in {90, 270} else page_size[0]
local_page_height = page_size[0] if angle in {90, 270} else page_size[1]
+ local_horizontal_rules = [
+ rule.bbox
+ for rule in _transform_axis_lines(
+ drawing_lines or [],
+ page_size,
+ angle,
+ )
+ if rule.orientation == "horizontal"
+ ]
lanes = _infer_text_lanes(angle_geometry, local_page_width, median_height)
for lane in lanes:
if lane.is_span:
@@ -653,10 +666,21 @@ def _build_formula_like_blocks(
dominant_body_font,
median_height,
)
- if _formula_component_has_left_prose(
- members,
- lane,
- median_height,
+ has_isolated_numbered_fraction = (
+ _formula_component_has_isolated_numbered_fraction(
+ members,
+ lane,
+ median_height,
+ local_horizontal_rules,
+ )
+ )
+ if (
+ _formula_component_has_left_prose(
+ members,
+ lane,
+ median_height,
+ )
+ and not has_isolated_numbered_fraction
):
for member_line, _member_bbox in members:
member_line.paragraph_formula_context = True
@@ -749,6 +773,95 @@ def _formula_component_has_left_prose(
return False
+def _formula_component_has_isolated_numbered_fraction(
+ members: list[tuple[_LineItem, BBox]],
+ lane: _TextLane,
+ median_height: float,
+ horizontal_rules: list[BBox],
+) -> bool:
+ """用右侧编号、内部分数线和上下留白确认独立多层公式。"""
+
+ if len(members) < 3 or not horizontal_rules:
+ return False
+ markers = [
+ (line, bbox)
+ for line, bbox in members
+ if _standalone_formula_number_marker(line.text) is not None
+ ]
+ if len(markers) != 1:
+ return False
+ marker_line, marker_bbox = markers[0]
+ lane_width = max(0.1, lane.right - lane.left)
+ if (
+ marker_bbox[2] < lane.right - max(3.0, 0.08 * lane_width)
+ or marker_bbox[2] - marker_bbox[0] > 0.12 * lane_width
+ ):
+ return False
+ body_members = [
+ (line, bbox)
+ for line, bbox in members
+ if line is not marker_line
+ ]
+ if len(body_members) < 2:
+ return False
+ body_bbox = _bbox_union_many(
+ [bbox for _line, bbox in body_members],
+ )
+ member_sources = {
+ line.source_index for line, _bbox in members
+ }
+ rows_above = [
+ bbox
+ for line, bbox in lane.lines
+ if line.source_index not in member_sources
+ and bbox[3] <= body_bbox[1]
+ ]
+ rows_below = [
+ bbox
+ for line, bbox in lane.lines
+ if line.source_index not in member_sources
+ and bbox[1] >= body_bbox[3]
+ ]
+ if not rows_above or not rows_below:
+ return False
+ gap_above = body_bbox[1] - max(bbox[3] for bbox in rows_above)
+ gap_below = min(bbox[1] for bbox in rows_below) - body_bbox[3]
+ if min(gap_above, gap_below) < 0.75 * median_height:
+ return False
+
+ body_centers = [
+ _bbox_center_y(bbox) for _line, bbox in body_members
+ ]
+ for rule_bbox in horizontal_rules:
+ rule_width = rule_bbox[2] - rule_bbox[0]
+ if not (
+ 3.0 * median_height
+ <= rule_width
+ <= 0.75 * lane_width
+ ):
+ continue
+ horizontal_overlap = max(
+ 0.0,
+ min(rule_bbox[2], body_bbox[2])
+ - max(rule_bbox[0], body_bbox[0]),
+ )
+ if horizontal_overlap < 0.6 * rule_width:
+ continue
+ rule_center = _bbox_center_y(rule_bbox)
+ if (
+ any(
+ center <= rule_center - 0.1 * median_height
+ for center in body_centers
+ )
+ and any(
+ center >= rule_center + 0.1 * median_height
+ for center in body_centers
+ )
+ ):
+ return True
+ return False
+
+
def _is_wide_tagged_formula_member(
anchor_line: _LineItem,
member_line: _LineItem,
diff --git a/mineru/model/flash/pdf/line_merging.py b/mineru/model/flash/pdf/line_merging.py
index c190d69a..eaf081d6 100644
--- a/mineru/model/flash/pdf/line_merging.py
+++ b/mineru/model/flash/pdf/line_merging.py
@@ -29,6 +29,7 @@ from .native_text import (
)
from .line_layout import (
_connection_crosses_table,
+ _font_signatures_share_family,
_infer_text_lanes,
_line_effective_height,
)
@@ -370,6 +371,9 @@ def _merge_overlapping_inline_cluster(
median_glyph_width=host.median_glyph_width,
leading_emphasis_width=ordered_members[0].leading_emphasis_width,
leading_typography_width=ordered_members[0].leading_typography_width,
+ paragraph_formula_context=any(
+ line.paragraph_formula_context for line in ordered_members
+ ),
split_from_row=any(line.split_from_row for line in ordered_members),
preserve_split_boundary=any(line.preserve_split_boundary for line in ordered_members),
semantic_type=host.semantic_type,
@@ -669,6 +673,9 @@ def _merge_same_baseline_group(
else None,
leading_emphasis_width=members[0].leading_emphasis_width,
leading_typography_width=members[0].leading_typography_width,
+ paragraph_formula_context=any(
+ member.paragraph_formula_context for member in members
+ ),
split_from_row=any(member.split_from_row for member in members),
preserve_split_boundary=any(member.preserve_split_boundary for member in members),
semantic_type=members[0].semantic_type,
@@ -798,8 +805,19 @@ def _merge_title_resolved_visual_rows(
members,
page_size,
)
+ sparse_short_prefix_text = (
+ _is_sparse_short_prefix_two_run_row(
+ members,
+ page_size,
+ )
+ )
if semantic_type != "paragraph_title" and not (
- semantic_type is None and (len(font_signatures) > 1 or dense_same_font_text)
+ semantic_type is None
+ and (
+ len(font_signatures) > 1
+ or dense_same_font_text
+ or sparse_short_prefix_text
+ )
):
continue
local_geometry = [
@@ -816,7 +834,11 @@ def _merge_title_resolved_visual_rows(
_line_effective_height(*previous),
current[1],
_line_effective_height(*current),
- maximum_gap=3.0
+ maximum_gap=(
+ 5.0
+ if sparse_short_prefix_text
+ else 3.0
+ )
* max(
_line_effective_height(*previous),
_line_effective_height(*current),
@@ -981,6 +1003,76 @@ def _is_dense_same_font_two_run_row(
return occupied_width / max(0.1, union_bbox[2] - union_bbox[0]) >= 0.85
+def _is_sparse_short_prefix_two_run_row(
+ members: list[_LineItem],
+ page_size: tuple[float, float],
+) -> bool:
+ """识别同视觉行中被宽空白拆开的短前缀与宽正文。"""
+
+ if (
+ len(members) != 2
+ or not all(member.split_from_row for member in members)
+ or any(member.preserve_split_boundary for member in members)
+ or any(member.semantic_type is not None for member in members)
+ or any(member.font_signature is None for member in members)
+ or any(member.font_coverage < 0.7 for member in members)
+ ):
+ return False
+ ordered = sorted(members, key=lambda member: member.run_index)
+ if [member.run_index for member in ordered] != [0, 1]:
+ return False
+ if not _font_signatures_share_family(
+ ordered[0].font_signature,
+ ordered[1].font_signature,
+ ):
+ return False
+
+ local_geometry = [
+ (
+ member,
+ _rotate_bbox_to_upright(
+ member.bbox,
+ page_size,
+ member.angle,
+ ),
+ )
+ for member in ordered
+ ]
+ local_geometry.sort(
+ key=lambda item: (
+ item[1][0],
+ item[1][1],
+ item[0].source_index,
+ )
+ )
+ first, second = local_geometry
+ pair_height = max(
+ _line_effective_height(*first),
+ _line_effective_height(*second),
+ )
+ local_page_width = (
+ page_size[1]
+ if ordered[0].angle in {90, 270}
+ else page_size[0]
+ )
+ horizontal_gap = second[1][0] - first[1][2]
+ return (
+ first[1][2] - first[1][0] <= 2.0 * pair_height
+ and second[1][2] - second[1][0]
+ >= 0.3 * local_page_width
+ and 3.0 * pair_height
+ < horizontal_gap
+ <= 5.0 * pair_height
+ and _same_baseline_geometry(
+ first[1],
+ _line_effective_height(*first),
+ second[1],
+ _line_effective_height(*second),
+ maximum_gap=5.0 * pair_height,
+ )
+ )
+
+
def _can_restore_dense_split_visual_row(
members: list[_LineItem],
page_size: tuple[float, float],
@@ -1120,6 +1212,9 @@ def _merge_dense_split_visual_row(
else None,
leading_emphasis_width=ordered_members[0].leading_emphasis_width,
leading_typography_width=ordered_members[0].leading_typography_width,
+ paragraph_formula_context=any(
+ member.paragraph_formula_context for member in ordered_members
+ ),
split_from_row=False,
preserve_split_boundary=any(member.preserve_split_boundary for member in ordered_members),
semantic_type=ordered_members[0].semantic_type,
diff --git a/mineru/model/flash/pdf/pipeline.py b/mineru/model/flash/pdf/pipeline.py
index 5dc40858..38844399 100644
--- a/mineru/model/flash/pdf/pipeline.py
+++ b/mineru/model/flash/pdf/pipeline.py
@@ -101,6 +101,7 @@ from .auxiliary_text import (
from .titles import (
_classify_body_height_section_titles,
_classify_explicit_section_titles,
+ _classify_inline_typography_reset_titles,
_classify_document_structural_titles,
_classify_page_titles,
_infer_document_body_profile,
@@ -653,6 +654,7 @@ def _rebuild_canonical_formula_blocks(
formula_input,
prepared.table_bboxes,
prepared.page_size,
+ drawing_lines=prepared.drawing_lines,
)
return blocks
@@ -758,6 +760,7 @@ def _finalize_prepared_page(
formula_input,
prepared.table_bboxes,
prepared.page_size,
+ drawing_lines=prepared.drawing_lines,
)
if prepared.canonical_formula_geometry and prepared.canonical_formula_source_lines:
canonical_formula_blocks = _rebuild_canonical_formula_blocks(
@@ -793,6 +796,12 @@ def _finalize_prepared_page(
container_bboxes=title_container_bboxes,
document_body_profile=document_body_profile,
)
+ _classify_inline_typography_reset_titles(
+ remaining_lines,
+ prepared.page_size,
+ container_bboxes=title_container_bboxes,
+ document_body_profile=document_body_profile,
+ )
_classify_body_height_section_titles(
remaining_lines,
prepared.page_size,
diff --git a/mineru/model/flash/pdf/text_blocks.py b/mineru/model/flash/pdf/text_blocks.py
index e32f00ea..a639ed6a 100644
--- a/mineru/model/flash/pdf/text_blocks.py
+++ b/mineru/model/flash/pdf/text_blocks.py
@@ -400,7 +400,7 @@ def _split_page_footnote_entries(
median_glyph_width,
base_left,
)
- if len(marker_rows) >= 2:
+ if marker_rows:
return _split_marked_page_footnote_entries(
line_geometry,
marker_rows,
@@ -454,9 +454,100 @@ def _find_page_footnote_marker_rows(
continue
marker_rows.append((marker, body))
marker_rows.sort(key=lambda pair: (min(pair[0][1][1], pair[1][1][1]), pair[0][1][0]))
+ if not marker_rows:
+ marker_rows = _find_geometric_page_footnote_marker_rows(
+ line_geometry,
+ median_height,
+ median_glyph_width,
+ base_left,
+ )
return marker_rows
+def _find_geometric_page_footnote_marker_rows(
+ line_geometry: list[tuple[_LineItem, BBox]],
+ median_height: float,
+ median_glyph_width: float,
+ base_left: float,
+) -> list[tuple[tuple[_LineItem, BBox], tuple[_LineItem, BBox]]]:
+ """在 row id 缺失时用同基线窄标记和右侧正文恢复脚注首行。"""
+
+ marker_width_limit = max(
+ 1.5 * median_glyph_width,
+ 0.75 * median_height,
+ )
+ candidates: list[
+ tuple[
+ float,
+ tuple[_LineItem, BBox],
+ tuple[_LineItem, BBox],
+ ]
+ ] = []
+ for marker in line_geometry:
+ marker_width = marker[1][2] - marker[1][0]
+ if (
+ marker_width > marker_width_limit
+ or marker[1][0] - base_left > 0.5 * median_height
+ ):
+ continue
+ for body in line_geometry:
+ if body is marker:
+ continue
+ body_width = body[1][2] - body[1][0]
+ horizontal_gap = body[1][0] - marker[1][2]
+ if (
+ not 0.0 <= horizontal_gap <= 1.5 * median_height
+ or body[1][0] - base_left
+ < max(
+ 1.5 * marker_width,
+ 0.4 * median_height,
+ )
+ or body_width
+ < max(
+ 4.0 * marker_width,
+ 4.0 * median_glyph_width,
+ )
+ or abs(
+ _bbox_center_y(marker[1])
+ - _bbox_center_y(body[1])
+ )
+ > 0.4 * median_height
+ or not _title_fonts_compatible(
+ marker[0],
+ body[0],
+ )
+ ):
+ continue
+ candidates.append(
+ (
+ horizontal_gap,
+ marker,
+ body,
+ )
+ )
+
+ output = []
+ consumed_sources: set[int] = set()
+ for _gap, marker, body in sorted(
+ candidates,
+ key=lambda item: (
+ min(item[1][1][1], item[2][1][1]),
+ item[0],
+ item[1][0].source_index,
+ item[2][0].source_index,
+ ),
+ ):
+ pair_sources = {
+ marker[0].source_index,
+ body[0].source_index,
+ }
+ if pair_sources & consumed_sources:
+ continue
+ output.append((marker, body))
+ consumed_sources.update(pair_sources)
+ return output
+
+
def _split_marked_page_footnote_entries(
line_geometry: list[tuple[_LineItem, BBox]],
marker_rows: list[tuple[tuple[_LineItem, BBox], tuple[_LineItem, BBox]]],
@@ -859,6 +950,67 @@ def _leading_typography_reset_break_sources(
return output
+def _formula_style_text_row_break_sources(
+ lane: _TextLane,
+) -> set[int]:
+ """按相邻显示行几何拆分被公式检测回退为正文的独立文本行。"""
+
+ rows = sorted(
+ (item for item in lane.lines if item[0].semantic_type is None),
+ key=lambda item: (item[1][1], item[1][0], item[0].source_index),
+ )
+ lane_width = max(0.1, lane.right - lane.left)
+ matching_edges: set[int] = set()
+ for index, (previous, current) in enumerate(zip(rows, rows[1:])):
+ previous_line, previous_bbox = previous
+ current_line, current_bbox = current
+ if not (
+ previous_line.paragraph_formula_context
+ and current_line.paragraph_formula_context
+ ):
+ continue
+ previous_height = _line_effective_height(*previous)
+ current_height = _line_effective_height(*current)
+ minimum_height = min(previous_height, current_height)
+ maximum_height = max(previous_height, current_height)
+ if minimum_height < 0.75 * maximum_height:
+ continue
+ previous_width = previous_bbox[2] - previous_bbox[0]
+ current_width = current_bbox[2] - current_bbox[0]
+ if (
+ min(previous_width, current_width) < 0.45 * lane_width
+ or max(previous_width, current_width) > 0.95 * lane_width
+ ):
+ continue
+ lane_center = 0.5 * (lane.left + lane.right)
+ if (
+ abs(_bbox_center_x(previous_bbox) - lane_center) > 0.15 * lane_width
+ or abs(_bbox_center_x(current_bbox) - lane_center) > 0.15 * lane_width
+ ):
+ continue
+ vertical_overlap = max(
+ 0.0,
+ min(previous_bbox[3], current_bbox[3])
+ - max(previous_bbox[1], current_bbox[1]),
+ )
+ top_pitch = current_bbox[1] - previous_bbox[1]
+ pair_height = statistics.median((previous_height, current_height))
+ if (
+ vertical_overlap <= 0.2 * minimum_height
+ and 0.9 * pair_height <= top_pitch <= 2.0 * pair_height
+ ):
+ matching_edges.add(index)
+
+ output: set[int] = set()
+ for index in matching_edges:
+ output.add(rows[index][0].source_index)
+ output.add(rows[index + 1][0].source_index)
+ if index + 2 < len(rows):
+ # 同时保护显示行组后的正文起点,避免上下文恢复阶段重新跨界合并。
+ output.add(rows[index + 2][0].source_index)
+ return output
+
+
def _front_matter_keyword_break_sources(
lane: _TextLane,
local_page_height: float,
@@ -1032,11 +1184,16 @@ def _build_text_blocks(
regular_gap,
gap_mad,
)
+ formula_text_break_sources = _formula_style_text_row_break_sources(
+ lane,
+ )
structured_break_sources.update(visual_reset_sources)
structured_break_sources.update(typography_reset_sources)
+ structured_break_sources.update(formula_text_break_sources)
protected_break_sources: set[int] = set()
protected_break_sources.update(visual_reset_sources)
protected_break_sources.update(typography_reset_sources)
+ protected_break_sources.update(formula_text_break_sources)
protected_break_sources.update(
_front_matter_keyword_break_sources(
lane,
diff --git a/mineru/model/flash/pdf/titles.py b/mineru/model/flash/pdf/titles.py
index 0ecd1636..1335ee84 100644
--- a/mineru/model/flash/pdf/titles.py
+++ b/mineru/model/flash/pdf/titles.py
@@ -428,6 +428,7 @@ def _classify_page_titles(
local_page_width,
local_page_height,
local_container_bboxes,
+ page_index=page_index,
document_title_bottom=document_title_bottom,
)
_classify_cross_lane_emphasized_section_titles(
@@ -459,6 +460,11 @@ def _classify_page_titles(
_expand_cross_lane_paragraph_title_neighbors(
line_geometry,
)
+ _demote_hanging_multiline_text_titles(
+ lanes,
+ document_body_profile,
+ page_index=page_index,
+ )
_demote_cross_lane_body_continuation_titles(
line_geometry,
lanes,
@@ -1125,6 +1131,138 @@ def _collect_legacy_paragraph_title_sources(
return output
+def _classify_inline_typography_reset_titles(
+ lines: list[_LineItem],
+ page_size: tuple[float, float],
+ *,
+ container_bboxes: list[BBox],
+ document_body_profile: _DocumentBodyProfile | None,
+) -> None:
+ """用短段尾、字体切换和缩进正文识别行内结构标题。"""
+
+ if document_body_profile is None:
+ return
+ for angle in sorted(
+ {
+ line.angle
+ for line in lines
+ if line.semantic_type is None and not line.title_suppressed
+ }
+ ):
+ line_geometry = [
+ (line, _rotate_bbox_to_upright(line.bbox, page_size, angle))
+ for line in lines
+ if line.angle == angle
+ and line.semantic_type is None
+ and not line.title_suppressed
+ ]
+ if len(line_geometry) < 3:
+ continue
+ median_height = statistics.median(
+ _line_effective_height(line, bbox)
+ for line, bbox in line_geometry
+ )
+ local_page_width = (
+ page_size[1] if angle in {90, 270} else page_size[0]
+ )
+ lanes = _infer_text_lanes(
+ line_geometry,
+ local_page_width,
+ median_height,
+ )
+ local_container_bboxes = [
+ _rotate_bbox_to_upright(bbox, page_size, angle)
+ for bbox in container_bboxes
+ ]
+ for lane in lanes:
+ rows = sorted(
+ lane.lines,
+ key=lambda item: (
+ item[1][1],
+ item[1][0],
+ item[0].source_index,
+ ),
+ )
+ lane_width = max(0.1, lane.right - lane.left)
+ for previous, current, following in zip(
+ rows,
+ rows[1:],
+ rows[2:],
+ ):
+ previous_line, previous_bbox = previous
+ current_line, current_bbox = current
+ following_line, following_bbox = following
+ if any(
+ line.semantic_type is not None
+ for line in (
+ previous_line,
+ current_line,
+ following_line,
+ )
+ ):
+ continue
+ if (
+ previous_line.font_signature is None
+ or current_line.font_signature is None
+ or following_line.font_signature is None
+ or previous_line.font_coverage < 0.65
+ or current_line.font_coverage < 0.65
+ or following_line.font_coverage < 0.65
+ ):
+ continue
+ if not _font_signatures_share_family(
+ previous_line.font_signature,
+ following_line.font_signature,
+ ) or _font_signatures_share_family(
+ current_line.font_signature,
+ previous_line.font_signature,
+ ):
+ continue
+ previous_height = _line_effective_height(*previous)
+ current_height = _line_effective_height(*current)
+ following_height = _line_effective_height(*following)
+ neighbor_height = statistics.median(
+ (previous_height, following_height),
+ )
+ pair_height = max(
+ previous_height,
+ current_height,
+ following_height,
+ )
+ previous_width = previous_bbox[2] - previous_bbox[0]
+ current_width = current_bbox[2] - current_bbox[0]
+ following_width = following_bbox[2] - following_bbox[0]
+ following_indent = following_bbox[0] - current_bbox[0]
+ if not (
+ previous_width <= 0.45 * lane_width
+ and 0.2 * lane_width
+ <= current_width
+ <= 0.65 * lane_width
+ and following_width >= 0.75 * lane_width
+ and abs(current_bbox[0] - lane.left)
+ <= 0.75 * pair_height
+ and 0.75 * pair_height
+ <= following_indent
+ <= 3.0 * pair_height
+ and 0.85
+ <= current_height / max(0.1, neighbor_height)
+ <= 1.15
+ and -0.25 * pair_height
+ <= _effective_text_row_gap(previous, current)
+ <= 0.5 * pair_height
+ and -0.25 * pair_height
+ <= _effective_text_row_gap(current, following)
+ <= 0.5 * pair_height
+ and not _line_inside_visual_container(
+ current_bbox,
+ local_container_bboxes,
+ )
+ ):
+ continue
+ current_line.semantic_type = "paragraph_title"
+ current_line.structural_title = True
+
+
def _classify_body_height_section_titles(
lines: list[_LineItem],
page_size: tuple[float, float],
@@ -1867,6 +2005,7 @@ def _classify_cross_lane_centered_section_titles(
local_page_height: float,
container_bboxes: list[BBox],
*,
+ page_index: int,
document_title_bottom: float | None,
) -> None:
"""用正文栏中心、上下留白和正文邻行补标被单独推成窄栏的标题。"""
@@ -1896,6 +2035,31 @@ def _classify_cross_lane_centered_section_titles(
continue
if not 0.75 <= line_height / max(0.1, profile.body_height) <= 1.3:
continue
+ uses_regular_font = (
+ profile.body_font is not None
+ and line.font_signature is not None
+ and line.font_coverage >= 0.75
+ and _font_signatures_share_family(
+ line.font_signature,
+ profile.body_font,
+ )
+ )
+ weight_emphasized = (
+ profile.body_weight is not None
+ and line.dominant_font_weight is not None
+ and line.dominant_font_weight
+ >= max(
+ profile.body_weight + 100.0,
+ 1.15 * profile.body_weight,
+ )
+ )
+ if (
+ page_index == 0
+ and line_height < 0.9 * profile.body_height
+ and uses_regular_font
+ and not weight_emphasized
+ ):
+ continue
if document_title_bottom is not None and bbox[1] <= document_title_bottom + profile.body_height:
continue
if _line_inside_visual_container(bbox, container_bboxes) or _line_near_visual_container(
@@ -2204,6 +2368,67 @@ def _expand_cross_lane_paragraph_title_neighbors(
break
+def _demote_hanging_multiline_text_titles(
+ lanes: list[_TextLane],
+ document_body_profile: _DocumentBodyProfile | None,
+ *,
+ page_index: int,
+) -> None:
+ """把缩进满行后回到栏左缘的紧邻标题行组降回正文。"""
+
+ if page_index != 0 or document_body_profile is None:
+ return
+ body_height = max(0.1, document_body_profile.body_height)
+ for lane in lanes:
+ rows = sorted(
+ lane.lines,
+ key=lambda item: (
+ item[1][1],
+ item[1][0],
+ item[0].source_index,
+ ),
+ )
+ lane_width = max(0.1, lane.right - lane.left)
+ for first, second in zip(rows, rows[1:]):
+ first_line, first_bbox = first
+ second_line, second_bbox = second
+ if (
+ first_line.semantic_type != "paragraph_title"
+ or second_line.semantic_type != "paragraph_title"
+ or first_line.font_signature is None
+ or second_line.font_signature is None
+ or first_line.font_coverage < 0.75
+ or second_line.font_coverage < 0.75
+ or not _font_signatures_share_family(
+ first_line.font_signature,
+ second_line.font_signature,
+ )
+ ):
+ continue
+ first_height = _line_effective_height(*first)
+ second_height = _line_effective_height(*second)
+ pair_height = max(first_height, second_height)
+ if not (
+ max(first_height, second_height) <= 1.1 * body_height
+ and min(first_height, second_height)
+ >= 0.85 * pair_height
+ and first_bbox[2] - first_bbox[0]
+ >= 0.9 * lane_width
+ and first_bbox[0] - lane.left
+ >= 0.75 * pair_height
+ and abs(second_bbox[0] - lane.left)
+ <= 0.5 * pair_height
+ and -0.25 * pair_height
+ <= _effective_text_row_gap(first, second)
+ <= 0.5 * pair_height
+ ):
+ continue
+ first_line.semantic_type = None
+ second_line.semantic_type = None
+ first_line.title_suppressed = True
+ second_line.title_suppressed = True
+
+
def _demote_visual_container_caption_titles(
line_geometry: list[tuple[_LineItem, BBox]],
container_bboxes: list[BBox],
@@ -2520,6 +2745,12 @@ def _classify_paragraph_titles_in_lane(
and width_ratio <= 0.7
and gap_above >= 0.35
and gap_below >= 0.2
+ and (
+ page_index != 0
+ or style_differs
+ or weight_emphasized
+ or title_prototype is not None
+ )
and _has_following_compact_text_section(
rows,
index,
diff --git a/tests/fixtures/flash_layout_block_expectations.json b/tests/fixtures/flash_layout_block_expectations.json
index f8844f71..38d4ddc3 100644
--- a/tests/fixtures/flash_layout_block_expectations.json
+++ b/tests/fixtures/flash_layout_block_expectations.json
@@ -13,7 +13,7 @@
"image": 8,
"page_footnote": 4,
"page_number": 9,
- "paragraph_title": 19,
+ "paragraph_title": 20,
"table": 3,
"text": 107
},
@@ -24,6 +24,11 @@
"page_index": 9,
"type": "doc_title",
"text": "Predictionoffrozensiltysandshearstrengthbasedonfourensemblelearningmethods:Boosting,Bagging,Stacking,andVoting"
+ },
+ {
+ "page_index": 6,
+ "type": "paragraph_title",
+ "text": "6.3模型可解释性分析"
}
],
"same_block_groups": [
@@ -58,6 +63,10 @@
"page_index": 6,
"fragments": ["图7 Voting模型SHAP", "Fig. 7 SHAP"]
},
+ {
+ "page_index": 6,
+ "fragments": ["复杂非线性关系", "6. 3 模型可解释性分析"]
+ },
{
"page_index": 9,
"fragments": ["Abstract: Traditional", "Key words: silty sand"]
diff --git a/tests/fixtures/flash_layout_geometry_manifest.json b/tests/fixtures/flash_layout_geometry_manifest.json
index 218cf822..b0597ffe 100644
--- a/tests/fixtures/flash_layout_geometry_manifest.json
+++ b/tests/fixtures/flash_layout_geometry_manifest.json
@@ -1688,8 +1688,8 @@
"pages": [
{
"page_index": 0,
- "fingerprint": "e46fca959d5504b15e732375aa734c40978f93c0de5195ff16bcc99dd19f54ee",
- "bbox_fingerprint": "ae846c6e1c50420352bd7f13831a1d6fefb8bf2cfa1ec42bd4710d7cef138af8",
+ "fingerprint": "b4bbd21b66f82e1803dd9a0ee3b0920b56673d75bd22f0af65629eb105206dc1",
+ "bbox_fingerprint": "08d17943df25430fd9d9b0ddb7154b749007d9b62b03a8a16b3e1797e5d3f8b1",
"approved": true,
"blocks": [
{
@@ -1743,8 +1743,8 @@
"text_sha256": "02cf9232fc3245b652ef4f7afa9070651c50d4db1570a58658829c44c8978c60"
},
{
- "id": "8d25c49da5d35d6d2ea5",
- "type": "paragraph_title",
+ "id": "95d31b7a9234880f8e89",
+ "type": "text",
"text_sha256": "7c7778403626878488f7023a3c4de3d791bfdad18e6e49b3c791f218701bacdf"
},
{
@@ -1995,8 +1995,8 @@
},
{
"page_index": 4,
- "fingerprint": "c31e76e61163cd637ddd7880b03ecd20d38af83a7f5e84f31887d26854a5608b",
- "bbox_fingerprint": "5a25a03d678fc4ac4119bfbe957914ea8fc8d62b683d4a66b0c7d4bafe13ee64",
+ "fingerprint": "9628364973945ac7ffbceb9094aa09b9d25a0c88783b814637291bce20122ea6",
+ "bbox_fingerprint": "2f52c50b71aa97c63efc144b8760ae9841b1b97b522f121dc366198d8e006300",
"approved": true,
"blocks": [
{
@@ -2030,22 +2030,32 @@
"text_sha256": "7552137a67fb2ac7a638e6692a4bc2726a4b97648ef4453cdb362ed56b9392f8"
},
{
- "id": "0a8438790b171f49603a",
+ "id": "90b762039431c0a6448b",
+ "type": "text",
+ "text_sha256": "4cd94246420951d0d567809fa3c727cc1977dffae66a8cf958e021cffe9bf908"
+ },
+ {
+ "id": "167b2b9852af9de174d7",
+ "type": "equation",
+ "text_sha256": "dfe3ef59c8039d28b0d0ca5196fb00048187a471e5c28ec92fb5e0303253000b"
+ },
+ {
+ "id": "0cbd1bfb78d0edc47805",
"type": "text",
- "text_sha256": "8b5ee5f6e28b02aad8ea0fbfa0edacc03ea8045985d2d56c9806d9709a317ea7"
+ "text_sha256": "562aec27358bc7b0b93bcbb0087f68e9cef37280537893343cffdb9df8ba68fd"
},
{
- "id": "453cfa56214ad489830a",
+ "id": "16143b1cf3c631d9174e",
"type": "paragraph_title",
"text_sha256": "12bf6117a0cf755f6032a86d5ce516263c3a2c6135d35589cb6025ea4d9b1c20"
},
{
- "id": "223733e5d1cf66934184",
+ "id": "728db11892f5e9d4ce4b",
"type": "text",
"text_sha256": "6042376df88b1af9dd545c6d43f1048db2ccfadf27c4bd2391e27fc1d37ed871"
},
{
- "id": "114814c50d26fafc922b",
+ "id": "6e4b731e3d2d75b6103c",
"type": "text",
"text_sha256": "9361fd7e69ebb026b08b0584d8dacbf1ba49110596c109a3d6e4c8ab57334766"
}
@@ -3318,8 +3328,8 @@
},
{
"page_index": 1,
- "fingerprint": "0312f0981bf8e43822179788fa84911f89bdedf2703cd23d33fd022952c63ddd",
- "bbox_fingerprint": "7fea71c3ca97609e6bba7bc667793d22444e95c87392514af71a248704a5d84b",
+ "fingerprint": "f731d4f92b827171331c9750a8bacdec710bcd0ee42d328e68b52e917cc5af91",
+ "bbox_fingerprint": "fea79353f1c7ae689e51b07626b50bc8f7fa086863b7713a2841a52efee92342",
"approved": true,
"blocks": [
{
@@ -3383,17 +3393,42 @@
"text_sha256": "4e0df7ac09bb1838dfe50dac0b1c0905c55dd6372fb155f5826d36a6fd988fed"
},
{
- "id": "1fd66179b489dbb9511e",
+ "id": "4b41479c78bf6a9ab3c4",
"type": "text",
- "text_sha256": "0bd5632323c7a3b6a115b608c61da8de99ec31fcb4596fc82a91784414b02a7c"
+ "text_sha256": "a7deb9ac13cef6a0b01791d528db80665e462ce43baf8b6650e184a900e5f80c"
},
{
- "id": "6372be2369f6ac2f5ba1",
+ "id": "b83d4b80d80194bfdbd5",
+ "type": "text",
+ "text_sha256": "cf96888be8eab4706a22af42f53aa145adc4cf0990ea6e74edb76522ce122a28"
+ },
+ {
+ "id": "e604e6732f6fa3a25370",
+ "type": "text",
+ "text_sha256": "e178e9c84e2552fd07e661002e8beb562983438e8023564830aa81b7533e7c88"
+ },
+ {
+ "id": "5156175f839c82a82fa4",
+ "type": "text",
+ "text_sha256": "96a895d38f61e5a6fd9f8c8b5ff532ee586cad7b7e18bd33e9126430fe089509"
+ },
+ {
+ "id": "53d2a8e949f4c703aa96",
+ "type": "text",
+ "text_sha256": "0f8a8d40d30d2d688f4e59230827e68a5c81245caeac732357e0dd5bef6ef9d1"
+ },
+ {
+ "id": "f8b7aa29fbe4b8280233",
+ "type": "text",
+ "text_sha256": "09330392069d15f92ede2d3b00e8319db9967eb9cd7c307bbcdcea4851235d38"
+ },
+ {
+ "id": "e0be937b1c24fb8d3761",
"type": "text",
"text_sha256": "5b701b612f7af50e675eec957793faf679450e0d65fcba083596f1f4352bc8a5"
},
{
- "id": "6372e868c6288265da7c",
+ "id": "078d2b6bc7b3ad4834a2",
"type": "text",
"text_sha256": "bb4b1c20e14fdf6f6c6007724149b97031c4c1b2e422bbef44b20c02c0742d18"
}
@@ -5305,24 +5340,14 @@
},
{
"page_index": 4,
- "fingerprint": "e04a6b24d35787220c21bb0862f3a29afd2f215b7fbb8c19155edbcab9659405",
- "bbox_fingerprint": "d4816d45a9161d255276bc9f749f8d7ce4d942cabbbc8880eef838714b74cfcd",
+ "fingerprint": "1fda4ad3d4e335660f9c69e5cf463d27c30162a1192dc2f6291958c69775484d",
+ "bbox_fingerprint": "0b138cb777fc302b11a8a7aa751f87b0ebf9935a39fa4498ae864f594daf513c",
"approved": true,
"blocks": [
{
- "id": "2d4c69ceae8ab5947c54",
- "type": "text",
- "text_sha256": "a73cd1cd354a4eb428eb6cf4b6afd0dbb9a86b7924d27d9482488182322d601f"
- },
- {
- "id": "cd2ab15a170578dec4f5",
- "type": "text",
- "text_sha256": "db74232fb5ae9916b2e31ed8e308f01b350f04a0268bb96cb952f36608353d4a"
- },
- {
- "id": "27f87331b49b473eebac",
+ "id": "4bd2d09a383d2905deed",
"type": "text",
- "text_sha256": "96536377e30cb9e0f2467ee564ab57998de0ae69ecb8a90fbd5b0912f99bf44e"
+ "text_sha256": "8fe66dd6c9a7811d715553d43a33b978e86b6f6a1147570db3a08375fc459f5b"
}
]
},
@@ -5776,8 +5801,8 @@
"pages": [
{
"page_index": 0,
- "fingerprint": "dab8a2742706b52d09610157d06c148130510dd1c22f671fe7ba7b03dda93ef3",
- "bbox_fingerprint": "d69cb86cd1706e2f0fc1e5eab5c44e57bf47d850f3b0f78a6ea9de809cd65c42",
+ "fingerprint": "72c39d7d1f3a7cc60d3dde026fb3654916d141334070ae02c1463db3e7b5aef0",
+ "bbox_fingerprint": "e7ab56193d2a18a40dfbcc9b6ee4b0727c1308086e22bfa84b0aeee148a730b8",
"approved": true,
"blocks": [
{
@@ -5806,8 +5831,8 @@
"text_sha256": "c11473547a75fce472bb77f54bb858831921ca198b72f013fb09567efcd64c25"
},
{
- "id": "78f66ff4b0d8f39026b7",
- "type": "paragraph_title",
+ "id": "ec5089ae604aee24d47a",
+ "type": "text",
"text_sha256": "9d30dde8ee163e6e73865feb0510d861f58e6c846f2c73ff148e8281dcacaf4b"
},
{
@@ -5831,60 +5856,60 @@
"text_sha256": "ec47451fa783f51c69c3d206031c740dd3f39cc2c1da8e893172c5358de299d7"
},
{
- "id": "7e265fb4b3eef4d7294d",
- "type": "text",
- "text_sha256": "a9fb955ce587f26703b04a272245b3d7bfe5a9d06f0fe1f42673fb511dd84d53"
- },
- {
- "id": "de911d857cf3fd5b7018",
+ "id": "c51c5e1ca429115e01ff",
"type": "text",
"text_sha256": "c5852fe0cd6e4fbad8ee1c831b5419cd16b11be64e982ed2919a1fc96574c9e7"
},
{
- "id": "0cab1b7ab368ab6ed1ac",
+ "id": "5d688bc44d3f6b231392",
"type": "text",
"text_sha256": "4dd1e53be4e52209c41df7a11a179f56196df1bfe1ac7299bf1ea0de49aa7f19"
},
{
- "id": "37ac29dee9f4af40a63c",
+ "id": "9e958e141c2844a253fd",
"type": "text",
"text_sha256": "5c189634bc6ca31edc8d8343ee2bb8cc879561d2897a64ff41da543e1f52bb83"
},
{
- "id": "83d497070d9bd61c5c17",
+ "id": "aaab80001c958d0597a7",
"type": "text",
"text_sha256": "d84e8068a6b7539e99ffab1f025a62be73ebc972299985da2b98b03574b4e1bc"
},
{
- "id": "d485d0d340541a40f380",
+ "id": "78e1bafe58f510bf9765",
"type": "text",
"text_sha256": "1cba3042e6b78643e22ae7cf43ba6210fee3ab2bfe0fb7c8786f89e173a1d4db"
},
{
- "id": "43f82388c4b774fd47d3",
+ "id": "e64ca7d3d7459e0f81bb",
"type": "paragraph_title",
"text_sha256": "d2007812d83bfb9742c8fd3ab52912770f5701e76133c1b1e76e854ce70eb266"
},
{
- "id": "6d7a6803fc3eb0835a39",
+ "id": "4383ad10bdd24915d5f2",
"type": "text",
"text_sha256": "6c410fb5fe214fd8d18bf95fc673bccabb735a13e02053cb92e00cff0220bf0a"
},
{
- "id": "d41aaf13f252a5fb65bc",
+ "id": "5e47cf2f26c6252ba96e",
"type": "paragraph_title",
"text_sha256": "c3304c4b3c7b07e2cbe68ac6f7395bad0704c5079c71511384403f54005c4fb2"
},
{
- "id": "b308202b3b8109c090f3",
+ "id": "2a61bf374c290f8c3793",
"type": "text",
"text_sha256": "58b75401b349adf9326133d107b23bfa7507e133fc08187df8110ccbc80cf114"
},
{
- "id": "f6f2c35f510855ae7c73",
+ "id": "f60ac1d965c54c0888cd",
"type": "text",
"text_sha256": "a78a29a531806c47fa2d74e9b1ec0e173e692fa5e1775ce08dd16e1ff1ab721f"
},
+ {
+ "id": "162acafa1975133b992f",
+ "type": "page_footnote",
+ "text_sha256": "a9fb955ce587f26703b04a272245b3d7bfe5a9d06f0fe1f42673fb511dd84d53"
+ },
{
"id": "c9e2ac9fc830f48d1182",
"type": "page_number",
@@ -6012,8 +6037,8 @@
},
{
"page_index": 2,
- "fingerprint": "0e8f2d2e800e88378a811f9ba625fdc09257440fe550b4c0938536f4b250600c",
- "bbox_fingerprint": "7159577d98c023709358082a5ce2bebe4096a59c8a321fe6b08b1889dbe0e562",
+ "fingerprint": "223f8dc3618805a8688095824d638d00348540403923ffddb74ba95a3a1961df",
+ "bbox_fingerprint": "d11e2e4659f5b97eaaa59b8f7f5e683cc1781e5a5f5d1429e111dad7141e4327",
"approved": true,
"blocks": [
{
@@ -6107,17 +6132,12 @@
"text_sha256": "fedaa1e2117c326bf9444f4579597c1256df4730bf40cdcc759997c56fb65dac"
},
{
- "id": "23045320ebc37dada848",
- "type": "footer",
- "text_sha256": "6b86b273ff34fce19d6b804eff5a3f5747ada4eaa22f1d49c01e52ddb7875b4b"
- },
- {
- "id": "ad2422a15e049f851739",
- "type": "footer",
- "text_sha256": "e716b531157f01340032ae8ed02522e9e494e6daee7a6eef6d76d5d5ad894486"
+ "id": "17e7f46dacc367a28a14",
+ "type": "page_footnote",
+ "text_sha256": "acaace41e0f4c0904b024bbdedfc903eacbbab7cd0354948fc95ba2b44e6230d"
},
{
- "id": "c032284058bedf167dd7",
+ "id": "9f3e3a007a3d7b70852a",
"type": "page_number",
"text_sha256": "c75de23d89df36ba921287616ee8edb4c986e328a78e033e57c1e5e2b59c838e"
}
@@ -7101,8 +7121,8 @@
},
{
"page_index": 4,
- "fingerprint": "bbda0829957990677363f6f28ad9f6f128106789cd530e70824c80cf81d4e68a",
- "bbox_fingerprint": "6d26b88152f2b0efac3260b9b368ce95eccad94a4ff31fbcce6dc58a21f9c31e",
+ "fingerprint": "ee5fa5b6311e65b4c30e668dab6f41b0acaff6a3e01c48aa17355037e136103c",
+ "bbox_fingerprint": "04a002521778c5ef296d5a3468c8c1c111cc54dcbac2457ee3f6a23f11c735ab",
"approved": true,
"blocks": [
{
@@ -7111,8 +7131,8 @@
"text_sha256": "e621e45a6e91c9b435d2277e5881e35c672bf4dea5f5fe97e4f3d31b1bd55ca9"
},
{
- "id": "f0100b41628690e6b70d",
- "type": "header",
+ "id": "3154ae97012af28fb1e4",
+ "type": "paragraph_title",
"text_sha256": "8646455289923797a9a5576483014d5c7491b57f5ccb9e2d03710d6973cc850a"
},
{
@@ -7194,8 +7214,8 @@
},
{
"page_index": 5,
- "fingerprint": "7b0ddefc03ea2ba86d6b3a5bc04afe7a3b40bdb2b93287bbe4419cf66ba3702e",
- "bbox_fingerprint": "0211730429cf3f6cd7da7892041ad80a0f32abba4bc4a7ef8b2f2bb15188f07d",
+ "fingerprint": "abee9b87128e89537f59454f078156f7981d9b35405b07ce7b5039cdb1c4f1a4",
+ "bbox_fingerprint": "1b3887124eca5f098165bcde0ead0c84c99f6d68821d0640164e5f585c2aa9fe",
"approved": true,
"blocks": [
{
@@ -7204,8 +7224,8 @@
"text_sha256": "e621e45a6e91c9b435d2277e5881e35c672bf4dea5f5fe97e4f3d31b1bd55ca9"
},
{
- "id": "177b5c4b7097edfb5abb",
- "type": "header",
+ "id": "f10b6a1c62513a22639f",
+ "type": "paragraph_title",
"text_sha256": "a87df76b1a09965db82f66c767ed64164699ca560708937019bd27e3d630d1f3"
},
{
@@ -7403,8 +7423,8 @@
},
{
"page_index": 8,
- "fingerprint": "a07314a7c4b68adb0ece7c2720dcbc1dd4e4d55c59e71d4e70fee44b327447e0",
- "bbox_fingerprint": "efb051c658ccdf1eb1561753201b99530d3c0c1efe100949b1791b8d0d3d475c",
+ "fingerprint": "bd2c489e665e33ef2be2698a6b2bb1a817ddb16684c472f18e34012222b85860",
+ "bbox_fingerprint": "43606b72732bb29ccf312bf71309022a826b33017d48b4650f7a0dcaf56598b0",
"approved": true,
"blocks": [
{
@@ -7413,8 +7433,8 @@
"text_sha256": "e621e45a6e91c9b435d2277e5881e35c672bf4dea5f5fe97e4f3d31b1bd55ca9"
},
{
- "id": "ef04c910f001283f66de",
- "type": "header",
+ "id": "a96645f7ceac79cb35f8",
+ "type": "paragraph_title",
"text_sha256": "7ee7507d3752347c3b6e65d15587c514f50048b70c8ce11eba61a0dd34759e72"
},
{
@@ -8399,8 +8419,8 @@
},
{
"page_index": 20,
- "fingerprint": "0a06e6a42842f8e97de7a2fa29f5658bbb255814f1683e7441db4aee684a30c9",
- "bbox_fingerprint": "82756c8f3d2c1d4eeeb3614cb2ff4de2daa40c6ef8bdd1dc750ca3f432ddc4fb",
+ "fingerprint": "4f72bd516fc028cfc2d17f2d1647d26e1dea45198a4705a820d87536d658bb11",
+ "bbox_fingerprint": "36cab81b68bfd7a83382069a7fb91858d50964898633c75d9923278997ecbb3b",
"approved": true,
"blocks": [
{
@@ -8409,8 +8429,8 @@
"text_sha256": "e621e45a6e91c9b435d2277e5881e35c672bf4dea5f5fe97e4f3d31b1bd55ca9"
},
{
- "id": "5032d14db36f668b6a0a",
- "type": "header",
+ "id": "4d246468ea4573e82436",
+ "type": "paragraph_title",
"text_sha256": "c5a05e28cb10b9843a7ce608d6ad356964a00905865a83ba5f3af9b90e96df25"
},
{
@@ -8540,8 +8560,8 @@
},
{
"page_index": 22,
- "fingerprint": "d3f16a83db63a9612880079c5818bb029ec766133764d56698382146350a3efc",
- "bbox_fingerprint": "a11f12941a85624f638011fbba9105debac09d13226a49f6cea8d0244730f933",
+ "fingerprint": "ebcfbcd786596b5c47f347926faef96aedde8edb48f25e0007e981e60b3385bb",
+ "bbox_fingerprint": "addbf86173a037336742ecbef64caf6651b996c3d06e029348e6b5b14d2121b4",
"approved": true,
"blocks": [
{
@@ -8550,8 +8570,8 @@
"text_sha256": "e621e45a6e91c9b435d2277e5881e35c672bf4dea5f5fe97e4f3d31b1bd55ca9"
},
{
- "id": "ee474615eb2c2da9507d",
- "type": "header",
+ "id": "d283ffe1a482bcd4d6d6",
+ "type": "paragraph_title",
"text_sha256": "3a64ec6c86e308ea41258d0ff0a7b9306fd9a7da5c4d4a59a159729b24896910"
},
{
@@ -9399,8 +9419,8 @@
},
{
"page_index": 6,
- "fingerprint": "80f5c0b1d5c1d4ee7ebe9c83ea0ce48596850c07c942d6be19cb9add993ff028",
- "bbox_fingerprint": "6f07236d9115ebb5914e5dccff8d327cb507c03798c310a037197b842eabdcca",
+ "fingerprint": "7093af52db65ac5491bd3a20e36a6299c73af7113004330af0c4172618c6381f",
+ "bbox_fingerprint": "3e679b89e6fef4a2a71c55bf4e52f046ed824301bbfb2c01408e841b62caa060",
"approved": true,
"blocks": [
{
@@ -9439,47 +9459,52 @@
"text_sha256": "7c11ebe833676e3a53d7c2777896574224cff03d4c884b4340417deeed4cc284"
},
{
- "id": "0ec99e88a58b6a346c61",
+ "id": "b4e821a41e572a89a5c1",
"type": "text",
- "text_sha256": "542a031e25466774aff8fd74df63c2fb645d334bd1db05dfd6b4f074fa40b452"
+ "text_sha256": "f51d0e57f573aeadd1c8132bf4bdaa338b7949f4cc8f79e2e79abc29b2377c1e"
+ },
+ {
+ "id": "4b46ebf20502d08b0bbf",
+ "type": "paragraph_title",
+ "text_sha256": "ff241d23124105fcf9c7eee91f34263230c6fdd5f72d7b0039d786c6a756edae"
},
{
- "id": "cc581d5fab902b76a74d",
+ "id": "0bf258dbf2c347d755da",
"type": "text",
"text_sha256": "bf81ef7deb799153eaa6d7e099886a0f21ca0fc091498a5f01b590e9542c214d"
},
{
- "id": "6d0cb52bcf2015eaad36",
+ "id": "7579f7af942447ca2c2f",
"type": "text",
"text_sha256": "f163947b758fe28bcec199a6c82ed0a8c7051d27976b07da135281ef6b795299"
},
{
- "id": "96c028b6e4a106f23cff",
+ "id": "b7a4675a867e0a13cad4",
"type": "image",
"text_sha256": "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855"
},
{
- "id": "b337b485803d0f9d7f14",
+ "id": "bdf58b5e396dfcafae0d",
"type": "caption",
"text_sha256": "57a288d618c4d708e98540cb72a54be8c812fa8d1e2d187090e44fd64821c967"
},
{
- "id": "6520b932bd07c3ebc85d",
+ "id": "8b59ebbfd59fe047f089",
"type": "caption",
"text_sha256": "cc4ad5f116908d1b0a072b4877631c71e5c2f7ba3fbdbb4e1192d5caae2b1d25"
},
{
- "id": "7ba5d8ec4f5168dcabf9",
+ "id": "82f5951220d50da45012",
"type": "image",
"text_sha256": "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855"
},
{
- "id": "80258a45b7ea7f492624",
+ "id": "13d19f5fcef9c948a7b5",
"type": "caption",
"text_sha256": "004d9889cd3a0cc90d9220af8822dd0467c206c717348c500518d292ccd486ab"
},
{
- "id": "d4d2c015eb7f098241e2",
+ "id": "ba432c116536a2a3e9a6",
"type": "caption",
"text_sha256": "22fb4bd58e5d4ea4833c165213c3d3bca00a7e6fe8aaf4d85faef339559c030d"
}
diff --git a/tests/unittest/_flash_pdf_test_utils.py b/tests/unittest/_flash_pdf_test_utils.py
index 76609b51..1de01564 100644
--- a/tests/unittest/_flash_pdf_test_utils.py
+++ b/tests/unittest/_flash_pdf_test_utils.py
@@ -22,6 +22,7 @@ def _text_line(
median_glyph_width: float | None = None,
leading_emphasis_width: float | None = None,
leading_typography_width: float | None = None,
+ paragraph_formula_context: bool = False,
preserve_split_boundary: bool = False,
semantic_type: str | None = None,
ink_bbox: tuple[float, float, float, float] | None = None,
@@ -43,6 +44,7 @@ def _text_line(
median_glyph_width=median_glyph_width,
leading_emphasis_width=leading_emphasis_width,
leading_typography_width=leading_typography_width,
+ paragraph_formula_context=paragraph_formula_context,
split_from_row=split_from_row,
preserve_split_boundary=preserve_split_boundary,
semantic_type=semantic_type,
diff --git a/tests/unittest/test_flash_pdf_auxiliary_text.py b/tests/unittest/test_flash_pdf_auxiliary_text.py
index aecbd8f8..46c3f38f 100644
--- a/tests/unittest/test_flash_pdf_auxiliary_text.py
+++ b/tests/unittest/test_flash_pdf_auxiliary_text.py
@@ -473,6 +473,103 @@ def test_page_footnote_rejects_left_shifted_rule_with_single_or_body_size_row()
assert all(line.semantic_type is None for line in lines)
+def test_page_footnote_accepts_centered_short_rule_with_small_rows() -> None:
+ """验证栏内居中短横线可凭页面位置和小字号连续行确认脚注。"""
+
+ body = [
+ _text_line(
+ f"body {index}",
+ (100.0, 180.0 + 80.0 * index, 500.0, 190.0 + 80.0 * index),
+ index,
+ effective_height=10.0,
+ )
+ for index in range(4)
+ ]
+ notes = [
+ _text_line(
+ "note one",
+ (100.0, 862.0, 490.0, 870.0),
+ 4,
+ effective_height=8.0,
+ ),
+ _text_line(
+ "note two",
+ (100.0, 874.0, 460.0, 882.0),
+ 5,
+ effective_height=8.0,
+ ),
+ ]
+ page = _prepared_text_page(
+ *body,
+ *notes,
+ page_size=(1000.0, 1000.0),
+ )
+ page.drawing_lines = [
+ models._AxisLine(
+ (200.0, 850.0, 400.0, 852.0),
+ 1.0,
+ "horizontal",
+ )
+ ]
+
+ auxiliary_text._classify_page_auxiliary_text(page)
+
+ assert all(line.semantic_type is None for line in body)
+ assert all(line.semantic_type == "page_footnote" for line in notes)
+ assert page.page_footnote_groups == [{4, 5}]
+
+
+def test_page_footnote_rejects_centered_fraction_rule_without_upper_clearance() -> None:
+ """验证夹在上下公式层级之间的居中横线不会触发页脚注。"""
+
+ body = [
+ _text_line(
+ f"body {index}",
+ (100.0, 180.0 + 80.0 * index, 500.0, 190.0 + 80.0 * index),
+ index,
+ effective_height=10.0,
+ )
+ for index in range(4)
+ ]
+ formula_rows = [
+ _text_line(
+ "formula numerator",
+ (100.0, 842.0, 500.0, 852.0),
+ 4,
+ effective_height=10.0,
+ ),
+ _text_line(
+ "formula denominator",
+ (100.0, 854.0, 490.0, 862.0),
+ 5,
+ effective_height=8.0,
+ ),
+ _text_line(
+ "formula tail",
+ (100.0, 866.0, 460.0, 874.0),
+ 6,
+ effective_height=8.0,
+ ),
+ ]
+ page = _prepared_text_page(
+ *body,
+ *formula_rows,
+ page_size=(1000.0, 1000.0),
+ )
+ page.drawing_lines = [
+ models._AxisLine(
+ (200.0, 850.0, 400.0, 852.0),
+ 1.0,
+ "horizontal",
+ )
+ ]
+
+ auxiliary_text._classify_page_auxiliary_text(page)
+
+ assert page.page_footnote_groups == []
+ assert all(line.semantic_type is None for line in formula_rows)
+
+
def test_page_footnote_accepts_lower_half_column_width_rule_with_smaller_text() -> None:
"""验证页面下半部的栏宽横线可凭字号收缩识别单栏脚注。"""
@@ -692,6 +789,65 @@ def test_page_footnote_entries_keep_same_left_compact_continuation() -> None:
]
+def test_page_footnote_single_geometric_marker_keeps_group_together() -> None:
+ """验证 row id 缺失的单个窄编号首行仍可聚合整组连续脚注。"""
+
+ note_font = ("NoteFont", 0)
+ lines = [
+ _text_line(
+ "1",
+ (10.0, 80.0, 12.0, 88.0),
+ 0,
+ visual_row_id=10,
+ effective_height=8.0,
+ font_signature=note_font,
+ font_coverage=1.0,
+ median_glyph_width=2.0,
+ semantic_type="page_footnote",
+ ),
+ _text_line(
+ "first source",
+ (14.0, 80.5, 60.0, 88.5),
+ 1,
+ visual_row_id=11,
+ effective_height=8.0,
+ font_signature=note_font,
+ font_coverage=1.0,
+ median_glyph_width=2.0,
+ semantic_type="page_footnote",
+ ),
+ _text_line(
+ "second source",
+ (10.0, 92.0, 70.0, 100.0),
+ 2,
+ effective_height=8.0,
+ font_signature=note_font,
+ font_coverage=1.0,
+ median_glyph_width=2.0,
+ semantic_type="page_footnote",
+ ),
+ _text_line(
+ "third source",
+ (10.0, 104.0, 65.0, 112.0),
+ 3,
+ effective_height=8.0,
+ font_signature=note_font,
+ font_coverage=1.0,
+ median_glyph_width=2.0,
+ semantic_type="page_footnote",
+ ),
+ ]
+
+ entries = text_blocks._split_page_footnote_entries(
+ lines,
+ (100.0, 140.0),
+ )
+
+ assert [[line.source_index for line in entry] for entry in entries] == [
+ [0, 1, 2, 3],
+ ]
+
+
def test_page_footnote_entries_split_hanging_indent_and_tighten_boxes() -> None:
"""验证 Boosting 型续行缩进拆成四条脚注,且异常高字符框不再互相覆盖。"""
@@ -1026,6 +1182,38 @@ def test_top_rule_marks_only_unclassified_text_above_it_as_header() -> None:
]
+def test_top_rule_uses_first_separator_before_later_section_rule() -> None:
+ """验证页首多条长横线只采用最上方有效页眉分隔线。"""
+
+ header = _text_line("header", (100.0, 30.0, 300.0, 40.0), 0)
+ section = _text_line("section", (100.0, 90.0, 300.0, 110.0), 1)
+ body = _text_line("body", (100.0, 150.0, 700.0, 160.0), 2)
+ page = _prepared_text_page(
+ header,
+ section,
+ body,
+ page_size=(1000.0, 1000.0),
+ )
+ page.drawing_lines = [
+ models._AxisLine(
+ (80.0, 60.0, 920.0, 62.0),
+ 1.0,
+ "horizontal",
+ ),
+ models._AxisLine(
+ (80.0, 120.0, 920.0, 122.0),
+ 1.0,
+ "horizontal",
+ ),
+ ]
+
+ auxiliary_text._classify_rule_delimited_headers([page])
+
+ assert header.semantic_type == "header"
+ assert section.semantic_type is None
+ assert body.semantic_type is None
+
+
def test_top_rule_uses_ink_bbox_when_loose_bbox_crosses_separator() -> None:
"""验证 loose 框穿过页首横线时仍按真实字形区分线上日期和线下 DOI。"""
diff --git a/tests/unittest/test_flash_pdf_char_geometry.py b/tests/unittest/test_flash_pdf_char_geometry.py
index 24849def..357afd49 100644
--- a/tests/unittest/test_flash_pdf_char_geometry.py
+++ b/tests/unittest/test_flash_pdf_char_geometry.py
@@ -428,7 +428,7 @@ def test_versioned_mixed_text_fixture_keeps_normal_geometry_identity() -> None:
diagnostics: list[dict[str, object]] = []
with pipeline.PDFDocument(pdf_bytes) as document:
- pipeline._analyze_native_document(document, geometry_diagnostics=diagnostics) # noqa: SLF001
+ pages = pipeline._analyze_native_document(document, geometry_diagnostics=diagnostics) # noqa: SLF001
geometry = diagnostics[0]
assert not any(
@@ -439,6 +439,25 @@ def test_versioned_mixed_text_fixture_keeps_normal_geometry_identity() -> None:
line["state"] != "healthy" # type: ignore[index]
for line in geometry["line_repairs"] # type: ignore[index]
)
+ for page_index, section_text in (
+ (4, "1 前言"),
+ (5, "2 准备工作"),
+ (8, "3 工具安装"),
+ (20, "4 常见问题"),
+ (22, "5 附录:"),
+ ):
+ section = next(
+ block
+ for block in pages[page_index]
+ if section_text in str(block.get("content"))
+ )
+ security = next(
+ block
+ for block in pages[page_index]
+ if "文档密级:秘密" in str(block.get("content"))
+ )
+ assert section["type"] == "paragraph_title"
+ assert security["type"] == "header"
def test_flash_layout_manifest_uses_portable_repository_paths() -> None:
diff --git a/tests/unittest/test_flash_pdf_formulas.py b/tests/unittest/test_flash_pdf_formulas.py
index 669f46c3..866dafc3 100644
--- a/tests/unittest/test_flash_pdf_formulas.py
+++ b/tests/unittest/test_flash_pdf_formulas.py
@@ -218,6 +218,67 @@ def test_inline_prose_formula_component_returns_to_paragraph_context() -> None:
assert {line.source_index for line in remaining} == set(range(7))
+def test_isolated_numbered_fraction_overrides_left_prose_shape() -> None:
+ """验证带分数线和上下净空的编号多层公式不被变量名误判为正文。"""
+
+ body_font = ("Body", 0)
+ lines = [
+ _text_line(
+ "preceding body",
+ (0.0, 0.0, 100.0, 10.0),
+ 0,
+ font_signature=body_font,
+ font_coverage=1.0,
+ ),
+ _text_line(
+ "FZ SSEs KSSEc=SSEx",
+ (0.0, 25.0, 60.0, 35.0),
+ 1,
+ font_signature=("Math", 0),
+ font_coverage=0.6,
+ ),
+ _text_line(
+ "SSEc=dfc",
+ (25.0, 36.0, 45.0, 46.0),
+ 2,
+ font_signature=("Math", 0),
+ font_coverage=0.6,
+ ),
+ _text_line(
+ "(7)",
+ (91.0, 31.0, 100.0, 41.0),
+ 3,
+ font_signature=body_font,
+ font_coverage=1.0,
+ ),
+ _text_line(
+ "following body",
+ (0.0, 60.0, 100.0, 70.0),
+ 4,
+ font_signature=body_font,
+ font_coverage=1.0,
+ ),
+ ]
+
+ lane = models._TextLane(
+ left=0.0,
+ right=100.0,
+ lines=[(line, line.bbox) for line in lines],
+ )
+
+ assert formulas._formula_component_has_left_prose(
+ [(line, line.bbox) for line in lines[1:4]],
+ lane,
+ 10.0,
+ )
+ assert formulas._formula_component_has_isolated_numbered_fraction(
+ [(line, line.bbox) for line in lines[1:4]],
+ lane,
+ 10.0,
+ [(10.0, 34.5, 60.0, 35.0)],
+ )
+
+
def _vector_path(
bbox: tuple[float, float, float, float],
source_index: int,
diff --git a/tests/unittest/test_flash_pdf_samples.py b/tests/unittest/test_flash_pdf_samples.py
index a83372d8..941e93c0 100644
--- a/tests/unittest/test_flash_pdf_samples.py
+++ b/tests/unittest/test_flash_pdf_samples.py
@@ -322,16 +322,16 @@ def test_explicit_pdf_fixtures_keep_expected_txt_block_inventory() -> None:
{
"caption": 10,
"doc_title": 1,
- "equation": 6,
+ "equation": 7,
"footer": 1,
"footnote": 5,
"header": 12,
"image": 8,
"page_footnote": 1,
"page_number": 12,
- "paragraph_title": 18,
+ "paragraph_title": 17,
"table": 5,
- "text": 84,
+ "text": 86,
}
),
),
@@ -363,7 +363,7 @@ def test_explicit_pdf_fixtures_keep_expected_txt_block_inventory() -> None:
"page_footnote": 6,
"paragraph_title": 21,
"table": 9,
- "text": 119,
+ "text": 124,
}
),
),
@@ -384,7 +384,7 @@ def test_explicit_pdf_fixtures_keep_expected_txt_block_inventory() -> None:
}
),
),
- "demo6.pdf": (7, Counter({"image": 2, "paragraph_title": 9, "text": 24})),
+ "demo6.pdf": (7, Counter({"image": 2, "paragraph_title": 9, "text": 22})),
"mixed_elements_pages_03_06.pdf": (
4,
Counter(
@@ -409,10 +409,11 @@ def test_explicit_pdf_fixtures_keep_expected_txt_block_inventory() -> None:
"caption": 6,
"code": 3,
"doc_title": 1,
- "footer": 3,
+ "footer": 1,
"image": 2,
"page_number": 4,
- "paragraph_title": 16,
+ "page_footnote": 2,
+ "paragraph_title": 15,
"table": 2,
"text": 57,
}
@@ -452,7 +453,7 @@ def test_explicit_pdf_fixtures_keep_expected_txt_block_inventory() -> None:
"image": 8,
"page_footnote": 4,
"page_number": 9,
- "paragraph_title": 19,
+ "paragraph_title": 20,
"table": 3,
"text": 107,
}
@@ -543,7 +544,7 @@ def test_demo1_keeps_five_real_tables_without_formula_false_positive() -> None:
model_list = _native_model_list("demo1.pdf")
- assert [len(page) for page in model_list] == [16, 9, 12, 18, 10, 13, 11, 10, 12, 7, 10, 26, 9]
+ assert [len(page) for page in model_list] == [16, 9, 12, 18, 12, 13, 11, 10, 12, 7, 10, 26, 9]
assert [sum(block["type"] == "table" for block in page) for page in model_list] == [
0,
0,
@@ -562,7 +563,7 @@ def test_demo1_keeps_five_real_tables_without_formula_false_positive() -> None:
assert sum(block["type"] == "doc_title" for page in model_list for block in page) == 1
assert sum(block["type"] == "header" for page in model_list for block in page) == 12
assert sum(block["type"] == "page_number" for page in model_list for block in page) == 12
- assert sum(block["type"] == "equation" for page in model_list for block in page) == 6
+ assert sum(block["type"] == "equation" for page in model_list for block in page) == 7
assert sum(block["type"] == "caption" for page in model_list for block in page) == 10
assert sum(block["type"] == "footnote" for page in model_list for block in page) == 5
middle_pages = model_json_to_pages(_model_json(model_list))
@@ -599,6 +600,18 @@ def test_demo1_keeps_five_real_tables_without_formula_false_positive() -> None:
assert "The F-statistic was calculated as:" in _visible_content(
inline_statistics,
)
+ formula7 = next(
+ block
+ for block in model_list[4]
+ if block["type"] == "equation"
+ and r"\tag{7}" in str(block.get("content", ""))
+ )
+ formula_index = model_list[4].index(formula7)
+ assert [
+ model_list[4][formula_index - 1]["type"],
+ formula7["type"],
+ model_list[4][formula_index + 1]["type"],
+ ] == ["text", "equation", "text"]
assert [[child["type"] for child in group["content"]] for group in _grouped_visual_blocks(model_list[5], "table")] == [
["table_caption", "table_body", "table_footnote"],
["table_caption", "table_body", "table_footnote"],
@@ -612,6 +625,12 @@ def test_demo1_keeps_five_real_tables_without_formula_false_positive() -> None:
assert "doi:10.1016/j.jhydrol.2005.01.006" in _visible_content(copyright_block)
assert model_list[0].index(page1_footnotes[0]) < model_list[0].index(copyright_block)
assert next(block for block in model_list[0] if _visible_content(block) == "Abstract")["type"] == "paragraph_title"
+ received = next(
+ block
+ for block in model_list[0]
+ if _visible_content(block).startswith("Received 1 October")
+ )
+ assert received["type"] == "text"
assert next(block for block in model_list[6] if _visible_content(block).startswith("4.2."))["type"] == "paragraph_title"
@@ -931,7 +950,7 @@ def test_demo3_keeps_tables_and_covers_every_native_source_line() -> None:
]
assert [len(page) for page in model_list] == [
23,
- 15,
+ 20,
13,
21,
19,
@@ -941,7 +960,7 @@ def test_demo3_keeps_tables_and_covers_every_native_source_line() -> None:
17,
18,
]
- assert sum(len(page) for page in model_list) == 173
+ assert sum(len(page) for page in model_list) == 178
assert sum(block["type"] == "caption" for page in model_list for block in page) == 8
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"])
@@ -1062,27 +1081,34 @@ def test_demo3_pages1_and2_fix_title_front_matter_and_embedding_list() -> None:
assert tables_body["type"] == "text"
section_title = next(block for block in page2 if _visible_content(block).startswith("2 Preliminaries:"))
- embedding_list = next(
- block
- for block in page2
- if _visible_content(block).startswith("token ids (W)")
+ embedding_labels = (
+ "token ids (W)",
+ "positional ids (B)",
+ "segment ids (G)",
+ "column ids (C)",
+ "row ids (R)",
+ "rank ids (Z)",
)
+ embedding_blocks = [
+ next(
+ block
+ for block in page2
+ if _visible_content(block).startswith(label)
+ )
+ for label in embedding_labels
+ ]
assert section_title["type"] == "paragraph_title"
assert _visible_content(section_title) == "2 Preliminaries: TAPAS for Table Encoding"
- assert embedding_list["type"] == "text"
- assert not [block for block in page2 if block["type"] == "equation"]
- embedding_text = _visible_content(embedding_list)
+ assert len({id(block) for block in embedding_blocks}) == 6
+ assert all(block["type"] == "text" for block in embedding_blocks)
assert all(
- label in embedding_text
- for label in (
- "token ids (W)",
- "positional ids (B)",
- "segment ids (G)",
- "column ids (C)",
- "row ids (R)",
- "rank ids (Z)",
+ page2.index(first) < page2.index(second)
+ for first, second in zip(
+ embedding_blocks,
+ embedding_blocks[1:],
)
)
+ assert not [block for block in page2 if block["type"] == "equation"]
as_model_blocks = [
block
for block in page2
@@ -1566,6 +1592,18 @@ def test_demo6_default3_targeted_title_regressions() -> None:
assert sum("签名" in str(block["content"]) for block in model_list[6]) == 1
assert sum("盖章" in str(block["content"]) for block in model_list[6]) == 1
assert not [block for page in model_list for block in page if block["type"] in {"caption", "footnote"}]
+ page5 = model_list[4]
+ assert len(page5) == 1
+ assert page5[0]["type"] == "text"
+ assert all(
+ probe in _visible_content(page5[0])
+ for probe in (
+ "1.2",
+ "[2014]68 号",
+ "1.3",
+ "1.4",
+ )
+ )
def test_mixed_elements_pages_03_06_force_txt_regressions() -> None:
@@ -2007,6 +2045,17 @@ def test_mixed_elements_pages_07_10_force_txt_regressions() -> None:
matches = _blocks_containing(page7, probe)
assert len(matches) == 1
assert matches[0]["type"] == "text"
+ keywords = _blocks_containing(
+ page7,
+ "Keywords-program slicing",
+ )
+ funding_note = _blocks_containing(
+ page7,
+ "The work was partially funded",
+ )
+ assert len(keywords) == len(funding_note) == 1
+ assert keywords[0]["type"] == "text"
+ assert funding_note[0]["type"] == "page_footnote"
code_blocks = [block for block in page8 if block["type"] == "code"]
assert len(code_blocks) == 3
@@ -2054,7 +2103,27 @@ def test_mixed_elements_pages_07_10_force_txt_regressions() -> None:
assert experiment_title[0]["type"] == "paragraph_title"
url_footer = _blocks_containing(page9, "http://klee.github.io")
assert len(url_footer) == 1
- assert url_footer[0]["type"] == "footer"
+ assert url_footer[0]["type"] == "page_footnote"
+ bottom_footnotes = [
+ block
+ for block in page9
+ if block["type"] == "page_footnote"
+ and block["bbox"][1] >= 0.85
+ ]
+ assert len(bottom_footnotes) == 1
+ assert all(
+ probe in _visible_content(bottom_footnotes[0])
+ for probe in (
+ "http://klee.github.io",
+ "llvm-slicing",
+ "liuml07/giri",
+ )
+ )
+ assert sum(
+ block["type"] == "page_footnote"
+ for page in model_list
+ for block in page
+ ) == 2
reference5 = _blocks_containing(page10, "[5] A. Srivastava")
assert len(reference5) == 1
diff --git a/tests/unittest/test_flash_pdf_text_blocks.py b/tests/unittest/test_flash_pdf_text_blocks.py
index 1fa1c3e4..a02e7bd8 100644
--- a/tests/unittest/test_flash_pdf_text_blocks.py
+++ b/tests/unittest/test_flash_pdf_text_blocks.py
@@ -832,6 +832,105 @@ def test_inline_scripts_and_touching_low_coverage_runs_are_recovered() -> None:
)
+def test_title_resolved_visual_row_merges_sparse_short_prefix() -> None:
+ """验证同视觉行的短前缀与远端宽正文按字体族和基线恢复。"""
+
+ lines = [
+ _text_line(
+ "prefix",
+ (0.0, 0.0, 15.0, 10.0),
+ 0,
+ visual_row_id=7,
+ run_index=0,
+ split_from_row=True,
+ font_signature=("SimSun", 0),
+ font_coverage=1.0,
+ ),
+ _text_line(
+ "wide body",
+ (60.0, 0.0, 140.0, 10.0),
+ 1,
+ visual_row_id=7,
+ run_index=1,
+ split_from_row=True,
+ font_signature=("ABCDEF+SimSun", 0),
+ font_coverage=1.0,
+ ),
+ ]
+
+ merged = line_merging._merge_title_resolved_visual_rows(
+ lines,
+ (200.0, 100.0),
+ )
+
+ assert len(merged) == 1
+ assert merged[0].text == "prefix wide body"
+
+
+ "failure_mode",
+ [
+ "font-conflict",
+ "different-row",
+ "protected-boundary",
+ "two-wide-runs",
+ ],
+)
+def test_title_resolved_visual_row_rejects_weak_sparse_prefix(
+ failure_mode: str,
+) -> None:
+ """验证字体、行身份、保护边界或前缀宽度不符时保留拆分。"""
+
+ prefix_bbox = (
+ (0.0, 0.0, 40.0, 10.0)
+ if failure_mode == "two-wide-runs"
+ else (0.0, 0.0, 15.0, 10.0)
+ )
+ lines = [
+ _text_line(
+ "prefix",
+ prefix_bbox,
+ 0,
+ visual_row_id=7,
+ run_index=0,
+ split_from_row=True,
+ preserve_split_boundary=(
+ failure_mode == "protected-boundary"
+ ),
+ font_signature=("SimSun", 0),
+ font_coverage=1.0,
+ ),
+ _text_line(
+ "wide body",
+ (85.0, 0.0, 165.0, 10.0)
+ if failure_mode == "two-wide-runs"
+ else (60.0, 0.0, 140.0, 10.0),
+ 1,
+ visual_row_id=(
+ 8 if failure_mode == "different-row" else 7
+ ),
+ run_index=1,
+ split_from_row=True,
+ font_signature=(
+ ("OtherFont", 0)
+ if failure_mode == "font-conflict"
+ else ("ABCDEF+SimSun", 0)
+ ),
+ font_coverage=1.0,
+ ),
+ ]
+
+ merged = line_merging._merge_title_resolved_visual_rows(
+ lines,
+ (200.0, 100.0),
+ )
+
+ assert [line.text for line in merged] == [
+ "prefix",
+ "wide body",
+ ]
+
+
def test_canonical_reference_scale_merges_bracket_marker_but_not_plain_number() -> None:
"""验证 loose 高度相同的括号引用可按 PDF 字号并入宿主,普通数字保持独立。"""
@@ -2540,6 +2639,104 @@ def test_short_tail_and_leading_typography_run_split_structured_text() -> None:
assert [block["content"] for block in control] == ["tail opener body"]
+def test_same_baseline_merge_preserves_paragraph_formula_context() -> None:
+ """验证公式回退上下文在同行碎片合并后不会丢失。"""
+
+ fragments = [
+ _text_line(
+ "left",
+ (0.0, 0.0, 40.0, 10.0),
+ 0,
+ font_signature=("Math", 0),
+ font_coverage=0.5,
+ paragraph_formula_context=True,
+ ),
+ _text_line(
+ "right",
+ (40.1, 0.0, 80.0, 10.0),
+ 1,
+ font_signature=("Math", 0),
+ font_coverage=0.5,
+ ),
+ ]
+
+ merged = line_merging._merge_same_baseline_text_lines(
+ fragments,
+ (100.0, 100.0),
+ [],
+ )
+
+ assert len(merged) == 1
+ assert merged[0].paragraph_formula_context
+
+
[email protected]("row_count", [2, 3, 6])
+def test_formula_style_text_rows_split_pairwise_without_count_gate(
+ row_count: int,
+) -> None:
+ """验证两行起任意长度的独立公式样式文本均按相邻行拆分。"""
+
+ rows = [
+ _text_line(
+ f"row {index}",
+ (20.0 - index, 14.0 * index, 80.0 + index, 14.0 * index + 10.0),
+ index,
+ font_signature=("Math", 0),
+ font_coverage=0.5,
+ paragraph_formula_context=True,
+ )
+ for index in range(row_count)
+ ]
+ lane = models._TextLane(
+ left=0.0,
+ right=100.0,
+ lines=[(row, row.bbox) for row in rows],
+ )
+
+ break_sources = text_blocks._formula_style_text_row_break_sources(lane)
+
+ assert break_sources == set(range(row_count))
+
+
+ "second_bbox",
+ [
+ (35.0, 11.0, 65.0, 21.0),
+ (20.0, 5.0, 80.0, 15.0),
+ ],
+ ids=["narrow-denominator", "overlapping-script-tier"],
+)
+def test_formula_style_text_rows_keep_connected_math_fragments(
+ second_bbox: tuple[float, float, float, float],
+) -> None:
+ """验证窄分母和垂直重叠层级不会被拆成独立正文块。"""
+
+ rows = [
+ _text_line(
+ "formula body",
+ (20.0, 0.0, 80.0, 10.0),
+ 0,
+ paragraph_formula_context=True,
+ ),
+ _text_line(
+ "math fragment",
+ second_bbox,
+ 1,
+ paragraph_formula_context=True,
+ ),
+ ]
+
+ blocks = text_blocks._build_text_blocks(
+ rows,
+ [],
+ (100.0, 100.0),
+ )
+
+ assert [block["content"] for block in blocks] == [
+ "formula body math fragment",
+ ]
+
+
def test_native_typography_separates_loose_height_from_canonical_em() -> None:
"""验证异常 loose 字符高度不会覆盖 PDF 字号形成的 canonical 尺度。"""
diff --git a/tests/unittest/test_flash_pdf_titles.py b/tests/unittest/test_flash_pdf_titles.py
index 64a43b65..b94233c9 100644
--- a/tests/unittest/test_flash_pdf_titles.py
+++ b/tests/unittest/test_flash_pdf_titles.py
@@ -7,6 +7,7 @@ import pytest
from mineru.model.flash.pdf import (
line_layout,
line_merging,
+ models,
text_blocks,
titles,
)
@@ -859,6 +860,187 @@ def test_first_page_centered_body_style_metadata_does_not_use_title_fallback() -
assert lines[0].semantic_type is None
+def test_first_page_centered_small_regular_metadata_requires_emphasis() -> None:
+ """验证首页正文区的小字号常规字体元数据不会仅凭居中和留白晋升。"""
+
+ body_font = ("Body", 0)
+ lines = [
+ _text_line(
+ "body above one",
+ (0.0, 10.0, 100.0, 20.0),
+ 0,
+ font_signature=body_font,
+ font_coverage=1.0,
+ ),
+ _text_line(
+ "body above two",
+ (0.0, 22.0, 100.0, 32.0),
+ 1,
+ font_signature=body_font,
+ font_coverage=1.0,
+ ),
+ _text_line(
+ "centered metadata",
+ (25.0, 48.0, 75.0, 55.0),
+ 2,
+ effective_height=7.0,
+ font_signature=body_font,
+ font_coverage=1.0,
+ dominant_font_weight=400.0,
+ ),
+ _text_line(
+ "compact body one",
+ (0.0, 72.0, 100.0, 80.0),
+ 3,
+ effective_height=8.0,
+ font_signature=body_font,
+ font_coverage=1.0,
+ ),
+ _text_line(
+ "compact body two",
+ (0.0, 82.0, 100.0, 90.0),
+ 4,
+ effective_height=8.0,
+ font_signature=body_font,
+ font_coverage=1.0,
+ ),
+ _text_line(
+ "compact body three",
+ (0.0, 92.0, 100.0, 100.0),
+ 5,
+ effective_height=8.0,
+ font_signature=body_font,
+ font_coverage=1.0,
+ ),
+ ]
+ profile = models._DocumentBodyProfile(
+ body_height=10.0,
+ body_weight=400.0,
+ regular_fonts=frozenset({body_font}),
+ )
+
+ titles._classify_page_titles(
+ lines,
+ (100.0, 140.0),
+ page_index=0,
+ container_bboxes=[],
+ document_body_profile=profile,
+ )
+
+ assert lines[2].semantic_type is None
+
+
+def test_first_page_hanging_title_rows_demote_to_text_geometry() -> None:
+ """验证近满栏缩进首行接栏左续行的标题组在首页降回正文。"""
+
+ heading_font = ("Heading", 0)
+ first = _text_line(
+ "first row",
+ (10.0, 10.0, 100.0, 20.0),
+ 0,
+ font_signature=heading_font,
+ font_coverage=1.0,
+ semantic_type="paragraph_title",
+ )
+ second = _text_line(
+ "second row",
+ (0.0, 20.2, 55.0, 30.2),
+ 1,
+ font_signature=heading_font,
+ font_coverage=1.0,
+ semantic_type="paragraph_title",
+ )
+ lane = models._TextLane(
+ left=0.0,
+ right=100.0,
+ lines=[
+ (first, first.bbox),
+ (second, second.bbox),
+ ],
+ )
+ profile = models._DocumentBodyProfile(
+ body_height=10.0,
+ body_weight=400.0,
+ regular_fonts=frozenset({("Body", 0)}),
+ )
+
+ titles._demote_hanging_multiline_text_titles(
+ [lane],
+ profile,
+ page_index=0,
+ )
+
+ assert first.semantic_type is None
+ assert second.semantic_type is None
+ assert first.title_suppressed
+ assert second.title_suppressed
+
+
+def test_inline_typography_reset_promotes_only_distinct_middle_row() -> None:
+ """验证短段尾、异字体短行和缩进正文的三行结构只提升中间行。"""
+
+ body_font = ("Body", 0)
+ heading_font = ("Heading", 0)
+ lines = [
+ _text_line(
+ "short tail",
+ (0.0, 0.0, 25.0, 10.0),
+ 0,
+ font_signature=body_font,
+ font_coverage=1.0,
+ ),
+ _text_line(
+ "inline heading",
+ (0.0, 11.0, 45.0, 21.0),
+ 1,
+ font_signature=heading_font,
+ font_coverage=1.0,
+ ),
+ _text_line(
+ "indented body",
+ (18.0, 22.0, 100.0, 32.0),
+ 2,
+ font_signature=body_font,
+ font_coverage=1.0,
+ ),
+ ]
+ profile = models._DocumentBodyProfile(
+ body_height=10.0,
+ body_weight=400.0,
+ regular_fonts=frozenset({body_font}),
+ )
+
+ titles._classify_inline_typography_reset_titles(
+ lines,
+ (100.0, 100.0),
+ container_bboxes=[],
+ document_body_profile=profile,
+ )
+ control = [
+ _text_line(
+ line.text,
+ line.bbox,
+ line.source_index,
+ font_signature=body_font,
+ font_coverage=1.0,
+ )
+ for line in lines
+ ]
+ titles._classify_inline_typography_reset_titles(
+ control,
+ (100.0, 100.0),
+ container_bboxes=[],
+ document_body_profile=profile,
+ )
+
+ assert [line.semantic_type for line in lines] == [
+ None,
+ "paragraph_title",
+ None,
+ ]
+ assert all(line.semantic_type is None for line in control)
+
+
def test_cross_column_document_title_uses_thirteen_tenths_body_height_fallback() -> None:
"""验证首页跨栏居中标题达到正文 1.30 倍时可命中,作者行保持正文类型。"""
@@ -1263,6 +1445,7 @@ def test_paragraph_title_detector_does_not_read_line_text() -> None:
inspect.getsource(function)
for function in (
titles._classify_page_titles,
+ titles._classify_inline_typography_reset_titles,
titles._infer_document_body_profile,
titles._classify_body_height_section_titles,
titles._body_height_section_followers,
@@ -1294,6 +1477,7 @@ def test_paragraph_title_detector_does_not_read_line_text() -> None:
titles._normalized_title_gap,
titles._line_near_visual_container,
titles._is_wide_leading_title_continuation,
+ titles._demote_hanging_multiline_text_titles,
line_layout._title_fonts_compatible,
titles._expand_paragraph_title_neighbors,
)