Skip to content
Open
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 Jenkinsfile
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ pipeline {
AR_TN_CACHE='/home/jenkins/TestData/text_norm/ci/grammars/04-24-24-0'
DE_TN_CACHE='/home/jenkins/TestData/text_norm/ci/grammars/10-23-24-0'
EN_TN_CACHE='/home/jenkins/TestData/text_norm/ci/grammars/09-25-25-0'
ES_TN_CACHE='/home/jenkins/TestData/text_norm/ci/grammars/09-25-24-0'
ES_TN_CACHE='/home/jenkins/TestData/text_norm/ci/grammars/05-20-26-0'
ES_EN_TN_CACHE='/home/jenkins/TestData/text_norm/ci/grammars/08-30-24-0'
FR_TN_CACHE='/home/jenkins/TestData/text_norm/ci/grammars/04-07-25-0'
HU_TN_CACHE='/home/jenkins/TestData/text_norm/ci/grammars/07-16-24-0'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ expy Expressway
fwy Freeway
hwy Highway
dr Drive
rd Road
road Road
ct Court
ave Avenue
av Avenue
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# Copyright (c) 2026, NVIDIA CORPORATION & AFFILIATES. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
Apt. Apartamento
Apt. Apartamento
Dept. Departamento
Dept Departamento
Depto. Departamento
Depto Departamento
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
E East
S South
W West
N North
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
P.O. Box P.O. Box
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Ste. Suite
Ste Suite
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Unit Unit
15 changes: 15 additions & 0 deletions nemo_text_processing/text_normalization/es/graph_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,21 @@ def strip_cardinal_apocope(fst: "pynini.FstLike") -> "pynini.FstLike":
return fst @ strip


def normalize_spanish_cardinal_for_us_address_street(fst: "pynini.FstLike") -> "pynini.FstLike":
"""
Spanish cardinals often apocopate before a following vowel (e.g. ``veintiún``). US street names
are ASCII and usually start with a consonant, but the cardinal FST does not see that context when
materializing digits alone. Normalize common ``…ún`` spoken forms to ``…uno`` / ``… y uno`` for
address surfaces (same intent as ``strip_cardinal_apocope`` but not restricted to string end).
"""
out = fst
out = out @ pynini.cdrewrite(pynini.cross("veintiún", "veintiuno"), "", "", NEMO_SIGMA)
out = out @ pynini.cdrewrite(pynini.cross("treintún", "treinta y uno"), "", "", NEMO_SIGMA)
out = out @ pynini.cdrewrite(pynini.cross(" y ún", " y uno"), "", "", NEMO_SIGMA)
Comment thread
folivoramanh marked this conversation as resolved.
out = out @ pynini.cdrewrite(pynini.cross(" y un", " y uno"), "", "", NEMO_SIGMA)
return strip_cardinal_apocope(out)


def add_cardinal_apocope_fem(fst: "pynini.FstLike") -> "pynini.FstLike":
"""
Adds apocope on cardinal strings in line with stressing rules. e.g. "una" -> "un". This only occurs when "una" precedes a stressed "a" sound in formal speech. This is not predictable
Expand Down
157 changes: 157 additions & 0 deletions nemo_text_processing/text_normalization/es/taggers/address.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,157 @@
# Copyright (c) 2026, NVIDIA CORPORATION & AFFILIATES. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

"""
US-style postal address surface for Spanish TN (embedded in ``MeasureFst`` as
``units: "address_us_es"``).

Street numbers and ZIP are Spanish; street types, states, and ordinals (e.g. ``42nd``)
use English expansions from shared ``en/data/address/`` lexicons.
"""

import pynini
from pynini.examples import plurals
from pynini.lib import pynutil

from nemo_text_processing.text_normalization.en.graph_utils import (
NEMO_ALPHA,
NEMO_DIGIT,
NEMO_SIGMA,
NEMO_SPACE,
NEMO_UPPER,
GraphFst,
insert_space,
)
from nemo_text_processing.text_normalization.en.taggers.cardinal import CardinalFst as EnCardinalFst
from nemo_text_processing.text_normalization.en.taggers.ordinal import OrdinalFst as OrdinalTagger
from nemo_text_processing.text_normalization.en.taggers.whitelist import get_formats
from nemo_text_processing.text_normalization.en.utils import get_abs_path as en_get_abs_path
from nemo_text_processing.text_normalization.en.utils import load_labels
from nemo_text_processing.text_normalization.en.verbalizers.ordinal import OrdinalFst as OrdinalVerbalizer
from nemo_text_processing.text_normalization.es.graph_utils import normalize_spanish_cardinal_for_us_address_street
from nemo_text_processing.text_normalization.es.utils import get_abs_path


class AddressUSSurfaceFst(GraphFst):
"""
Surface FST for US addresses inside Spanish sentences.

Output is the spoken string stored in ``measure { units: "address_us_es" cardinal { integer: "..." } }``.
Not registered in ``tokenize_and_classify``; consumed by :class:`~nemo_text_processing.text_normalization.es.taggers.measure.MeasureFst`.

Args:
cardinal: Spanish :class:`~nemo_text_processing.text_normalization.es.taggers.cardinal.CardinalFst`
deterministic: passed to English ordinal/cardinal helpers
"""

def __init__(self, cardinal: GraphFst, deterministic: bool = True):
super().__init__(name="address_us_es_surface", kind="classify", deterministic=deterministic)

graph_direction = pynini.string_file(get_abs_path("data/address/direction.tsv"))
graph_zip_digit = pynini.invert(
pynini.string_file(get_abs_path("data/numbers/zero.tsv"))
| pynini.string_file(get_abs_path("data/numbers/digit.tsv"))
).optimize()
graph_zip_digit @= pynini.cdrewrite(pynini.cross("un", "uno"), "", "", NEMO_SIGMA)
graph_suite_designator = pynini.string_file(get_abs_path("data/address/suite_designator.tsv"))
graph_apt_designator = pynini.string_file(get_abs_path("data/address/apt_designator.tsv"))
graph_unit_designator = pynini.string_file(get_abs_path("data/address/unit_designator.tsv"))
graph_po_box = pynini.string_file(get_abs_path("data/address/po_box.tsv"))

en_cardinal = EnCardinalFst(deterministic=deterministic)
g = cardinal.graph

ordinal_en = pynini.compose(
pynutil.insert('integer: "') + OrdinalTagger(cardinal=en_cardinal).graph + pynutil.insert('"'),
OrdinalVerbalizer().graph,
)

address_num = NEMO_DIGIT ** (1, 2) @ cardinal.graph_hundreds_component_at_least_one_none_zero_digit
address_num += insert_space + NEMO_DIGIT**2 @ (
pynini.closure(pynini.cross("0", "cero "), 0, 1)
+ cardinal.graph_hundreds_component_at_least_one_none_zero_digit
)
address_num = pynini.compose(NEMO_DIGIT ** (3, 4), address_num)
address_num = normalize_spanish_cardinal_for_us_address_street(
plurals._priority_union(address_num, g, NEMO_SIGMA).optimize()
)

direction = pynini.closure(
pynini.accep(NEMO_SPACE) + graph_direction + pynini.closure(pynutil.delete("."), 0, 1),
0,
1,
)

address_words = get_formats(en_get_abs_path("data/address/address_word.tsv"))
street = (
pynini.accep(NEMO_SPACE)
+ (pynini.closure(ordinal_en, 0, 1) | NEMO_UPPER + pynini.closure(NEMO_ALPHA, 1))
+ NEMO_SPACE
+ pynini.closure(NEMO_UPPER + pynini.closure(NEMO_ALPHA) + NEMO_SPACE)
+ address_words
)

zip_five = (pynini.closure(graph_zip_digit + insert_space, 4) + graph_zip_digit).optimize()

city = pynini.closure(NEMO_ALPHA | pynini.accep(NEMO_SPACE), 1)
city = pynini.closure(pynini.accep(",") + pynini.accep(NEMO_SPACE) + city, 0, 1)

states = load_labels(en_get_abs_path("data/address/state.tsv"))
states_extra = [(x, f"{y[0]}.{y[1:]}") for x, y in states]
states.extend(states_extra)
state = pynini.closure(
pynini.accep(",") + pynini.accep(NEMO_SPACE) + pynini.invert(pynini.string_map(states)), 0, 1
)

zip_code = pynini.closure(
pynini.closure(pynini.accep(","), 0, 1) + pynini.accep(NEMO_SPACE) + zip_five,
0,
1,
)
tail = pynini.closure(city + state + zip_code, 0, 1).optimize()

suite_num = normalize_spanish_cardinal_for_us_address_street((pynini.closure(NEMO_DIGIT, 1, 4) @ g).optimize())
unit_num = normalize_spanish_cardinal_for_us_address_street((pynini.closure(NEMO_DIGIT, 1, 3) @ g).optimize())
apt_char = graph_zip_digit | NEMO_UPPER
apt_num = (apt_char + pynini.closure(insert_space + apt_char, 0, 3)).optimize()

comma_sp = pynini.accep(",") + pynini.accep(NEMO_SPACE)
suite = graph_suite_designator + pynini.closure(NEMO_SPACE, 0, 1) + suite_num
apt = graph_apt_designator + pynini.closure(NEMO_SPACE, 0, 1) + apt_num
unit = graph_unit_designator + unit_num
middle = pynini.closure(comma_sp + (suite | apt | unit), 0, 3).optimize()

po_box = (
graph_po_box
+ normalize_spanish_cardinal_for_us_address_street(pynini.closure(NEMO_DIGIT, 1, 4) @ g)
+ tail
).optimize()

standard = address_num + direction + street + middle + tail
hyphen = pynini.accep("-")
alpha_chars = NEMO_ALPHA | hyphen
standard_eos = (
address_num
+ direction
+ street
+ middle
+ pynini.accep(".")
+ pynini.closure(NEMO_SPACE, 1, 2)
+ NEMO_UPPER
+ pynini.closure(alpha_chars)
)
standard |= pynutil.add_weight(standard_eos, -0.001)
standard |= address_num + direction + street + middle + pynini.closure(pynini.cross(".", ""), 0, 1)

self.graph = (po_box | standard.optimize()).optimize()
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,10 @@ def __init__(self, deterministic: bool = True):

self.graph = filter_punctuation(self.graph).optimize()

self.graph_hundreds_component_at_least_one_none_zero_digit = (
graph_hundreds_component_at_least_one_none_zero_digit.optimize()
)

optional_minus_graph = pynini.closure(pynutil.insert("negative: ") + pynini.cross("-", "\"true\" "), 0, 1)

final_graph = optional_minus_graph + pynutil.insert("integer: \"") + self.graph + pynutil.insert("\"")
Expand Down
9 changes: 9 additions & 0 deletions nemo_text_processing/text_normalization/es/taggers/measure.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
insert_space,
)
from nemo_text_processing.text_normalization.es.graph_utils import strip_cardinal_apocope
from nemo_text_processing.text_normalization.es.taggers.address import AddressUSSurfaceFst
from nemo_text_processing.text_normalization.es.utils import get_abs_path

unit = pynini.string_file(get_abs_path("data/measures/measurements.tsv"))
Expand Down Expand Up @@ -199,6 +200,13 @@ def __init__(self, cardinal: GraphFst, decimal: GraphFst, fraction: GraphFst, de
+ pynutil.insert("\" } preserve_order: true")
)

address_us_es_inner = AddressUSSurfaceFst(cardinal, deterministic=deterministic).graph
Comment thread
folivoramanh marked this conversation as resolved.
address_us_es = (
pynutil.insert('units: "address_us_es" cardinal { integer: "')
+ address_us_es_inner
+ pynutil.insert('" } preserve_order: true')
)

final_graph = (
subgraph_decimal
| subgraph_cardinal
Expand All @@ -210,6 +218,7 @@ def __init__(self, cardinal: GraphFst, decimal: GraphFst, fraction: GraphFst, de
| cardinal_times
| alpha_dash_decimal
| math
| address_us_es
)
final_graph = self.add_tokens(final_graph)

Expand Down
34 changes: 31 additions & 3 deletions nemo_text_processing/text_normalization/es/verbalizers/measure.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
delete_extra_space,
delete_preserve_order,
delete_space,
insert_space,
)
from nemo_text_processing.text_normalization.es.graph_utils import ones
from nemo_text_processing.text_normalization.es.utils import get_abs_path
Expand Down Expand Up @@ -65,12 +66,20 @@ def __init__(self, decimal: GraphFst, cardinal: GraphFst, fraction: GraphFst, de
NEMO_WHITE_SPACE + "por" + pynini.closure(NEMO_NOT_QUOTE, 1), 0, 1
)
unit_masc |= "por" + pynini.closure(NEMO_NOT_QUOTE, 1)
unit_masc = pynutil.delete("units: \"") + (pynini.closure(NEMO_NOT_QUOTE) @ unit_masc) + pynutil.delete("\"")
unit_masc = (
pynutil.delete("units: \"")
+ (pynini.difference(pynini.closure(NEMO_NOT_QUOTE, 1), pynini.union("math", "address_us_es")) @ unit_masc)
+ pynutil.delete("\"")
)

unit_fem = (unit_plural_fem | unit_singular_fem) + pynini.closure(
NEMO_WHITE_SPACE + "por" + pynini.closure(NEMO_NOT_QUOTE, 1), 0, 1
)
unit_fem = pynutil.delete("units: \"") + (pynini.closure(NEMO_NOT_QUOTE) @ unit_fem) + pynutil.delete("\"")
unit_fem = (
pynutil.delete("units: \"")
+ (pynini.difference(pynini.closure(NEMO_NOT_QUOTE, 1), pynini.union("math", "address_us_es")) @ unit_fem)
+ pynutil.delete("\"")
)

graph_masc = (graph_cardinal_masc | graph_decimal_masc) + NEMO_WHITE_SPACE + unit_masc
graph_masc |= graph_fraction_masc + NEMO_WHITE_SPACE + pynutil.insert("de ") + unit_masc
Expand All @@ -96,7 +105,11 @@ def __init__(self, decimal: GraphFst, cardinal: GraphFst, fraction: GraphFst, de
graph @= pynini.cdrewrite(pynini.cross(ones, "uno"), "", NEMO_WHITE_SPACE + "por", NEMO_SIGMA)

# To manage alphanumeric combonations ("a-8, 5x"), we let them use a weighted default path.
alpha_num_unit = pynutil.delete("units: \"") + pynini.closure(NEMO_NOT_QUOTE) + pynutil.delete("\"")
alpha_num_unit = (
pynutil.delete("units: \"")
+ pynini.difference(pynini.closure(NEMO_NOT_QUOTE), pynini.union("math", "address_us_es"))
+ pynutil.delete("\"")
)
graph_alpha_num = pynini.union(
(graph_cardinal_masc | graph_decimal_masc) + NEMO_SPACE + alpha_num_unit,
alpha_num_unit + delete_extra_space + (graph_cardinal_masc | graph_decimal_masc),
Expand All @@ -106,8 +119,23 @@ def __init__(self, decimal: GraphFst, cardinal: GraphFst, fraction: GraphFst, de
pynutil.delete("units: \"math\"") + delete_space + graph_cardinal_masc + delete_space, -1
)

preserve_order_tail = pynutil.delete("preserve_order:") + delete_space + pynutil.delete("true") + delete_space
address_us_es = (
pynutil.delete('units: "address_us_es" ')
+ delete_space
+ pynutil.delete("cardinal { integer: \"")
+ delete_space
+ pynini.closure(NEMO_NOT_QUOTE)
+ pynutil.delete("\"")
+ delete_space
+ pynutil.delete("}")
+ delete_space
+ pynini.closure(preserve_order_tail)
)

graph |= pynutil.add_weight(graph_alpha_num, 0.01)
graph |= math
graph |= address_us_es

graph += delete_preserve_order

Expand Down
Loading