33
44import click
55
6- LANGS = None
7- LANG_NAMES = None
8-
96
107def get_langs_deferred () -> Iterable :
118 """Lazilly get the list of language codes supported by g2p library
@@ -24,69 +21,9 @@ def get_langs():
2421 LANGS is the list of valid language codes supported
2522 LANG_NAMES maps each code to its full language name
2623 """
24+ import g2p # Defer expensive import, which loads the whole g2p database
2725
28- global LANGS
29- global LANG_NAMES
30-
31- if LANGS is not None and LANG_NAMES is not None :
32- # Cache the results so we only calculate this information once.
33- return LANGS , LANG_NAMES
34- else :
35- # Imports deferred to when the function is actually first called, because
36- # they load the g2p database and take a fair bit of time. And importing
37- # networkx is also quite expensive.
38- import g2p .mappings .langs as g2p_langs
39- from networkx import has_path
40-
41- # langs_available in g2p lists langs inferred by the directory structure of
42- # g2p/mappings/langs, but in ReadAlongs, we need all input languages to any mappings.
43- # E.g., for Michif, we need to allow crg-dv and crg-tmd, but not crg, which is what
44- # langs_available contains. So we define our own list of languages here.
45- langs_available = []
46-
47- # this will be the set of all langs in g2p + "eng", which we need temporarily
48- full_lang_names = {"eng" : "English" }
49-
50- for _ , v in g2p_langs .LANGS .items ():
51- for mapping in v ["mappings" ]:
52- # add mapping to names hash table
53- full_lang_names [mapping ["in_lang" ]] = mapping ["language_name" ]
54- # add input id to all available langs list
55- if mapping ["in_lang" ] not in langs_available :
56- langs_available .append (mapping ["in_lang" ])
57-
58- # get the key from all networks in g2p module that have a path to 'eng-arpabet',
59- # which is needed for the readalongs
60- # Filter out <lang>-ipa: we only want "normal" input languages.
61- # Filter out *-norm and crk-no-symbols, these are just intermediate representations.
62- LANGS = [
63- x
64- for x in langs_available
65- if not x .endswith ("-ipa" )
66- and not x .endswith ("-equiv" )
67- and not x .endswith ("-no-symbols" )
68- and x not in ["und-ascii" , "moh-festival" ]
69- and g2p_langs .LANGS_NETWORK .has_node (x )
70- and has_path (g2p_langs .LANGS_NETWORK , x , "eng-arpabet" )
71- ]
72-
73- # Hack to allow old English LexiconG2P
74- LANGS += ["eng" ]
75- # Sort LANGS so the -h messages list them alphabetically
76- LANGS = sorted (LANGS )
77-
78- # Set up LANG_NAMES hash table for studio UI to properly name the dropdown options
79- LANG_NAMES = {lang_code : full_lang_names [lang_code ] for lang_code in LANGS }
80-
81- return LANGS , LANG_NAMES
82-
83-
84- # For backwards compatibility, we keep the old names getLangs and getLangsDeferred around.
85- # For example, ReadAlongsDesktop
86- # (https://github.com/tobyatgithub/ReadalongsDesktop) depended on the old name,
87- # and even when it's updated, it'll be helpful to avoid breaking older versions.
88- getLangs = get_langs
89- getLangsDeferred = get_langs_deferred
26+ return g2p .get_langs ()
9027
9128
9229class JoinerCallbackForClick :
0 commit comments