Update lang_detect.py
Browse files- lang_detect.py +13 -13
lang_detect.py
CHANGED
@@ -1,16 +1,16 @@
|
|
1 |
-
from
|
2 |
-
import io
|
3 |
|
4 |
-
def
|
5 |
try:
|
6 |
-
if
|
7 |
-
return
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
|
|
15 |
except Exception:
|
16 |
-
return
|
|
|
1 |
+
from langdetect import detect_langs
|
|
|
2 |
|
3 |
+
def detect_language(text):
|
4 |
try:
|
5 |
+
if len(text) < 10:
|
6 |
+
return [("English", 1.0, "English")]
|
7 |
+
lang_detections = detect_langs(text)
|
8 |
+
native_lang_map = {
|
9 |
+
"en": ("English", "English"), "fr": ("Français", "French"), "es": ("Español", "Spanish"),
|
10 |
+
"de": ("Deutsch", "German"), "hi": ("हिन्दी", "Hindi"), "zh": ("中文", "Chinese"),
|
11 |
+
"ar": ("العربية", "Arabic"), "ru": ("Русский", "Russian"), "ja": ("日本語", "Japanese")
|
12 |
+
}
|
13 |
+
detected_options = [(native_lang_map.get(lang.lang, ("English", "English"))[1], lang.prob, native_lang_map.get(lang.lang, ("English", "English"))[0]) for lang in lang_detections if lang.prob >= 0.7]
|
14 |
+
return detected_options[:3] if detected_options else [("English", 0.5, "English")]
|
15 |
except Exception:
|
16 |
+
return [("English", 0.5, "English")]
|