1313# limitations under the License.
1414
1515from pynini import closure , cross , string_file , union
16- from pynini .lib .pynutil import delete , insert
16+ from pynini .lib .pynutil import add_weight , delete , insert
1717
1818from itn .english .rules .cardinal import Cardinal
1919from itn .english .rules .decimal import Decimal
@@ -33,6 +33,7 @@ def __init__(self, cardinal=None, decimal=None):
3333
3434 def build_tagger (self ):
3535 cardinal_graph = self .cardinal .graph
36+ cardinal_small = self .cardinal .up_to_999
3637 ds = delete (" " )
3738
3839 currency_labels = load_labels (get_abs_path ("../itn/english/data/currency.tsv" ))
@@ -49,51 +50,70 @@ def build_tagger(self):
4950 magnitudes = load_labels (get_abs_path ("../itn/english/data/magnitudes.tsv" ))
5051 magnitude = union (* [name for symbol , name in magnitudes ])
5152
53+ # "two dollars"
5254 integer_graph = (
5355 insert ('value: "' ) + cardinal_graph + insert ('"' )
5456 + ds + insert (' currency: "' ) + currency + insert ('"' )
5557 )
56- # "fifty million dollars" => $50 million
57- cardinal_small = self .cardinal .up_to_999
58+ # "fifty million dollars" / "four hundred billion won"
5859 quantity_graph = (
5960 insert ('value: "' ) + cardinal_small + insert ('"' )
6061 + ds + insert (' quantity: "' ) + magnitude + insert ('"' )
6162 + ds + insert (' currency: "' ) + currency + insert ('"' )
6263 )
63- # cents: pad single digit (1-9 => 01-09)
64- cents_graph = union (* [cross (_num_to_word (x ), f"{ x :02d} " ) for x in range (1 , 100 ) if _num_to_word (x )])
65- with_cents = (
66- insert ('value: "' ) + cardinal_graph + insert ('"' )
67- + ds + insert (' currency: "' ) + currency + insert ('"' )
68- + ds + (delete ("and" ) + ds ).ques
69- + insert (' decimal: "' ) + cents_graph + insert ('"' )
70- + ds + cent
71- )
72- cents_only = (
73- insert ('currency: "$" decimal: "' ) + cents_graph + insert ('"' )
74- + ds + cent
75- )
76-
7764 # "two point five billion dollars"
78- frac_digit = string_file (get_abs_path ("../itn/english/data/numbers/digit.tsv" ))
79- frac_zero = string_file (get_abs_path ("../itn/english/data/numbers/zero.tsv" ))
80- frac_d = frac_digit | frac_zero | cross ("o" , "0" )
65+ digit = string_file (get_abs_path ("../itn/english/data/numbers/digit.tsv" ))
66+ zero = string_file (get_abs_path ("../itn/english/data/numbers/zero.tsv" ))
67+ frac_d = digit | zero | cross ("o" , "0" )
8168 frac = closure (frac_d + ds ) + frac_d
8269 decimal_quantity_graph = (
8370 insert ('value: "' ) + cardinal_graph + insert ("." )
8471 + ds + delete ("point" ) + ds + frac + insert ('"' )
8572 + ds + insert (' quantity: "' ) + magnitude + insert ('"' )
8673 + ds + insert (' currency: "' ) + currency + insert ('"' )
8774 )
75+ # "twenty point five o six dollars" (decimal without quantity)
76+ decimal_graph = (
77+ insert ('value: "' ) + cardinal_graph + insert ("." )
78+ + ds + delete ("point" ) + ds + frac + insert ('"' )
79+ + ds + insert (' currency: "' ) + currency + insert ('"' )
80+ )
81+ # "point five o six dollars"
82+ decimal_no_int = (
83+ insert ('value: ".' ) + delete ("point" ) + ds + frac + insert ('"' )
84+ + ds + insert (' currency: "' ) + currency + insert ('"' )
85+ )
86+ # "one fifty five dollars" => $155 (missing "hundred")
87+ with_hundred = (
88+ insert ('value: "' ) + cardinal_small + insert ('"' )
89+ + ds + insert (' currency: "' ) + currency + insert ('"' )
90+ )
8891
92+ # cents
93+ cents_graph = union (* [cross (_num_to_word (x ), f"{ x :02d} " ) for x in range (1 , 100 ) if _num_to_word (x )])
94+ with_cents = (
95+ insert ('value: "' ) + cardinal_graph + insert ('"' )
96+ + ds + insert (' currency: "' ) + currency + insert ('"' )
97+ + ds + (delete ("and" ) + ds ).ques
98+ + insert (' decimal: "' ) + cents_graph + insert ('"' )
99+ + ds + cent
100+ )
89101 # "seventy five dollars sixty three" (no "cents" word)
90102 dollars_amount = (
91103 insert ('value: "' ) + cardinal_graph + insert ('"' )
92104 + ds + insert (' currency: "' ) + currency + insert ('"' )
93105 + ds + insert (' decimal: "' ) + cents_graph + insert ('"' )
94106 )
107+ cents_only = (
108+ insert ('currency: "$" decimal: "' ) + cents_graph + insert ('"' )
109+ + ds + cent
110+ )
95111
96- graph = integer_graph | quantity_graph | decimal_quantity_graph | with_cents | dollars_amount | cents_only
112+ graph = (
113+ integer_graph | add_weight (quantity_graph , - 1 ) | add_weight (decimal_quantity_graph , - 1 )
114+ | decimal_graph | decimal_no_int
115+ | with_cents | dollars_amount | cents_only
116+ )
97117 self .tagger = self .add_tokens (graph )
98118
99119 def build_verbalizer (self ):
0 commit comments