Skip to content

fix(language): remove invalid UTF-16 surrogate pairs from input text #1546

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 15, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions magic_pdf/libs/language.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,20 @@
from fast_langdetect import detect_language


def remove_invalid_surrogates(text):
# 移除无效的 UTF-16 代理对
return ''.join(c for c in text if not (0xD800 <= ord(c) <= 0xDFFF))


def detect_lang(text: str) -> str:

if len(text) == 0:
return ""

text = text.replace("\n", "")
text = remove_invalid_surrogates(text)

# print(text)
try:
lang_upper = detect_language(text)
except:
Expand All @@ -37,3 +45,4 @@ def detect_lang(text: str) -> str:
print(detect_lang("<html>This is a test</html>"))
print(detect_lang("这个是中文测试。"))
print(detect_lang("<html>这个是中文测试。</html>"))
print(detect_lang("〖\ud835\udc46\ud835〗这是个包含utf-16的中文测试"))
Loading