Skip to content

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

fix: recover internal PDF punctuation during span fill

myhlolid4ede7dff31c
diff --git a/mineru/backend/analysis/pdf/text/native.py b/mineru/backend/analysis/pdf/text/native.py
index 74405349..28e53a9a 100644
--- a/mineru/backend/analysis/pdf/text/native.py
+++ b/mineru/backend/analysis/pdf/text/native.py
@@ -446,6 +446,47 @@ def _match_whitespace_bbox_to_first_span(
return None
+def _bbox_center_inside_span(char_bbox: BBox, span_bbox: BBox) -> bool:
+ """判断字符框中心是否位于 Span 完整矩形内。"""
+ char_center_x = (char_bbox[0] + char_bbox[2]) / 2
+ char_center_y = (char_bbox[1] + char_bbox[3]) / 2
+ return span_bbox[0] <= char_center_x <= span_bbox[2] and span_bbox[1] <= char_center_y <= span_bbox[3]
+
+
+def _bridge_unassigned_punctuation(
+ assigned_span_indices: list[int | None],
+ all_chars: list[Char],
+ previous_visible_owners: list[int | None],
+ next_visible_owners: list[int | None],
+ span_bboxes: list[BBox],
+ tight_bboxes: dict[int, BBox],
+) -> None:
+ """用同行两侧一致的 Span 归属恢复中心仍在框内的未分配标点。"""
+ for char_position, char in enumerate(all_chars):
+ char_text = str(char.get("char", ""))
+ if (
+ assigned_span_indices[char_position] is not None
+ or len(char_text) != 1
+ or not unicodedata.category(char_text).startswith("P")
+ ):
+ continue
+
+ previous_owner = previous_visible_owners[char_position]
+ next_owner = next_visible_owners[char_position]
+ if previous_owner is None or previous_owner != next_owner:
+ continue
+
+ char_idx = _char_geometry_key(char)
+ tight_bbox = _coerce_finite_bbox(tight_bboxes.get(char_idx)) if char_idx is not None else None
+ loose_bbox = _coerce_finite_bbox(char.get("bbox"))
+ owner_bbox = span_bboxes[previous_owner]
+ if any(
+ candidate_bbox is not None and _bbox_center_inside_span(candidate_bbox, owner_bbox)
+ for candidate_bbox in (tight_bbox, loose_bbox)
+ ):
+ assigned_span_indices[char_position] = previous_owner
+
+
def fill_char_in_spans(
spans: list[_AnalyzeSpan],
all_chars: list[Char],
@@ -514,6 +555,15 @@ def fill_char_in_spans(
if not char_text.isspace() and assigned_span_indices[char_position] is not None:
next_owner = assigned_span_indices[char_position]
+ _bridge_unassigned_punctuation(
+ assigned_span_indices,
+ all_chars,
+ previous_visible_owners,
+ next_visible_owners,
+ span_bboxes,
+ tight_bboxes,
+ )
+
for char_position, char in enumerate(all_chars):
char_text = str(char.get("char", ""))
if not char_text.isspace():
diff --git a/tests/unittest/test_pdf_native_script_detection.py b/tests/unittest/test_pdf_native_script_detection.py
index e422706c..547d94a5 100644
--- a/tests/unittest/test_pdf_native_script_detection.py
+++ b/tests/unittest/test_pdf_native_script_detection.py
@@ -319,6 +319,148 @@ def test_span_fill_uses_loose_bbox_when_tight_punctuation_does_not_match() -> No
assert span.content == "."
+def test_zh2_heading_number_bridges_low_internal_periods() -> None:
+ """验证中文论文2窄标题框可恢复同行数字之间偏低的句点。"""
+ probe = "1.3.6 评估结果缺乏可解释性"
+ fixture = _contiguous_chars_containing(_ZH_2_PDF, 6, probe)
+ span_bbox = (308.16, 628.56, 444.96, 637.56)
+ periods = [char for char in fixture.chars if char["char"] == "."]
+ assert len(periods) == 2
+ for period in periods:
+ char_idx = int(period["char_idx"])
+ assert not native.calculate_char_in_span(fixture.tight_bboxes[char_idx], span_bbox, ".")
+ assert not native.calculate_char_in_span(tuple(float(value) for value in period["bbox"]), span_bbox, ".")
+
+ span = _fillable_span(span_bbox)
+ native.fill_char_in_spans(
+ [span],
+ fixture.chars,
+ 8.64,
+ tight_bboxes=fixture.tight_bboxes,
+ origins=fixture.origins,
+ detect_scripts=False,
+ )
+
+ assert span.content == probe
+
+
+def test_span_fill_bridges_consecutive_internal_punctuation() -> None:
+ """验证连续内部标点在同行两侧同属一个 Span 时均可恢复。"""
+ span = _fillable_span((10.0, 10.0, 40.0, 20.0))
+ chars = [
+ _script_char(0, "1", height=6.0, center_y=15.0),
+ _script_char(1, ".", height=2.4, center_y=18.4),
+ _script_char(2, ".", height=2.4, center_y=18.4),
+ _script_char(3, "3", height=6.0, center_y=15.0),
+ ]
+ loose_bboxes = [
+ Bbox([12.0, 12.0, 15.0, 18.0]),
+ Bbox([15.0, 17.2, 16.0, 19.6]),
+ Bbox([16.0, 17.2, 17.0, 19.6]),
+ Bbox([17.0, 12.0, 20.0, 18.0]),
+ ]
+ for char, bbox in zip(chars, loose_bboxes, strict=True):
+ char["bbox"] = bbox
+ tight_bboxes = {index: tuple(float(value) for value in bbox) for index, bbox in enumerate(loose_bboxes)}
+
+ native.fill_char_in_spans(
+ [span],
+ chars,
+ 10.0,
+ tight_bboxes=tight_bboxes,
+ origins={index: (float(index), 18.0) for index in range(len(chars))},
+ detect_scripts=False,
+ )
+
+ assert span.content == "1..3"
+
+
+def test_span_fill_does_not_bridge_punctuation_between_different_spans() -> None:
+ """验证前后字符归属不同 Span 时不会吸收中间标点。"""
+ first = _fillable_span((10.0, 10.0, 20.0, 20.0))
+ second = _fillable_span((20.0, 10.0, 30.0, 20.0))
+ chars = [
+ _script_char(0, "A", height=6.0, center_y=15.0),
+ _script_char(1, ".", height=2.4, center_y=18.4),
+ _script_char(2, "B", height=6.0, center_y=15.0),
+ ]
+ loose_bboxes = [
+ Bbox([12.0, 12.0, 15.0, 18.0]),
+ Bbox([19.5, 17.2, 20.5, 19.6]),
+ Bbox([22.0, 12.0, 25.0, 18.0]),
+ ]
+ for char, bbox in zip(chars, loose_bboxes, strict=True):
+ char["bbox"] = bbox
+
+ native.fill_char_in_spans(
+ [first, second],
+ chars,
+ 10.0,
+ tight_bboxes={index: tuple(float(value) for value in bbox) for index, bbox in enumerate(loose_bboxes)},
+ origins={index: (float(index), 18.0) for index in range(len(chars))},
+ detect_scripts=False,
+ )
+
+ assert first.content == "A"
+ assert second.content == "B"
+
+
+def test_span_fill_does_not_bridge_punctuation_across_line_break_or_outside_span() -> None:
+ """验证跨物理行或中心位于 Span 外部的标点不会被桥接。"""
+ line_break_span = _fillable_span((10.0, 10.0, 40.0, 20.0))
+ line_break_chars = [
+ _script_char(0, "A", height=6.0, center_y=15.0),
+ _script_char(1, "\r", height=0.0, center_y=15.0),
+ _script_char(2, ".", height=2.4, center_y=18.4),
+ _script_char(3, "B", height=6.0, center_y=15.0),
+ ]
+ line_break_bboxes = [
+ Bbox([12.0, 12.0, 15.0, 18.0]),
+ Bbox([15.0, 15.0, 15.0, 15.0]),
+ Bbox([15.0, 17.2, 16.0, 19.6]),
+ Bbox([16.0, 12.0, 19.0, 18.0]),
+ ]
+ for char, bbox in zip(line_break_chars, line_break_bboxes, strict=True):
+ char["bbox"] = bbox
+ native.fill_char_in_spans(
+ [line_break_span],
+ line_break_chars,
+ 10.0,
+ tight_bboxes={
+ 0: (12.0, 12.0, 15.0, 18.0),
+ 2: (15.0, 17.2, 16.0, 19.6),
+ 3: (16.0, 12.0, 19.0, 18.0),
+ },
+ origins={index: (float(index), 18.0) for index in range(len(line_break_chars))},
+ detect_scripts=False,
+ )
+
+ outside_span = _fillable_span((10.0, 10.0, 40.0, 20.0))
+ outside_chars = [
+ _script_char(0, "A", height=6.0, center_y=15.0),
+ _script_char(1, ".", height=2.0, center_y=22.0),
+ _script_char(2, "B", height=6.0, center_y=15.0),
+ ]
+ outside_bboxes = [
+ Bbox([12.0, 12.0, 15.0, 18.0]),
+ Bbox([15.0, 21.0, 16.0, 23.0]),
+ Bbox([16.0, 12.0, 19.0, 18.0]),
+ ]
+ for char, bbox in zip(outside_chars, outside_bboxes, strict=True):
+ char["bbox"] = bbox
+ native.fill_char_in_spans(
+ [outside_span],
+ outside_chars,
+ 10.0,
+ tight_bboxes={index: tuple(float(value) for value in bbox) for index, bbox in enumerate(outside_bboxes)},
+ origins={index: (float(index), 18.0) for index in range(len(outside_chars))},
+ detect_scripts=False,
+ )
+
+ assert line_break_span.content == "AB"
+ assert outside_span.content == "A B"
+
+
def test_span_fill_attaches_whitespace_to_tight_owned_neighbors() -> None:
"""验证空格跟随两侧 tight 字符进入同一 Span,而不是留在旧的重叠 Span。"""
first = _fillable_span((0.0, 0.0, 12.0, 20.0))