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
3 changes: 3 additions & 0 deletions nemo_text_processing/text_normalization/normalize.py
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,9 @@ def __init__(

if post_process:
self.post_processor = PostProcessingFst(cache_dir=cache_dir, overwrite_cache=overwrite_cache)
elif lang == 'pt':
from nemo_text_processing.text_normalization.pt.taggers.tokenize_and_classify import ClassifyFst
from nemo_text_processing.text_normalization.pt.verbalizers.verbalize_final import VerbalizeFinalFst
elif lang == 'ko':
from nemo_text_processing.text_normalization.ko.taggers.tokenize_and_classify import ClassifyFst
from nemo_text_processing.text_normalization.ko.verbalizers.verbalize_final import VerbalizeFinalFst
Expand Down
13 changes: 13 additions & 0 deletions nemo_text_processing/text_normalization/pt/__init__.py
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 it 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.
13 changes: 13 additions & 0 deletions nemo_text_processing/text_normalization/pt/data/__init__.py
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,2 @@
segundo meio
terceiro terço
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
mil milésimo
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
connector e
minus menos
plural_suffix s
avos_suffix avos
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,4 @@
connector e
thousand mil
hundred_100 cem
hundred_1 cento
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
001 mil e um
010 mil e dez
100 mil e cem
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
separator vírgula
minus menos
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
1 um
2 dois
3 três
4 quatro
5 cinco
6 seis
7 sete
8 oito
9 nove
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
2 duzentos
3 trezentos
4 quatrocentos
5 quinhentos
6 seiscentos
7 setecentos
8 oitocentos
9 novecentos
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
mil
milhão
milhões
bilhão
bilhões
trilhão
trilhões
quatrilhão
quatrilhões
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
one_label plural_suffix magnitude_zeros
um milhão milhões 0
um bilhão bilhões 9
um trilhão trilhões 12
10 changes: 10 additions & 0 deletions nemo_text_processing/text_normalization/pt/data/numbers/teens.tsv
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
10 dez
11 onze
12 doze
13 treze
14 quatorze
15 quinze
16 dezesseis
17 dezessete
18 dezoito
19 dezenove
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
2 vinte
3 trinta
4 quarenta
5 cinquenta
6 sessenta
7 setenta
8 oitenta
9 noventa
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
0 zero
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.
10 changes: 10 additions & 0 deletions nemo_text_processing/text_normalization/pt/data/ordinals/digit.tsv
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
primeiro um
segundo dois
terceiro três
quarto quatro
quinto cinco
sexto seis
sétimo sete
oitavo oito
nono nove
décimo dez
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
primeiro primeira
segundo segunda
terceiro terceira
quarto quarta
quinto quinta
sexto sexta
sétimo sétima
oitavo oitava
nono nona
décimo décima
ésimo ésima
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
centésimo cem
centésimo cento
ducentésimo duzentos
trecentésimo trezentos
quadringentésimo quatrocentos
quincentésimo quinhentos
sexcentésimo seiscentos
septingentésimo setecentos
octingentésimo oitocentos
noningentésimo novecentos
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
connector_in e
connector_out
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
décimo primeiro onze
décimo segundo doze
décimo terceiro treze
décimo quarto catorze
décimo quinto quinze
décimo sexto dezesseis
décimo sétimo dezessete
décimo oitavo dezoito
décimo nono dezenove
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
vigésimo vinte
trigésimo trinta
quadragésimo quarenta
quinquagésimo cinquenta
sexagésimo sessenta
septuagésimo setenta
octogésimo oitenta
nonagésimo noventa
171 changes: 171 additions & 0 deletions nemo_text_processing/text_normalization/pt/graph_utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,171 @@
# Copyright (c) 2026, NVIDIA CORPORATION & AFFILIATES. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use it 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.


"""
Portuguese (PT) text normalization graph utilities.

Self-contained module with no dependency on en.graph_utils. Provides character/digit
symbols (NEMO_*), space helpers (delete_space, insert_space, delete_extra_space),
GraphFst base class, generator_main for FAR export, and PT-specific helpers
(filter_cardinal_punctuation, shift_cardinal_gender_pt).
"""

import os
import string
from pathlib import Path
from typing import Dict

import pynini
from pynini import Far
from pynini.export import export
from pynini.lib import byte, pynutil, utf8

from nemo_text_processing.utils.logging import logger

# ---- Character/digit symbols (same semantics as EN) ----
NEMO_CHAR = utf8.VALID_UTF8_CHAR
NEMO_DIGIT = byte.DIGIT
NEMO_LOWER = pynini.union(*string.ascii_lowercase).optimize()
NEMO_UPPER = pynini.union(*string.ascii_uppercase).optimize()
NEMO_ALPHA = pynini.union(NEMO_LOWER, NEMO_UPPER).optimize()
NEMO_SPACE = " "
NEMO_WHITE_SPACE = pynini.union(" ", "\t", "\n", "\r", "\u00a0").optimize()
NEMO_NOT_QUOTE = pynini.difference(NEMO_CHAR, pynini.accep('"')).optimize()
NEMO_SIGMA = pynini.closure(NEMO_CHAR)

delete_space = pynutil.delete(pynini.closure(NEMO_WHITE_SPACE))
insert_space = pynutil.insert(" ")
delete_extra_space = pynini.cross(pynini.closure(NEMO_WHITE_SPACE, 1), " ").optimize()


def generator_main(file_name: str, graphs: Dict[str, "pynini.FstLike"]) -> None:
"""
Export one or more graphs to an OpenFst Finite State Archive (FAR) file.

Args:
file_name: path to the output .far file.
graphs: mapping of rule names to FST graphs to export.
"""
exporter = export.Exporter(file_name)
for rule, graph in graphs.items():
exporter[rule] = graph.optimize()
exporter.close()
logger.info(f"Created {file_name}")


class GraphFst:
"""
Base class for all Portuguese text normalization grammar FSTs.

Args:
name: name of the grammar (e.g. "cardinal", "decimal").
kind: either "classify" or "verbalize".
deterministic: if True will provide a single transduction option,
for False multiple options (used for audio-based normalization).
"""

def __init__(self, name: str, kind: str, deterministic: bool = True):
self.name = name
self.kind = kind
self._fst = None
self.deterministic = deterministic

self.far_path = Path(os.path.dirname(os.path.abspath(__file__)) + "/grammars/" + kind + "/" + name + ".far")
if self.far_exist():
self._fst = Far(self.far_path, mode="r", arc_type="standard", far_type="default").get_fst()

def far_exist(self) -> bool:
return self.far_path.exists()

@property
def fst(self) -> "pynini.FstLike":
return self._fst

@fst.setter
def fst(self, fst):
self._fst = fst

def add_tokens(self, fst) -> "pynini.FstLike":
return pynutil.insert(f"{self.name} {{ ") + fst + pynutil.insert(" }")

def delete_tokens(self, fst) -> "pynini.FstLike":
res = (
pynutil.delete(f"{self.name}")
+ delete_space
+ pynutil.delete("{")
+ delete_space
+ fst
+ delete_space
+ pynutil.delete("}")
)
return res @ pynini.cdrewrite(pynini.cross("\u00a0", " "), "", "", NEMO_SIGMA)


# ---- PT-specific (Brazilian: 1.000.000 or 1 000 000) ----
cardinal_separator = pynini.string_map([".", " "])


def filter_cardinal_punctuation(fst: "pynini.FstLike") -> "pynini.FstLike":
"""
Parse digit groups separated by cardinal_separator (e.g. 1.000.000) then apply fst.

Args:
fst: FST that maps digit string to verbalized cardinal.

Returns:
Composed FST that accepts digit strings with optional thousand separators.
"""
exactly_three = NEMO_DIGIT**3
up_to_three = pynini.closure(NEMO_DIGIT, 1, 3)
cardinal_string = pynini.closure(NEMO_DIGIT, 1)
cardinal_string |= (
up_to_three
+ pynutil.delete(cardinal_separator)
+ pynini.closure(exactly_three + pynutil.delete(cardinal_separator))
+ exactly_three
)
return cardinal_string @ fst


def shift_cardinal_gender_pt(fst: "pynini.FstLike") -> "pynini.FstLike":
"""
Apply Portuguese masculine-to-feminine conversion for cardinal strings, e.g.
"um" -> "uma", "dois" -> "duas", "duzentos" -> "duzentas".

Args:
fst: FST producing masculine cardinal verbalization.

Returns:
FST that produces feminine form when composed with the same input.
"""
fem_ones = pynini.cdrewrite(
pynini.cross("um", "uma"),
"",
pynini.union(NEMO_SPACE, pynini.accep("[EOS]"), pynini.accep('"')),
NEMO_SIGMA,
)
fem_twos = pynini.cdrewrite(
pynini.cross("dois", "duas"),
"",
pynini.union(NEMO_SPACE, pynini.accep("[EOS]"), pynini.accep('"')),
NEMO_SIGMA,
)
fem_hundreds = pynini.cdrewrite(
pynini.cross("entos", "entas"),
pynini.union("duz", "trez", "quatroc", "quinh", "seisc", "setec", "oitoc", "novec"),
pynini.union(NEMO_SPACE, pynini.accep("[EOS]"), pynini.accep('"')),
NEMO_SIGMA,
)
return fst @ fem_ones @ fem_twos @ fem_hundreds
13 changes: 13 additions & 0 deletions nemo_text_processing/text_normalization/pt/taggers/__init__.py
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.
Loading