feat: add OCR language normalization and validation for improved accuracy

This commit is contained in:
myhloli
2026-06-16 15:57:49 +08:00
parent b9494f7465
commit a97c8c50c7
2 changed files with 22 additions and 3 deletions
+16 -2
View File
@@ -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.
+6 -1
View File
@@ -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: