游雁
2024-12-12 2139ef696bbf844ab0c1636778f68291d3613fcb
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
import pynini
from fun_text_processing.text_normalization.en.graph_utils import (
    DAMO_ALPHA,
    DAMO_NON_BREAKING_SPACE,
    DAMO_SIGMA,
    DAMO_SPACE,
    GraphFst,
    convert_space,
    delete_space,
    insert_space,
)
from fun_text_processing.text_normalization.es.graph_utils import strip_cardinal_apocope
from fun_text_processing.text_normalization.es.utils import get_abs_path
from pynini.lib import pynutil
 
unit = pynini.string_file(get_abs_path("data/measures/measurements.tsv"))
unit_plural_fem = pynini.string_file(get_abs_path("data/measures/measurements_plural_fem.tsv"))
unit_plural_masc = pynini.string_file(get_abs_path("data/measures/measurements_plural_masc.tsv"))
 
 
class MeasureFst(GraphFst):
    """
    Finite state transducer for classifying measure,  e.g.
        "2,4 g" -> measure { cardinal { integer_part: "dos" fractional_part: "cuatro" units: "gramos" preserve_order: true } }
        "1 g" -> measure { cardinal { integer: "un" units: "gramo" preserve_order: true } }
        "1 millón g" -> measure { cardinal { integer: "un quantity: "millón" units: "gramos" preserve_order: true } }
        e.g. "a-8" —> "a ocho"
        e.g. "1,2-a" —> "uno coma dos a"
        This class also converts words containing numbers and letters
        e.g. "a-8" —> "a ocho"
        e.g. "1,2-a" —> "uno coma dos a"
 
 
    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
 
        unit_singular = unit
        unit_plural = unit_singular @ (unit_plural_fem | unit_plural_masc)
 
        graph_unit_singular = convert_space(unit_singular)
        graph_unit_plural = convert_space(unit_plural)
 
        optional_graph_negative = pynini.closure("-", 0, 1)
 
        graph_unit_denominator = (
            pynini.cross("/", "por") + pynutil.insert(DAMO_NON_BREAKING_SPACE) + graph_unit_singular
        )
 
        optional_unit_denominator = pynini.closure(
            pynutil.insert(DAMO_NON_BREAKING_SPACE) + graph_unit_denominator,
            0,
            1,
        )
 
        unit_plural = (
            pynutil.insert('units: "')
            + ((graph_unit_plural + optional_unit_denominator) | graph_unit_denominator)
            + pynutil.insert('"')
        )
 
        unit_singular_graph = (
            pynutil.insert('units: "')
            + ((graph_unit_singular + optional_unit_denominator) | graph_unit_denominator)
            + pynutil.insert('"')
        )
 
        subgraph_decimal = (
            decimal.fst + insert_space + pynini.closure(DAMO_SPACE, 0, 1) + unit_plural
        )
 
        subgraph_cardinal = (
            (optional_graph_negative + (DAMO_SIGMA - "1")) @ cardinal.fst
            + insert_space
            + pynini.closure(delete_space, 0, 1)
            + unit_plural
        )
 
        subgraph_cardinal |= (
            (optional_graph_negative + pynini.accep("1")) @ cardinal.fst
            + insert_space
            + pynini.closure(delete_space, 0, 1)
            + unit_singular_graph
        )
 
        subgraph_fraction = (
            fraction.fst + insert_space + pynini.closure(delete_space, 0, 1) + unit_singular_graph
        )
 
        decimal_times = (
            pynutil.insert("decimal { ")
            + decimal.final_graph_wo_negative
            + pynutil.insert(' } units: "')
            + pynini.union("x", "X")
            + pynutil.insert('"')
        )
 
        cardinal_times = (
            pynutil.insert('cardinal { integer: "')
            + strip_cardinal_apocope(cardinal_graph)
            + pynutil.insert('" } units: "')
            + pynini.union("x", "X")
            + pynutil.insert('"')
        )
 
        cardinal_dash_alpha = (
            pynutil.insert('cardinal { integer: "')
            + strip_cardinal_apocope(cardinal_graph)
            + pynutil.delete("-")
            + pynutil.insert('" } units: "')
            + pynini.closure(DAMO_ALPHA, 1)
            + pynutil.insert('"')
        )
 
        decimal_dash_alpha = (
            pynutil.insert("decimal { ")
            + decimal.final_graph_wo_negative
            + pynutil.delete("-")
            + pynutil.insert(' } units: "')
            + pynini.closure(DAMO_ALPHA, 1)
            + pynutil.insert('"')
        )
 
        alpha_dash_cardinal = (
            pynutil.insert('units: "')
            + pynini.closure(DAMO_ALPHA, 1)
            + pynutil.delete("-")
            + pynutil.insert('"')
            + pynutil.insert(' cardinal { integer: "')
            + cardinal_graph
            + pynutil.insert('" } preserve_order: true')
        )
 
        alpha_dash_decimal = (
            pynutil.insert('units: "')
            + pynini.closure(DAMO_ALPHA, 1)
            + pynutil.delete("-")
            + pynutil.insert('"')
            + pynutil.insert(" decimal { ")
            + decimal.final_graph_wo_negative
            + pynutil.insert(" } preserve_order: true")
        )
 
        final_graph = (
            subgraph_decimal
            | subgraph_cardinal
            | cardinal_dash_alpha
            | alpha_dash_cardinal
            | decimal_dash_alpha
            | subgraph_fraction
            | decimal_times
            | cardinal_times
            | alpha_dash_decimal
        )
        final_graph += pynutil.insert(" preserve_order: true")
        final_graph = self.add_tokens(final_graph)
 
        self.fst = final_graph.optimize()