diff --git a/nemo_text_processing/text_normalization/normalize.py b/nemo_text_processing/text_normalization/normalize.py index d8ebf2f4d..34834f081 100644 --- a/nemo_text_processing/text_normalization/normalize.py +++ b/nemo_text_processing/text_normalization/normalize.py @@ -166,6 +166,7 @@ def __init__( if post_process: self.post_processor = PostProcessingFst(cache_dir=cache_dir, overwrite_cache=overwrite_cache) + elif lang == 'it': from nemo_text_processing.text_normalization.it.taggers.tokenize_and_classify import ClassifyFst from nemo_text_processing.text_normalization.it.verbalizers.verbalize_final import VerbalizeFinalFst @@ -191,6 +192,13 @@ def __init__( 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 + elif lang == 'ta': + from nemo_text_processing.text_normalization.ta.taggers.tokenize_and_classify import ClassifyFst + from nemo_text_processing.text_normalization.ta.verbalizers.post_processing import PostProcessingFst + from nemo_text_processing.text_normalization.ta.verbalizers.verbalize_final import VerbalizeFinalFst + + if post_process: + self.post_processor = PostProcessingFst(cache_dir=cache_dir, overwrite_cache=overwrite_cache) else: raise NotImplementedError(f"Language {lang} has not been supported yet.") @@ -391,7 +399,11 @@ def normalize( return text output = SPACE_DUP.sub(' ', output[1:]) - if self.lang in ["en", "hi", "vi"] and hasattr(self, 'post_processor') and self.post_processor is not None: + if ( + self.lang in ["en", "hi", "ta", "vi"] + and hasattr(self, 'post_processor') + and self.post_processor is not None + ): output = self.post_process(output) if punct_post_process: @@ -737,7 +749,7 @@ def parse_args(): parser.add_argument( "--language", help="language", - choices=["en", "de", "es", "fr", "hu", "sv", "zh", "ar", "it", "hy", "ja", "hi", "ko", "vi", "pt"], + choices=["en", "de", "es", "fr", "hu", "sv", "zh", "ar", "it", "hy", "ja", "hi", "ta", "ko", "vi", "pt"], default="en", type=str, ) diff --git a/nemo_text_processing/text_normalization/run_evaluate.py b/nemo_text_processing/text_normalization/run_evaluate.py index 3a1964bbd..fd0fac207 100644 --- a/nemo_text_processing/text_normalization/run_evaluate.py +++ b/nemo_text_processing/text_normalization/run_evaluate.py @@ -35,7 +35,7 @@ def parse_args(): parser.add_argument( "--lang", help="language", - choices=['ar', 'de', 'en', 'es', 'fr', 'hu', 'it', 'ru', 'sv', 'zh', 'hy', 'hi', 'ko', 'vi', 'pt'], + choices=['ar', 'de', 'en', 'es', 'fr', 'hu', 'it', 'ru', 'sv', 'zh', 'hy', 'hi', 'ta', 'ko', 'vi', 'pt'], default="en", type=str, ) @@ -67,6 +67,10 @@ def parse_args(): to_lower = args.input_case == "lower_cased" training_data = load_files([file_path], to_lower=to_lower) + # print("Loading training data: " + file_path) + # to_lower = True if args.input_case == "lower_cased" else False + # training_data = load_files([file_path], to_lower=to_lower) + if args.filter: training_data = filter_loaded_data(training_data) diff --git a/nemo_text_processing/text_normalization/ta/__init__.py b/nemo_text_processing/text_normalization/ta/__init__.py new file mode 100644 index 000000000..4fc25d0d3 --- /dev/null +++ b/nemo_text_processing/text_normalization/ta/__init__.py @@ -0,0 +1,13 @@ +# Copyright (c) 2026, NVIDIA CORPORATION. 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. diff --git a/nemo_text_processing/text_normalization/ta/data/__init__.py b/nemo_text_processing/text_normalization/ta/data/__init__.py new file mode 100644 index 000000000..4fc25d0d3 --- /dev/null +++ b/nemo_text_processing/text_normalization/ta/data/__init__.py @@ -0,0 +1,13 @@ +# Copyright (c) 2026, NVIDIA CORPORATION. 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. diff --git a/nemo_text_processing/text_normalization/ta/data/numbers/digit.tsv b/nemo_text_processing/text_normalization/ta/data/numbers/digit.tsv new file mode 100644 index 000000000..92111ede3 --- /dev/null +++ b/nemo_text_processing/text_normalization/ta/data/numbers/digit.tsv @@ -0,0 +1,18 @@ +௧ ஒன்று +௨ இரண்டு +௩ மூன்று +௪ நான்கு +௫ ஐந்து +௬ ஆறு +௭ ஏழு +௮ எட்டு +௯ ஒன்பது +1 ஒன்று +2 இரண்டு +3 மூன்று +4 நான்கு +5 ஐந்து +6 ஆறு +7 ஏழு +8 எட்டு +9 ஒன்பது \ No newline at end of file diff --git a/nemo_text_processing/text_normalization/ta/data/numbers/hundred_ta.tsv b/nemo_text_processing/text_normalization/ta/data/numbers/hundred_ta.tsv new file mode 100644 index 000000000..29621c1a9 --- /dev/null +++ b/nemo_text_processing/text_normalization/ta/data/numbers/hundred_ta.tsv @@ -0,0 +1,18 @@ +100 நூறு +200 இருநூற்று +300 முந்நூற்று +400 நானூற்று +500 ஐந்நூற்று +600 அறுநூற்று +700 எழுநூற்று +800 எட்டுநூற்று +900 ஒன்பதுநூற்று +௧௦௦ நூறு +௨௦௦ இருநூற்று +௩௦௦ முந்நூற்று +௪௦௦ நானூற்று +௫௦௦ ஐந்நூற்று +௬௦௦ அறுநூற்று +௭௦௦ எழுநூற்று +௮௦௦ எட்டுநூற்று +௯௦௦ ஒன்பதுநூற்று \ No newline at end of file diff --git a/nemo_text_processing/text_normalization/ta/data/numbers/teens_and_ties.tsv b/nemo_text_processing/text_normalization/ta/data/numbers/teens_and_ties.tsv new file mode 100644 index 000000000..058253fa1 --- /dev/null +++ b/nemo_text_processing/text_normalization/ta/data/numbers/teens_and_ties.tsv @@ -0,0 +1,90 @@ +௧௦ பத்து +௧௧ பதினொன்று +௧௨ பன்னிரண்டு +௧௩ பதிமூன்று +௧௪ பதினான்கு +௧௫ பதினைந்து +௧௬ பதினாறு +௧௭ பதினேழு +௧௮ பதினெட்டு +௧௯ பத்தொன்பது +௨௦ இருபது +௨௧ இருபத்தொன்று +௨௨ இருபத்திரண்டு +௨௩ இருபத்திமூன்று +௨௪ இருபத்திநான்கு +௨௫ இருபத்திஐந்து +௨௬ இருபத்திஆறு +௨௭ இருபத்திஏழு +௨௮ இருபத்திஎட்டு +௨௯ இருபத்திஒன்பது +௩௦ முப்பது +௩௧ முப்பத்தொன்று +௩௨ முப்பத்திரண்டு +௩௩ முப்பத்திமூன்று +௩௪ முப்பத்திநான்கு +௩௫ முப்பத்திஐந்து +௩௬ முப்பத்திஆறு +௩௭ முப்பத்திஏழு +௩௮ முப்பத்திஎட்டு +௩௯ முப்பத்திஒன்பது +௪௦ நாற்பது +௪௧ நாற்பத்தொன்று +௪௨ நாற்பத்திரண்டு +௪௩ நாற்பத்திமூன்று +௪௪ நாற்பத்திநான்கு +௪௫ நாற்பத்திஐந்து +௪௬ நாற்பத்திஆறு +௪௭ நாற்பத்திஏழு +௪௮ நாற்பத்திஎட்டு +௪௯ நாற்பத்திஒன்பது +௫௦ ஐம்பது +௫௧ ஐம்பத்தொன்று +௫௨ ஐம்பத்திரண்டு +௫௩ ஐம்பத்திமூன்று +௫௪ ஐம்பத்திநான்கு +௫௫ ஐம்பத்திஐந்து +௫௬ ஐம்பத்திஆறு +௫௭ ஐம்பத்திஏழு +௫௮ ஐம்பத்திஎட்டு +௫௯ ஐம்பத்திஒன்பது +௬௦ அறுபது +௬௧ அறுபத்தொன்று +௬௨ அறுபத்திரண்டு +௬௩ அறுபத்திமூன்று +௬௪ அறுபத்திநான்கு +௬௫ அறுபத்திஐந்து +௬௬ அறுபத்திஆறு +௬௭ அறுபத்திஏழு +௬௮ அறுபத்திஎட்டு +௬௯ அறுபத்திஒன்பது +௭௦ எழுபது +௭௧ எழுபத்தொன்று +௭௨ எழுபத்திரண்டு +௭௩ எழுபத்திமூன்று +௭௪ எழுபத்திநான்கு +௭௫ எழுபத்திஐந்து +௭௬ எழுபத்திஆறு +௭௭ எழுபத்திஏழு +௭௮ எழுபத்திஎட்டு +௭௯ எழுபத்திஒன்பது +௮௦ எண்பது +௮௧ எண்பத்தொன்று +௮௨ எண்பத்திரண்டு +௮௩ எண்பத்திமூன்று +௮௪ எண்பத்திநான்கு +௮௫ எண்பத்திஐந்து +௮௬ எண்பத்திஆறு +௮௭ எண்பத்திஏழு +௮௮ எண்பத்திஎட்டு +௮௯ எண்பத்திஒன்பது +௯௦ தொண்ணூறு +௯௧ தொண்ணூற்றொன்று +௯௨ தொண்ணூற்றிரண்டு +௯௩ தொண்ணூற்றிமூன்று +௯௪ தொண்ணூற்றிநான்கு +௯௫ தொண்ணூற்றிஐந்து +௯௬ தொண்ணூற்றிஆறு +௯௭ தொண்ணூற்றிஏழு +௯௮ தொண்ணூற்றிஎட்டு +௯௯ தொண்ணூற்றிஒன்பது \ No newline at end of file diff --git a/nemo_text_processing/text_normalization/ta/data/numbers/teens_and_ties_en.tsv b/nemo_text_processing/text_normalization/ta/data/numbers/teens_and_ties_en.tsv new file mode 100644 index 000000000..b796a9028 --- /dev/null +++ b/nemo_text_processing/text_normalization/ta/data/numbers/teens_and_ties_en.tsv @@ -0,0 +1,90 @@ +10 பத்து +11 பதினொன்று +12 பன்னிரண்டு +13 பதிமூன்று +14 பதினான்கு +15 பதினைந்து +16 பதினாறு +17 பதினேழு +18 பதினெட்டு +19 பத்தொன்பது +20 இருபது +21 இருபத்தொன்று +22 இருபத்திரண்டு +23 இருபத்திமூன்று +24 இருபத்திநான்கு +25 இருபத்திஐந்து +26 இருபத்திஆறு +27 இருபத்திஏழு +28 இருபத்திஎட்டு +29 இருபத்திஒன்பது +30 முப்பது +31 முப்பத்தொன்று +32 முப்பத்திரண்டு +33 முப்பத்திமூன்று +34 முப்பத்திநான்கு +35 முப்பத்திஐந்து +36 முப்பத்திஆறு +37 முப்பத்திஏழு +38 முப்பத்திஎட்டு +39 முப்பத்திஒன்பது +40 நாற்பது +41 நாற்பத்தொன்று +42 நாற்பத்திரண்டு +43 நாற்பத்திமூன்று +44 நாற்பத்திநான்கு +45 நாற்பத்திஐந்து +46 நாற்பத்திஆறு +47 நாற்பத்திஏழு +48 நாற்பத்திஎட்டு +49 நாற்பத்திஒன்பது +50 ஐம்பது +51 ஐம்பத்தொன்று +52 ஐம்பத்திரண்டு +53 ஐம்பத்திமூன்று +54 ஐம்பத்திநான்கு +55 ஐம்பத்திஐந்து +56 ஐம்பத்திஆறு +57 ஐம்பத்திஏழு +58 ஐம்பத்திஎட்டு +59 ஐம்பத்திஒன்பது +60 அறுபது +61 அறுபத்தொன்று +62 அறுபத்திரண்டு +63 அறுபத்திமூன்று +64 அறுபத்திநான்கு +65 அறுபத்திஐந்து +66 அறுபத்திஆறு +67 அறுபத்திஏழு +68 அறுபத்திஎட்டு +69 அறுபத்திஒன்பது +70 எழுபது +71 எழுபத்தொன்று +72 எழுபத்திரண்டு +73 எழுபத்திமூன்று +74 எழுபத்திநான்கு +75 எழுபத்திஐந்து +76 எழுபத்திஆறு +77 எழுபத்திஏழு +78 எழுபத்திஎட்டு +79 எழுபத்திஒன்பது +80 எண்பது +81 எண்பத்தொன்று +82 எண்பத்திரண்டு +83 எண்பத்திமூன்று +84 எண்பத்திநான்கு +85 எண்பத்திஐந்து +86 எண்பத்திஆறு +87 எண்பத்திஏழு +88 எண்பத்திஎட்டு +89 எண்பத்திஒன்பது +90 தொண்ணூறு +91 தொண்ணூற்றொன்று +92 தொண்ணூற்றிரண்டு +93 தொண்ணூற்றிமூன்று +94 தொண்ணூற்றிநான்கு +95 தொண்ணூற்றிஐந்து +96 தொண்ணூற்றிஆறு +97 தொண்ணூற்றிஏழு +98 தொண்ணூற்றிஎட்டு +99 தொண்ணூற்றிஒன்பது \ No newline at end of file diff --git a/nemo_text_processing/text_normalization/ta/data/numbers/thousands.tsv b/nemo_text_processing/text_normalization/ta/data/numbers/thousands.tsv new file mode 100644 index 000000000..5c6838935 --- /dev/null +++ b/nemo_text_processing/text_normalization/ta/data/numbers/thousands.tsv @@ -0,0 +1,3 @@ +௧௦௦௦ ஆயிரம் +௧௦௦௦௦௦ லட்சம் +௧௦௦௦௦௦௦௦ கோடி diff --git a/nemo_text_processing/text_normalization/ta/data/numbers/zero.tsv b/nemo_text_processing/text_normalization/ta/data/numbers/zero.tsv new file mode 100644 index 000000000..c5011b29f --- /dev/null +++ b/nemo_text_processing/text_normalization/ta/data/numbers/zero.tsv @@ -0,0 +1,2 @@ +௦ சுழியம் +0 சுழியம் \ No newline at end of file diff --git a/nemo_text_processing/text_normalization/ta/graph_utils.py b/nemo_text_processing/text_normalization/ta/graph_utils.py new file mode 100644 index 000000000..ee097e256 --- /dev/null +++ b/nemo_text_processing/text_normalization/ta/graph_utils.py @@ -0,0 +1,224 @@ +# Copyright (c) 2026, NVIDIA CORPORATION. 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 logging +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 + +NEMO_CHAR = utf8.VALID_UTF8_CHAR +NEMO_DIGIT = byte.DIGIT + +NEMO_TA_DIGIT = pynini.union("௦", "௧", "௨", "௩", "௪", "௫", "௬", "௭", "௮", "௯").optimize() +NEMO_TA_NON_ZERO = pynini.union("௧", "௨", "௩", "௪", "௫", "௬", "௭", "௮", "௯").optimize() +NEMO_TA_ZERO = "௦" +# Combined TAMIL and Arabic digits for graphs that need to accept both +NEMO_ALL_DIGIT = pynini.union(NEMO_TA_DIGIT, NEMO_DIGIT).optimize() +NEMO_ALL_ZERO = pynini.union("௦", "0").optimize() +NEMO_ALL_NON_ZERO = pynini.union(NEMO_TA_NON_ZERO, "1", "2", "3", "4", "5", "6", "7", "8", "9").optimize() + +TA_ONRARAI = "ஒன்றரை" # 1.5 +TA_IRANDARAI = "இரண்டரை" # 2.5 +TA_ONRUKAAL = "ஒன்றுகால்" # quarter more (1.25) +TA_ARAI = "அரை" # half more (X.5) +TA_MUKKAAL = "முக்கால்" # quarter less (0.75) + +# Tamil decimal representations +TA_POINT_FIVE = ".௫" # .5 +TA_ONE_POINT_FIVE = "௧.௫" # 1.5 +TA_TWO_POINT_FIVE = "௨.௫" # 2.5 +TA_DECIMAL_25 = ".௨௫" # .25 +TA_DECIMAL_75 = ".௭௫" # .75 + +# Arabic/English decimal representations +EN_POINT_FIVE = ".5" +EN_ONE_POINT_FIVE = "1.5" +EN_TWO_POINT_FIVE = "2.5" +EN_DECIMAL_25 = ".25" +EN_DECIMAL_75 = ".75" + +# Combined Tamil and English decimal patterns +POINT_FIVE = pynini.union(TA_POINT_FIVE, EN_POINT_FIVE).optimize() +ONE_POINT_FIVE = pynini.union(TA_ONE_POINT_FIVE, EN_ONE_POINT_FIVE).optimize() +TWO_POINT_FIVE = pynini.union(TA_TWO_POINT_FIVE, EN_TWO_POINT_FIVE).optimize() +DECIMAL_25 = pynini.union(TA_DECIMAL_25, EN_DECIMAL_25).optimize() +DECIMAL_75 = pynini.union(TA_DECIMAL_75, EN_DECIMAL_75).optimize() + +# Symbol constants +TA_BY = "வகுத்தல்" +LOWERCASE_X = "x" +UPPERCASE_X = "X" +ASTERISK = "*" +HYPHEN = "-" +SLASH = "/" +COMMA = "," +PERIOD = "." +TA_PERIOD = "।" + +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_HEX = pynini.union(*string.hexdigits).optimize() +NEMO_NON_BREAKING_SPACE = u"\u00a0" +NEMO_SPACE = " " +NEMO_WHITE_SPACE = pynini.union(" ", "\t", "\n", "\r", u"\u00a0").optimize() +NEMO_NOT_SPACE = pynini.difference(NEMO_CHAR, NEMO_WHITE_SPACE).optimize() +NEMO_NOT_QUOTE = pynini.difference(NEMO_CHAR, r'"').optimize() +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) +NEMO_SIGMA = pynini.closure(NEMO_CHAR) + + +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("\"")) +) + + +MIN_NEG_WEIGHT = -0.0001 +MIN_POS_WEIGHT = 0.0001 +INPUT_CASED = "cased" +INPUT_LOWER_CASED = "lower_cased" +MINUS = pynini.union(" எதிர்மறை ", " எதிர்மறை ").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() + logging.info(f'Created {file_name}') + + +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) + + +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(u"\u00a0", " "), "", "", NEMO_SIGMA) diff --git a/nemo_text_processing/text_normalization/ta/taggers/__init__.py b/nemo_text_processing/text_normalization/ta/taggers/__init__.py new file mode 100644 index 000000000..4fc25d0d3 --- /dev/null +++ b/nemo_text_processing/text_normalization/ta/taggers/__init__.py @@ -0,0 +1,13 @@ +# Copyright (c) 2026, NVIDIA CORPORATION. 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. diff --git a/nemo_text_processing/text_normalization/ta/taggers/cardinal.py b/nemo_text_processing/text_normalization/ta/taggers/cardinal.py new file mode 100644 index 000000000..78080c548 --- /dev/null +++ b/nemo_text_processing/text_normalization/ta/taggers/cardinal.py @@ -0,0 +1,424 @@ +# Copyright (c) 2026, NVIDIA CORPORATION. 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 pynini +from pynini.lib import pynutil +from nemo_text_processing.text_normalization.ta.graph_utils import ( + NEMO_ALL_DIGIT, + NEMO_ALL_ZERO, + GraphFst, + insert_space, +) +from nemo_text_processing.text_normalization.ta.utils import get_abs_path + + +class CardinalFst(GraphFst): + """ + Finite state transducer for classifying cardinals, e.g. + -23 -> cardinal { negative: "true" integer: "இருபத்து மூன்று" } + The highest unit used is கோடி. + """ + + def __init__(self, deterministic: bool = True, lm: bool = False): + super().__init__(name="cardinal", kind="classify", deterministic=deterministic) + digit = pynini.string_file(get_abs_path("data/numbers/digit.tsv")) + zero = pynini.string_file(get_abs_path("data/numbers/zero.tsv")) + hundred_ta = pynini.string_file(get_abs_path("data/numbers/hundred_ta.tsv")) + teens_ties_hi = pynini.string_file(get_abs_path("data/numbers/teens_and_ties.tsv")) + teens_ties_en = pynini.string_file(get_abs_path("data/numbers/teens_and_ties_en.tsv")) + teens_ties = pynini.union(teens_ties_hi, teens_ties_en) + teens_and_ties = pynutil.add_weight(teens_ties, -0.1) + self.digit = digit + self.zero = zero + self.hundred_ta = hundred_ta + self.teens_and_ties = teens_and_ties + + single_digit = digit | zero + single_digit_graph = digit | zero + self.single_digits_graph = single_digit_graph + pynini.closure(insert_space + single_digit_graph) + zero_delete = pynutil.add_weight(pynutil.delete(NEMO_ALL_ZERO | pynini.accep("௦")), -0.1) + + def create_graph_suffix(digit_graph, suffix, zeros_counts): + z = pynutil.add_weight(pynutil.delete(NEMO_ALL_ZERO | pynini.accep("௦")), -0.1) + if zeros_counts == 0: + return digit_graph + suffix + return digit_graph + (z**zeros_counts) + suffix + + def create_larger_number_graph(digit_graph, suffix, zeros_counts, sub_graph): + ins = pynutil.insert(" ") + z = pynutil.add_weight(pynutil.delete(NEMO_ALL_ZERO | pynini.accep("௦")), -0.1) + if zeros_counts == 0: + return digit_graph + suffix + ins + sub_graph + return digit_graph + suffix + (z**zeros_counts) + ins + sub_graph + + # HUNDREDS GRAPH (100-999) + + # exact 100 → நூறு + exact_hundred = pynutil.add_weight( + pynini.union( + pynini.cross("100", "நூறு"), + pynini.cross("௧௦௦", "நூறு"), + ).optimize(), + -1.0, + ) + + # hundred prefixes: 200-900 + fused_hundred_prefix = pynini.union( + pynini.cross("2", "இருநூற்று"), + pynini.cross("3", "முந்நூற்று"), + pynini.cross("4", "நானூற்று"), + pynini.cross("5", "ஐநூற்று"), + pynini.cross("6", "அறுநூற்று"), + pynini.cross("7", "எழுநூற்று"), + pynini.cross("8", "எட்டுநூற்று"), + pynini.cross("9", "ஒன்பதுநூற்று"), + pynini.cross("௨", "இருநூற்று"), + pynini.cross("௩", "முந்நூற்று"), + pynini.cross("௪", "நானூற்று"), + pynini.cross("௫", "ஐநூற்று"), + pynini.cross("௬", "அறுநூற்று"), + pynini.cross("௭", "எழுநூற்று"), + pynini.cross("௮", "எட்டுநூற்று"), + pynini.cross("௯", "ஒன்பதுநூற்று"), + ).optimize() + + graph_hundreds = hundred_ta + # 101-109 + graph_hundreds |= ( + (pynini.cross("1", "நூற்று") | pynini.cross("௧", "நூற்று")) + + zero_delete + + pynutil.insert(" ") + + single_digit + ) + # 110-199 + graph_hundreds |= (pynini.cross("1", "நூற்று") | pynini.cross("௧", "நூற்று")) + pynutil.insert(" ") + teens_ties + # 201-209 ... 901-909 + graph_hundreds |= fused_hundred_prefix + zero_delete + pynutil.insert(" ") + single_digit + # 210-999 + graph_hundreds |= fused_hundred_prefix + pynutil.insert(" ") + teens_ties + graph_hundreds = graph_hundreds.optimize() + self.graph_hundreds = graph_hundreds + + # THOUSANDS GRAPH (1000-9999) + # 1000-1999 → ஆயிரம்/ஆயிரத்து + one_k_exact = pynini.cross("1", "ஆயிரம்") | pynini.cross("௧", "ஆயிரம்") + one_k_tail = pynini.cross("1", "ஆயிரத்து") | pynini.cross("௧", "ஆயிரத்து") + # 2000-9000 + fused_thousands_exact = pynini.union( + pynini.cross("2", "இரண்டாயிரம்"), + pynini.cross("3", "மூவாயிரம்"), + pynini.cross("4", "நான்காயிரம்"), + pynini.cross("5", "ஐந்தாயிரம்"), + pynini.cross("6", "ஆறாயிரம்"), + pynini.cross("7", "ஏழாயிரம்"), + pynini.cross("8", "எட்டாயிரம்"), + pynini.cross("9", "ஒன்பதாயிரம்"), + pynini.cross("௨", "இரண்டாயிரம்"), + pynini.cross("௩", "மூவாயிரம்"), + pynini.cross("௪", "நான்காயிரம்"), + pynini.cross("௫", "ஐந்தாயிரம்"), + pynini.cross("௬", "ஆறாயிரம்"), + pynini.cross("௭", "ஏழாயிரம்"), + pynini.cross("௮", "எட்டாயிரம்"), + pynini.cross("௯", "ஒன்பதாயிரம்"), + ).optimize() + + fused_thousands_prefix = pynini.union( + pynini.cross("2", "இரண்டாயிரத்து"), + pynini.cross("3", "மூவாயிரத்து"), + pynini.cross("4", "நான்காயிரத்து"), + pynini.cross("5", "ஐந்தாயிரத்து"), + pynini.cross("6", "ஆறாயிரத்து"), + pynini.cross("7", "ஏழாயிரத்து"), + pynini.cross("8", "எட்டாயிரத்து"), + pynini.cross("9", "ஒன்பதாயிரத்து"), + pynini.cross("௨", "இரண்டாயிரத்து"), + pynini.cross("௩", "மூவாயிரத்து"), + pynini.cross("௪", "நான்காயிரத்து"), + pynini.cross("௫", "ஐந்தாயிரத்து"), + pynini.cross("௬", "ஆறாயிரத்து"), + pynini.cross("௭", "ஏழாயிரத்து"), + pynini.cross("௮", "எட்டாயிரத்து"), + pynini.cross("௯", "ஒன்பதாயிரத்து"), + ).optimize() + + graph_thousands = one_k_exact + (zero_delete**3) + graph_thousands |= one_k_tail + (zero_delete**2) + pynutil.insert(" ") + single_digit + graph_thousands |= one_k_tail + zero_delete + pynutil.insert(" ") + teens_ties + graph_thousands |= one_k_tail + pynutil.insert(" ") + graph_hundreds + graph_thousands |= fused_thousands_exact + (zero_delete**3) + graph_thousands |= fused_thousands_prefix + (zero_delete**2) + pynutil.insert(" ") + single_digit + graph_thousands |= fused_thousands_prefix + zero_delete + pynutil.insert(" ") + teens_ties + graph_thousands |= fused_thousands_prefix + pynutil.insert(" ") + graph_hundreds + graph_thousands = graph_thousands.optimize() + self.graph_thousands = graph_thousands + + # TEN-THOUSANDS GRAPH (10000-99999) + # 10000-19999 + fused_ten_k_exact = pynini.union( + pynini.cross("௧௩", "பதிமூன்றுஆயிரம்"), + pynini.cross("௧௨", "பன்னிரண்டுஆயிரம்"), + pynini.cross("௧௧", "பதினொன்றுஆயிரம்"), + pynini.cross("௪௫", "நாற்பத்தைந்தாயிரம்"), + pynini.cross("௫௫", "ஐம்பத்தைந்தாயிரம்"), + ).optimize() + + fused_ten_k_prefix = pynini.union( + pynini.cross("௧௩", "பதிமூன்றுஆயிரத்து"), + pynini.cross("௧௨", "பன்னிரண்டுஆயிரத்து"), + pynini.cross("௧௧", "பதினொன்றுஆயிரத்து"), + pynini.cross("௪௫", "நாற்பத்தைந்தாயிரத்து"), + pynini.cross("௫௫", "ஐம்பத்தைந்தாயிரத்து"), + ).optimize() + + suffix_ten_k_exact = pynutil.insert("ஆயிரம்") + suffix_ten_k_tail = pynutil.insert("ஆயிரத்து") + + graph_ten_thousands = fused_ten_k_exact + (zero_delete**3) + graph_ten_thousands |= fused_ten_k_prefix + (zero_delete**2) + pynutil.insert(" ") + single_digit + graph_ten_thousands |= fused_ten_k_prefix + zero_delete + pynutil.insert(" ") + teens_ties + graph_ten_thousands |= fused_ten_k_prefix + pynutil.insert(" ") + graph_hundreds + graph_ten_thousands |= create_graph_suffix(teens_and_ties, suffix_ten_k_exact, 3) + graph_ten_thousands |= create_larger_number_graph(teens_and_ties, suffix_ten_k_tail, 2, single_digit) + graph_ten_thousands |= create_larger_number_graph(teens_and_ties, suffix_ten_k_tail, 1, teens_ties) + graph_ten_thousands |= create_larger_number_graph(teens_and_ties, suffix_ten_k_tail, 0, graph_hundreds) + graph_ten_thousands = graph_ten_thousands.optimize() + self.graph_ten_thousands = graph_ten_thousands + + # LAKHS GRAPH (100000-9999999) + # 1 lakh → ஒரு லட்சம் + suffix_lakhs = pynutil.insert(" லட்சம்") + suffix_lakhs_tail = pynutil.insert(" லட்சத்து") + + digit_one_as_oru = pynini.union( + pynini.cross("1", "ஒரு"), + pynini.cross("௧", "ஒரு"), + pynini.cross("2", "இரண்டு"), + pynini.cross("3", "மூன்று"), + pynini.cross("4", "நான்கு"), + pynini.cross("5", "ஐந்து"), + pynini.cross("6", "ஆறு"), + pynini.cross("7", "ஏழு"), + pynini.cross("8", "எட்டு"), + pynini.cross("9", "ஒன்பது"), + pynini.cross("௨", "இரண்டு"), + pynini.cross("௩", "மூன்று"), + pynini.cross("௪", "நான்கு"), + pynini.cross("௫", "ஐந்து"), + pynini.cross("௬", "ஆறு"), + pynini.cross("௭", "ஏழு"), + pynini.cross("௮", "எட்டு"), + pynini.cross("௯", "ஒன்பது"), + ).optimize() + + graph_lakhs = create_graph_suffix(digit_one_as_oru, suffix_lakhs, 5) + graph_lakhs |= create_larger_number_graph(digit_one_as_oru, suffix_lakhs_tail, 4, single_digit) + graph_lakhs |= create_larger_number_graph(digit_one_as_oru, suffix_lakhs_tail, 3, teens_ties) + graph_lakhs |= create_larger_number_graph(digit_one_as_oru, suffix_lakhs_tail, 2, graph_hundreds) + graph_lakhs |= create_larger_number_graph(digit_one_as_oru, suffix_lakhs_tail, 1, graph_thousands) + graph_lakhs |= create_larger_number_graph(digit_one_as_oru, suffix_lakhs_tail, 0, graph_ten_thousands) + graph_lakhs = graph_lakhs.optimize() + self.graph_lakhs = graph_lakhs + + graph_ten_lakhs = create_graph_suffix(teens_and_ties, suffix_lakhs, 5) + graph_ten_lakhs |= create_larger_number_graph(teens_and_ties, suffix_lakhs_tail, 4, single_digit) + graph_ten_lakhs |= create_larger_number_graph(teens_and_ties, suffix_lakhs_tail, 3, teens_ties) + graph_ten_lakhs |= create_larger_number_graph(teens_and_ties, suffix_lakhs_tail, 2, graph_hundreds) + graph_ten_lakhs |= create_larger_number_graph(teens_and_ties, suffix_lakhs_tail, 1, graph_thousands) + graph_ten_lakhs |= create_larger_number_graph(teens_and_ties, suffix_lakhs_tail, 0, graph_ten_thousands) + graph_ten_lakhs = graph_ten_lakhs.optimize() + self.graph_ten_lakhs = graph_ten_lakhs + + # CRORES GRAPH — கோடி + # 1 crore → ஒரு கோடி / ஒரு கோடியே + suffix_crores = pynutil.insert(" கோடி") + suffix_crores_tail = pynutil.insert(" கோடியே") + + graph_crores = create_graph_suffix(digit_one_as_oru, suffix_crores, 7) + graph_crores |= create_larger_number_graph(digit_one_as_oru, suffix_crores_tail, 6, single_digit) + graph_crores |= create_larger_number_graph(digit_one_as_oru, suffix_crores_tail, 5, teens_ties) + graph_crores |= create_larger_number_graph(digit_one_as_oru, suffix_crores_tail, 4, graph_hundreds) + graph_crores |= create_larger_number_graph(digit_one_as_oru, suffix_crores_tail, 3, graph_thousands) + graph_crores |= create_larger_number_graph(digit_one_as_oru, suffix_crores_tail, 2, graph_ten_thousands) + graph_crores |= create_larger_number_graph(digit_one_as_oru, suffix_crores_tail, 1, graph_lakhs) + graph_crores |= create_larger_number_graph(digit_one_as_oru, suffix_crores_tail, 0, graph_ten_lakhs) + graph_crores = graph_crores.optimize() + + graph_ten_crores = create_graph_suffix(teens_and_ties, suffix_crores, 7) + graph_ten_crores |= create_larger_number_graph(teens_and_ties, suffix_crores_tail, 6, single_digit) + graph_ten_crores |= create_larger_number_graph(teens_and_ties, suffix_crores_tail, 5, teens_ties) + graph_ten_crores |= create_larger_number_graph(teens_and_ties, suffix_crores_tail, 4, graph_hundreds) + graph_ten_crores |= create_larger_number_graph(teens_and_ties, suffix_crores_tail, 3, graph_thousands) + graph_ten_crores |= create_larger_number_graph(teens_and_ties, suffix_crores_tail, 2, graph_ten_thousands) + graph_ten_crores |= create_larger_number_graph(teens_and_ties, suffix_crores_tail, 1, graph_lakhs) + graph_ten_crores |= create_larger_number_graph(teens_and_ties, suffix_crores_tail, 0, graph_ten_lakhs) + graph_ten_crores = graph_ten_crores.optimize() + + # hundreds of crores: 100-999 கோடி + graph_hundreds_of_crores = create_graph_suffix(exact_hundred, suffix_crores, 7) + graph_hundreds_of_crores |= create_graph_suffix(graph_hundreds, suffix_crores, 7) + graph_hundreds_of_crores |= create_larger_number_graph(exact_hundred, suffix_crores_tail, 6, single_digit) + graph_hundreds_of_crores |= create_larger_number_graph(exact_hundred, suffix_crores_tail, 5, teens_ties) + graph_hundreds_of_crores |= create_larger_number_graph(exact_hundred, suffix_crores_tail, 4, graph_hundreds) + graph_hundreds_of_crores |= create_larger_number_graph(exact_hundred, suffix_crores_tail, 3, graph_thousands) + graph_hundreds_of_crores |= create_larger_number_graph( + exact_hundred, suffix_crores_tail, 2, graph_ten_thousands + ) + graph_hundreds_of_crores |= create_larger_number_graph(exact_hundred, suffix_crores_tail, 1, graph_lakhs) + graph_hundreds_of_crores |= create_larger_number_graph(exact_hundred, suffix_crores_tail, 0, graph_ten_lakhs) + graph_hundreds_of_crores |= create_larger_number_graph(graph_hundreds, suffix_crores_tail, 6, single_digit) + graph_hundreds_of_crores |= create_larger_number_graph(graph_hundreds, suffix_crores_tail, 5, teens_ties) + graph_hundreds_of_crores |= create_larger_number_graph(graph_hundreds, suffix_crores_tail, 4, graph_hundreds) + graph_hundreds_of_crores |= create_larger_number_graph(graph_hundreds, suffix_crores_tail, 3, graph_thousands) + graph_hundreds_of_crores |= create_larger_number_graph( + graph_hundreds, suffix_crores_tail, 2, graph_ten_thousands + ) + graph_hundreds_of_crores |= create_larger_number_graph(graph_hundreds, suffix_crores_tail, 1, graph_lakhs) + graph_hundreds_of_crores |= create_larger_number_graph(graph_hundreds, suffix_crores_tail, 0, graph_ten_lakhs) + graph_hundreds_of_crores = graph_hundreds_of_crores.optimize() + + # thousands of crores: 1000-9999 கோடி + graph_thousands_of_crores = create_graph_suffix(graph_thousands, suffix_crores, 7) + graph_thousands_of_crores |= create_larger_number_graph(graph_thousands, suffix_crores_tail, 6, single_digit) + graph_thousands_of_crores |= create_larger_number_graph(graph_thousands, suffix_crores_tail, 5, teens_ties) + graph_thousands_of_crores |= create_larger_number_graph(graph_thousands, suffix_crores_tail, 4, graph_hundreds) + graph_thousands_of_crores |= create_larger_number_graph( + graph_thousands, suffix_crores_tail, 3, graph_thousands + ) + graph_thousands_of_crores |= create_larger_number_graph( + graph_thousands, suffix_crores_tail, 2, graph_ten_thousands + ) + graph_thousands_of_crores |= create_larger_number_graph(graph_thousands, suffix_crores_tail, 1, graph_lakhs) + graph_thousands_of_crores |= create_larger_number_graph( + graph_thousands, suffix_crores_tail, 0, graph_ten_lakhs + ) + graph_thousands_of_crores = graph_thousands_of_crores.optimize() + + # ten-thousands of crores + graph_ten_thousands_of_crores = create_graph_suffix(graph_ten_thousands, suffix_crores, 7) + graph_ten_thousands_of_crores |= create_larger_number_graph( + graph_ten_thousands, suffix_crores_tail, 6, single_digit + ) + graph_ten_thousands_of_crores |= create_larger_number_graph( + graph_ten_thousands, suffix_crores_tail, 5, teens_ties + ) + graph_ten_thousands_of_crores |= create_larger_number_graph( + graph_ten_thousands, suffix_crores_tail, 4, graph_hundreds + ) + graph_ten_thousands_of_crores |= create_larger_number_graph( + graph_ten_thousands, suffix_crores_tail, 3, graph_thousands + ) + graph_ten_thousands_of_crores |= create_larger_number_graph( + graph_ten_thousands, suffix_crores_tail, 2, graph_ten_thousands + ) + graph_ten_thousands_of_crores |= create_larger_number_graph( + graph_ten_thousands, suffix_crores_tail, 1, graph_lakhs + ) + graph_ten_thousands_of_crores |= create_larger_number_graph( + graph_ten_thousands, suffix_crores_tail, 0, graph_ten_lakhs + ) + graph_ten_thousands_of_crores = graph_ten_thousands_of_crores.optimize() + + # lakhs of crores + graph_lakhs_of_crores = create_graph_suffix(graph_lakhs, suffix_crores, 7) + graph_lakhs_of_crores |= create_larger_number_graph(graph_lakhs, suffix_crores_tail, 6, single_digit) + graph_lakhs_of_crores |= create_larger_number_graph(graph_lakhs, suffix_crores_tail, 5, teens_ties) + graph_lakhs_of_crores |= create_larger_number_graph(graph_lakhs, suffix_crores_tail, 4, graph_hundreds) + graph_lakhs_of_crores |= create_larger_number_graph(graph_lakhs, suffix_crores_tail, 3, graph_thousands) + graph_lakhs_of_crores |= create_larger_number_graph(graph_lakhs, suffix_crores_tail, 2, graph_ten_thousands) + graph_lakhs_of_crores |= create_larger_number_graph(graph_lakhs, suffix_crores_tail, 1, graph_lakhs) + graph_lakhs_of_crores |= create_larger_number_graph(graph_lakhs, suffix_crores_tail, 0, graph_ten_lakhs) + graph_lakhs_of_crores = graph_lakhs_of_crores.optimize() + + graph_ten_lakhs_of_crores = create_graph_suffix(graph_ten_lakhs, suffix_crores, 7) + graph_ten_lakhs_of_crores |= create_larger_number_graph(graph_ten_lakhs, suffix_crores_tail, 6, single_digit) + graph_ten_lakhs_of_crores |= create_larger_number_graph(graph_ten_lakhs, suffix_crores_tail, 5, teens_ties) + graph_ten_lakhs_of_crores |= create_larger_number_graph(graph_ten_lakhs, suffix_crores_tail, 4, graph_hundreds) + graph_ten_lakhs_of_crores |= create_larger_number_graph( + graph_ten_lakhs, suffix_crores_tail, 3, graph_thousands + ) + graph_ten_lakhs_of_crores |= create_larger_number_graph( + graph_ten_lakhs, suffix_crores_tail, 2, graph_ten_thousands + ) + graph_ten_lakhs_of_crores |= create_larger_number_graph(graph_ten_lakhs, suffix_crores_tail, 1, graph_lakhs) + graph_ten_lakhs_of_crores |= create_larger_number_graph( + graph_ten_lakhs, suffix_crores_tail, 0, graph_ten_lakhs + ) + graph_ten_lakhs_of_crores = graph_ten_lakhs_of_crores.optimize() + + # crores of crores + graph_crores_of_crores = create_graph_suffix(graph_crores, suffix_crores, 7) + graph_crores_of_crores |= create_larger_number_graph(graph_crores, suffix_crores_tail, 6, single_digit) + graph_crores_of_crores |= create_larger_number_graph(graph_crores, suffix_crores_tail, 5, teens_ties) + graph_crores_of_crores |= create_larger_number_graph(graph_crores, suffix_crores_tail, 4, graph_hundreds) + graph_crores_of_crores |= create_larger_number_graph(graph_crores, suffix_crores_tail, 3, graph_thousands) + graph_crores_of_crores |= create_larger_number_graph(graph_crores, suffix_crores_tail, 2, graph_ten_thousands) + graph_crores_of_crores |= create_larger_number_graph(graph_crores, suffix_crores_tail, 1, graph_lakhs) + graph_crores_of_crores |= create_larger_number_graph(graph_crores, suffix_crores_tail, 0, graph_ten_lakhs) + graph_crores_of_crores = graph_crores_of_crores.optimize() + + graph_ten_crores_of_crores = create_graph_suffix(graph_ten_crores, suffix_crores, 7) + graph_ten_crores_of_crores |= create_larger_number_graph(graph_ten_crores, suffix_crores_tail, 6, single_digit) + graph_ten_crores_of_crores |= create_larger_number_graph(graph_ten_crores, suffix_crores_tail, 5, teens_ties) + graph_ten_crores_of_crores |= create_larger_number_graph( + graph_ten_crores, suffix_crores_tail, 4, graph_hundreds + ) + graph_ten_crores_of_crores |= create_larger_number_graph( + graph_ten_crores, suffix_crores_tail, 3, graph_thousands + ) + graph_ten_crores_of_crores |= create_larger_number_graph( + graph_ten_crores, suffix_crores_tail, 2, graph_ten_thousands + ) + graph_ten_crores_of_crores |= create_larger_number_graph(graph_ten_crores, suffix_crores_tail, 1, graph_lakhs) + graph_ten_crores_of_crores |= create_larger_number_graph( + graph_ten_crores, suffix_crores_tail, 0, graph_ten_lakhs + ) + graph_ten_crores_of_crores = graph_ten_crores_of_crores.optimize() + + # LEADING ZERO and FINAL GRAPH + graph_leading_zero = zero + insert_space + single_digit + graph_leading_zero = pynutil.add_weight(graph_leading_zero, 0.5) + + graph_without_leading_zeros = ( + digit + | zero + | teens_and_ties + | exact_hundred + | graph_hundreds + | graph_thousands + | graph_ten_thousands + | graph_lakhs + | graph_ten_lakhs + | graph_crores + | graph_ten_crores + | graph_hundreds_of_crores + | graph_thousands_of_crores + | graph_ten_thousands_of_crores + | graph_lakhs_of_crores + | graph_ten_lakhs_of_crores + | graph_crores_of_crores + | graph_ten_crores_of_crores + ) + self.graph_without_leading_zeros = graph_without_leading_zeros.optimize() + + cardinal_with_leading_zeros = pynini.compose( + NEMO_ALL_ZERO + pynini.closure(NEMO_ALL_DIGIT), self.single_digits_graph + ) + cardinal_with_leading_zeros = pynutil.add_weight(cardinal_with_leading_zeros, 0.5) + + final_graph = graph_without_leading_zeros | cardinal_with_leading_zeros + optional_minus_graph = pynini.closure(pynutil.insert("negative: ") + pynini.cross("-", "\"true\" "), 0, 1) + self.final_graph = final_graph.optimize() + final_graph = optional_minus_graph + pynutil.insert("integer: \"") + self.final_graph + pynutil.insert("\"") + final_graph = self.add_tokens(final_graph) + self.fst = final_graph diff --git a/nemo_text_processing/text_normalization/ta/taggers/punctuation.py b/nemo_text_processing/text_normalization/ta/taggers/punctuation.py new file mode 100644 index 000000000..82aa82c93 --- /dev/null +++ b/nemo_text_processing/text_normalization/ta/taggers/punctuation.py @@ -0,0 +1,61 @@ +# Copyright (c) 2026, NVIDIA CORPORATION. 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 sys +from unicodedata import category + +import pynini +from pynini.examples import plurals +from pynini.lib import pynutil + +from nemo_text_processing.text_normalization.ta.graph_utils import NEMO_NOT_SPACE, NEMO_SIGMA, GraphFst + + +class PunctuationFst(GraphFst): + """ + Finite state transducer for classifying punctuation + e.g. a, -> tokens { name: "a" } tokens { name: "," } + + Args: + deterministic: if True will provide a single transduction option, + for False multiple transductions are generated (used for audio-based normalization) + """ + + def __init__(self, deterministic: bool = True): + super().__init__(name="punctuation", kind="classify", deterministic=deterministic) + s = "!#%&\'()*+,-./:;<=>?@^_`{|}~\"" + + punct_symbols_to_exclude = ["[", "]"] + punct_unicode = [ + chr(i) + for i in range(sys.maxunicode) + if category(chr(i)).startswith("P") and chr(i) not in punct_symbols_to_exclude + ] + + self.punct_marks = [p for p in punct_unicode + list(s)] + + punct = pynini.union(*self.punct_marks) + punct = pynini.closure(punct, 1) + + emphasis = ( + pynini.accep("<") + + pynini.union( + (pynini.closure(NEMO_NOT_SPACE - pynini.union("<", ">"), 1) + pynini.closure(pynini.accep("/"), 0, 1)), + (pynini.accep("/") + pynini.closure(NEMO_NOT_SPACE - pynini.union("<", ">"), 1)), + ) + + pynini.accep(">") + ) + punct = plurals._priority_union(emphasis, punct, NEMO_SIGMA) + + self.graph = punct + self.fst = (pynutil.insert("name: \"") + self.graph + pynutil.insert("\"")).optimize() diff --git a/nemo_text_processing/text_normalization/ta/taggers/tokenize_and_classify.py b/nemo_text_processing/text_normalization/ta/taggers/tokenize_and_classify.py new file mode 100644 index 000000000..7d173ff2f --- /dev/null +++ b/nemo_text_processing/text_normalization/ta/taggers/tokenize_and_classify.py @@ -0,0 +1,170 @@ +# Copyright (c) 2026, NVIDIA CORPORATION. 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 logging +import os + +import pynini +from pynini.lib import pynutil + +from nemo_text_processing.text_normalization.ta.graph_utils import ( + NEMO_SPACE, + NEMO_WHITE_SPACE, + GraphFst, + delete_extra_space, + delete_space, + generator_main, +) +from nemo_text_processing.text_normalization.ta.taggers.cardinal import CardinalFst + +# from nemo_text_processing.text_normalization.hi.taggers.date import DateFst +# from nemo_text_processing.text_normalization.hi.taggers.decimal import DecimalFst +# from nemo_text_processing.text_normalization.hi.taggers.electronic import ElectronicFst +# from nemo_text_processing.text_normalization.hi.taggers.fraction import FractionFst +# from nemo_text_processing.text_normalization.hi.taggers.measure import MeasureFst +# from nemo_text_processing.text_normalization.hi.taggers.money import MoneyFst +# from nemo_text_processing.text_normalization.hi.taggers.ordinal import OrdinalFst +from nemo_text_processing.text_normalization.ta.taggers.punctuation import PunctuationFst + +# from nemo_text_processing.text_normalization.hi.taggers.telephone import TelephoneFst +# from nemo_text_processing.text_normalization.hi.taggers.time import TimeFst +# from nemo_text_processing.text_normalization.hi.taggers.whitelist import WhiteListFst +from nemo_text_processing.text_normalization.ta.taggers.word import WordFst + + +class ClassifyFst(GraphFst): + """ + Final class that composes all other classification grammars. This class can process an entire sentence including punctuation. + For deployment, this grammar will be compiled and exported to OpenFst Finite State Archive (FAR) File. + More details to deployment at NeMo/tools/text_processing_deployment. + + Args: + input_case: accepting either "lower_cased" or "cased" input. + deterministic: if True will provide a single transduction option, + for False multiple options (used for audio-based normalization) + cache_dir: path to a dir with .far grammar file. Set to None to avoid using cache. + overwrite_cache: set to True to overwrite .far files + whitelist: path to a file with whitelist replacements + """ + + def __init__( + self, + input_case: str, + deterministic: bool = True, + cache_dir: str = None, + overwrite_cache: bool = False, + whitelist: str = None, + ): + super().__init__(name="tokenize_and_classify", kind="classify", deterministic=deterministic) + + far_file = None + if cache_dir is not None and cache_dir != "None": + os.makedirs(cache_dir, exist_ok=True) + whitelist_file = os.path.basename(whitelist) if whitelist else "" + far_file = os.path.join( + cache_dir, + f"hi_tn_{deterministic}_deterministic_{input_case}_{whitelist_file}_tokenize.far", + ) + if not overwrite_cache and far_file and os.path.exists(far_file): + self.fst = pynini.Far(far_file, mode="r")["tokenize_and_classify"] + logging.info(f"ClassifyFst.fst was restored from {far_file}.") + else: + logging.info(f"Creating ClassifyFst grammars.") + + cardinal = CardinalFst(deterministic=deterministic) + cardinal_graph = cardinal.fst + + # decimal = DecimalFst(cardinal=cardinal, deterministic=deterministic) + # decimal_graph = decimal.fst + + # fraction = FractionFst(cardinal=cardinal, deterministic=deterministic) + # fraction_graph = fraction.fst + + # date = DateFst(cardinal=cardinal) + # date_graph = date.fst + + # timefst = TimeFst(cardinal=cardinal) + # time_graph = timefst.fst + + # ordinal = OrdinalFst(cardinal=cardinal, deterministic=deterministic) + # ordinal_graph = ordinal.fst + + # measure = MeasureFst(cardinal=cardinal, decimal=decimal, ordinal=ordinal, input_case=input_case) + # measure_graph = measure.fst + + # money = MoneyFst(cardinal=cardinal) + # money_graph = money.fst + + # whitelist_graph = WhiteListFst( + # input_case=input_case, deterministic=deterministic, input_file=whitelist + # ).fst + + punctuation = PunctuationFst(deterministic=deterministic) + punct_graph = punctuation.fst + + # telephone = TelephoneFst() + # telephone_graph = telephone.fst + + # electronic = ElectronicFst(deterministic=deterministic) + # electronic_graph = electronic.fst + + classify = ( + # pynutil.add_weight(whitelist_graph, 1.01)| + pynutil.add_weight(cardinal_graph, 1.1) + # | pynutil.add_weight(decimal_graph, 1.1) + # | pynutil.add_weight(fraction_graph, 1.1) + # | pynutil.add_weight(date_graph, 1.1) + # | pynutil.add_weight(time_graph, 1.1) + # | pynutil.add_weight(measure_graph, 1.1) + # | pynutil.add_weight(money_graph, 1.1) + # | pynutil.add_weight(telephone_graph, 1.1) + # | pynutil.add_weight(ordinal_graph, 1.1) + # | pynutil.add_weight(electronic_graph, 1.1) + ) + + word_graph = WordFst(punctuation=punctuation, deterministic=deterministic).fst + + punct = pynutil.insert("tokens { ") + pynutil.add_weight(punct_graph, weight=2.1) + pynutil.insert(" }") + punct = pynini.closure( + pynini.union( + pynini.compose(pynini.closure(NEMO_WHITE_SPACE, 1), delete_extra_space), + (pynutil.insert(NEMO_SPACE) + punct), + ), + 1, + ) + + classify = pynini.union(classify, pynutil.add_weight(word_graph, 100)) + token = pynutil.insert("tokens { ") + classify + pynutil.insert(" }") + token_plus_punct = ( + pynini.closure(punct + pynutil.insert(NEMO_SPACE)) + + token + + pynini.closure(pynutil.insert(NEMO_SPACE) + punct) + ) + + graph = token_plus_punct + pynini.closure( + pynini.union( + pynini.compose(pynini.closure(NEMO_WHITE_SPACE, 1), delete_extra_space), + (pynutil.insert(NEMO_SPACE) + punct + pynutil.insert(NEMO_SPACE)), + ) + + token_plus_punct + ) + + graph = delete_space + graph + delete_space + graph = pynini.union(graph, punct) + + self.fst = graph.optimize() + + if far_file: + generator_main(far_file, {"tokenize_and_classify": self.fst}) + logging.info(f"ClassifyFst grammars are saved to {far_file}.") diff --git a/nemo_text_processing/text_normalization/ta/taggers/word.py b/nemo_text_processing/text_normalization/ta/taggers/word.py new file mode 100644 index 000000000..ed2566ade --- /dev/null +++ b/nemo_text_processing/text_normalization/ta/taggers/word.py @@ -0,0 +1,61 @@ +# Copyright (c) 2026, NVIDIA CORPORATION. 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 pynini +from pynini.lib import pynutil + +from nemo_text_processing.text_normalization.ta.graph_utils import ( + MIN_NEG_WEIGHT, + NEMO_NOT_SPACE, + GraphFst, + convert_space, +) +from nemo_text_processing.text_normalization.ta.taggers.punctuation import PunctuationFst + + +class WordFst(GraphFst): + """ + Finite state transducer for classifying Tamil words. + e.g. சோனா -> tokens { name: " சோனா" } + + Args: + punctuation: PunctuationFst + deterministic: if True will provide a single transduction option, + for False multiple transductions are generated (used for audio-based normalization) + """ + + def __init__(self, punctuation: PunctuationFst, deterministic: bool = True): + super().__init__(name="word", kind="classify", deterministic=deterministic) + + # Define Tamil characters and symbols using pynini.union + TAMIL_CHAR = pynini.union( + *[chr(i) for i in range(0x0B80, 0x0B83 + 1)], # Tamil vowels and consonants + *[chr(i) for i in range(0x0B85, 0x0BB9 + 1)], # More Tamil characters + *[chr(i) for i in range(0x0BBE, 0x0BCD + 1)], # Tamil diacritics + ).optimize() + + # Include punctuation in the graph + punct = punctuation.graph + default_graph = pynini.closure(pynini.difference(NEMO_NOT_SPACE, punct.project("input")), 1) + symbols_to_exclude = (pynini.union("$", "€", "₩", "£", "¥", "#", "%") | punct).optimize() + + # Use TAMIL_CHAR in the graph + graph = pynini.closure(pynini.difference(TAMIL_CHAR, symbols_to_exclude), 1) + graph = pynutil.add_weight(graph, MIN_NEG_WEIGHT) | default_graph + + # Ensure no spaces around punctuation + graph = pynini.closure(graph + pynini.closure(punct + graph, 0, 1)) + + self.graph = convert_space(graph) + self.fst = (pynutil.insert("name: \"") + self.graph + pynutil.insert("\"")).optimize() diff --git a/nemo_text_processing/text_normalization/ta/utils.py b/nemo_text_processing/text_normalization/ta/utils.py new file mode 100644 index 000000000..fa0471fb6 --- /dev/null +++ b/nemo_text_processing/text_normalization/ta/utils.py @@ -0,0 +1,74 @@ +# Copyright (c) 2026, NVIDIA CORPORATION. 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 csv +import os +import pynini + + +def get_abs_path(rel_path): + """ + Get absolute path + + Args: + rel_path: relative path to this file + + Returns absolute path + """ + return os.path.dirname(os.path.abspath(__file__)) + '/' + rel_path + + +def load_labels(abs_path): + """ + loads relative path file as dictionary + + Args: + abs_path: absolute path + + Returns dictionary of mappings + """ + label_tsv = open(abs_path, encoding="utf-8") + labels = list(csv.reader(label_tsv, delimiter="\t")) + label_tsv.close() + return labels + + +def augment_labels_with_punct_at_end(labels): + """ + augments labels: if key ends on a punctuation that value does not have, add a new label + where the value maintains the punctuation + + Args: + labels : input labels + Returns: + additional labels + """ + res = [] + for label in labels: + if len(label) > 1: + if label[0][-1] == "." and label[1][-1] != ".": + res.append([label[0], label[1] + "."] + label[2:]) + return res + + +def apply_fst(text, fst): + """Given a string input, returns the output string + produced by traversing the path with lowest weight. + If no valid path accepts input string, returns an + error. + """ + try: + print(pynini.shortestpath(text @ fst).string()) + except pynini.FstOpError: + print(f"Error: No valid output with given input: '{text}'") diff --git a/nemo_text_processing/text_normalization/ta/verbalizers/__init__.py b/nemo_text_processing/text_normalization/ta/verbalizers/__init__.py new file mode 100644 index 000000000..4fc25d0d3 --- /dev/null +++ b/nemo_text_processing/text_normalization/ta/verbalizers/__init__.py @@ -0,0 +1,13 @@ +# Copyright (c) 2026, NVIDIA CORPORATION. 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. diff --git a/nemo_text_processing/text_normalization/ta/verbalizers/cardinal.py b/nemo_text_processing/text_normalization/ta/verbalizers/cardinal.py new file mode 100644 index 000000000..59676b890 --- /dev/null +++ b/nemo_text_processing/text_normalization/ta/verbalizers/cardinal.py @@ -0,0 +1,49 @@ +# Copyright (c) 2026, NVIDIA CORPORATION. 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 pynini +from pynini.lib import pynutil + +from nemo_text_processing.text_normalization.hi.graph_utils import NEMO_NOT_QUOTE, GraphFst, delete_space + + +class CardinalFst(GraphFst): + """ + Finite state transducer for verbalizing cardinal, e.g. + cardinal { negative: "true" integer: "23" } -> minus twenty three + + Args: + deterministic: if True will provide a single transduction option, + for False multiple options (used for audio-based normalization) + """ + + def __init__(self, deterministic: bool = True): + super().__init__(name="cardinal", kind="verbalize", deterministic=deterministic) + + self.optional_sign = pynini.cross("negative: \"true\"", "minus ") + if not deterministic: + self.optional_sign |= pynini.cross("negative: \"true\"", "negative ") + self.optional_sign |= pynini.cross("negative: \"true\"", "dash ") + + self.optional_sign = pynini.closure(self.optional_sign + delete_space, 0, 1) + + integer = pynini.closure(NEMO_NOT_QUOTE) + + self.integer = delete_space + pynutil.delete("\"") + integer + pynutil.delete("\"") + integer = pynutil.delete("integer:") + self.integer + + self.numbers = self.optional_sign + integer + delete_tokens = self.delete_tokens(self.numbers) + + self.fst = delete_tokens.optimize() diff --git a/nemo_text_processing/text_normalization/ta/verbalizers/post_processing.py b/nemo_text_processing/text_normalization/ta/verbalizers/post_processing.py new file mode 100644 index 000000000..2d05b7cea --- /dev/null +++ b/nemo_text_processing/text_normalization/ta/verbalizers/post_processing.py @@ -0,0 +1,96 @@ +# 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 os + +import pynini +from pynini.lib import pynutil + +from nemo_text_processing.text_normalization.ta.graph_utils import ( + MIN_NEG_WEIGHT, + NEMO_CHAR, + NEMO_SIGMA, + generator_main, +) +from nemo_text_processing.text_normalization.ta.taggers.punctuation import PunctuationFst +from nemo_text_processing.utils.logging import logger + + +class PostProcessingFst: + """ + Finite state transducer that post-processing an entire sentence after verbalization is complete, e.g. + removes extra spaces around punctuation marks " ( one hundred and twenty three ) " -> "(one hundred and twenty three)" + + Args: + cache_dir: path to a dir with .far grammar file. Set to None to avoid using cache. + overwrite_cache: set to True to overwrite .far files + """ + + def __init__(self, cache_dir: str = None, overwrite_cache: bool = False): + + far_file = None + if cache_dir is not None and cache_dir != "None": + os.makedirs(cache_dir, exist_ok=True) + far_file = os.path.join(cache_dir, "ta_tn_post_processing.far") + if not overwrite_cache and far_file and os.path.exists(far_file): + self.fst = pynini.Far(far_file, mode="r")["post_process_graph"] + logger.info(f'Post processing graph was restored from {far_file}.') + else: + self.fst = self.get_punct_postprocess_graph() + + if far_file: + generator_main(far_file, {"post_process_graph": self.fst}) + + def get_punct_postprocess_graph(self): + """ + Returns graph to post process punctuation marks. + + By default, spaces are removed before punctuation marks like comma, period, etc. + """ + punct_marks_all = PunctuationFst().punct_marks + + # Punctuation marks that should NOT have space before them + # (most punctuation except quotes, dashes, and opening brackets) + quotes = ["'", "\"", "«"] + dashes = ["-", "—"] + brackets = ["<", "{", "(", r"\["] + allow_space_before_punct = quotes + dashes + brackets + + no_space_before_punct = [m for m in punct_marks_all if m not in allow_space_before_punct] + # Add Hindi-specific punctuation + no_space_before_punct.extend(["।", ",", ".", ";", ":", "!", "?"]) + # Remove duplicates + no_space_before_punct = list(set(no_space_before_punct)) + no_space_before_punct = pynini.union(*no_space_before_punct) + + delete_space = pynutil.delete(" ") + + # Delete space before no_space_before_punct marks + non_punct = pynini.difference(NEMO_CHAR, no_space_before_punct).optimize() + graph = ( + pynini.closure(non_punct) + + pynini.closure( + no_space_before_punct | pynutil.add_weight(delete_space + no_space_before_punct, MIN_NEG_WEIGHT) + ) + + pynini.closure(non_punct) + ) + graph = pynini.closure(graph).optimize() + + # Remove space after opening brackets + no_space_after_punct = pynini.union(*brackets) + no_space_after_punct = pynini.cdrewrite(delete_space, no_space_after_punct, NEMO_SIGMA, NEMO_SIGMA).optimize() + graph = pynini.compose(graph, no_space_after_punct).optimize() + + return graph diff --git a/nemo_text_processing/text_normalization/ta/verbalizers/verbalize.py b/nemo_text_processing/text_normalization/ta/verbalizers/verbalize.py new file mode 100644 index 000000000..e3012e7bd --- /dev/null +++ b/nemo_text_processing/text_normalization/ta/verbalizers/verbalize.py @@ -0,0 +1,49 @@ +# Copyright (c) 2026, NVIDIA CORPORATION. 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. + +from nemo_text_processing.text_normalization.ta.graph_utils import GraphFst +from nemo_text_processing.text_normalization.ta.verbalizers.cardinal import CardinalFst + +# from nemo_text_processing.text_normalization.ta.verbalizers.date import DateFst +# from nemo_text_processing.text_normalization.hi.verbalizers.decimal import DecimalFst +# from nemo_text_processing.text_normalization.hi.verbalizers.electronic import ElectronicFst +# from nemo_text_processing.text_normalization.hi.verbalizers.fraction import FractionFst +# from nemo_text_processing.text_normalization.hi.verbalizers.measure import MeasureFst +# from nemo_text_processing.text_normalization.hi.verbalizers.money import MoneyFst +# from nemo_text_processing.text_normalization.hi.verbalizers.ordinal import OrdinalFst +# from nemo_text_processing.text_normalization.hi.verbalizers.telephone import TelephoneFst +# from nemo_text_processing.text_normalization.hi.verbalizers.time import TimeFst +# from nemo_text_processing.text_normalization.ta.verbalizers.whitelist import WhiteListFst + + +class VerbalizeFst(GraphFst): + """ + Composes other verbalizer grammars. + For deployment, this grammar will be compiled and exported to OpenFst Finite State Archive (FAR) File. + More details to deployment at NeMo/tools/text_processing_deployment. + + Args: + deterministic: if True will provide a single transduction option, + for False multiple options (used for audio-based normalization) + """ + + def __init__(self, deterministic: bool = True): + super().__init__(name="verbalize", kind="verbalize", deterministic=deterministic) + + cardinal = CardinalFst(deterministic=deterministic) + cardinal_graph = cardinal.fst + + graph = cardinal_graph + + self.fst = graph diff --git a/nemo_text_processing/text_normalization/ta/verbalizers/verbalize_final.py b/nemo_text_processing/text_normalization/ta/verbalizers/verbalize_final.py new file mode 100644 index 000000000..5eb01cd97 --- /dev/null +++ b/nemo_text_processing/text_normalization/ta/verbalizers/verbalize_final.py @@ -0,0 +1,76 @@ +# Copyright (c) 2026, NVIDIA CORPORATION. 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 logging +import os + +import pynini +from pynini.lib import pynutil + +from nemo_text_processing.text_normalization.ta.graph_utils import ( + GraphFst, + delete_extra_space, + delete_space, + generator_main, +) +from nemo_text_processing.text_normalization.ta.verbalizers.verbalize import VerbalizeFst +from nemo_text_processing.text_normalization.ta.verbalizers.word import WordFst + + +class VerbalizeFinalFst(GraphFst): + """ + Finite state transducer that verbalizes an entire sentence, e.g. + tokens { name: "its" } tokens { time { hours: "twelve" minutes: "thirty" } } tokens { name: "now" } tokens { name: "." } -> its twelve thirty now . + + Args: + deterministic: if True will provide a single transduction option, + for False multiple options (used for audio-based normalization) + cache_dir: path to a dir with .far grammar file. Set to None to avoid using cache. + overwrite_cache: set to True to overwrite .far files + """ + + def __init__(self, deterministic: bool = True, cache_dir: str = None, overwrite_cache: bool = False): + super().__init__(name="verbalize_final", kind="verbalize", deterministic=deterministic) + + far_file = None + if cache_dir is not None and cache_dir != "None": + os.makedirs(cache_dir, exist_ok=True) + far_file = os.path.join(cache_dir, f"en_tn_{deterministic}_deterministic_verbalizer.far") + if not overwrite_cache and far_file and os.path.exists(far_file): + self.fst = pynini.Far(far_file, mode="r")["verbalize"] + logging.info(f'VerbalizeFinalFst graph was restored from {far_file}.') + else: + verbalize = VerbalizeFst(deterministic=deterministic).fst + word = WordFst(deterministic=deterministic).fst + types = verbalize | word + + if deterministic: + graph = ( + pynutil.delete("tokens") + + delete_space + + pynutil.delete("{") + + delete_space + + types + + delete_space + + pynutil.delete("}") + ) + else: + graph = delete_space + types + delete_space + + graph = delete_space + pynini.closure(graph + delete_extra_space) + graph + delete_space + + self.fst = graph.optimize() + if far_file: + generator_main(far_file, {"verbalize": self.fst}) + logging.info(f"VerbalizeFinalFst grammars are saved to {far_file}.") diff --git a/nemo_text_processing/text_normalization/ta/verbalizers/word.py b/nemo_text_processing/text_normalization/ta/verbalizers/word.py new file mode 100644 index 000000000..f9683a102 --- /dev/null +++ b/nemo_text_processing/text_normalization/ta/verbalizers/word.py @@ -0,0 +1,43 @@ +# Copyright (c) 2026, NVIDIA CORPORATION. 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 pynini +from pynini.lib import pynutil + +from nemo_text_processing.text_normalization.ta.graph_utils import NEMO_CHAR, NEMO_SIGMA, GraphFst, delete_space + + +class WordFst(GraphFst): + """ + Finite state transducer for verbalizing Tamil words. + e.g. tokens { name: " சோனா" } -> சோனா + + Args: + deterministic: if True will provide a single transduction option, + for False multiple transduction are generated (used for audio-based normalization) + """ + + def __init__(self, deterministic: bool = True): + super().__init__(name="word", kind="verbalize", deterministic=deterministic) + chars = pynini.closure(NEMO_CHAR - " ", 1) + punct = pynini.union("!", "?", ".", ",", "-", ":", ";", "।") # Add other punctuation marks as needed + char = pynutil.delete("name:") + delete_space + pynutil.delete("\"") + chars + pynutil.delete("\"") + + # Ensure no spaces around punctuation + graph = char + pynini.closure(delete_space + punct, 0, 1) + + # Explicitly remove spaces before punctuation + remove_space_before_punct = pynini.cdrewrite(pynini.cross(" ", ""), "", punct, NEMO_SIGMA) + graph = graph @ remove_space_before_punct + + self.fst = graph.optimize() diff --git a/tests/nemo_text_processing/ta/__init__.py b/tests/nemo_text_processing/ta/__init__.py new file mode 100644 index 000000000..4fc25d0d3 --- /dev/null +++ b/tests/nemo_text_processing/ta/__init__.py @@ -0,0 +1,13 @@ +# Copyright (c) 2026, NVIDIA CORPORATION. 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. diff --git a/tests/nemo_text_processing/ta/data_text_normalization/test_cases_cardinal.txt b/tests/nemo_text_processing/ta/data_text_normalization/test_cases_cardinal.txt new file mode 100644 index 000000000..143acc686 --- /dev/null +++ b/tests/nemo_text_processing/ta/data_text_normalization/test_cases_cardinal.txt @@ -0,0 +1,148 @@ +௪ ஃபோர்கள்~நான்கு ஃபோர்கள் +௬ வீரர்கள் அவுட்~ஆறு வீரர்கள் அவுட் +ஒன்பிளஸ் ௮ ப்ரோ~ஒன்பிளஸ் எட்டு ப்ரோ +௫ சார்ஜர்கள்~ஐந்து சார்ஜர்கள் +௪ ஓவரில் ௧௭ ரன்கள்~நான்கு ஓவரில் பதினேழு ரன்கள் +௫ சாக்லெட்டுகள் ௯ டாஃபிகள்~ஐந்து சாக்லெட்டுகள் ஒன்பது டாஃபிகள் +௧௦௦௯௯~பத்துஆயிரத்து தொண்ணூற்றிஒன்பது +௧௦௦௦௦௧~ஒரு லட்சத்து ஒன்று +௪ சிக்ஸர்கள் ௧௪ நான்குகள்~நான்கு சிக்ஸர்கள் பதினான்கு நான்குகள் +௬ ரன்கள் எடுத்தார்~ஆறு ரன்கள் எடுத்தார் +௩ கோல்கள் அடித்தார்~மூன்று கோல்கள் அடித்தார் +௫ நான்குகள்~ஐந்து நான்குகள் +௨ சிக்ஸர்கள் ௧௨ ரன்கள்~இரண்டு சிக்ஸர்கள் பன்னிரண்டு ரன்கள் +௯ புள்ளிகள்~ஒன்பது புள்ளிகள் +௪ உயிரிழப்பு ௧௮ காயம்~நான்கு உயிரிழப்பு பதினெட்டு காயம் +௫ கோல்கள் அடி~ஐந்து கோல்கள் அடி +௩ பேட்ஸ்மேன்கள் ௧௨ வீரர்கள்~மூன்று பேட்ஸ்மேன்கள் பன்னிரண்டு வீரர்கள் +௫ தோல்வி ௧ டிரா ௧௭ வெற்றி~ஐந்து தோல்வி ஒன்று டிரா பதினேழு வெற்றி +௫௧௦௨௨௩௪௫௫௬௭~ஐந்தாயிரத்து நூற்று இரண்டு கோடியே இருபத்திமூன்று லட்சத்து நாற்பத்திஐந்துஆயிரத்து ஐநூற்று அறுபத்திஏழு +பாடம் ௧௦ ஸ்லோகம் ௨~பாடம் பத்து ஸ்லோகம் இரண்டு +௧௦௧~நூற்று ஒன்று +௧௦௨~நூற்று இரண்டு +௧௦௩~நூற்று மூன்று +௧௦௪~நூற்று நான்கு +௧௦௫~நூற்று ஐந்து +௧௦௬~நூற்று ஆறு +௧௦௭~நூற்று ஏழு +௧௦௮~நூற்று எட்டு +௧௦௯~நூற்று ஒன்பது +௧௧௦~நூற்று பத்து +௧௧௧~நூற்று பதினொன்று +௧௧௨~நூற்று பன்னிரண்டு +௧௧௩~நூற்று பதிமூன்று +௧௧௪~நூற்று பதினான்கு +௧௧௫~நூற்று பதினைந்து +௧௧௬~நூற்று பதினாறு +௮௧௭~எட்டுநூற்று பதினேழு +௮௧௮~எட்டுநூற்று பதினெட்டு +௮௧௯~எட்டுநூற்று பத்தொன்பது +௮௨௦~எட்டுநூற்று இருபது +௮௨௧~எட்டுநூற்று இருபத்தொன்று +௮௨௨~எட்டுநூற்று இருபத்திரண்டு +௮௨௩~எட்டுநூற்று இருபத்திமூன்று +௮௨௪~எட்டுநூற்று இருபத்திநான்கு +௮௨௫~எட்டுநூற்று இருபத்திஐந்து +௮௨௬~எட்டுநூற்று இருபத்திஆறு +௮௨௭~எட்டுநூற்று இருபத்திஏழு +௮௨௮~எட்டுநூற்று இருபத்திஎட்டு +௮௨௯~எட்டுநூற்று இருபத்திஒன்பது +௮௩௦~எட்டுநூற்று முப்பது +௮௩௧~எட்டுநூற்று முப்பத்தொன்று +௮௩௨~எட்டுநூற்று முப்பத்திரண்டு +௮௩௩~எட்டுநூற்று முப்பத்திமூன்று +௮௩௪~எட்டுநூற்று முப்பத்திநான்கு +௮௩௫~எட்டுநூற்று முப்பத்திஐந்து +௮௩௬~எட்டுநூற்று முப்பத்திஆறு +௭௩௭~எழுநூற்று முப்பத்திஏழு +௭௩௮~எழுநூற்று முப்பத்திஎட்டு +௭௩௯~எழுநூற்று முப்பத்திஒன்பது +௭௪௦~எழுநூற்று நாற்பது +௭௪௧~எழுநூற்று நாற்பத்தொன்று +௭௪௨~எழுநூற்று நாற்பத்திரண்டு +௭௪௩~எழுநூற்று நாற்பத்திமூன்று +௭௪௪~எழுநூற்று நாற்பத்திநான்கு +௭௪௫~எழுநூற்று நாற்பத்திஐந்து +௪௪௬~நானூற்று நாற்பத்திஆறு +௪௪௭~நானூற்று நாற்பத்திஏழு +௪௪௮~நானூற்று நாற்பத்திஎட்டு +௪௪௯~நானூற்று நாற்பத்திஒன்பது +௪௫௦~நானூற்று ஐம்பது +௪௬௧~நானூற்று அறுபத்தொன்று +௧௭௫~நூற்று எழுபத்திஐந்து +௧௮௧~நூற்று எண்பத்தொன்று +௧௯௦~நூற்று தொண்ணூறு +௧௯௧~நூற்று தொண்ணூற்றொன்று +௧௯௯~நூற்று தொண்ணூற்றிஒன்பது +௧௦௦௧~ஆயிரத்து ஒன்று +௧௦௯௯~ஆயிரத்து தொண்ணூற்றிஒன்பது +௫௫௫௧ வாழைப்பழங்கள்~ஐந்தாயிரத்து ஐநூற்று ஐம்பத்தொன்று வாழைப்பழங்கள் +௫௫௫௫௨ ஆப்பிள்கள்~ஐம்பத்திஐந்துஆயிரத்து ஐநூற்று ஐம்பத்திரண்டு ஆப்பிள்கள் +௫௩~ஐம்பத்திமூன்று +௫௪~ஐம்பத்திநான்கு +௫௫~ஐம்பத்திஐந்து +௫௬~ஐம்பத்திஆறு +௫௭~ஐம்பத்திஏழு +௫௮~ஐம்பத்திஎட்டு +௫௯~ஐம்பத்திஒன்பது +௬௦~அறுபது +௬௧~அறுபத்தொன்று +௬௨~அறுபத்திரண்டு +௬௩~அறுபத்திமூன்று +௬௪~அறுபத்திநான்கு +௬௫~அறுபத்திஐந்து +௬௬~அறுபத்திஆறு +௬௭~அறுபத்திஏழு +௬௮~அறுபத்திஎட்டு +௬௯~அறுபத்திஒன்பது +௭௦~எழுபது +௭௧~எழுபத்தொன்று +௭௨~எழுபத்திரண்டு +௭௩~எழுபத்திமூன்று +௭௪~எழுபத்திநான்கு +௭௫~எழுபத்திஐந்து +௭௬~எழுபத்திஆறு +௭௭~எழுபத்திஏழு +௭௮~எழுபத்திஎட்டு +௭௯~எழுபத்திஒன்பது +௮௦~எண்பது +௮௧~எண்பத்தொன்று +௮௨~எண்பத்திரண்டு +௮௩~எண்பத்திமூன்று +௮௪~எண்பத்திநான்கு +௮௫~எண்பத்திஐந்து +௮௬~எண்பத்திஆறு +௮௭~எண்பத்திஏழு +௮௮~எண்பத்திஎட்டு +௮௯~எண்பத்திஒன்பது +௯௦~தொண்ணூறு +௯௧~தொண்ணூற்றொன்று +௯௨~தொண்ணூற்றிரண்டு +௯௩~தொண்ணூற்றிமூன்று +௯௪~தொண்ணூற்றிநான்கு +௯௫~தொண்ணூற்றிஐந்து +௯௬~தொண்ணூற்றிஆறு +௯௭~தொண்ணூற்றிஏழு +௯௮~தொண்ணூற்றிஎட்டு +௯௯~தொண்ணூற்றிஒன்பது +௧௩௨௩~ஆயிரத்து முந்நூற்று இருபத்திமூன்று +௧௩௪௫~ஆயிரத்து முந்நூற்று நாற்பத்திஐந்து +௧௩௪௫௬~பதிமூன்றுஆயிரத்து நானூற்று ஐம்பத்திஆறு +௧௨௩௪௬~பன்னிரண்டுஆயிரத்து முந்நூற்று நாற்பத்திஆறு +௧௨௩௪௫௬~ஒரு லட்சத்து இருபத்திமூன்றுஆயிரத்து நானூற்று ஐம்பத்திஆறு +௮௭௨௯௮௭~எட்டு லட்சத்து எழுபத்திரண்டுஆயிரத்து ஒன்பதுநூற்று எண்பத்திஏழு +௯௮௭௬௦௯~ஒன்பது லட்சத்து எண்பத்திஏழுஆயிரத்து அறுநூற்று ஒன்பது +௯௮௭௬௭௮௯~தொண்ணூற்றிஎட்டு லட்சத்து எழுபத்திஆறுஆயிரத்து எழுநூற்று எண்பத்திஒன்பது +௨௩௪௫௫௬௭~இருபத்திமூன்று லட்சத்து நாற்பத்திஐந்துஆயிரத்து ஐநூற்று அறுபத்திஏழு +௧௨௩௪௫௫௬௭~ஒரு கோடியே இருபத்திமூன்று லட்சத்து நாற்பத்திஐந்துஆயிரத்து ஐநூற்று அறுபத்திஏழு +௧௨௧௨௧௨௧௨~ஒரு கோடியே இருபத்தொன்று லட்சத்து இருபத்தொன்றுஆயிரத்து இருநூற்று பன்னிரண்டு +௧௧௨௨௩௪௫௫௬௭~நூற்று பன்னிரண்டு கோடியே இருபத்திமூன்று லட்சத்து நாற்பத்திஐந்துஆயிரத்து ஐநூற்று அறுபத்திஏழு +௧௦௨௨௩௪௫௫௬௭~நூற்று இரண்டு கோடியே இருபத்திமூன்று லட்சத்து நாற்பத்திஐந்துஆயிரத்து ஐநூற்று அறுபத்திஏழு +௧௧௦௨௨௩௪௫௫௬௭~ஆயிரத்து நூற்று இரண்டு கோடியே இருபத்திமூன்று லட்சத்து நாற்பத்திஐந்துஆயிரத்து ஐநூற்று அறுபத்திஏழு +௫௧௦௨௨௩௪௫௫௬௭~ஐந்தாயிரத்து நூற்று இரண்டு கோடியே இருபத்திமூன்று லட்சத்து நாற்பத்திஐந்துஆயிரத்து ஐநூற்று அறுபத்திஏழு +௨ புள்ளிகள் ௧௨ கோல்கள்~இரண்டு புள்ளிகள் பன்னிரண்டு கோல்கள் +௦௫~சுழியம் ஐந்து +௦௧~சுழியம் ஒன்று +௦௭௩~சுழியம் ஏழு மூன்று +௦௦௦௧~சுழியம் சுழியம் சுழியம் ஒன்று +௦௦௦~சுழியம் சுழியம் சுழியம் \ No newline at end of file diff --git a/tests/nemo_text_processing/ta/test_cardinal.py b/tests/nemo_text_processing/ta/test_cardinal.py new file mode 100644 index 000000000..4b9ba5cbd --- /dev/null +++ b/tests/nemo_text_processing/ta/test_cardinal.py @@ -0,0 +1,42 @@ +# Copyright (c) 2026, NVIDIA CORPORATION. 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.inverse_text_normalization.inverse_normalize import InverseNormalizer +from nemo_text_processing.text_normalization.normalize import Normalizer + +from ..utils import CACHE_DIR, parse_test_case_file + + +class TestCardinal: + normalizer = Normalizer( + input_case='cased', lang='ta', cache_dir=CACHE_DIR, overwrite_cache=False, post_process=False + ) + # inverse_normalizer = InverseNormalizer(lang='ta', cache_dir=CACHE_DIR, overwrite_cache=False) + + @parameterized.expand(parse_test_case_file('ta/data_text_normalization/test_cases_cardinal.txt')) + @pytest.mark.run_only_on('CPU') + @pytest.mark.unit + def test_norm(self, test_input, expected): + pred = self.normalizer.normalize(test_input, verbose=False) + assert pred.strip() == expected.strip() + + # @parameterized.expand(parse_test_case_file('ta/data_inverse_text_normalization/test_cases_cardinal.txt')) + # @pytest.mark.run_only_on('CPU') + # @pytest.mark.unit + # def test_denorm(self, test_input, expected): + # pred = self.inverse_normalizer.inverse_normalize(test_input, verbose=False) + # assert pred == expected