-
Notifications
You must be signed in to change notification settings - Fork 170
Hi TN: Implement Serial tagger #440
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
shrpawar-alt
wants to merge
2
commits into
NVIDIA:staging/hi_tn_v3
Choose a base branch
from
shrpawar-alt:hi-tn-serialtagger
base: staging/hi_tn_v3
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
68 changes: 68 additions & 0 deletions
68
nemo_text_processing/text_normalization/hi/data/serial/chars.tsv
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,68 @@ | ||
| अ अ | ||
| आ आ | ||
| इ इ | ||
| ई ई | ||
| उ उ | ||
| ऊ ऊ | ||
| ऋ ऋ | ||
| ए ए | ||
| ऐ ऐ | ||
| ओ ओ | ||
| औ औ | ||
| ऑ ऑ | ||
| ा ा | ||
| ि ि | ||
| ी ी | ||
| ु ु | ||
| ू ू | ||
| ृ ृ | ||
| े े | ||
| ै ै | ||
| ो ो | ||
| ौ ौ | ||
| ॉ ॉ | ||
| ं ं | ||
| ः ः | ||
| ँ ँ | ||
| क क | ||
| ख ख | ||
| ग ग | ||
| घ घ | ||
| ङ ङ | ||
| च च | ||
| छ छ | ||
| ज ज | ||
| झ झ | ||
| ञ ञ | ||
| ट ट | ||
| ठ ठ | ||
| ड ड | ||
| ढ ढ | ||
| ण ण | ||
| त त | ||
| थ थ | ||
| द द | ||
| ध ध | ||
| न न | ||
| प प | ||
| फ फ | ||
| ब ब | ||
| भ भ | ||
| म म | ||
| य य | ||
| र र | ||
| ल ल | ||
| व व | ||
| श श | ||
| ष ष | ||
| स स | ||
| ह ह | ||
| क़ क़ | ||
| ख़ ख़ | ||
| ग़ ग़ | ||
| ज़ ज़ | ||
| ड़ ड़ | ||
| ढ़ ढ़ | ||
| फ़ फ़ | ||
| य़ य़ | ||
| ् ् | ||
4 changes: 4 additions & 0 deletions
4
nemo_text_processing/text_normalization/hi/data/serial/power_special.tsv
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| ^2 स्क्वेर्ड | ||
| ^२ स्क्वेर्ड | ||
| ^3 क्यूब | ||
| ^३ क्यूब |
4 changes: 4 additions & 0 deletions
4
nemo_text_processing/text_normalization/hi/data/serial/special_symbols.tsv
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| # हैशटैग | ||
| % प्रतिशत | ||
| & एंड | ||
| @ एट |
161 changes: 161 additions & 0 deletions
161
nemo_text_processing/text_normalization/hi/taggers/serial.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,161 @@ | ||
| # 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_ALPHA, | ||
| NEMO_DIGIT, | ||
| NEMO_NOT_SPACE, | ||
| NEMO_SIGMA, | ||
| GraphFst, | ||
| convert_space, | ||
| ) | ||
| from nemo_text_processing.text_normalization.hi.utils import get_abs_path | ||
|
|
||
|
|
||
| class SerialFst(GraphFst): | ||
| """ | ||
| Finite state transducer for classifying serial strings in Hindi. | ||
| Handles Devanagari-numeric mixtures, complex delimited number chains, | ||
| symbols, and powers. Supports both ASCII (0-9) and Devanagari (०-९) digits. | ||
|
|
||
| e.g. कोविड-19 -> tokens { name: "कोविड-उन्नीस" } | ||
| e.g. 5जी -> tokens { name: "पाँच जी" } | ||
| e.g. ३जी -> tokens { name: "तीन जी" } | ||
| e.g. 2^2 -> tokens { name: "दो स्क्वेर्ड" } | ||
| e.g. 2^4 -> tokens { name: "दो टु द पावर चार" } | ||
| e.g. 1-800-555 -> tokens { name: "एक-आठ सौ-पाँच सौ पचपन" } | ||
|
|
||
| Note: Pure Latin-alpha + digit patterns (A12, B-60) are intentionally | ||
| excluded here so they fall through to the electronic classifier. | ||
| """ | ||
|
|
||
| def __init__( | ||
| self, | ||
| cardinal: GraphFst, | ||
| deterministic: bool = True, | ||
| ): | ||
| super().__init__(name="serial", kind="classify", deterministic=deterministic) | ||
|
|
||
| digit_graph = pynini.string_file(get_abs_path("data/numbers/digit.tsv")) | ||
| zero_graph = pynini.string_file(get_abs_path("data/numbers/zero.tsv")) | ||
|
|
||
| devanagari_digits = pynini.project( | ||
| pynini.union(digit_graph, zero_graph), | ||
| "input", | ||
| ).optimize() | ||
|
|
||
| any_digit = pynini.union(NEMO_DIGIT, devanagari_digits).optimize() | ||
|
|
||
| not_quote = pynini.closure(pynini.difference(NEMO_SIGMA, pynini.accep('"')), 1) | ||
| strip_cardinal_tags = pynutil.delete('cardinal { integer: "') + not_quote + pynutil.delete('" }') | ||
|
|
||
| pure_cardinal_words = pynini.compose(cardinal.fst, strip_cardinal_tags).optimize() | ||
|
|
||
| length_filter = pynini.closure(any_digit, 1, 3) | ||
| limited_cardinal = pynini.compose(length_filter, pure_cardinal_words).optimize() | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. instead of using |
||
|
|
||
| num_graph = limited_cardinal | ||
|
|
||
| symbols_graph = pynini.string_file(get_abs_path("data/serial/special_symbols.tsv")).optimize() | ||
|
|
||
| devanagari_chars = pynini.project( | ||
| pynini.string_file(get_abs_path("data/serial/chars.tsv")), | ||
| "input", | ||
| ).optimize() | ||
|
|
||
| letter_graph = pynini.string_file(get_abs_path("data/address/letters.tsv")) | ||
| latin_letters = letter_graph + pynini.closure(pynutil.insert(" ") + letter_graph) | ||
| latin_letters = latin_letters.optimize() | ||
|
|
||
| devanagari_word = pynini.closure(devanagari_chars, 2).optimize() | ||
|
|
||
| delimiter = (pynini.accep("-") | pynini.accep("/") | pynini.accep(" ")).optimize() | ||
|
|
||
| alphas = (latin_letters | devanagari_word).optimize() | ||
| segment = (alphas | num_graph | symbols_graph).optimize() | ||
|
|
||
| serial_core = segment + pynini.closure(delimiter + segment, 1) | ||
| serial_core = serial_core.optimize() | ||
|
|
||
| serial_graph = serial_core | ||
|
|
||
| all_alphas = pynini.union(NEMO_ALPHA, devanagari_chars).optimize() | ||
|
|
||
| insert_space_alpha_digit = pynini.cdrewrite(pynutil.insert(" "), all_alphas, any_digit, NEMO_SIGMA) | ||
| insert_space_digit_alpha = pynini.cdrewrite(pynutil.insert(" "), any_digit, all_alphas, NEMO_SIGMA) | ||
| space_inserter = pynini.compose(insert_space_alpha_digit, insert_space_digit_alpha).optimize() | ||
|
|
||
| glued_serial = pynini.compose(space_inserter, serial_core).optimize() | ||
| serial_graph = pynini.union(serial_graph, glued_serial).optimize() | ||
|
|
||
| power_special = pynutil.add_weight( | ||
| pynini.string_file(get_abs_path("data/serial/power_special.tsv")), -1.0 | ||
| ).optimize() | ||
|
|
||
| power_generic = pynutil.add_weight( | ||
| (pynutil.delete("^") + pynutil.insert(" टु द पावर ") + num_graph), 1.0 | ||
| ).optimize() | ||
|
|
||
| power_suffix = pynini.union(power_special, power_generic).optimize() | ||
| power_graph = num_graph + power_suffix | ||
| serial_graph = pynini.union(serial_graph, power_graph).optimize() | ||
|
|
||
| serial_graph = pynini.compose(pynini.closure(NEMO_NOT_SPACE, 2), serial_graph).optimize() | ||
|
|
||
| pure_word_slash = pynini.closure(NEMO_ALPHA, 1) + pynini.accep("/") + pynini.closure(NEMO_ALPHA, 1) | ||
|
|
||
| dimension_pattern = ( | ||
| pynini.closure(any_digit, 1) + (pynini.accep("x") | pynini.accep("X")) + pynini.closure(any_digit, 1) | ||
| ) | ||
|
|
||
| _opt_delim = pynini.closure(pynini.accep("-") | pynini.accep(" "), 0, 1) | ||
| latin_alphanum = (pynini.closure(NEMO_ALPHA, 1) + _opt_delim + pynini.closure(any_digit, 1)) | ( | ||
| pynini.closure(any_digit, 1) + _opt_delim + pynini.closure(NEMO_ALPHA, 1) | ||
| ) | ||
|
|
||
| ordinal_suffixes = pynini.project( | ||
| pynini.union( | ||
| pynini.string_file(get_abs_path("data/ordinal/suffixes.tsv")), | ||
| pynini.string_file(get_abs_path("data/ordinal/suffixes_map.tsv")), | ||
| ), | ||
| "input", | ||
| ).optimize() | ||
| ordinal_pattern = pynini.closure(any_digit, 1) + ordinal_suffixes | ||
|
|
||
| date_year_suffix = pynini.project( | ||
| pynini.string_file(get_abs_path("data/date/year_suffix.tsv")), | ||
| "input", | ||
| ).optimize() | ||
| date_suffixes = pynini.project( | ||
| pynini.string_file(get_abs_path("data/date/suffixes.tsv")), | ||
| "input", | ||
| ).optimize() | ||
| date_pattern = ( | ||
| pynini.closure(any_digit, 1) | ||
| + pynini.closure(pynini.accep("-") + pynini.closure(any_digit, 1), 0) | ||
| + pynini.accep(" ") | ||
| + pynini.union(date_year_suffix, date_suffixes) | ||
| ) | ||
|
|
||
| exclusions = pure_word_slash | dimension_pattern | latin_alphanum | ordinal_pattern | date_pattern | ||
| accepted_inputs = pynini.difference(NEMO_SIGMA, exclusions).optimize() | ||
|
|
||
| serial_graph = pynini.compose(accepted_inputs, serial_graph).optimize() | ||
|
|
||
| self.graph = serial_graph.optimize() | ||
| graph = pynutil.insert('name: "') + convert_space(self.graph).optimize() + pynutil.insert('"') | ||
| self.fst = graph.optimize() | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
21 changes: 21 additions & 0 deletions
21
tests/nemo_text_processing/hi/data_text_normalization/test_cases_serial.txt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| कोविड-19~कोविड-उन्नीस | ||
| कोविड-१९~कोविड-उन्नीस | ||
| 5जी~पाँच जी | ||
| ५जी~पाँच जी | ||
| 2^2~दो स्क्वेर्ड | ||
| २^२~दो स्क्वेर्ड | ||
| 1-800-555~एक-आठ सौ-पाँच सौ पचपन | ||
| 3जी~तीन जी | ||
| 4जी~चार जी | ||
| कोरोना-2~कोरोना-दो | ||
| अग्नि-5~अग्नि-पाँच | ||
| ओमिक्रॉन-2~ओमिक्रॉन-दो | ||
| 3^2~तीन स्क्वेर्ड | ||
| 2^3~दो क्यूब | ||
| 5^3~पाँच क्यूब | ||
| ४^५~चार टु द पावर पाँच | ||
| 99-1~निन्यानबे-एक | ||
| 10-20-30~दस-बीस-तीस | ||
| 1-800-999~एक-आठ सौ-नौ सौ निन्यानबे | ||
| पृथ्वी-4~पृथ्वी-चार | ||
| ब्रह्मोस-1~ब्रह्मोस-एक |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| # 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.text_normalization.normalize import Normalizer | ||
|
|
||
| from ..utils import CACHE_DIR, parse_test_case_file | ||
|
|
||
|
|
||
| class TestSerial: | ||
| normalizer = Normalizer( | ||
| input_case='cased', lang='hi', cache_dir=CACHE_DIR, overwrite_cache=False, post_process=True | ||
| ) | ||
|
|
||
| @parameterized.expand(parse_test_case_file('hi/data_text_normalization/test_cases_serial.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, punct_post_process=True) | ||
| assert pred == expected |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if these chars don't go through any changes, let's not map them as a transformation