From a97c8c50c7e190cf35dc2ce532bbdade982c4ed1 Mon Sep 17 00:00:00 2001 From: myhloli Date: Tue, 16 Jun 2026 15:57:49 +0800 Subject: [PATCH] feat: add OCR language normalization and validation for improved accuracy --- mineru/cli/client.py | 18 ++++++++++++++++-- mineru/utils/ocr_language.py | 7 ++++++- 2 files changed, 22 insertions(+), 3 deletions(-) diff --git a/mineru/cli/client.py b/mineru/cli/client.py index 24ea8f29..d3881f03 100644 --- a/mineru/cli/client.py +++ b/mineru/cli/client.py @@ -30,7 +30,7 @@ from mineru.utils.config_reader import ( get_max_concurrent_requests as read_max_concurrent_requests, ) from mineru.utils.guess_suffix_or_lang import guess_suffix_by_path -from mineru.utils.ocr_language import PUBLIC_OCR_LANGUAGES +from mineru.utils.ocr_language import PUBLIC_OCR_LANGUAGES, validate_public_ocr_lang from mineru.utils.pdf_page_id import get_end_page_id from mineru.utils.pdfium_guard import ( close_pdfium_document, @@ -118,6 +118,18 @@ def normalize_effort_option( raise click.BadParameter(str(exc), ctx=ctx, param=param) from exc +def normalize_ocr_lang_option( + ctx: click.Context, + param: click.Parameter, + value: str, +) -> str: + """校验 CLI OCR 语言参数,并将兼容别名归一到实际模型语言。""" + try: + return validate_public_ocr_lang(value) + except ValueError as exc: + raise click.BadParameter(str(exc), ctx=ctx, param=param) from exc + + @dataclass(frozen=True) class TaskFailure: task_index: int @@ -1097,8 +1109,10 @@ async def run_orchestrated_cli( "-l", "--lang", "lang", - type=click.Choice(PUBLIC_OCR_LANGUAGES), + type=str, default="ch", + callback=normalize_ocr_lang_option, + metavar="[" + "|".join(PUBLIC_OCR_LANGUAGES) + "]", help=""" Input the languages in the pdf (if known) to improve OCR accuracy. Without languages specified, 'ch' will be used by default. diff --git a/mineru/utils/ocr_language.py b/mineru/utils/ocr_language.py index b17ab7b5..3d32825c 100644 --- a/mineru/utils/ocr_language.py +++ b/mineru/utils/ocr_language.py @@ -49,6 +49,7 @@ PUBLIC_OCR_LANGUAGE_CHOICES = tuple( PUBLIC_OCR_LANGUAGE_SCHEMA_EXTRA = {"items": {"enum": list(PUBLIC_OCR_LANGUAGES)}} _ARABIC_LANG_ALIASES = {"ar", "fa", "ug", "ur", "ps", "ku", "sd", "bal"} +_CH_LANG_ALIASES = {"en", "japan", "chinese_cht", "latin"} _EAST_SLAVIC_LANG_ALIASES = {"ru", "be", "uk"} _CYRILLIC_LANG_ALIASES = { "rs_cyrillic", @@ -113,7 +114,9 @@ def format_public_ocr_lang_description() -> str: def validate_public_ocr_lang(lang: str) -> str: - """校验公开入口允许的 OCR 语言,已下线语言必须显式拒绝。""" + """校验公开入口允许的 OCR 语言,并将兼容入口规范到实际模型 key。""" + if lang in _CH_LANG_ALIASES: + return "ch" if lang not in PUBLIC_OCR_LANGUAGES: raise ValueError( f"Language {lang} not supported. Allowed values: " @@ -138,6 +141,8 @@ def normalize_ocr_model_lang( normalized_lang = lang or "ch" if device == "cpu" and normalized_lang == "seal": normalized_lang = "seal_lite" + elif normalized_lang in _CH_LANG_ALIASES: + normalized_lang = "ch" elif normalized_lang in _EAST_SLAVIC_LANG_ALIASES: normalized_lang = "east_slavic" elif normalized_lang in _ARABIC_LANG_ALIASES: