-
Notifications
You must be signed in to change notification settings - Fork 0
feat(ta): add cardinal ITN tagger, verbalizer and test cases #2
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| 1 ஒன்று | ||
| 2 இரண்டு | ||
| 3 மூன்று | ||
| 4 நான்கு | ||
| 5 ஐந்து | ||
| 6 ஆறு | ||
| 7 ஏழு | ||
| 8 எட்டு | ||
| 9 ஒன்பது | ||
|
|
|
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. Approved |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| 10 பத்து | ||
| 11 பதினொன்று | ||
| 12 பன்னிரண்டு | ||
| 13 பதின்மூன்று | ||
| 14 பதினான்கு | ||
| 15 பதினைந்து | ||
| 16 பதினாறு | ||
| 17 பதினேழு | ||
| 18 பதினெட்டு | ||
| 19 பத்தொன்பது | ||
| 20 இருபது | ||
| 30 முப்பது | ||
| 40 நாற்பது | ||
| 50 ஐம்பது | ||
| 60 அறுபது | ||
| 70 எழுபது | ||
| 80 எண்பது | ||
| 90 தொண்ணூறு | ||
|
|
|
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. Approved |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| 0 சுழியம் | ||
|
|
|
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. Status: Approved. |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,204 @@ | ||
| # Copyright (c) 2024, NVIDIA CORPORATION. All rights reserved. | ||
| # Copyright 2024 and onwards Google, Inc. | ||
| # | ||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| # you may not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
|
|
||
| import logging | ||
| import os | ||
| import string | ||
| from pathlib import Path | ||
| from typing import Dict | ||
|
|
||
| import pynini | ||
| from pynini import Far | ||
| from pynini.examples import plurals | ||
| from pynini.export import export | ||
| from pynini.lib import byte, pynutil, utf8 | ||
|
|
||
| from nemo_text_processing.inverse_text_normalization.hi.utils import get_abs_path, load_labels | ||
|
|
||
| NEMO_CHAR = utf8.VALID_UTF8_CHAR | ||
|
|
||
| graph_digit = pynini.string_file(get_abs_path("data/numbers/digit.tsv")) | ||
|
|
||
| NEMO_HI_DIGIT = pynini.union("०", "१", "२", "३", "४", "५", "६", "७", "८", "९").optimize() | ||
| DEVANAGARI_DIGIT = ["०", "१", "२", "३", "४", "५", "६", "७", "८", "९"] | ||
|
|
||
| NEMO_HEX = pynini.union(*string.hexdigits).optimize() | ||
| NEMO_NON_BREAKING_SPACE = u"\u00a0" | ||
| NEMO_ZWNJ = u"\u200c" | ||
| 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() | ||
|
|
||
| NEMO_PUNCT = pynini.union(*map(pynini.escape, string.punctuation)).optimize() | ||
| NEMO_GRAPH = pynini.union(NEMO_CHAR, NEMO_PUNCT).optimize() | ||
|
|
||
| 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 integer_to_devanagari(n: int) -> str: | ||
| return ''.join(DEVANAGARI_DIGIT[int(d)] for d in str(n)) | ||
|
|
||
|
|
||
| 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) | ||
|
|
||
|
|
||
| def string_map_cased(input_file: str, input_case: str = INPUT_LOWER_CASED): | ||
| labels = load_labels(input_file) | ||
|
|
||
| if input_case == INPUT_CASED: | ||
| additional_labels = [] | ||
| for written, spoken, *weight in labels: | ||
| written_capitalized = written[0].upper() + written[1:] | ||
| additional_labels.extend( | ||
| [ | ||
| [written_capitalized, spoken.capitalize()], # first letter capitalized | ||
| [ | ||
| written_capitalized, | ||
| spoken.upper().replace(" AND ", " and "), | ||
| ], # # add pairs with the all letters capitalized | ||
| ] | ||
| ) | ||
|
|
||
| spoken_no_space = spoken.replace(" ", "") | ||
| # add abbreviations without spaces (both lower and upper case), i.e. "BMW" not "B M W" | ||
| if len(spoken) == (2 * len(spoken_no_space) - 1): | ||
| logging.debug(f"This is weight {weight}") | ||
| if len(weight) == 0: | ||
| additional_labels.extend( | ||
| [[written, spoken_no_space], [written_capitalized, spoken_no_space.upper()]] | ||
| ) | ||
| else: | ||
| additional_labels.extend( | ||
| [ | ||
| [written, spoken_no_space, weight[0]], | ||
| [written_capitalized, spoken_no_space.upper(), weight[0]], | ||
| ] | ||
| ) | ||
| labels += additional_labels | ||
|
|
||
| whitelist = pynini.string_map(labels).invert().optimize() | ||
| return whitelist | ||
|
|
||
|
|
||
| class GraphFst: | ||
| """ | ||
| Base class for all grammar fsts. | ||
|
|
||
| Args: | ||
| name: name of grammar class | ||
| kind: either 'classify' or 'verbalize' | ||
| deterministic: if True will provide a single transduction option, | ||
| for False multiple transduction are generated (used for audio-based normalization) | ||
| """ | ||
|
|
||
| def __init__(self, name: str, kind: str, deterministic: bool = True): | ||
| self.name = name | ||
| self.kind = kind | ||
| self._fst = None | ||
| self.deterministic = deterministic | ||
|
|
||
| self.far_path = Path(os.path.dirname(__file__) + '/grammars/' + kind + '/' + name + '.far') | ||
| if self.far_exist(): | ||
| self._fst = Far(self.far_path, mode="r", arc_type="standard", far_type="default").get_fst() | ||
|
|
||
| def far_exist(self) -> bool: | ||
| """ | ||
| Returns true if FAR can be loaded | ||
| """ | ||
| return self.far_path.exists() | ||
|
|
||
| @property | ||
| def fst(self) -> 'pynini.FstLike': | ||
| return self._fst | ||
|
|
||
| @fst.setter | ||
| def fst(self, fst): | ||
| self._fst = fst | ||
|
|
||
| def add_tokens(self, fst) -> 'pynini.FstLike': | ||
| """ | ||
| Wraps class name around to given fst | ||
|
|
||
| Args: | ||
| fst: input fst | ||
|
|
||
| Returns: | ||
| Fst: fst | ||
| """ | ||
| return pynutil.insert(f"{self.name} {{ ") + fst + pynutil.insert(" }") | ||
|
|
||
| def delete_tokens(self, fst) -> 'pynini.FstLike': | ||
| """ | ||
| Deletes class name wrap around output of given fst | ||
|
|
||
| Args: | ||
| fst: input fst | ||
|
|
||
| Returns: | ||
| Fst: fst | ||
| """ | ||
| res = ( | ||
| pynutil.delete(f"{self.name}") | ||
| + delete_space | ||
| + pynutil.delete("{") | ||
| + delete_space | ||
| + fst | ||
| + delete_space | ||
| + pynutil.delete("}") | ||
| ) | ||
| return res @ pynini.cdrewrite(pynini.cross(u"\u00a0", " "), "", "", NEMO_SIGMA) |
|
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. Status: Approved. Correctly left empty as a package marker, per Section 5. No action required. |
|
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. Status: Approved. Core implementation is correct: .invert() applied to all three sources, the union is well-formed, and the tagging wrapper (integer: "…" + add_tokens) is right. |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| import pynini | ||
| from pynini.lib import pynutil | ||
|
|
||
| from nemo_text_processing.inverse_text_normalization.ta.graph_utils import GraphFst | ||
| from nemo_text_processing.inverse_text_normalization.ta.utils import get_abs_path | ||
|
|
||
|
|
||
| class CardinalFst(GraphFst): | ||
| """ | ||
| Classifies spoken numbers back to digits, e.g. <word> -> cardinal { integer: "5" } | ||
| """ | ||
|
|
||
| def __init__(self): | ||
| super().__init__(name="cardinal", kind="classify") | ||
|
|
||
| graph_digit = pynini.string_file(get_abs_path("data/numbers/digit.tsv")).invert() | ||
| graph_zero = pynini.string_file(get_abs_path("data/numbers/zero.tsv")).invert() | ||
| graph_teens_and_ties = pynini.string_file(get_abs_path("data/numbers/teens_and_ties.tsv")).invert() | ||
|
|
||
| graph = graph_digit | graph_zero | graph_teens_and_ties | ||
| graph = graph.optimize() | ||
|
|
||
| final_graph = pynutil.insert('integer: "') + graph + pynutil.insert('"') | ||
| final_graph = self.add_tokens(final_graph) | ||
| self.fst = final_graph.optimize() |
|
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. Status: Acceptable; one minor cleanup. |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,63 @@ | ||
| # Copyright (c) 2024, 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 csv | ||
| import logging | ||
| import os | ||
| import pynini | ||
|
|
||
|
|
||
| def get_abs_path(rel_path): | ||
| """ | ||
| Get absolute path | ||
|
|
||
| Args: | ||
| rel_path: relative path to this file | ||
|
|
||
| Returns absolute path | ||
| """ | ||
| abs_path = os.path.dirname(os.path.abspath(__file__)) + os.sep + rel_path | ||
|
|
||
| if not os.path.exists(abs_path): | ||
| logging.warning(f'{abs_path} does not exist') | ||
| return abs_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")) | ||
| return labels | ||
|
|
||
|
|
||
| from pynini.lib import pynutil | ||
|
|
||
|
|
||
| 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}'") |
|
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. Status: Approved. Correctly left empty as a package marker, per Section 5. No action required. |
|
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. Status: Approved. -TODO 3 is correct: pynini.closure(NEMO_NOT_QUOTE, 1) correctly captures the digit value between the quotes, and the token-deletion logic is right. |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| import pynini | ||
| from pynini.lib import pynutil | ||
|
|
||
| from nemo_text_processing.inverse_text_normalization.ta.graph_utils import NEMO_NOT_QUOTE, GraphFst, delete_space | ||
|
|
||
|
|
||
| class CardinalFst(GraphFst): | ||
| """ | ||
| Verbalizes the digits, e.g. cardinal { integer: "5" } -> 5 | ||
| """ | ||
|
|
||
| def __init__(self): | ||
| super().__init__(name="cardinal", kind="verbalize") | ||
|
|
||
| graph = ( | ||
| pynutil.delete("integer:") | ||
| + delete_space | ||
| + pynutil.delete('"') | ||
| + pynini.closure(NEMO_NOT_QUOTE, 1) | ||
| + pynutil.delete('"') | ||
| ) | ||
|
|
||
| delete_tokens = self.delete_tokens(graph) | ||
| self.fst = delete_tokens.optimize() |
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.
Approved