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