Spaces:
Sleeping
Sleeping
Optimize the recognition of mixed Chinese and English characters in numbers. (#108)
Browse files- utils/classify_language.py +15 -8
utils/classify_language.py
CHANGED
|
@@ -1,6 +1,9 @@
|
|
| 1 |
-
import re
|
| 2 |
|
| 3 |
-
|
|
|
|
|
|
|
|
|
|
| 4 |
|
| 5 |
langid_languages = ["af", "am", "an", "ar", "as", "az", "be", "bg", "bn", "br", "bs", "ca", "cs", "cy", "da", "de",
|
| 6 |
"dz", "el",
|
|
@@ -16,8 +19,10 @@ langid_languages = ["af", "am", "an", "ar", "as", "az", "be", "bg", "bn", "br",
|
|
| 16 |
|
| 17 |
|
| 18 |
def classify_language(text: str, target_languages: list = None) -> str:
|
| 19 |
-
|
| 20 |
-
|
|
|
|
|
|
|
| 21 |
if module == "fastlid" or module == "fasttext":
|
| 22 |
from fastlid import fastlid, supported_langs
|
| 23 |
classifier = fastlid
|
|
@@ -71,11 +76,13 @@ def split_alpha_nonalpha(text, mode=1):
|
|
| 71 |
- list: A list of substrings after the split.
|
| 72 |
"""
|
| 73 |
if mode == 1:
|
| 74 |
-
|
| 75 |
-
r'(?<=[\u4e00-\u9fff\u3040-\u30FF\d])(?=[a-zA-Z])|(?<=[a-zA-Z])(?=[\u4e00-\u9fff\u3040-\u30FF\d])', text)
|
| 76 |
elif mode == 2:
|
| 77 |
-
|
| 78 |
-
|
|
|
|
|
|
|
|
|
|
| 79 |
|
| 80 |
|
| 81 |
if __name__ == "__main__":
|
|
|
|
| 1 |
+
import regex as re
|
| 2 |
|
| 3 |
+
try:
|
| 4 |
+
from utils.config_manager import global_config
|
| 5 |
+
except:
|
| 6 |
+
pass
|
| 7 |
|
| 8 |
langid_languages = ["af", "am", "an", "ar", "as", "az", "be", "bg", "bn", "br", "bs", "ca", "cs", "cy", "da", "de",
|
| 9 |
"dz", "el",
|
|
|
|
| 19 |
|
| 20 |
|
| 21 |
def classify_language(text: str, target_languages: list = None) -> str:
|
| 22 |
+
try:
|
| 23 |
+
module = global_config["LANGUAGE_IDENTIFICATION_LIBRARY"].lower()
|
| 24 |
+
except:
|
| 25 |
+
module = "langid"
|
| 26 |
if module == "fastlid" or module == "fasttext":
|
| 27 |
from fastlid import fastlid, supported_langs
|
| 28 |
classifier = fastlid
|
|
|
|
| 76 |
- list: A list of substrings after the split.
|
| 77 |
"""
|
| 78 |
if mode == 1:
|
| 79 |
+
pattern = r'(?<=[\u4e00-\u9fff\u3040-\u30FF\d])(?=[\p{Latin}])|(?<=[\p{Latin}])(?=[\u4e00-\u9fff\u3040-\u30FF\d])'
|
|
|
|
| 80 |
elif mode == 2:
|
| 81 |
+
pattern = r'(?<=[\u4e00-\u9fff\u3040-\u30FF])(?=[\p{Latin}\d])|(?<=[\p{Latin}\d])(?=[\u4e00-\u9fff\u3040-\u30FF])'
|
| 82 |
+
else:
|
| 83 |
+
raise ValueError("Invalid mode. Supported modes are 1 and 2.")
|
| 84 |
+
|
| 85 |
+
return re.split(pattern, text)
|
| 86 |
|
| 87 |
|
| 88 |
if __name__ == "__main__":
|