aboutsummaryrefslogtreecommitdiff
path: root/tools/python/tts_languages.py
blob: c3fa317ec5a18e33a176475cd0f7e4b40d89df0a (plain) (blame)
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
#!/usr/bin/env python3
from __future__ import print_function
from optparse import OptionParser
import re

LANGUAGES_HPP_TEMPLATE = """\
#pragma once

#include <array>
#include <string>

// This file is autogenerated while exporting sounds.csv from the google table.
// It contains the list of languages which can be used by TTS.
// It shall be included to Android(jni) and iOS part to get the languages list.

namespace routing
{{
namespace turns
{{
namespace sound
{{
std::array<std::string, {lang_list_size}> const kLanguageList =
{{{{
{lang_list}
}}}};
}}  // namespace sound
}}  // namespace turns
}}  // namespace routing
"""


def parse_args():
    opt_parser = OptionParser(
        description="Creates a language.hpp out of the sound.txt file.",
        usage="python %prog <path_to_sound.txt> <path_to_languages.hpp>",
        version="%prog 1.0"
    )
    (options, args) = opt_parser.parse_args()
    if len(args) != 2:
        opt_parser.error("Wrong number of arguments.")
    return args


def read_languages(strings_name):
    langs = set()
    RE_LANG = re.compile(r'^ *([\w-]+) *=')
    with open(strings_name, "r") as langs_file:
        for line in langs_file:
            m = RE_LANG.search(line)
            if m:
                langs.add(m.group(1))
    return langs


def make_languages_hpp(langs, hpp_name):
    print ("Creating {}".format(hpp_name))
    lang_str = ",\n".join(["  \"{}\"".format(language) for language in sorted(langs)])
    with open(hpp_name, "w") as hpp_file:
        hpp_file.write(LANGUAGES_HPP_TEMPLATE.format(lang_list_size=len(langs), lang_list=lang_str))


def run():
    strings_name, langs_hpp_name = parse_args()
    langs = read_languages(strings_name)
    make_languages_hpp(langs, langs_hpp_name)


if __name__ == "__main__":
    run()