opus-translate / iso639_wrapper.py
Gundeep Singh
Main
ebee301
raw
history blame
1.54 kB
from iso639 import Lang, iter_langs
langs = [lang for lang in iter_langs()]
# iso 1
iso1_code_to_name = {lg.pt1: lg.name for lg in langs}
iso1_name_to_code = {lg.name: lg.pt1 for lg in langs}
# iso 2
iso2b_code_to_name = {lg.pt2b: lg.name for lg in langs}
iso2b_name_to_code = {lg.name: lg.pt2b for lg in langs}
iso2t_code_to_name = {lg.pt2t: lg.name for lg in langs}
iso2t_name_to_code = {lg.name: lg.pt2t for lg in langs}
# iso 3
iso3_code_to_name = {lg.pt3: lg.name for lg in langs}
iso3_name_to_code = {lg.name: lg.pt3 for lg in langs}
# iso 5
iso5_code_to_name = {lg.pt5: lg.name for lg in langs}
iso5_name_to_code = {lg.name: lg.pt5 for lg in langs}
# https://github.com/Helsinki-NLP/Tatoeba-Challenge/blob/master/README.md#in-more-detail
helsinki_precendence = ["iso3", "iso5", "iso1", "iso2t", "iso2b"]
def get_name_from_iso_code(iso_code, precedence=helsinki_precendence):
for code_type in precedence:
if code_type == "iso1" and iso_code in iso1_code_to_name.keys():
return iso1_code_to_name[iso_code]
elif code_type == "iso2b" and iso_code in iso2b_code_to_name.keys():
return iso2b_code_to_name[iso_code]
elif code_type == "iso2t" and iso_code in iso2t_code_to_name.keys():
return iso2t_code_to_name[iso_code]
elif code_type == "iso3" and iso_code in iso3_code_to_name.keys():
return iso3_code_to_name[iso_code]
elif code_type == "iso5" and iso_code in iso5_code_to_name.keys():
return iso5_code_to_name[iso_code]