Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
wangsen
MinerU
Commits
fbc7611e
Unverified
Commit
fbc7611e
authored
Jan 15, 2025
by
Xiaomeng Zhao
Committed by
GitHub
Jan 15, 2025
Browse files
Merge pull request #1546 from myhloli/dev
fix(language): remove invalid UTF-16 surrogate pairs from input text
parents
1954573b
1a549a0e
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
9 additions
and
0 deletions
+9
-0
magic_pdf/libs/language.py
magic_pdf/libs/language.py
+9
-0
No files found.
magic_pdf/libs/language.py
View file @
fbc7611e
...
@@ -12,12 +12,20 @@ if not os.getenv("FTLANG_CACHE"):
...
@@ -12,12 +12,20 @@ if not os.getenv("FTLANG_CACHE"):
from
fast_langdetect
import
detect_language
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
:
def
detect_lang
(
text
:
str
)
->
str
:
if
len
(
text
)
==
0
:
if
len
(
text
)
==
0
:
return
""
return
""
text
=
text
.
replace
(
"
\n
"
,
""
)
text
=
text
.
replace
(
"
\n
"
,
""
)
text
=
remove_invalid_surrogates
(
text
)
# print(text)
try
:
try
:
lang_upper
=
detect_language
(
text
)
lang_upper
=
detect_language
(
text
)
except
:
except
:
...
@@ -37,3 +45,4 @@ if __name__ == '__main__':
...
@@ -37,3 +45,4 @@ if __name__ == '__main__':
print
(
detect_lang
(
"<html>This is a test</html>"
))
print
(
detect_lang
(
"<html>This is a test</html>"
))
print
(
detect_lang
(
"这个是中文测试。"
))
print
(
detect_lang
(
"这个是中文测试。"
))
print
(
detect_lang
(
"<html>这个是中文测试。</html>"
))
print
(
detect_lang
(
"<html>这个是中文测试。</html>"
))
print
(
detect_lang
(
"〖
\ud835\udc46\ud835
〗这是个包含utf-16的中文测试"
))
\ No newline at end of file
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment