zhifu gao
2023-03-16 d783b24ba7d8a03dabfa2139fcbf40c216e0ea3d
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
 
from collections import defaultdict
 
import pynini
from fun_text_processing.text_normalization.de.utils import get_abs_path, load_labels
from fun_text_processing.text_normalization.en.graph_utils import (
    DAMO_DIGIT,
    DAMO_SIGMA,
    GraphFst,
    delete_space,
    insert_space,
)
from pynini.lib import pynutil
 
AND = "und"
 
 
def get_ties_digit(digit_path: str, tie_path: str) -> 'pynini.FstLike':
    """
    getting all inverse normalizations for numbers between 21 - 100
 
    Args:
        digit_path: file to digit tsv
        tie_path: file to tie tsv, e.g. 20, 30, etc.
    Returns:
        res: fst that converts numbers to their verbalization
    """
 
    digits = defaultdict(list)
    ties = defaultdict(list)
    for k, v in load_labels(digit_path):
        digits[v].append(k)
    digits["1"] = ["ein"]
 
    for k, v in load_labels(tie_path):
        ties[v].append(k)
 
    d = []
    for i in range(21, 100):
        s = str(i)
        if s[1] == "0":
            continue
 
        for di in digits[s[1]]:
            for ti in ties[s[0]]:
                word = di + f" {AND} " + ti
                d.append((word, s))
 
    res = pynini.string_map(d)
    return res
 
 
class CardinalFst(GraphFst):
    """
    Finite state transducer for classifying cardinals, e.g. 
        "101" ->  cardinal { integer: "ein hundert und zehn" }
 
    Args:
        deterministic: if True will provide a single transduction option,
            for False multiple transduction are generated (used for audio-based normalization)
    """
 
    def __init__(self, deterministic: bool = False):
        super().__init__(name="cardinal", kind="classify", deterministic=deterministic)
 
        graph_zero = pynini.string_file(get_abs_path("data/numbers/zero.tsv")).invert()
        graph_digit_no_one = pynini.string_file(get_abs_path("data/numbers/digit.tsv")).invert()
        graph_one = pynini.string_file(get_abs_path("data/numbers/ones.tsv")).invert()
        graph_digit = graph_digit_no_one | graph_one
        self.digit = (graph_digit | graph_zero).optimize()
        graph_teen = pynini.string_file(get_abs_path("data/numbers/teen.tsv")).invert()
 
        graph_ties = pynini.string_file(get_abs_path("data/numbers/ties.tsv")).invert()
        # separator = "."
 
        def tens_no_zero():
            return (
                pynutil.delete("0") + graph_digit
                | get_ties_digit(
                    get_abs_path("data/numbers/digit.tsv"), get_abs_path("data/numbers/ties.tsv")
                ).invert()
                | graph_teen
                | (graph_ties + pynutil.delete("0"))
            )
 
        def hundred_non_zero():
            return (graph_digit_no_one + insert_space | pynini.cross("1", "ein ")) + pynutil.insert("hundert") + (
                pynini.closure(insert_space + pynutil.insert(AND, weight=0.0001), 0, 1) + insert_space + tens_no_zero()
                | pynutil.delete("00")
            ) | pynutil.delete("0") + tens_no_zero()
 
        def thousand():
            return (hundred_non_zero() + insert_space + pynutil.insert("tausend") | pynutil.delete("000")) + (
                insert_space + hundred_non_zero() | pynutil.delete("000")
            )
 
        optional_plural_quantity_en = pynini.closure(pynutil.insert("en", weight=-0.0001), 0, 1)
        optional_plural_quantity_n = pynini.closure(pynutil.insert("n", weight=-0.0001), 0, 1)
        graph_million = pynini.union(
            hundred_non_zero() + insert_space + pynutil.insert("million") + optional_plural_quantity_en,
            pynutil.delete("000"),
        )
 
        graph_billion = pynini.union(
            hundred_non_zero() + insert_space + pynutil.insert("milliarde") + optional_plural_quantity_n,
            pynutil.delete("000"),
        )
 
        graph_trillion = pynini.union(
            hundred_non_zero() + insert_space + pynutil.insert("billion") + optional_plural_quantity_en,
            pynutil.delete("000"),
        )
 
        graph_quadrillion = pynini.union(
            hundred_non_zero() + insert_space + pynutil.insert("billiarde") + optional_plural_quantity_n,
            pynutil.delete("000"),
        )
 
        graph_quintillion = pynini.union(
            hundred_non_zero() + insert_space + pynutil.insert("trillion") + optional_plural_quantity_en,
            pynutil.delete("000"),
        )
 
        graph_sextillion = pynini.union(
            hundred_non_zero() + insert_space + pynutil.insert("trilliarde") + optional_plural_quantity_n,
            pynutil.delete("000"),
        )
        graph = pynini.union(
            graph_sextillion
            + insert_space
            + graph_quintillion
            + insert_space
            + graph_quadrillion
            + insert_space
            + graph_trillion
            + insert_space
            + graph_billion
            + insert_space
            + graph_million
            + insert_space
            + thousand()
        )
 
        fix_syntax = [
            ("eins tausend", "ein tausend"),
            ("eins millionen", "eine million"),
            ("eins milliarden", "eine milliarde"),
            ("eins billionen", "eine billion"),
            ("eins billiarden", "eine billiarde"),
        ]
        fix_syntax = pynini.union(*[pynini.cross(*x) for x in fix_syntax])
        self.graph = (
            ((DAMO_DIGIT - "0" + pynini.closure(DAMO_DIGIT, 0)) - "0" - "1")
            @ pynini.cdrewrite(pynini.closure(pynutil.insert("0")), "[BOS]", "", DAMO_SIGMA)
            @ DAMO_DIGIT ** 24
            @ graph
            @ pynini.cdrewrite(delete_space, "[BOS]", "", DAMO_SIGMA)
            @ pynini.cdrewrite(delete_space, "", "[EOS]", DAMO_SIGMA)
            @ pynini.cdrewrite(pynini.cross("  ", " "), "", "", DAMO_SIGMA)
            @ pynini.cdrewrite(fix_syntax, "[BOS]", "", DAMO_SIGMA)
        )
        self.graph |= graph_zero | pynini.cross("1", "eins")
 
        # self.graph = pynini.cdrewrite(pynutil.delete(separator), "", "", DAMO_SIGMA) @ self.graph
        self.graph = self.graph.optimize()
 
        self.graph_hundred_component_at_least_one_none_zero_digit = (
            ((DAMO_DIGIT - "0" + pynini.closure(DAMO_DIGIT, 0)) - "0" - "1")
            @ pynini.cdrewrite(pynini.closure(pynutil.insert("0")), "[BOS]", "", DAMO_SIGMA)
            @ DAMO_DIGIT ** 3
            @ hundred_non_zero()
        ) | pynini.cross("1", "eins")
 
        self.graph_hundred_component_at_least_one_none_zero_digit = (
            self.graph_hundred_component_at_least_one_none_zero_digit.optimize()
        )
 
        self.two_digit_non_zero = (
            pynini.closure(DAMO_DIGIT, 1, 2) @ self.graph_hundred_component_at_least_one_none_zero_digit
        )
 
        optional_minus_graph = pynini.closure(pynutil.insert("negative: ") + pynini.cross("-", "\"true\" "), 0, 1)
 
        final_graph = optional_minus_graph + pynutil.insert("integer: \"") + self.graph + pynutil.insert("\"")
        final_graph = self.add_tokens(final_graph)
        self.fst = final_graph.optimize()