Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
36d900d
Decouple German ITN Cardinal in de from TN
adelina23dunina Sep 4, 2026
9ced732
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Sep 4, 2026
ea1456a
Split Cardinal denormalized.tsv into zero, digits, and irregular teens
adelina23dunina Sep 4, 2026
dceebc9
Load Cardinal 0-12 from zero, digits, and irregular teens
adelina23dunina Sep 4, 2026
17c642c
Replace Cardinal flips.tsv with a digit-tie flip function
adelina23dunina Sep 4, 2026
e7119b0
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Sep 4, 2026
d51c2bb
Remove Cardinal single_digit_years.tsv until Date
adelina23dunina Sep 4, 2026
4d52912
Derive Cardinal ties from tens plus inserted 0
adelina23dunina Sep 4, 2026
1e72f3f
Drop Measure noun-forced denorm from Cardinal PR
adelina23dunina Sep 4, 2026
90717ba
Restore original CardinalFst tagger docstring and add examples
adelina23dunina Sep 4, 2026
4220d82
Load Cardinal German magnitude words from TSV files
adelina23dunina Sep 4, 2026
27d3bdb
Load Cardinal magnitude words from one lexicon.tsv
adelina23dunina Sep 4, 2026
89063b2
Restore 2021 copyright on existing de Cardinal files
adelina23dunina Sep 4, 2026
06b4498
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Sep 4, 2026
138a544
Use NEMO_NOT_QUOTE in German ITN Cardinal verbalizer
adelina23dunina Sep 8, 2026
8904842
Use negative: true in Cardinal tagger; verbalizer emits minus
adelina23dunina Sep 8, 2026
4d26e10
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Sep 8, 2026
ff52a2e
Move noun-forced Cardinal cases to the Measure PR
adelina23dunina Sep 8, 2026
6cdf818
Load Cardinal hundreds outputs from TSV files
adelina23dunina Sep 8, 2026
1b84ec1
Load hundreds and magnitude outputs from combined TSV files
adelina23dunina Sep 8, 2026
c8205fd
Load Cardinal grouping dots from format.tsv
adelina23dunina Sep 8, 2026
d47d6e8
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Sep 8, 2026
48d3233
Load Cardinal hundreds and magnitude values from TSV files
adelina23dunina Sep 8, 2026
4809a2f
Remove format.tsv and n000dot from Cardinal
adelina23dunina Sep 8, 2026
c5474a7
Load Cardinal grouping dots from format.tsv
adelina23dunina Sep 8, 2026
e2891d7
Replace lexicon with minus.tsv, und.tsv, and magnitude spoken forms
adelina23dunina Sep 8, 2026
f68e89e
Align DE ITN cardinal with DE TN cardinal structure
adelina23dunina Sep 9, 2026
d472d42
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Sep 9, 2026
bd67e20
Align DE ITN cardinal with DE TN cardinal structure
adelina23dunina Sep 9, 2026
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 @@ -11,7 +11,7 @@ pipeline {
}
environment {
AR_TN_CACHE='/home/jenkins/TestData/text_norm/ci/grammars/06-11-26-0'
DE_TN_CACHE='/home/jenkins/TestData/text_norm/ci/grammars/10-23-24-0'
DE_TN_CACHE='/home/jenkins/TestData/text_norm/ci/grammars/09-04-26-0'
EN_TN_CACHE='/home/jenkins/TestData/text_norm/ci/grammars/06-11-26-1'
ES_TN_CACHE='/home/jenkins/TestData/text_norm/ci/grammars/09-25-24-0'
ES_EN_TN_CACHE='/home/jenkins/TestData/text_norm/ci/grammars/08-30-24-0'
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# Copyright (c) 2021, 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,10 @@
zwei 2
zwo 2
zwö 2
drei 3
vier 4
fünf 5
sechs 6
sieben 7
acht 8
neun 9
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
minus -
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
eine 1
ein 1
eins 1
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
tausend 1.000
million 1.000.000
millionen 1.000.000
milliarde 1.000.000.000
milliarden 1.000.000.000
billion 1.000.000.000.000
billionen 1.000.000.000.000
billiarde 1.000.000.000.000.000
billiarden 1.000.000.000.000.000
trillion 1.000.000.000.000.000.000
trillionen 1.000.000.000.000.000.000
trilliarde 1.000.000.000.000.000.000.000
trilliarden 1.000.000.000.000.000.000.000
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
zehn 10
elf 11
zwölf 12
dreizehn 13
vierzehn 14
fünfzehn 15
sechzehn 16
siebzehn 17
achtzehn 18
neunzehn 19
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
zwanzig 2
dreißig 3
vierzig 4
fünfzig 5
sechzig 6
siebzig 7
achtzig 8
neunzig 9
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
und
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
null 0
319 changes: 319 additions & 0 deletions nemo_text_processing/inverse_text_normalization/de/graph_utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,319 @@
# Copyright (c) 2024, NVIDIA CORPORATION. All rights reserved.
# Copyright 2015 and onwards Google, Inc.
#
# 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 os
import string
from pathlib import Path
from typing import Dict

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

from nemo_text_processing.text_normalization.en.utils import get_abs_path, load_labels
from nemo_text_processing.utils.logging import logger

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_ALNUM = pynini.union(NEMO_DIGIT, NEMO_ALPHA).optimize()
NEMO_HEX = pynini.union(*string.hexdigits).optimize()
NEMO_NON_BREAKING_SPACE = "\u00a0"
NEMO_SPACE = " "
NEMO_WHITE_SPACE = pynini.union(" ", "\t", "\n", "\r", "\u00a0").optimize()
NEMO_NOT_SPACE = pynini.difference(NEMO_CHAR, NEMO_WHITE_SPACE).optimize()
NEMO_NOT_QUOTE = pynini.difference(NEMO_CHAR, r'"').optimize()

NEMO_PUNCT = pynini.union(*map(pynini.escape, string.punctuation)).optimize()
NEMO_GRAPH = pynini.union(NEMO_ALNUM, NEMO_PUNCT).optimize()

NEMO_SIGMA = pynini.closure(NEMO_CHAR)
NEMO_LOWER_NOT_A = pynini.union(
"b",
"c",
"d",
"e",
"f",
"g",
"h",
"i",
"j",
"k",
"l",
"m",
"n",
"o",
"p",
"q",
"r",
"s",
"t",
"u",
"v",
"w",
"x",
"y",
"z",
).optimize()

delete_space = pynutil.delete(pynini.closure(NEMO_WHITE_SPACE))
delete_zero_or_one_space = pynutil.delete(pynini.closure(NEMO_WHITE_SPACE, 0, 1))
insert_space = pynutil.insert(" ")
delete_extra_space = pynini.cross(pynini.closure(NEMO_WHITE_SPACE, 1), " ")
delete_preserve_order = pynini.closure(
pynutil.delete(" preserve_order: true")
| (pynutil.delete(' field_order: "') + NEMO_NOT_QUOTE + pynutil.delete('"'))
)

suppletive = pynini.string_file(get_abs_path("data/suppletive.tsv"))
# _v = pynini.union("a", "e", "i", "o", "u")
_c = pynini.union(
"b",
"c",
"d",
"f",
"g",
"h",
"j",
"k",
"l",
"m",
"n",
"p",
"q",
"r",
"s",
"t",
"v",
"w",
"x",
"y",
"z",
)
_ies = NEMO_SIGMA + _c + pynini.cross("y", "ies")
_es = NEMO_SIGMA + pynini.union("s", "sh", "ch", "x", "z") + pynutil.insert("es")
_s = NEMO_SIGMA + pynutil.insert("s")

graph_plural = plurals._priority_union(
suppletive,
plurals._priority_union(_ies, plurals._priority_union(_es, _s, NEMO_SIGMA), NEMO_SIGMA),
NEMO_SIGMA,
).optimize()

SINGULAR_TO_PLURAL = graph_plural
PLURAL_TO_SINGULAR = pynini.invert(graph_plural)
TO_LOWER = pynini.union(*[pynini.cross(x, y) for x, y in zip(string.ascii_uppercase, string.ascii_lowercase)])
TO_UPPER = pynini.invert(TO_LOWER)
MIN_NEG_WEIGHT = -0.0001
MIN_POS_WEIGHT = 0.0001
INPUT_CASED = "cased"
INPUT_LOWER_CASED = "lower_cased"
MINUS = pynini.union("minus", "Minus").optimize()


def capitalized_input_graph(
graph: "pynini.FstLike",
original_graph_weight: float = None,
capitalized_graph_weight: float = None,
) -> "pynini.FstLike":
"""
Allow graph input to be capitalized, e.g. for ITN)

Args:
graph: FstGraph
original_graph_weight: weight to add to the original `graph`
capitalized_graph_weight: weight to add to the capitalized graph
"""
capitalized_graph = pynini.compose(TO_LOWER + NEMO_SIGMA, graph).optimize()

if original_graph_weight is not None:
graph = pynutil.add_weight(graph, weight=original_graph_weight)

if capitalized_graph_weight is not None:
capitalized_graph = pynutil.add_weight(capitalized_graph, weight=capitalized_graph_weight)

graph |= capitalized_graph
return graph


def generator_main(file_name: str, graphs: Dict[str, "pynini.FstLike"]):
"""
Exports graph as OpenFst finite state archive (FAR) file with given file name and rule name.

Args:
file_name: exported file name
graphs: Mapping of a rule name and Pynini WFST graph to be exported
"""
exporter = export.Exporter(file_name)
for rule, graph in graphs.items():
exporter[rule] = graph.optimize()
exporter.close()
logger.info(f"Created {file_name}")


def get_plurals(fst):
"""
Given singular returns plurals

Args:
fst: Fst

Returns plurals to given singular forms
"""
return SINGULAR_TO_PLURAL @ fst


def get_singulars(fst):
"""
Given plural returns singulars

Args:
fst: Fst

Returns singulars to given plural forms
"""
return PLURAL_TO_SINGULAR @ fst


def convert_space(fst) -> "pynini.FstLike":
"""
Converts space to nonbreaking space.
Used only in tagger grammars for transducing token values within quotes, e.g. name: "hello kitty"
This is making transducer significantly slower, so only use when there could be potential spaces within quotes, otherwise leave it.

Args:
fst: input fst

Returns output fst where breaking spaces are converted to non breaking spaces
"""
return fst @ pynini.cdrewrite(pynini.cross(NEMO_SPACE, NEMO_NON_BREAKING_SPACE), "", "", NEMO_SIGMA)


def string_map_cased(input_file: str, input_case: str = INPUT_LOWER_CASED):
labels = load_labels(input_file)

if input_case == INPUT_CASED:
additional_labels = []
for written, spoken, *weight in labels:
written_capitalized = written[0].upper() + written[1:]
additional_labels.extend(
[
[
written_capitalized,
spoken.capitalize(),
], # first letter capitalized
[
written_capitalized,
spoken.upper().replace(" AND ", " and "),
], # # add pairs with the all letters capitalized
]
)

spoken_no_space = spoken.replace(" ", "")
# add abbreviations without spaces (both lower and upper case), i.e. "BMW" not "B M W"
if len(spoken) == (2 * len(spoken_no_space) - 1):
logger.debug(f"This is weight {weight}")
if len(weight) == 0:
additional_labels.extend(
[
[written, spoken_no_space],
[written_capitalized, spoken_no_space.upper()],
]
)
else:
additional_labels.extend(
[
[written, spoken_no_space, weight[0]],
[written_capitalized, spoken_no_space.upper(), weight[0]],
]
)
labels += additional_labels

whitelist = pynini.string_map(labels).invert().optimize()
return whitelist


class GraphFst:
"""
Base class for all grammar fsts.

Args:
name: name of grammar class
kind: either 'classify' or 'verbalize'
deterministic: if True will provide a single transduction option,
for False multiple transduction are generated (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(__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:
"""
Returns true if FAR can be loaded
"""
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":
"""
Wraps class name around to given fst

Args:
fst: input fst

Returns:
Fst: fst
"""
return pynutil.insert(f"{self.name} {{ ") + fst + pynutil.insert(" }")

def delete_tokens(self, fst) -> "pynini.FstLike":
"""
Deletes class name wrap around output of given fst

Args:
fst: input fst

Returns:
Fst: fst
"""
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)
Loading