File size: 1,536 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
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]