Skip to content

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

feat: enhance language guessing by normalizing text for surrogate pairs

myhloli66df0cd3670b
diff --git a/mineru/utils/guess_suffix_or_lang.py b/mineru/utils/guess_suffix_or_lang.py
index ad39ef67..70e8376f 100644
--- a/mineru/utils/guess_suffix_or_lang.py
+++ b/mineru/utils/guess_suffix_or_lang.py
@@ -8,9 +8,49 @@ DEFAULT_LANG = "txt"
PDF_SIG_BYTES = b'%PDF'
magika = Magika()
+def _normalize_text_for_language_guess(code: str) -> str:
+ if not code:
+ return ""
+
+ normalized = []
+ index = 0
+ while index < len(code):
+ current_char = code[index]
+ current_ord = ord(current_char)
+
+ if 0xD800 <= current_ord <= 0xDBFF:
+ if index + 1 < len(code):
+ next_char = code[index + 1]
+ next_ord = ord(next_char)
+ if 0xDC00 <= next_ord <= 0xDFFF:
+ pair = current_char + next_char
+ normalized.append(pair.encode("utf-16", "surrogatepass").decode("utf-16"))
+ index += 2
+ continue
+ index += 1
+ continue
+
+ if 0xDC00 <= current_ord <= 0xDFFF:
+ index += 1
+ continue
+
+ normalized.append(current_char)
+ index += 1
+
+ return "".join(normalized)
+
+
def guess_language_by_text(code):
- codebytes = code.encode(encoding="utf-8")
- lang = magika.identify_bytes(codebytes).prediction.output.label
+ normalized_code = _normalize_text_for_language_guess(code)
+ if not normalized_code:
+ return DEFAULT_LANG
+
+ try:
+ codebytes = normalized_code.encode("utf-8", errors="replace")
+ lang = magika.identify_bytes(codebytes).prediction.output.label
+ except Exception:
+ return DEFAULT_LANG
+
return lang if lang != "unknown" else DEFAULT_LANG
@@ -32,4 +72,4 @@ def guess_suffix_by_path(file_path) -> str:
suffix = "pdf"
except Exception as e:
logger.warning(f"Failed to read file {file_path} for PDF signature check: {e}")
- return suffix
\ No newline at end of file
+ return suffix