-
Notifications
You must be signed in to change notification settings - Fork 163
spanish tn code switch #423
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
folivoramanh
wants to merge
3
commits into
NVIDIA:main
Choose a base branch
from
folivoramanh:es_tn_v1
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -4,6 +4,8 @@ expy Expressway | |
| fwy Freeway | ||
| hwy Highway | ||
| dr Drive | ||
| rd Road | ||
| road Road | ||
| ct Court | ||
| ave Avenue | ||
| av Avenue | ||
|
|
||
13 changes: 13 additions & 0 deletions
13
nemo_text_processing/text_normalization/es/data/address/__init__.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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. |
6 changes: 6 additions & 0 deletions
6
nemo_text_processing/text_normalization/es/data/address/apt_designator.tsv
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |
4 changes: 4 additions & 0 deletions
4
nemo_text_processing/text_normalization/es/data/address/direction.tsv
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| E East | ||
| S South | ||
| W West | ||
| N North |
1 change: 1 addition & 0 deletions
1
nemo_text_processing/text_normalization/es/data/address/po_box.tsv
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| P.O. Box P.O. Box |
2 changes: 2 additions & 0 deletions
2
nemo_text_processing/text_normalization/es/data/address/suite_designator.tsv
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| Ste. Suite | ||
| Ste Suite |
1 change: 1 addition & 0 deletions
1
nemo_text_processing/text_normalization/es/data/address/unit_designator.tsv
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| Unit Unit |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
157 changes: 157 additions & 0 deletions
157
nemo_text_processing/text_normalization/es/taggers/address.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
10 changes: 10 additions & 0 deletions
10
tests/nemo_text_processing/es/data_text_normalization/test_cases_address.txt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| Mi dirección es 1234 Maple St., Springfield, IL 62704~Mi dirección es mil doscientos treinta y cuatro Maple Street, Springfield, Illinois seis dos siete cero cuatro | ||
| La oficina está ubicada en 567 Main St., Ste. 200, Dallas, TX 75201~La oficina está ubicada en quinientos sesenta y siete Main Street, Suite doscientos, Dallas, Texas siete cinco dos cero uno | ||
| Por favor envía el paquete a 890 Oak Ave., Apt. 5B, Brooklyn, NY 11201~Por favor envía el paquete a ochocientos noventa Oak Avenue, Apartamento cinco B, Brooklyn, New York uno uno dos cero uno | ||
| Vivo en 4321 Sunset Blvd., Los Angeles, CA 90028, cerca del centro~Vivo en cuatro mil trescientos veintiuno Sunset Boulevard, Los Angeles, California nueve cero cero dos ocho, cerca del centro | ||
| La nueva tienda abrirá en 100 Market Rd., San Francisco, CA 94105~La nueva tienda abrirá en cien Market Road, San Francisco, California nueve cuatro uno cero cinco | ||
| Su casa queda en 25 W 42nd St., New York, NY 10036~Su casa queda en veinticinco West forty second Street, New York, New York uno cero cero tres seis | ||
| El hospital se encuentra en 7890 Lincoln Dr., Miami, FL 33133~El hospital se encuentra en siete mil ochocientos noventa Lincoln Drive, Miami, Florida tres tres uno tres tres | ||
| Mándame la carta a P.O. Box 456, Austin, TX 78701, por favor~Mándame la carta a P.O. Box cuatrocientos cincuenta y seis, Austin, Texas siete ocho siete cero uno, por favor | ||
| La escuela de mis hijos está en 321 Elm St., Boston, MA 02108~La escuela de mis hijos está en trescientos veintiuno Elm Street, Boston, Massachusetts cero dos uno cero ocho | ||
| Nos mudamos a 9876 Pine Rd., Unit 12, Seattle, WA 98101 el mes pasado~Nos mudamos a nueve mil ochocientos setenta y seis Pine Road, Unit doce, Seattle, Washington nueve ocho uno cero uno el mes pasado |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,47 @@ | ||
| # 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. | ||
|
|
||
| import pytest | ||
| from parameterized import parameterized | ||
|
|
||
| from nemo_text_processing.text_normalization.normalize import Normalizer | ||
| from nemo_text_processing.text_normalization.normalize_with_audio import NormalizerWithAudio | ||
|
|
||
| from ..utils import CACHE_DIR, RUN_AUDIO_BASED_TESTS, parse_test_case_file | ||
|
|
||
|
|
||
| class TestAddress: | ||
| normalizer_es = Normalizer(input_case='cased', lang='es', cache_dir=CACHE_DIR, overwrite_cache=False) | ||
|
|
||
| normalizer_with_audio_es = ( | ||
| NormalizerWithAudio(input_case='cased', lang='es', cache_dir=CACHE_DIR, overwrite_cache=False) | ||
| if RUN_AUDIO_BASED_TESTS | ||
| else None | ||
| ) | ||
|
|
||
| # Spanish US-address code-switching is tagged by the measure class. | ||
| @parameterized.expand(parse_test_case_file('es/data_text_normalization/test_cases_address.txt')) | ||
| @pytest.mark.run_only_on('CPU') | ||
| @pytest.mark.unit | ||
| def test_norm(self, test_input, expected): | ||
| pred = self.normalizer_es.normalize(test_input, verbose=False, punct_post_process=True) | ||
| assert pred == expected | ||
|
|
||
| if self.normalizer_with_audio_es: | ||
| pred_non_deterministic = self.normalizer_with_audio_es.normalize( | ||
| test_input, | ||
| n_tagged=500, | ||
| punct_post_process=False, | ||
| ) | ||
| assert expected in pred_non_deterministic |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.