File size: 926 Bytes
e44647f
0329cdf
e44647f
0329cdf
e44647f
 
 
 
 
 
 
 
 
 
689854b
e44647f
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
from langdetect import detect_langs

def detect_language(text):
    try:
        if len(text) < 10:
            return [("English", 1.0, "English")]
        lang_detections = detect_langs(text)
        native_lang_map = {
            "en": ("English", "English"), "fr": ("Français", "French"), "es": ("Español", "Spanish"),
            "de": ("Deutsch", "German"), "hi": ("हिन्दी", "Hindi"), "zh": ("中文", "Chinese"),
            "ar": ("العربية", "Arabic"), "ru": ("Русский", "Russian"), "ja": ("日本語", "Japanese")
        }
        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]
        return detected_options[:3] if detected_options else [("English", 0.5, "English")]
    except Exception:
        return [("English", 0.5, "English")]