-
Notifications
You must be signed in to change notification settings - Fork 189
Update tokenize_and_classify.py #499
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
folivoramanh
wants to merge
5
commits into
main
Choose a base branch
from
pt_pnc_bug_fix
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+173
−14
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
3fa0f22
Update tokenize_and_classify.py
folivoramanh 28e5af5
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 1d52681
update pnc test
folivoramanh abd8bc1
update jenkins date pt
folivoramanh b25bf2a
add post process for pt and and config name eval for ja
folivoramanh 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
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 |
|---|---|---|
|
|
@@ -47,6 +47,8 @@ | |
| "ADDRESS", | ||
| "ROMAN", | ||
| "RANGE", | ||
| "SERIAL", | ||
| "WHITELIST", | ||
| ] | ||
|
|
||
|
|
||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
78 changes: 78 additions & 0 deletions
78
nemo_text_processing/text_normalization/pt/verbalizers/post_processing.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,78 @@ | ||
| # 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.pt.graph_utils import NEMO_SIGMA, NEMO_SPACE, generator_main | ||
| from nemo_text_processing.utils.logging import logger | ||
|
|
||
|
|
||
| class PostProcessingFst: | ||
| """ | ||
| Finite state transducer that post-processes an entire Portuguese sentence after verbalization is complete, e.g. | ||
| removes extra spaces around punctuation marks " ( cento e vinte e três ) " -> "(cento e vinte e três)" | ||
|
|
||
| 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, "pt_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 for Portuguese, removing the spaces | ||
| that the tokenizer/verbalizer unconditionally inserts between tokens. | ||
| """ | ||
| no_space_before_punct = [",", ".", "!", "?", ":", ";", ")", r"\]", "}"] | ||
| no_space_after_punct = ["(", r"\[", "{"] | ||
|
Member
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. ditto |
||
|
|
||
| no_space_before_punct_fst = pynini.union(*no_space_before_punct) | ||
| no_space_after_punct_fst = pynini.union(*no_space_after_punct) | ||
|
|
||
| delete_space = pynutil.delete(NEMO_SPACE) | ||
|
|
||
| # "word ," -> "word," | ||
| remove_space_before = pynini.cdrewrite( | ||
| delete_space + no_space_before_punct_fst, | ||
| "", | ||
| "", | ||
| NEMO_SIGMA, | ||
| ).optimize() | ||
|
|
||
| # "( word" -> "(word" | ||
| remove_space_after = pynini.cdrewrite( | ||
| no_space_after_punct_fst + delete_space, | ||
| "", | ||
| "", | ||
| NEMO_SIGMA, | ||
| ).optimize() | ||
|
|
||
| graph = pynini.compose(remove_space_before, remove_space_after) | ||
| return 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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', 'ko', 'vi', 'pt', 'ja'], | ||
|
Member
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. since we have this open, mind just alphabetizing this list? |
||
| default="en", | ||
| type=str, | ||
| ) | ||
|
|
||
20 changes: 20 additions & 0 deletions
20
tests/nemo_text_processing/pt/data_text_normalization/test_cases_punctuation.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,20 @@ | ||
| A versão beta sai às 17:00; a compilação 42 está pronta.~A versão beta sai às dezessete horas; a compilação quarenta e dois está pronta. | ||
| Ela terminou em 1.º, depois em 23.º e por fim em 100.º.~Ela terminou em primeiro, depois em vigésimo terceiro e por fim em centésimo. | ||
| O preço é R$ 4,99 e o outro valor é € 1.234,78.~O preço é quatro reais e noventa e nove centavos e o outro valor é euro um ponto dois três quatro vírgula sete oito. | ||
| A reunião começa às 14:00 e termina às 23:59.~A reunião começa às catorze horas e termina às vinte e três horas e cinquenta e nove. | ||
| O resultado foi 95% e depois 3,5%.~O resultado foi noventa e cinco por cento e depois três vírgula cinco por cento. | ||
| 301(a)~trezentos e um (a) | ||
| 42(b)(1)~quarenta e dois (b)(um) | ||
| (42)~(quarenta e dois) | ||
| 42;~quarenta e dois; | ||
| 42,,99~quarenta e dois,, noventa e nove | ||
| 42...~quarenta e dois... | ||
| 42!!~quarenta e dois!! | ||
| R$4,99.~quatro reais e noventa e nove centavos. | ||
| A consulta é em 20/05/2023 e 31/12/2025.~A consulta é em vinte de maio de dois mil e vinte e três e trinta e um de dezembro de dois mil e vinte e cinco. | ||
| O evento começa em 01/01/2025 às 14:30.~O evento começa em primeiro de janeiro de dois mil e vinte e cinco às catorze horas e trinta. | ||
| O valor de pi é 3,14159.~O valor de pi é três vírgula um quatro um cinco nove. | ||
| Instale v1.5.3 depois da versão 2.0.~Instale v um ponto cinco ponto três depois da versão dois ponto zero. | ||
| O número romano MCMXC representa 1990.~O número romano MCMXC representa mil novecentos e noventa. | ||
| Ligue para +55 11 91234-5678.~Ligue para mais cinco cinco um um nove um dois três quatro cinco seis sete oito. | ||
| Consulte § 301(a) e 42 U.S.C. § 1983.~Consulte parágrafo trezentos e um (a) e quarenta e dois USC parágrafo mil novecentos e oitenta e três. |
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,31 @@ | ||
| # Copyright (c) 2026, NVIDIA CORPORATION & AFFILIATES. All rights reserved. | ||
| # | ||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| # you may not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
|
|
||
| import pytest | ||
| from parameterized import parameterized | ||
|
|
||
| from nemo_text_processing.text_normalization.normalize import Normalizer | ||
|
|
||
| from ..utils import CACHE_DIR, parse_test_case_file | ||
|
|
||
|
|
||
| class TestPunctuation: | ||
| normalizer = Normalizer(lang='pt', cache_dir=CACHE_DIR, overwrite_cache=False, input_case='cased') | ||
|
|
||
| @parameterized.expand(parse_test_case_file('pt/data_text_normalization/test_cases_punctuation.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 == 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
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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Hoist into global variable in script so not generated every call. (I know it's stylistic but these are serving as globals anyhow)