From 6ee663e7700f3e7b6964268f8b6d943f93b1fef3 Mon Sep 17 00:00:00 2001 From: rohitgaikwad17 Date: Mon, 11 Aug 2025 12:36:50 +0530 Subject: [PATCH] Update main.py Added DetectorFactory.seed for consistent results Moved detection logic into a reusable function Corrected Kisii language code to ISO 639-3 (guz) Removed duplicate code by looping through sample text --- Language Detector/main.py | 35 +++++++++++++++++++++-------------- 1 file changed, 21 insertions(+), 14 deletions(-) diff --git a/Language Detector/main.py b/Language Detector/main.py index bff53e01..97522a35 100644 --- a/Language Detector/main.py +++ b/Language Detector/main.py @@ -1,18 +1,25 @@ # Language Detector -from langdetect import detect +from langdetect import detect, DetectorFactory +DetectorFactory.seed = 0 # Ensure consistent results -# Detecting languages from input text -language1 = detect('¿Qué te gusta hacer') -language2 = detect('Nyasae no omuya') +def detect_language(text): + """Detect the language of a given text and return code + name.""" + lang_code = detect(text) + language_mapping = { + 'es': 'Spanish', + 'en': 'English', + 'sw': 'Swahili', + 'guz': 'Kisii' # ISO 639-3 code for Ekegusii (Kisii) + } + return lang_code, language_mapping.get(lang_code, lang_code) -# Mapping of language codes to human-readable names -language_mapping = { - 'es': 'Spanish', - 'en': 'English', - 'sw': 'Swahili', - 'ek': 'Kisii' -} +# Sample texts +texts = [ + '¿Qué te gusta hacer', + 'Nyasae no omuya' +] -# Print the detected language in a more user-friendly format -print(f'This is the {language_mapping.get(language1, language1)} language shortened by: {language1}') -print(f'This is the {language_mapping.get(language2, language2)} language shortened by: {language2}') +# Detect and print results +for text in texts: + code, name = detect_language(text) + print(f'This is the {name} language shortened by: {code}')