liugz18
2024-07-18 d80ac2fd2df4e7fb8a28acfa512bb11472b5cc99
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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
import pynini
from fun_text_processing.text_normalization.en.graph_utils import (
    DAMO_ALPHA,
    DAMO_DIGIT,
    DAMO_NON_BREAKING_SPACE,
    DAMO_SIGMA,
    DAMO_SPACE,
    DAMO_UPPER,
    SINGULAR_TO_PLURAL,
    TO_LOWER,
    GraphFst,
    convert_space,
    delete_space,
    delete_zero_or_one_space,
    insert_space,
)
from fun_text_processing.text_normalization.en.taggers.ordinal import OrdinalFst as OrdinalTagger
from fun_text_processing.text_normalization.en.taggers.whitelist import get_formats
from fun_text_processing.text_normalization.en.utils import get_abs_path, load_labels
from fun_text_processing.text_normalization.en.verbalizers.ordinal import (
    OrdinalFst as OrdinalVerbalizer,
)
from pynini.examples import plurals
from pynini.lib import pynutil
 
 
class MeasureFst(GraphFst):
    """
    Finite state transducer for classifying measure, suppletive aware, e.g.
        -12kg -> measure { negative: "true" cardinal { integer: "twelve" } units: "kilograms" }
        1kg -> measure { cardinal { integer: "one" } units: "kilogram" }
        .5kg -> measure { decimal { fractional_part: "five" } units: "kilograms" }
 
    Args:
        cardinal: CardinalFst
        decimal: DecimalFst
        fraction: FractionFst
        deterministic: if True will provide a single transduction option,
            for False multiple transduction are generated (used for audio-based normalization)
    """
 
    def __init__(
        self, cardinal: GraphFst, decimal: GraphFst, fraction: GraphFst, deterministic: bool = True
    ):
        super().__init__(name="measure", kind="classify", deterministic=deterministic)
        cardinal_graph = cardinal.graph_with_and | self.get_range(cardinal.graph_with_and)
 
        graph_unit = pynini.string_file(get_abs_path("data/measure/unit.tsv"))
        if not deterministic:
            graph_unit |= pynini.string_file(get_abs_path("data/measure/unit_alternatives.tsv"))
 
        graph_unit |= pynini.compose(
            pynini.closure(TO_LOWER, 1)
            + (DAMO_ALPHA | TO_LOWER)
            + pynini.closure(DAMO_ALPHA | TO_LOWER),
            graph_unit,
        ).optimize()
 
        graph_unit_plural = convert_space(graph_unit @ SINGULAR_TO_PLURAL)
        graph_unit = convert_space(graph_unit)
 
        optional_graph_negative = pynini.closure(
            pynutil.insert("negative: ") + pynini.cross("-", '"true" '), 0, 1
        )
 
        graph_unit2 = (
            pynini.cross("/", "per")
            + delete_zero_or_one_space
            + pynutil.insert(DAMO_NON_BREAKING_SPACE)
            + graph_unit
        )
 
        optional_graph_unit2 = pynini.closure(
            delete_zero_or_one_space + pynutil.insert(DAMO_NON_BREAKING_SPACE) + graph_unit2,
            0,
            1,
        )
 
        unit_plural = (
            pynutil.insert('units: "')
            + (graph_unit_plural + optional_graph_unit2 | graph_unit2)
            + pynutil.insert('"')
        )
 
        unit_singular = (
            pynutil.insert('units: "')
            + (graph_unit + optional_graph_unit2 | graph_unit2)
            + pynutil.insert('"')
        )
 
        subgraph_decimal = (
            pynutil.insert("decimal { ")
            + optional_graph_negative
            + decimal.final_graph_wo_negative
            + delete_space
            + pynutil.insert(" } ")
            + unit_plural
        )
 
        # support radio FM/AM
        subgraph_decimal |= (
            pynutil.insert("decimal { ")
            + decimal.final_graph_wo_negative
            + delete_space
            + pynutil.insert(" } ")
            + pynutil.insert('units: "')
            + pynini.union("AM", "FM")
            + pynutil.insert('"')
        )
 
        subgraph_cardinal = (
            pynutil.insert("cardinal { ")
            + optional_graph_negative
            + pynutil.insert('integer: "')
            + ((DAMO_SIGMA - "1") @ cardinal_graph)
            + delete_space
            + pynutil.insert('"')
            + pynutil.insert(" } ")
            + unit_plural
        )
 
        subgraph_cardinal |= (
            pynutil.insert("cardinal { ")
            + optional_graph_negative
            + pynutil.insert('integer: "')
            + pynini.cross("1", "one")
            + delete_space
            + pynutil.insert('"')
            + pynutil.insert(" } ")
            + unit_singular
        )
 
        unit_graph = (
            pynutil.insert('cardinal { integer: "-" } units: "')
            + pynini.cross(pynini.union("/", "per"), "per")
            + delete_zero_or_one_space
            + pynutil.insert(DAMO_NON_BREAKING_SPACE)
            + graph_unit
            + pynutil.insert('" preserve_order: true')
        )
 
        decimal_dash_alpha = (
            pynutil.insert("decimal { ")
            + decimal.final_graph_wo_negative
            + pynini.cross("-", "")
            + pynutil.insert(' } units: "')
            + pynini.closure(DAMO_ALPHA, 1)
            + pynutil.insert('"')
        )
 
        decimal_times = (
            pynutil.insert("decimal { ")
            + decimal.final_graph_wo_negative
            + pynutil.insert(' } units: "')
            + pynini.cross(pynini.union("x", "X"), "x")
            + pynutil.insert('"')
        )
 
        alpha_dash_decimal = (
            pynutil.insert('units: "')
            + pynini.closure(DAMO_ALPHA, 1)
            + pynini.accep("-")
            + pynutil.insert('"')
            + pynutil.insert(" decimal { ")
            + decimal.final_graph_wo_negative
            + pynutil.insert(" } preserve_order: true")
        )
 
        subgraph_fraction = (
            pynutil.insert("fraction { ")
            + fraction.graph
            + delete_space
            + pynutil.insert(" } ")
            + unit_plural
        )
 
        address = self.get_address_graph(cardinal)
        address = (
            pynutil.insert('units: "address" cardinal { integer: "')
            + address
            + pynutil.insert('" } preserve_order: true')
        )
 
        math_operations = pynini.string_file(get_abs_path("data/measure/math_operation.tsv"))
        delimiter = pynini.accep(" ") | pynutil.insert(" ")
 
        math = (
            (cardinal_graph | DAMO_ALPHA)
            + delimiter
            + math_operations
            + (delimiter | DAMO_ALPHA)
            + cardinal_graph
            + delimiter
            + pynini.cross("=", "equals")
            + delimiter
            + (cardinal_graph | DAMO_ALPHA)
        )
 
        math |= (
            (cardinal_graph | DAMO_ALPHA)
            + delimiter
            + pynini.cross("=", "equals")
            + delimiter
            + (cardinal_graph | DAMO_ALPHA)
            + delimiter
            + math_operations
            + delimiter
            + cardinal_graph
        )
 
        math = (
            pynutil.insert('units: "math" cardinal { integer: "')
            + math
            + pynutil.insert('" } preserve_order: true')
        )
        final_graph = (
            subgraph_decimal
            | subgraph_cardinal
            | unit_graph
            | decimal_dash_alpha
            | decimal_times
            | alpha_dash_decimal
            | subgraph_fraction
            | address
            | math
        )
 
        final_graph = self.add_tokens(final_graph)
        self.fst = final_graph.optimize()
 
    def get_range(self, cardinal: GraphFst):
        """
        Returns range forms for measure tagger, e.g. 2-3, 2x3, 2*2
 
        Args:
            cardinal: cardinal GraphFst
        """
        range_graph = cardinal + pynini.cross(pynini.union("-", " - "), " to ") + cardinal
 
        for x in [" x ", "x"]:
            range_graph |= cardinal + pynini.cross(x, " by ") + cardinal
            if not self.deterministic:
                range_graph |= cardinal + pynini.cross(x, " times ") + cardinal
 
        for x in ["*", " * "]:
            range_graph |= cardinal + pynini.cross(x, " times ") + cardinal
        return range_graph.optimize()
 
    def get_address_graph(self, cardinal):
        """
        Finite state transducer for classifying serial.
            The serial is a combination of digits, letters and dashes, e.g.:
            2788 San Tomas Expy, Santa Clara, CA 95051 ->
                units: "address" cardinal
                { integer: "two seven eight eight San Tomas Expressway Santa Clara California nine five zero five one" }
                 preserve_order: true
        """
        ordinal_verbalizer = OrdinalVerbalizer().graph
        ordinal_tagger = OrdinalTagger(cardinal=cardinal).graph
        ordinal_num = pynini.compose(
            pynutil.insert('integer: "') + ordinal_tagger + pynutil.insert('"'), ordinal_verbalizer
        )
 
        address_num = (
            DAMO_DIGIT ** (1, 2) @ cardinal.graph_hundred_component_at_least_one_none_zero_digit
        )
        address_num += insert_space + DAMO_DIGIT**2 @ (
            pynini.closure(pynini.cross("0", "zero "), 0, 1)
            + cardinal.graph_hundred_component_at_least_one_none_zero_digit
        )
        # to handle the rest of the numbers
        address_num = pynini.compose(DAMO_DIGIT ** (3, 4), address_num)
        address_num = plurals._priority_union(address_num, cardinal.graph, DAMO_SIGMA)
 
        direction = (
            pynini.cross("E", "East")
            | pynini.cross("S", "South")
            | pynini.cross("W", "West")
            | pynini.cross("N", "North")
        ) + pynini.closure(pynutil.delete("."), 0, 1)
 
        direction = pynini.closure(pynini.accep(DAMO_SPACE) + direction, 0, 1)
        address_words = get_formats(get_abs_path("data/address/address_word.tsv"))
        address_words = (
            pynini.accep(DAMO_SPACE)
            + (pynini.closure(ordinal_num, 0, 1) | DAMO_UPPER + pynini.closure(DAMO_ALPHA, 1))
            + DAMO_SPACE
            + pynini.closure(DAMO_UPPER + pynini.closure(DAMO_ALPHA) + DAMO_SPACE)
            + address_words
        )
 
        city = pynini.closure(DAMO_ALPHA | pynini.accep(DAMO_SPACE), 1)
        city = pynini.closure(pynini.accep(",") + pynini.accep(DAMO_SPACE) + city, 0, 1)
 
        states = load_labels(get_abs_path("data/address/state.tsv"))
 
        additional_options = []
        for x, y in states:
            additional_options.append((x, f"{y[0]}.{y[1:]}"))
        states.extend(additional_options)
        state_graph = pynini.string_map(states)
        state = pynini.invert(state_graph)
        state = pynini.closure(pynini.accep(",") + pynini.accep(DAMO_SPACE) + state, 0, 1)
 
        zip_code = pynini.compose(DAMO_DIGIT**5, cardinal.single_digits_graph)
        zip_code = pynini.closure(
            pynini.closure(pynini.accep(","), 0, 1) + pynini.accep(DAMO_SPACE) + zip_code,
            0,
            1,
        )
 
        address = (
            address_num + direction + address_words + pynini.closure(city + state + zip_code, 0, 1)
        )
 
        address |= (
            address_num + direction + address_words + pynini.closure(pynini.cross(".", ""), 0, 1)
        )
 
        return address