-
Notifications
You must be signed in to change notification settings - Fork 31
/
languages.py
272 lines (227 loc) · 9.08 KB
/
languages.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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
import json
import logging
import pkgutil
import re
from collections import defaultdict
from collections import namedtuple
try:
import pycountry
except ImportError:
pycountry = None
logger = logging.getLogger("le_utils")
logger.setLevel(logging.INFO)
LTR_LANGUAGE = "ltr"
RTL_LANGUAGE = "rtl"
LANGUAGE_DIRECTIONS = (
(LTR_LANGUAGE, "Left to Right"),
(RTL_LANGUAGE, "Right to Left"),
)
RTL_LANG_CODES = [
"ar", # Arabic
"arq", # Algerian
"dv", # Divehi; Dhivehi; Maldivian
"he", # Hebrew (modern)
"fa", # Persian
"ps", # Pashto; Pushto
"ur", # Urdu
"yi", # Yiddish
]
class Language(
namedtuple(
"Language", ["native_name", "primary_code", "subcode", "name", "ka_name"]
)
):
@property
def code(self):
if self.subcode:
return "{primary_code}-{subcode}".format(
primary_code=self.primary_code, subcode=self.subcode
)
else:
return self.primary_code
@property
def id(self):
return self.code
@property
def first_native_name(self):
"""
Return the first native name in the comma-seprated list of `native_name`.
"""
return self.native_name.split(",")[0]
def _parse_out_iso_639_code(code):
code_regex = r"(?P<primary_code>\w{2,3})(-(?P<subcode>\w{2,4}))?"
match = re.match(code_regex, code)
if match:
return defaultdict(lambda: None, **match.groupdict())
else:
return None
def generate_list(constantlist):
for code, lang in constantlist.items():
values = _parse_out_iso_639_code(code)
values.update(lang)
# add a default value to ka_name
if "ka_name" not in values:
values["ka_name"] = None
yield Language(**values)
def _initialize_language_list():
langlist = json.loads(
pkgutil.get_data("le_utils", "resources/languagelookup.json").decode("utf-8")
)
return generate_list(langlist)
def _iget(key, lookup_dict):
"""
Case-insensitive search for `key` within keys of `lookup_dict`.
"""
for k, v in lookup_dict.items():
if k.lower() == key.lower():
return v
return None
LANGUAGELIST = list(_initialize_language_list())
_LANGLOOKUP = {l.code: l for l in LANGUAGELIST}
def getlang(code, default=None):
"""
Try to lookup a Language object for `code` in internal representation defined
in resources/languagelookup.json.
Returns None if lookup by internal representation fails.
"""
return _LANGLOOKUP.get(code) or None
# should this be return _LANGLOOKUP.get(code) or _LANGLOOKUP[default] ???
# NOTES ON INTERNAL REPRESENTATION FOR LANGUAGE CODES
################################################################################
# The language code lookup table in `resources/languagelookup.json` is complex:
# 1. "<code>":{
# 2. "name":"EnglishName, variant; AlternativeName",
# 3. "native_name":"NativeName, AlternativeNativeName",
# }
#
# 1. <code> can be two letter code, three letter code, or <code>-<locale> string
# 2. `name` attr. contains ";"-separated list of alternative names for the language,
# some names have a ","-separated variant, e.g., "Spanish, Spain"
# 3. `native_name` contains ","-separated list of strings for language name in that language
#
# The following lookup tables and lookup functions perform the necessary logic
# to deal with the ";" and "," separators used in `resources/languagelookup.json`
################################################################################
_LANGUAGE_NAME_LOOKUP = {l.name: l for l in LANGUAGELIST}
# Enrich _LANGUAGE_NAME_LOOKUP with aliases for list-names, and simplified names
new_items = {}
for lang_name, lang_obj in _LANGUAGE_NAME_LOOKUP.items():
# Add language names that are separated by semicolons, e.g. "Catalan; Valencian"
if ";" in lang_name:
new_names = [n.strip() for n in lang_name.split(";")]
for new_name in new_names:
if new_name in _LANGUAGE_NAME_LOOKUP.keys() or new_name in new_items:
logger.debug("Skip " + new_name + " because it already exisits")
else:
new_items[new_name] = lang_obj
# Add base names without modifies in brackets or country/region after comma
elif "(" in lang_name or "," in lang_name:
simple_name = lang_name.split(",")[0] # take part before comma
simple_name = simple_name.split("(")[0].strip() # and before any bracket
if simple_name in _LANGUAGE_NAME_LOOKUP.keys() or simple_name in new_items:
logger.debug("Skip " + simple_name + " because it already exisits")
else:
new_items[simple_name] = lang_obj
_LANGUAGE_NAME_LOOKUP.update(new_items)
def getlang_by_name(name):
"""
Try to lookup a Language object by name, e.g. 'English', in internal language list.
Returns None if lookup by language name fails in resources/languagelookup.json.
"""
direct_match = _iget(name, _LANGUAGE_NAME_LOOKUP)
if direct_match:
return direct_match
else:
simple_name = name.split(",")[0] # take part before comma
simple_name = simple_name.split("(")[0].strip() # and before any bracket
return _LANGUAGE_NAME_LOOKUP.get(simple_name, None)
_LANGUAGE_NATIVE_NAME_LOOKUP = {l.native_name: l for l in LANGUAGELIST}
# Enrich _LANGUAGE_NATIVE_NAME_LOOKUP with aliases for list-like names
new_items = {}
for lang_native_name, lang_obj in _LANGUAGE_NATIVE_NAME_LOOKUP.items():
# Add base native names without modifies in brackets or after comma
if "," in lang_native_name:
new_native_names = [n.strip() for n in lang_native_name.split(",")]
for new_native_name in new_native_names:
simple_native_name = new_native_name.split("(")[
0
].strip() # text before any bracket
if (
simple_native_name in _LANGUAGE_NATIVE_NAME_LOOKUP.keys()
or new_native_name in new_items
):
logger.debug(
"Skip " + simple_native_name + " because it already exisits"
)
else:
new_items[simple_native_name] = lang_obj
elif "(" in lang_native_name:
simple_native_name = lang_native_name.split("(")[
0
].strip() # text before any bracket
if (
simple_native_name in _LANGUAGE_NATIVE_NAME_LOOKUP.keys()
or simple_native_name in new_items
):
logger.debug("Skip " + simple_native_name + " because it already exisits")
else:
new_items[simple_native_name] = lang_obj
_LANGUAGE_NATIVE_NAME_LOOKUP.update(new_items)
def getlang_by_native_name(native_name):
"""
Try to lookup a Language object by native_name, e.g. 'English', in internal language list.
Returns None if lookup by language name fails in resources/languagelookup.json.
"""
direct_match = _iget(native_name, _LANGUAGE_NATIVE_NAME_LOOKUP)
if direct_match:
return direct_match
else:
simple_native_name = native_name.split(",")[0] # take part before comma
simple_native_name = simple_native_name.split("(")[
0
].strip() # and before any bracket
return _LANGUAGE_NATIVE_NAME_LOOKUP.get(simple_native_name, None)
def getlang_by_alpha2(code): # noqa: C901
"""
Lookup a Language object for language code `code` based on these strategies:
- Special case rules for Hebrew and Chinese Hans/Hant scripts
- Using `alpha_2` lookup in `pycountry.languages` followed by lookup for a
language with the same `name` in the internal representaion
Returns `None` if no matching language is found.
"""
if pycountry is None:
logger.warn("pycountry is not installed, cannot lookup language by alpha2")
return None
# First try exact match to a language code in the internal representaion
exact_match = getlang(code)
if exact_match:
return exact_match
# Handle special cases for language codes returned by YouTube API
if code == "iw": # handle old Hebrew code 'iw' and return modern code 'he'
return getlang("he")
elif "zh-Hans" in code: # use code `zh-CN` for all simplified Chinese
return getlang("zh-CN")
elif re.match("zh(.*)?-TW", code): # matches all Taiwan chinese codes
return getlang("zh-TW")
elif re.match("zh(.*)?-HK", code): # use code `zh-Hant` for Hong Kong
return getlang("zh-Hant")
# extract prefix only if specified with subcode: e.g. zh-Hans --> zh
first_part = code.split("-")[0]
# See if pycountry can find this language
try:
pyc_lang = pycountry.languages.get(alpha_2=first_part)
if pyc_lang:
if hasattr(pyc_lang, "inverted_name"):
lang_name = pyc_lang.inverted_name
else:
lang_name = pyc_lang.name
return getlang_by_name(lang_name)
else:
return None
except KeyError:
return None
def getlang_direction(code):
if code in RTL_LANG_CODES:
return RTL_LANGUAGE
else:
return LTR_LANGUAGE