Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion tn/english/rules/cardinal.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ def build_tagger(self):
| cardinal_with_leading_zeros).optimize()

one_to_a_replacement_graph = (pynutil.add_weight(pynini.cross("one hundred", "a hundred"), 0.0001)
| pynutil.add_weight(pynini.cross("one thousand", "thousand"), -0.0001)
| pynutil.add_weight(pynini.cross("one thousand", "thousand"), 0.0001)
| pynutil.add_weight(pynini.cross("one million", "a million"), 0.0001))
final_graph |= pynini.compose(
final_graph,
Expand Down
31 changes: 29 additions & 2 deletions tn/english/rules/money.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ def build_tagger(self):
def build_verbalizer(self):
"""
Finite state transducer for verbalizing money, e.g.
money { currency_maj: "$" integer_part: "12" fractional_part: "05" } -> twelve point oh five dollars
money { currency_maj: "$" integer_part: "12" fractional_part: "05" } -> twelve dollars five cents
"""
decimal = self.decimal
keep_space = pynini.accep(" ")
Expand All @@ -112,7 +112,34 @@ def build_verbalizer(self):
| optional_integer + fractional + decimal.optional_quantity)
graph_decimal = money_decimal + keep_space + maj_plural

graph = graph_integer | graph_decimal
# Ordinary one- or two-digit fractions denote minor currency units.
# Keep quantity expressions and higher precision on the decimal path.
minor_singular = dict(load_labels(get_abs_path("english/data/money/currency_minor_singular.tsv")))
minor_plural = dict(load_labels(get_abs_path("english/data/money/currency_minor_plural.tsv")))
minor_singular["US$"] = minor_singular["$"]
cents = pynini.string_map([(str(i), str(i * 10)) for i in range(1, 10)] +
[(f"{i:02d}", str(i)) for i in range(1, 100)])
cents_one = cents @ pynini.cross("1", "one")
cents_other = cents @ ((self.VCHAR.star - "1") @ self.cardinal.graph_with_and)
graph_cents = pynini.Fst()
for symbol, major in load_labels(get_abs_path("english/data/money/currency_major.tsv")):
if symbol not in minor_singular or symbol not in minor_plural:
continue
plural = pynini.project(pynini.accep(major) @ SINGULAR_TO_PLURAL, "output")
integer = integer_one + pynutil.insert(" " + major + " ")
integer |= integer_other + pynutil.insert(" ") + pynutil.insert(plural) + pynutil.insert(" ")
fraction = (self.DELETE_SPACE + pynutil.delete('fractional_part: "') +
(cents_one + pynutil.insert(" " + minor_singular[symbol]) |
cents_other + pynutil.insert(" " + minor_plural[symbol])) + pynutil.delete('"'))
currency = self.DELETE_SPACE + pynutil.delete(f'currency_maj: "{symbol}"')
graph_cents |= integer + fraction + currency

# Exclude these token strings from the decimal fallback rather than
# relying on weights to choose the currency reading.
cents_inputs = pynini.determinize(pynini.arcmap(
pynini.project(graph_cents, "input"), map_type="rmweight").rmepsilon()).optimize()
fallback_inputs = pynini.difference(pynini.project(graph_decimal, "input"), cents_inputs)
graph = graph_integer | graph_cents | (fallback_inputs @ graph_decimal)

