/
utils.py
79 lines (56 loc) · 2.19 KB
/
utils.py
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
73
74
75
76
77
78
79
"""utility functions for language objects"""
from __future__ import annotations
from typing import TYPE_CHECKING, Union
from sign_language_translator.config.enums import (
SignLanguages,
TextLanguages,
normalize_short_code,
)
from sign_language_translator.languages.sign import PakistanSignLanguage
from sign_language_translator.languages.text import Hindi, Urdu
if TYPE_CHECKING:
from enum import Enum
from sign_language_translator.languages.sign import SignLanguage
from sign_language_translator.languages.text import TextLanguage
# TODO: AbstractFactory to store str to class mappings
def get_text_language(language_name: Union[str, Enum]) -> TextLanguage:
"""
Retrieves a TextLanguage object based on the provided language name.
Args:
language_name (str): The name of the language.
Returns:
TextLanguage: An instance of the TextLanguage class corresponding to the provided language name.
Raises:
ValueError: If no TextLanguage class is known for the provided language name.
"""
code_to_class = {
TextLanguages.URDU.value: Urdu,
TextLanguages.HINDI.value: Hindi,
}
class_ = code_to_class.get(normalize_short_code(language_name), None)
if class_:
return class_() # constructor called
# Unknown
raise ValueError(f"no text language class known for '{language_name = }'")
def get_sign_language(language_name: Union[str, Enum]) -> SignLanguage:
"""
Retrieves a SignLanguage object based on the provided language name.
Args:
language_name (str): The name of the language.
Returns:
SignLanguage: An instance of SignLanguage class corresponding to the provided language name.
Raises:
ValueError: If no SignLanguage class is known for the provided language name.
"""
code_to_class = {
SignLanguages.PAKISTAN_SIGN_LANGUAGE.value: PakistanSignLanguage,
}
class_ = code_to_class.get(normalize_short_code(language_name), None)
if class_:
return class_() # constructor called
# Unknown
raise ValueError(f"no sign language class known for '{language_name = }'")
__all__ = [
"get_text_language",
"get_sign_language",
]