Skip to content

Commit dbaab50

Browse files
committed
feat: money quantity and decimal support, 442/470 (94%)
1 parent 4482d8e commit dbaab50

2 files changed

Lines changed: 42 additions & 22 deletions

File tree

itn/english/inverse_normalizer.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ def build_tagger_and_verbalizer(self):
5656
add_weight(date.tagger, 1.09)
5757
| add_weight(time.tagger, 1.1)
5858
| add_weight(measure.tagger, 1.1)
59-
| add_weight(money.tagger, 1.1)
59+
| add_weight(money.tagger, 1.08)
6060
| add_weight(whitelist.tagger, 1.01)
6161
| add_weight(telephone.tagger, 1.1)
6262
| add_weight(electronic.tagger, 1.1)

itn/english/rules/money.py

Lines changed: 41 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
# limitations under the License.
1414

1515
from 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

1818
from itn.english.rules.cardinal import Cardinal
1919
from 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

Comments
 (0)