delete_tokens = self.delete_tokens(graph)
self.verbalizer = delete_tokens.optimize()
9 changes: 5 additions & 4 deletions tn/english/test/data/cardinal.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,11 @@
101 => one hundred and one
111 => one hundred and eleven
200 => two hundred
1000 => thousand
1001 => thousand and one
1011 => thousand and eleven
1111 => thousand one hundred and eleven
1000 => one thousand
1001 => one thousand and one
1011 => one thousand and eleven
1111 => one thousand one hundred and eleven
1,022 => one thousand and twenty two
2000 => two thousand
10000 => ten thousand
10001 => ten thousand and one
Expand Down
20 changes: 18 additions & 2 deletions tn/english/test/data/money.txt
Original file line number Diff line number Diff line change
@@ -1,8 +1,24 @@
$12.05 => twelve point oh five dollars
$12.05 => twelve dollars five cents
$12.0500 => twelve point oh five dollars
$1 => one dollar
$1.00 => one dollar
$0.05 => zero point oh five dollars
$0.05 => zero dollars five cents
$1 million => one million dollars
$1.2 million => one point two million dollars
$1.2320 => one point two three two dollars
$18.50 => eighteen dollars fifty cents
$18.05 => eighteen dollars five cents
$7.25 => seven dollars twenty five cents
$18.5 => eighteen dollars fifty cents
$1.01 => one dollar one cent
$0.01 => zero dollars one cent
$0.00 => zero dollars
$2.10 => two dollars ten cents
$2.99 => two dollars ninety nine cents
£1.01 => one pound one penny
£2.02 => two pounds two pence
€1.01 => one euro one cent
€2.50 => two euros fifty cents
US$1.01 => one us dollar one cent
¥2.50 => two point five yen
$1.05 million => one point oh five million dollars
11 changes: 11 additions & 0 deletions tn/english/test/money_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,14 @@ class TestMoney:
@pytest.mark.parametrize("written, spoken", money_cases)
def test_money(self, written, spoken):
assert self.money.normalize(written) == spoken

@pytest.mark.parametrize("written, spoken", [
("$18.50", "eighteen dollars fifty cents"),
("$18.05", "eighteen dollars five cents"),
("$7.25", "seven dollars twenty five cents"),
("$1.01", "one dollar one cent"),
])
def test_minor_units_do_not_fall_back_to_decimal_in_nbest(self, written, spoken):
outputs = self.money.normalize(written, nbest=8)
assert outputs[0] == spoken
assert all("point" not in output for output in outputs)
8 changes: 8 additions & 0 deletions tn/english/test/nbest_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,14 @@ def test_cardinal_nbest_preserves_digit_and_article_alternatives():
assert "a hundred" in hundred_outputs


def test_thousand_defaults_to_one_and_preserves_shortened_alternative():
cardinal = Cardinal(deterministic=False)
outputs = cardinal.normalize("1,022", nbest=8)

assert outputs[0] == "one thousand and twenty two"
assert "thousand and twenty two" in outputs


def test_decimal_nbest_preserves_zero_and_oh_alternatives():
decimal = Decimal(deterministic=False)

Expand Down
16 changes: 14 additions & 2 deletions tn/english/test/normalizer_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,15 @@ def test_normalize_with_mapping_preserves_punctuation_adjacency(self, normalizer
(12, 13),
]

@pytest.mark.parametrize("written, spoken", [
("It costs $18.50.", "It costs eighteen dollars fifty cents."),
("It costs $18.05.", "It costs eighteen dollars five cents."),
("It costs $7.25.", "It costs seven dollars twenty five cents."),
("There are 1,022 items.", "There are one thousand and twenty two items."),
])
def test_issue_406_in_sentence_context(self, normalizer, written, spoken):
assert normalizer.normalize(written) == spoken

def test_cardinal_default_is_independent_of_sentence_context(self, normalizer):
# The old spoken-field tagger exposed a whole-sentence shortest-path
# tie: the same 256 selected different verbalizations in two fixtures.
Expand Down Expand Up @@ -146,12 +155,15 @@ def test_semantic_tags_preserve_written_fields(self, normalizer, written, token_
("23", "twenty three", "cardinal"),
("2024-05-06", "the sixth of may twenty twenty four", "date"),
("03:43 p.m.", "three forty three PM", "time"),
("$12.05", "twelve point oh five dollars", "money"),
("$12.05", "twelve dollars five cents", "money"),
("$18.50", "eighteen dollars fifty cents", "money"),
("$1.01", "one dollar one cent", "money"),
("$1.00", "one dollar", "money"),
("$1.2000", "one point two dollars", "money"),
("31.990 billion", "thirty one point nine nine oh billion", "decimal"),
("3/4", "three quarters", "fraction"),
("1,000", "thousand", "cardinal"),
("1,000", "one thousand", "cardinal"),
("1,022", "one thousand and twenty two", "cardinal"),
("¾", "three quarters", "fraction"),
],
)
Expand Down
Loading