Skip to content

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

Restore CJK boundary punctuation in native span matching; Refs #5526

myhloliee8244be0fac
diff --git a/mineru/backend/analysis/pdf/text/native.py b/mineru/backend/analysis/pdf/text/native.py
index d1338b8f..2b293731 100644
--- a/mineru/backend/analysis/pdf/text/native.py
+++ b/mineru/backend/analysis/pdf/text/native.py
@@ -780,6 +780,11 @@ LINE_STOP_FLAG = (
"}",
">",
"》",
+ "」",
+ "』",
+ "〉",
+ "〕",
+ "〗",
"、",
",",
",",
@@ -798,6 +803,9 @@ LINE_START_FLAG = (
"<",
"「",
"『",
+ "〈",
+ "〔",
+ "〖",
"【",
"[",
)
diff --git a/tests/unittest/test_pdf_native_cjk_quote_span_edge.py b/tests/unittest/test_pdf_native_cjk_quote_span_edge.py
new file mode 100644
index 00000000..be5e9341
--- /dev/null
+++ b/tests/unittest/test_pdf_native_cjk_quote_span_edge.py
@@ -0,0 +1,122 @@
+from __future__ import annotations
+
+import pytest
+
+from mineru.backend.analysis.pdf.text.models import _AnalyzeSpan
+from mineru.backend.analysis.pdf.text.native import calculate_char_in_span, fill_char_in_spans
+from mineru.types import ContentType
+
+SPAN_BBOX = (0.0, 0.0, 100.0, 20.0)
+RIGHT_EDGE_CHAR_BBOX = [98.0, 2.0, 106.0, 18.0]
+LEFT_EDGE_CHAR_BBOX = [-6.0, 2.0, 2.0, 18.0]
+CJK_LINE_STOP_CLOSERS = ("」", "』", "〉", "〕", "〗")
+CJK_LINE_START_OPENERS = ("〈", "〔", "〖")
+
+
+def _fillable_span(bbox: tuple[float, float, float, float] = SPAN_BBOX) -> _AnalyzeSpan:
+ """构造已初始化字符容器的可回填文本 Span。"""
+ return _AnalyzeSpan(
+ type=ContentType.TEXT,
+ bbox=bbox,
+ score=1.0,
+ metadata={
+ "chars": [],
+ "height": bbox[3] - bbox[1],
+ "width": bbox[2] - bbox[0],
+ },
+ )
+
+
[email protected]("char", CJK_LINE_STOP_CLOSERS)
+def test_calculate_char_in_span_accepts_cjk_closers_past_right_edge(char: str) -> None:
+ """右边缘略超出 span 的 CJK 结束引号/括弧应与已支持的 》 同样命中。"""
+ assert calculate_char_in_span(RIGHT_EDGE_CHAR_BBOX, SPAN_BBOX, "》") is True
+ assert calculate_char_in_span(RIGHT_EDGE_CHAR_BBOX, SPAN_BBOX, char) is True
+
+
[email protected]("char", CJK_LINE_START_OPENERS)
+def test_calculate_char_in_span_accepts_cjk_openers_past_left_edge(char: str) -> None:
+ """左边缘略超出 span 的 CJK 起始括弧应与已支持的 「 同样命中。"""
+ assert calculate_char_in_span(LEFT_EDGE_CHAR_BBOX, SPAN_BBOX, "「") is True
+ assert calculate_char_in_span(LEFT_EDGE_CHAR_BBOX, SPAN_BBOX, char) is True
+
+
+def test_calculate_char_in_span_rejects_letter_at_closing_quote_geometry() -> None:
+ """同一越界几何下,非标点汉字仍不能靠 LINE_STOP 边缘规则进入 span。"""
+ assert calculate_char_in_span(RIGHT_EDGE_CHAR_BBOX, SPAN_BBOX, "好") is False
+
+
+def test_fill_char_in_spans_keeps_dialogue_closing_corner_quote() -> None:
+ """行末 」 中心略超出 span 右边界时仍应回填为 他说「好」。"""
+ span = _fillable_span()
+ chars = [
+ {"char": "他", "bbox": [42.0, 2.0, 60.0, 18.0], "char_idx": 0},
+ {"char": "说", "bbox": [60.0, 2.0, 78.0, 18.0], "char_idx": 1},
+ {"char": "「", "bbox": [78.0, 2.0, 87.0, 18.0], "char_idx": 2},
+ {"char": "好", "bbox": [87.0, 2.0, 98.0, 18.0], "char_idx": 3},
+ {"char": "」", "bbox": [98.0, 2.0, 106.0, 18.0], "char_idx": 4},
+ ]
+ assert calculate_char_in_span(chars[-1]["bbox"], span.bbox, "」") is True
+ fill_char_in_spans([span], chars, median_span_height=20.0, detect_scripts=False) # type: ignore[arg-type]
+ assert span.content == "他说「好」"
+
+
+def test_fill_char_in_spans_keeps_left_edge_white_bracket_opener() -> None:
+ """行首 〈 中心略超出 span 左边界时仍应回填,而不是丢掉起始括弧。"""
+ span = _fillable_span()
+ chars = [
+ {"char": "〈", "bbox": [-6.0, 2.0, 2.0, 18.0], "char_idx": 0},
+ {"char": "好", "bbox": [2.0, 2.0, 20.0, 18.0], "char_idx": 1},
+ ]
+ assert calculate_char_in_span(chars[0]["bbox"], span.bbox, "〈") is True
+ fill_char_in_spans([span], chars, median_span_height=20.0, detect_scripts=False) # type: ignore[arg-type]
+ assert span.content == "〈好"
+
+
+def test_real_pdf_boundary_punctuation_matches_native_and_keeps_neighbor() -> None:
+ """嵌入真实 CJK 字形,确认原生/Python 回填一致且相邻框不串入正文。"""
+ from io import BytesIO
+ from pathlib import Path
+
+ import docvortex
+ from docvortex.document.pdf import PDFDocument
+ from reportlab.pdfbase import pdfmetrics
+ from reportlab.pdfbase.ttfonts import TTFont
+ from reportlab.pdfgen.canvas import Canvas
+
+ font = Path(docvortex.__file__).parent / "resources/fonts/DroidSansFallbackFull.ttf"
+ pdfmetrics.registerFont(TTFont("CjkBoundaryRegression", str(font)))
+ stream = BytesIO()
+ canvas = Canvas(stream, pagesize=(300, 420))
+ canvas.setFont("CjkBoundaryRegression", 18)
+ rows = ["他说「好" + mark + "邻" for mark in CJK_LINE_STOP_CLOSERS]
+ rows += [mark + "好邻" for mark in CJK_LINE_START_OPENERS]
+ for index, text in enumerate(rows):
+ canvas.drawString(40, 390 - index * 42, text)
+ canvas.save()
+ with PDFDocument(stream.getvalue()) as document:
+ owner = document[0].get_text_snapshot()
+ assert owner is not None and owner.supports_span_matching()
+ geometry = owner.materialize_geometry()
+ boxes = []
+ for index, text in enumerate(rows):
+ chars = [char for char in geometry.chars if 10 + index * 42 < char["bbox"][1] < 30 + index * 42]
+ chars = [char for char in chars if char["char"] not in {"\r", "\n", "\r\n"}]
+ assert "".join(char["char"] for char in chars) == text
+ first, last, neighbor = chars[0]["bbox"], chars[-2]["bbox"], chars[-1]["bbox"]
+ left = first[0] + (first[2] - first[0]) * 0.75 if index >= 5 else first[0]
+ right = last[0] + (last[2] - last[0]) * 0.25 if index < 5 else last[2]
+ boxes.extend([(left, first[1], right, first[3]), tuple(neighbor)])
+ for native_owner in (None, owner):
+ spans = [_fillable_span(box) for box in boxes]
+ fill_char_in_spans(
+ spans,
+ geometry.chars,
+ 20.0,
+ detect_scripts=False,
+ tight_bboxes=geometry.tight_bboxes,
+ origins=geometry.origins,
+ _native_text=native_owner,
+ )
+ assert [span.content for span in spans[::2]] == [row[:-1] for row in rows]
+ assert [span.content for span in spans[1::2]] == ["邻"] * len(rows)