Skip to content

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

fix(pdf): replace residual private-use text with spaces after OCR

myhloli2c435ebddb1c
diff --git a/mineru/backend/analysis/pdf/text/content.py b/mineru/backend/analysis/pdf/text/content.py
index 97053c5c..7bf2d639 100644
--- a/mineru/backend/analysis/pdf/text/content.py
+++ b/mineru/backend/analysis/pdf/text/content.py
@@ -33,6 +33,7 @@ from .native import (
__replace_unicode,
_clear_post_ocr_fallback,
_is_supported_rotation,
+ _replace_private_use_chars,
_restore_post_ocr_fallback,
txt_spans_extract,
)
@@ -407,6 +408,15 @@ def _line_content_parts(line: _AnalyzeLine) -> list[tuple[str, str]]:
return parts
+def _replace_residual_private_use_text(block_lines: dict[int, list[_AnalyzeLine]]) -> None:
+ """后置 OCR 及失败恢复完成后清理正文 PUA,避免提前替换影响检测或紧排。"""
+ for lines in block_lines.values():
+ for line in lines:
+ for span in line.spans:
+ if span.type == ContentType.TEXT:
+ span.content = _replace_private_use_chars(span.content)
+
+
def _lines_to_block_content(lines: list[_AnalyzeLine], block_type: str) -> str:
"""将真实行折叠为统一 block content,保留目录/代码换行并处理自然语言跨行连接。"""
content_lines = [parts for line in lines if (parts := _line_content_parts(line))]
@@ -628,6 +638,8 @@ def _fill_window_block_content_and_lines(
page_size,
evidence,
) in page_block_line_results:
+ if parse_mode == "txt":
+ _replace_residual_private_use_text(block_lines)
_apply_block_content_and_line_metadata(
page_model_list,
block_lines,
diff --git a/mineru/backend/analysis/pdf/text/native.py b/mineru/backend/analysis/pdf/text/native.py
index be1ba8e8..bf35434c 100644
--- a/mineru/backend/analysis/pdf/text/native.py
+++ b/mineru/backend/analysis/pdf/text/native.py
@@ -24,8 +24,7 @@ from ..images import get_crop_img
from .models import _AnalyzeSpan
MAX_NATIVE_TEXT_CHARS_PER_PAGE = 65535
-PRIVATE_USE_AREA_START = 0xE000
-PRIVATE_USE_AREA_END = 0xF8FF
+PRIVATE_USE_RANGES = ((0xE000, 0xF8FF), (0xF0000, 0xFFFFD), (0x100000, 0x10FFFD))
PRIVATE_USE_TEXT_COUNT_THRESHOLD = 2
PRIVATE_USE_TEXT_RATIO_THRESHOLD = 0.05
PRIVATE_USE_TEXT_RUN_THRESHOLD = 2
@@ -597,7 +596,7 @@ def _owned_span_texts(owner: Any, spans: list[_AnalyzeSpan], median_height: floa
threshold = SPACING_DIACRITIC_MIN_OVERLAP_RATIO
if type(threshold) not in (int, float) or not math.isfinite(threshold):
return None
- if any(type(value) is not int or not 0 <= value <= 0x10FFFF for value in (PRIVATE_USE_AREA_START, PRIVATE_USE_AREA_END)):
+ if any(type(value) is not int or not 0 <= value <= 0x10FFFF for pair in PRIVATE_USE_RANGES for value in pair):
return None
inputs = _owned_span_inputs(owner, spans, median_height)
if inputs is None:
@@ -607,7 +606,7 @@ def _owned_span_texts(owner: Any, spans: list[_AnalyzeSpan], median_height: floa
list(CONTROL_LINE_BREAK_CHARS),
_SPACING_DIACRITIC_TO_COMBINING,
threshold,
- (PRIVATE_USE_AREA_START, PRIVATE_USE_AREA_END),
+ PRIVATE_USE_RANGES,
[span.metadata.get("_native_tight_spacing", True) for span in spans],
)
@@ -821,7 +820,12 @@ _ScriptRole = ScriptRole
def _is_private_use_char(char: str) -> bool:
"""判断单个字符是否落在 Unicode 私用区,用于识别字体映射异常。"""
- return len(char) == 1 and PRIVATE_USE_AREA_START <= ord(char) <= PRIVATE_USE_AREA_END
+ return len(char) == 1 and any(start <= ord(char) <= end for start, end in PRIVATE_USE_RANGES)
+
+
+def _replace_private_use_chars(text: str) -> str:
+ """仅在原生正文最终回填时逐个替换残留 PUA,保留已有空白和其他 Unicode 字符。"""
+ return "".join(" " if _is_private_use_char(char) else char for char in text)
def _get_private_use_text_signal(chars: list[Char]) -> dict[str, float | int]:
diff --git a/tests/unittest/test_pdf_private_use_text.py b/tests/unittest/test_pdf_private_use_text.py
new file mode 100644
index 00000000..e09940c5
--- /dev/null
+++ b/tests/unittest/test_pdf_private_use_text.py
@@ -0,0 +1,171 @@
+"""验证三个私用区的检测、真实 Rust 回填与最终 ASCII 空格占位。"""
+
+from __future__ import annotations
+
+from dataclasses import asdict
+from io import BytesIO
+from pathlib import Path
+import pickle
+from unittest.mock import MagicMock
+
+import numpy as np
+import pytest
+import reportlab
+from docvortex import _native
+from docvortex.analyzers.pdf import PDFTextEvidence
+from docvortex.document.pdf import PDFDocument, PDFPageTextGeometry
+from pypdf import PdfReader, PdfWriter
+from pypdf.generic import DecodedStreamObject, NameObject
+from reportlab.pdfbase import pdfmetrics
+from reportlab.pdfbase.ttfonts import TTFont
+from reportlab.pdfgen.canvas import Canvas
+
+from mineru.backend.analysis.pdf.text import content, native
+from mineru.backend.analysis.pdf.text.models import _AnalyzeLine, _AnalyzeSpan
+from mineru.types import BlockType, ContentType
+
+
+def _mapped_pdf(text: str) -> bytes:
+ """用嵌入字体和真实 UTF-16 ToUnicode 映射构造 PUA,避免缺字被制 PDF 工具改为零。"""
+ private_chars = list(dict.fromkeys(char for char in text if native._is_private_use_char(char)))
+ replacements = dict(zip(private_chars, "xyz", strict=False))
+ assert len(replacements) == len(private_chars) <= 3
+ pdfmetrics.registerFont(TTFont("PrivateUseVera", str(Path(reportlab.__file__).parent / "fonts/Vera.ttf")))
+ source = BytesIO()
+ canvas = Canvas(source, pagesize=(1024, 100))
+ canvas.setFont("PrivateUseVera", 12)
+ canvas.drawString(10, 50, "".join(replacements.get(char, char) for char in text))
+ canvas.save()
+ writer = PdfWriter()
+ writer.add_page(PdfReader(source).pages[0])
+ for font_ref in writer.pages[0]["/Resources"]["/Font"].values():
+ font = font_ref.get_object()
+ if "/ToUnicode" not in font:
+ continue
+ mapping = font["/ToUnicode"].get_data()
+ for private, marker in replacements.items():
+ original = f"<{ord(marker):02X}> <{ord(marker):04X}>".encode()
+ assert original in mapping
+ mapping = mapping.replace(original, f"<{ord(marker):02X}> <{private.encode('utf-16-be').hex()}>".encode())
+ stream = DecodedStreamObject()
+ stream.set_data(mapping)
+ font[NameObject("/ToUnicode")] = writer._add_object(stream)
+ output = BytesIO()
+ writer.write(output)
+ return output.getvalue()
+
+
+ ("code", "expected"),
+ [
+ (0xDFFF, False),
+ (0xE000, True),
+ (0xF8FF, True),
+ (0xF900, False),
+ (0xEFFFF, False),
+ (0xF0000, True),
+ (0xFFFFD, True),
+ (0xFFFFE, False),
+ (0xFFFFF, False),
+ (0x100000, True),
+ (0x10FFFD, True),
+ (0x10FFFE, False),
+ (0x10FFFF, False),
+ ],
+)
+def test_private_use_range_boundaries(code: int, expected: bool) -> None:
+ """只包含三个正式私用区,边界外字符和非字符码点不被扩大替换。"""
+ assert native._is_private_use_char(chr(code)) is expected
+
+
[email protected]("private", ["\ue000", "\uf8ff", "\U000f0000", "\U000ffffd", "\U00100000", "\U0010fffd", "\U001001b0"])
[email protected]("kind", ["single", "consecutive", "sparse", "ratio"])
+def test_owned_pua_counts_and_ocr_decision_match_python(private: str, kind: str) -> None:
+ """三个私用区真实提取后,Rust 与 Python 的计数、连续长度和原阈值决策一致。"""
+ text = {
+ "single": f"A0{private}93B",
+ "consecutive": f"A{private}{private}B",
+ "sparse": f"{'A' * 80}{private}B{private}C",
+ "ratio": f"A{private}B{private}C",
+ }[kind]
+ with PDFDocument(_mapped_pdf(text)) as document:
+ owner = document[0].get_text_snapshot()
+ geometry = owner.materialize_geometry()
+ assert "".join(char["char"] for char in geometry.chars) == text
+ original = pickle.dumps(geometry.chars)
+ boxes = [char["bbox"] for char in geometry.chars]
+ box = (min(b[0] for b in boxes), min(b[1] for b in boxes), max(b[2] for b in boxes), max(b[3] for b in boxes))
+ results = []
+ before = _native.text_snapshot_stats()[2]
+ for current in (None, owner):
+ span = _AnalyzeSpan(ContentType.TEXT, box, metadata={"chars": [], "height": box[3] - box[1], "width": box[2] - box[0]})
+ pending = native.fill_char_in_spans(
+ [span],
+ geometry.chars,
+ 12.0,
+ tight_bboxes=geometry.tight_bboxes,
+ origins=geometry.origins,
+ detect_scripts=False,
+ _native_text=current,
+ )
+ assert bool(pending) is (kind in {"consecutive", "ratio"})
+ assert private in span.content # 必须先检测原文,不能在 OCR 决策前替换。
+ results.append((asdict(span), bool(pending)))
+ assert _native.text_snapshot_stats()[2] == before + 1
+ assert results[0] == results[1]
+ assert pickle.dumps(geometry.chars) == original
+
+
[email protected]("mode", ["no_ocr", "success", "low_confidence", "low_contrast", "ocr_pua"])
+def test_pipeline_replaces_residual_pua_after_ocr(monkeypatch: pytest.MonkeyPatch, mode: str) -> None:
+ """完整窗口出口覆盖不识别、识别成功和两种恢复原文路径,公式不参与替换。"""
+ original = "0\U001001b093 A\ue000B\U000f0000C"
+ span = _AnalyzeSpan(ContentType.TEXT, (0.0, 0.0, 100.0, 20.0), content=original, score=0.9)
+ formula = _AnalyzeSpan(ContentType.INLINE_EQUATION, (100.0, 0.0, 120.0, 20.0), content="x\ue000")
+ lines = {0: [_AnalyzeLine((0.0, 0.0, 120.0, 20.0), [span, formula])]}
+ context = MagicMock()
+ if mode != "no_ocr":
+ span.metadata[native.POST_OCR_FALLBACK_CONTENT_KEY] = original
+ span.metadata[native.POST_OCR_FALLBACK_SCORE_KEY] = span.score
+ span.metadata[native.POST_OCR_REASON_KEY] = native.POST_OCR_REASON_PRIVATE_USE_TEXT
+ if mode == "low_contrast":
+ monkeypatch.setattr(native, "get_crop_img", lambda *_args: np.zeros((20, 100, 3), dtype=np.uint8))
+ native._prepare_post_ocr_spans([span], [span], object(), 1.0)
+ elif mode != "no_ocr":
+ span.image = np.zeros((20, 100, 3), dtype=np.uint8)
+ span.content = ""
+ recognized = "0.93 ABC" if mode == "success" else original
+ context.ocr_model.ocr.return_value = [[(recognized, 0.1 if mode == "low_confidence" else 0.99)]]
+ monkeypatch.setattr(content, "_build_page_text_formula_spans", lambda *_args: [])
+ monkeypatch.setattr(content, "_fill_native_pdf_text_spans", lambda _page, spans, *_args, **_kwargs: spans)
+ monkeypatch.setattr(content, "_group_page_spans_by_block", lambda *_args: lines)
+ monkeypatch.setattr(
+ content,
+ "prepare_text_evidence",
+ lambda *_args, **_kwargs: PDFTextEvidence((120.0, 100.0), PDFPageTextGeometry([], {}, {})),
+ )
+ model = [[{"type": BlockType.TEXT, "bbox": [0.0, 0.0, 1.0, 1.0]}]]
+ content._fill_window_block_content_and_lines(
+ [{"img_pil": object(), "scale": 1.0}],
+ [MagicMock(size=(120.0, 100.0))],
+ model,
+ [[]],
+ [[]],
+ "txt",
+ "medium",
+ {BlockType.TEXT},
+ context,
+ )
+ expected = "0.93 ABC" if mode == "success" else "0 93 A B C"
+ assert span.content == expected
+ assert formula.content == "x\ue000"
+ assert expected in str(model[0][0]["content"])
+ assert "093" not in span.content
+ assert native.POST_OCR_FALLBACK_CONTENT_KEY not in span.metadata
+ assert context.ocr_model.ocr.call_count == (0 if mode in {"no_ocr", "low_contrast"} else 1)
+
+
+def test_replacement_preserves_non_pua_and_existing_whitespace() -> None:
+ """逐码点替换不折叠原空白,不影响非 BMP 汉字、表情或非字符码点。"""
+ text = " A\t中\U00020000😀\ue000\U000f0000\U001001b0\U0010fffeB\n "
+ assert native._replace_private_use_chars(text) == " A\t中\U00020000😀 \U0010fffeB\n "