File size: 915 Bytes
c4d001b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
def can_form_string(x, symbol_dict):
    def helper(x, symbol_dict, matched_parts):
        if not x:
            return True, matched_parts

        for key in symbol_dict.keys():
            if x.startswith(key):
                result, parts = helper(
                    x[len(key) :], symbol_dict, matched_parts + [key]
                )
                if result:
                    return True, parts

        return False, []

    return helper(x, symbol_dict, [])


def text_to_ipa(text, lang_tag, g2p):
    ipa = []
    words = text.split()

    print(words)

    for word in words:
        ipa_parts = ""
        result, matched_parts = can_form_string(word, g2p[lang_tag])

        if result is False:
            return ""

        for matched_part in matched_parts:
            ipa_parts = ipa_parts + g2p[lang_tag][matched_part]

        ipa.append(ipa_parts)
    ipa = " ".join(ipa)
    return ipa