zhifu gao
2023-02-20 0856ea2ebdcb976db6e786de5cd79fae3d35cd4c
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
 
 
import pynini
from fun_text_processing.text_normalization.en.graph_utils import (
    DAMO_DIGIT,
    DAMO_SIGMA,
    GraphFst,
    delete_space,
    insert_space,
)
from fun_text_processing.text_normalization.es.utils import get_abs_path
from pynini.lib import pynutil
 
time_zone_graph = pynini.string_file(get_abs_path("data/time/time_zone.tsv"))
suffix = pynini.string_file(get_abs_path("data/time/time_suffix.tsv"))
 
 
class TimeFst(GraphFst):
    """
    Finite state transducer for classifying time, e.g.
        "02:15 est" -> time { hours: "dos" minutes: "quince" zone: "e s t"}
        "2 h" -> time { hours: "dos" }
        "9 h" -> time { hours: "nueve" }
        "02:15:10 h" -> time { hours: "dos" minutes: "quince" seconds: "diez"}
 
    Args:
        cardinal: CardinalFst
        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, deterministic: bool = True):
        super().__init__(name="time", kind="classify", deterministic=deterministic)
 
        delete_time_delimiter = pynutil.delete(pynini.union(".", ":"))
 
        one = pynini.string_map([("un", "una"), ("ún", "una")])
        change_one = pynini.cdrewrite(one, "", "", DAMO_SIGMA)
        cardinal_graph = cardinal.graph @ change_one
 
        day_suffix = pynutil.insert("suffix: \"") + suffix + pynutil.insert("\"")
        day_suffix = delete_space + insert_space + day_suffix
 
        delete_hora_suffix = delete_space + insert_space + pynutil.delete("h")
        delete_minute_suffix = delete_space + insert_space + pynutil.delete("min")
        delete_second_suffix = delete_space + insert_space + pynutil.delete("s")
 
        labels_hour_24 = [
            str(x) for x in range(0, 25)
        ]  # Can see both systems. Twelve hour requires am/pm for ambiguity resolution
        labels_hour_12 = [str(x) for x in range(1, 13)]
        labels_minute_single = [str(x) for x in range(1, 10)]
        labels_minute_double = [str(x) for x in range(10, 60)]
 
        delete_leading_zero_to_double_digit = (
            pynini.closure(pynutil.delete("0") | (DAMO_DIGIT - "0"), 0, 1) + DAMO_DIGIT
        )
 
        graph_24 = (
            pynini.closure(DAMO_DIGIT, 1, 2) @ delete_leading_zero_to_double_digit @ pynini.union(*labels_hour_24)
        )
        graph_12 = (
            pynini.closure(DAMO_DIGIT, 1, 2) @ delete_leading_zero_to_double_digit @ pynini.union(*labels_hour_12)
        )
 
        graph_hour_24 = graph_24 @ cardinal_graph
        graph_hour_12 = graph_12 @ cardinal_graph
 
        graph_minute_single = delete_leading_zero_to_double_digit @ pynini.union(*labels_minute_single)
        graph_minute_double = pynini.union(*labels_minute_double)
 
        graph_minute = pynini.union(graph_minute_single, graph_minute_double) @ cardinal_graph
 
        final_graph_hour_only_24 = (
            pynutil.insert("hours: \"") + graph_hour_24 + pynutil.insert("\"") + delete_hora_suffix
        )
        final_graph_hour_only_12 = pynutil.insert("hours: \"") + graph_hour_12 + pynutil.insert("\"") + day_suffix
 
        final_graph_hour_24 = pynutil.insert("hours: \"") + graph_hour_24 + pynutil.insert("\"")
        final_graph_hour_12 = pynutil.insert("hours: \"") + graph_hour_12 + pynutil.insert("\"")
 
        final_graph_minute = pynutil.insert("minutes: \"") + graph_minute + pynutil.insert("\"")
        final_graph_second = pynutil.insert("seconds: \"") + graph_minute + pynutil.insert("\"")
        final_time_zone_optional = pynini.closure(
            delete_space + insert_space + pynutil.insert("zone: \"") + time_zone_graph + pynutil.insert("\""), 0, 1,
        )
 
        # 02.30 h
        graph_hm = (
            final_graph_hour_24
            + delete_time_delimiter
            + (pynutil.delete("00") | (insert_space + final_graph_minute))
            + pynini.closure(
                delete_time_delimiter + (pynini.cross("00", " seconds: \"0\"") | (insert_space + final_graph_second)),
                0,
                1,
            )  # For seconds 2.30.35 h
            + pynini.closure(delete_hora_suffix, 0, 1)  # 2.30 is valid if unambiguous
            + final_time_zone_optional
        )
 
        # 2 h 30 min
        graph_hm |= (
            final_graph_hour_24
            + delete_hora_suffix
            + delete_space
            + (pynutil.delete("00") | (insert_space + final_graph_minute))
            + delete_minute_suffix
            + pynini.closure(
                delete_space
                + (pynini.cross("00", " seconds: \"0\"") | (insert_space + final_graph_second))
                + delete_second_suffix,
                0,
                1,
            )  # For seconds
            + final_time_zone_optional
        )
 
        # 2.30 a. m. (Only for 12 hour clock)
        graph_hm |= (
            final_graph_hour_12
            + delete_time_delimiter
            + (pynutil.delete("00") | (insert_space + final_graph_minute))
            + pynini.closure(
                delete_time_delimiter + (pynini.cross("00", " seconds: \"0\"") | (insert_space + final_graph_second)),
                0,
                1,
            )  # For seconds 2.30.35 a. m.
            + day_suffix
            + final_time_zone_optional
        )
 
        graph_h = (
            pynini.union(final_graph_hour_only_24, final_graph_hour_only_12) + final_time_zone_optional
        )  # Should always have a time indicator, else we'll pass to cardinals
 
        if not deterministic:
            # This includes alternate vocalization (hour menos min, min para hour), here we shift the times and indicate a `style` tag
            hour_shift_24 = pynini.invert(pynini.string_file(get_abs_path("data/time/hour_to_24.tsv")))
            hour_shift_12 = pynini.invert(pynini.string_file(get_abs_path("data/time/hour_to_12.tsv")))
            minute_shift = pynini.string_file(get_abs_path("data/time/minute_to.tsv"))
 
            graph_hour_to_24 = graph_24 @ hour_shift_24 @ cardinal_graph
            graph_hour_to_12 = graph_12 @ hour_shift_12 @ cardinal_graph
 
            graph_minute_to = pynini.union(graph_minute_single, graph_minute_double) @ minute_shift @ cardinal_graph
 
            final_graph_hour_to_24 = pynutil.insert("hours: \"") + graph_hour_to_24 + pynutil.insert("\"")
            final_graph_hour_to_12 = pynutil.insert("hours: \"") + graph_hour_to_12 + pynutil.insert("\"")
 
            final_graph_minute_to = pynutil.insert("minutes: \"") + graph_minute_to + pynutil.insert("\"")
 
            graph_menos = pynutil.insert(" style: \"1\"")
            graph_para = pynutil.insert(" style: \"2\"")
 
            final_graph_style = graph_menos | graph_para
 
            # 02.30 h (omitting seconds since a bit awkward)
            graph_hm |= (
                final_graph_hour_to_24
                + delete_time_delimiter
                + insert_space
                + final_graph_minute_to
                + pynini.closure(delete_hora_suffix, 0, 1)  # 2.30 is valid if unambiguous
                + final_time_zone_optional
                + final_graph_style
            )
 
            # 2 h 30 min
            graph_hm |= (
                final_graph_hour_to_24
                + delete_hora_suffix
                + delete_space
                + insert_space
                + final_graph_minute_to
                + delete_minute_suffix
                + final_time_zone_optional
                + final_graph_style
            )
 
            # 2.30 a. m. (Only for 12 hour clock)
            graph_hm |= (
                final_graph_hour_to_12
                + delete_time_delimiter
                + insert_space
                + final_graph_minute_to
                + day_suffix
                + final_time_zone_optional
                + final_graph_style
            )
 
        final_graph = graph_hm | graph_h
        if deterministic:
            final_graph = final_graph + pynutil.insert(" preserve_order: true")
        final_graph = final_graph.optimize()
        final_graph = self.add_tokens(final_graph)
        self.fst = final_graph.optimize()