Spaces:
Running
Running
File size: 3,265 Bytes
ebee301 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 |
from helsinki_models import helsinki_models, get_clearly_formatted_langauge_directions
from iso639_wrapper import get_name_from_iso_code
from language_detection import detect_language
from collections import OrderedDict
from utils import convert_keys_to_lowercase
def get_all_source_languages():
"""
Returns a human-readable `dict of source languages names to codes`
based on the available models.
"""
source_languages = {}
language_directions = get_clearly_formatted_langauge_directions()
for direction in language_directions:
source_lang_code = direction.split('-')[0]
source_language_name = get_name_from_iso_code(source_lang_code)
if source_language_name:
source_languages[source_language_name] = source_lang_code
source_languages = OrderedDict(sorted(source_languages.items()))
all_source_langs_including_auto_detect = \
{ **{'Auto Detect' : 'Auto Detect'}, **source_languages}
return all_source_langs_including_auto_detect
def get_target_languages(source_language_code, input_text=None):
"""
Returns a human-readable `dict of target languages names to codes`
based on the available models and the source language passed.
"""
include_all_languages = False
if source_language_code == "Auto Detect":
source_language_code, include_all_languages = auto_detect_language_code(input_text)
target_languages = {}
language_directions = get_clearly_formatted_langauge_directions()
for direction in language_directions:
if direction.startswith(f"{source_language_code}-") or include_all_languages:
target_language = direction.split('-')[1] # Extracting the last part as the target language
target_language_name = get_name_from_iso_code(target_language)
if target_language_name:
target_languages[target_language_name] = target_language
return OrderedDict(sorted(target_languages.items()))
def auto_detect_language_code(input_text):
if not input_text:
return None, True
language = detect_language(input_text)
if language == "unknown":
print("unknown tesxt for ", input_text)
return "unknown", True
elif language in list(get_all_source_languages().keys())\
or language.lower() in [k.lower() for k in list(get_all_source_languages().keys())]:
source_languages_dict = convert_keys_to_lowercase(get_all_source_languages())
source_language_code = source_languages_dict.get(language.lower())
return source_language_code, False
elif language in list(get_all_source_languages().values())\
or language.lower() in [k.lower() for k in list(get_all_source_languages().values())]:
source_language_code = language
return source_language_code, False
else:
raise ValueError(f"Language {source_language_code} not supported")
# Example usage:
# all_source_languages = get_all_source_languages()
# print("All Source Languages:", all_source_languages)
# source_language_code = "pa" # Replace with your desired source language
# target_languages = get_target_languages(source_language_code)
# print(f"Target Languages for {source_language_code}:", target_languages)
|