|
| 1 | +# Copyright (c) 2025, NVIDIA CORPORATION. All rights reserved. |
| 2 | +# Copyright 2015 and onwards Google, Inc. |
| 3 | +# |
| 4 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +# you may not use this file except in compliance with the License. |
| 6 | +# You may obtain a copy of the License at |
| 7 | +# |
| 8 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +# |
| 10 | +# Unless required by applicable law or agreed to in writing, software |
| 11 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +# See the License for the specific language governing permissions and |
| 14 | +# limitations under the License. |
| 15 | + |
| 16 | +import os |
| 17 | +import string |
| 18 | +from pathlib import Path |
| 19 | +from typing import Dict |
| 20 | + |
| 21 | +import pynini |
| 22 | +from pynini import Far |
| 23 | +from pynini.export import export |
| 24 | +from pynini.lib import byte, pynutil, utf8 |
| 25 | + |
| 26 | +from nemo_text_processing.text_normalization.en.utils import load_labels |
| 27 | +from nemo_text_processing.utils.logging import logger |
| 28 | + |
| 29 | +NEMO_CHAR = utf8.VALID_UTF8_CHAR |
| 30 | + |
| 31 | +NEMO_DIGIT = byte.DIGIT |
| 32 | +NEMO_ALPHA = pynini.union(*[chr(i) for i in range(ord('가'), ord('힣') + 1)]).optimize() |
| 33 | +NEMO_ALNUM = pynini.union(NEMO_DIGIT, NEMO_ALPHA).optimize() |
| 34 | +NEMO_HEX = pynini.union(*string.hexdigits).optimize() |
| 35 | +NEMO_SPACE = " " |
| 36 | +NEMO_WHITE_SPACE = pynini.union(" ", "\t", "\n", "\r", "\u00a0").optimize() |
| 37 | +NEMO_NOT_SPACE = pynini.difference(NEMO_CHAR, NEMO_WHITE_SPACE).optimize() |
| 38 | +NEMO_NOT_QUOTE = pynini.difference(NEMO_CHAR, r'"').optimize() |
| 39 | + |
| 40 | +NEMO_PUNCT = pynini.union(*map(pynini.escape, string.punctuation)).optimize() |
| 41 | +NEMO_GRAPH = pynini.union(NEMO_ALNUM, NEMO_PUNCT).optimize() |
| 42 | + |
| 43 | +NEMO_SIGMA = pynini.closure(NEMO_CHAR) |
| 44 | + |
| 45 | +delete_space = pynutil.delete(pynini.closure(NEMO_WHITE_SPACE)) |
| 46 | +delete_zero_or_one_space = pynutil.delete(pynini.closure(NEMO_WHITE_SPACE, 0, 1)) |
| 47 | +insert_space = pynutil.insert(" ") |
| 48 | +delete_extra_space = pynini.cross(pynini.closure(NEMO_WHITE_SPACE, 1), " ") |
| 49 | +delete_preserve_order = pynini.closure( |
| 50 | + pynutil.delete(" preserve_order: true") |
| 51 | + | (pynutil.delete(' field_order: "') + NEMO_NOT_QUOTE + pynutil.delete('"')) |
| 52 | +) |
| 53 | + |
| 54 | + |
| 55 | +# Common string literals; expand as you see fit. |
| 56 | +username_string = "username" |
| 57 | +double_quotes = '"' |
| 58 | +domain_string = "domain" |
| 59 | +protocol_string = "protocol" |
| 60 | +slash = "/" |
| 61 | +double_slash = "//" |
| 62 | +triple_slash = "///" |
| 63 | +file = "file" |
| 64 | +period = "." |
| 65 | +at = "@" |
| 66 | +colon = ":" |
| 67 | +https = "https" |
| 68 | +http = "http" |
| 69 | +www = "www" |
| 70 | + |
| 71 | + |
| 72 | +def generator_main(file_name: str, graphs: Dict[str, "pynini.FstLike"]): |
| 73 | + """ |
| 74 | + Exports graph as OpenFst finite state archive (FAR) file with given file name and rule name. |
| 75 | +
|
| 76 | + Args: |
| 77 | + file_name: exported file name |
| 78 | + graphs: Mapping of a rule name and Pynini WFST graph to be exported |
| 79 | + """ |
| 80 | + exporter = export.Exporter(file_name) |
| 81 | + for rule, graph in graphs.items(): |
| 82 | + exporter[rule] = graph.optimize() |
| 83 | + exporter.close() |
| 84 | + logger.info(f"Created {file_name}") |
| 85 | + |
| 86 | + |
| 87 | +def convert_space(fst) -> "pynini.FstLike": |
| 88 | + """ |
| 89 | + Converts space to nonbreaking space. |
| 90 | + Used only in tagger grammars for transducing token values within quotes, e.g. name: "hello kitty" |
| 91 | + This is making transducer significantly slower, so only use when there could be potential spaces within quotes, otherwise leave it. |
| 92 | +
|
| 93 | + Args: |
| 94 | + fst: input fst |
| 95 | +
|
| 96 | + Returns output fst where breaking spaces are converted to non breaking spaces |
| 97 | + """ |
| 98 | + return fst @ pynini.cdrewrite(pynini.cross(NEMO_SPACE, "\u00a0"), "", "", NEMO_SIGMA) |
| 99 | + |
| 100 | + |
| 101 | +def string_map_cased(input_file: str, input_case: str = "lower_cased"): |
| 102 | + labels = load_labels(input_file) |
| 103 | + whitelist = pynini.string_map(labels).invert().optimize() |
| 104 | + return whitelist |
| 105 | + |
| 106 | + |
| 107 | +class GraphFst: |
| 108 | + """ |
| 109 | + Base class for all grammar fsts. |
| 110 | +
|
| 111 | + Args: |
| 112 | + name: name of grammar class |
| 113 | + kind: either 'classify' or 'verbalize' |
| 114 | + deterministic: if True will provide a single transduction option, |
| 115 | + for False multiple transduction are generated (used for audio-based normalization) |
| 116 | + """ |
| 117 | + |
| 118 | + def __init__(self, name: str, kind: str, deterministic: bool = True): |
| 119 | + self.name = name |
| 120 | + self.kind = kind |
| 121 | + self._fst = None |
| 122 | + self.deterministic = deterministic |
| 123 | + |
| 124 | + self.far_path = Path(os.path.dirname(__file__) + "/grammars/" + kind + "/" + name + ".far") |
| 125 | + if self.far_exist(): |
| 126 | + self._fst = Far(self.far_path, mode="r", arc_type="standard", far_type="default").get_fst() |
| 127 | + |
| 128 | + def far_exist(self) -> bool: |
| 129 | + """ |
| 130 | + Returns true if FAR can be loaded |
| 131 | + """ |
| 132 | + return self.far_path.exists() |
| 133 | + |
| 134 | + @property |
| 135 | + def fst(self) -> "pynini.FstLike": |
| 136 | + return self._fst |
| 137 | + |
| 138 | + @fst.setter |
| 139 | + def fst(self, fst): |
| 140 | + self._fst = fst |
| 141 | + |
| 142 | + def add_tokens(self, fst) -> "pynini.FstLike": |
| 143 | + """ |
| 144 | + Wraps class name around to given fst |
| 145 | +
|
| 146 | + Args: |
| 147 | + fst: input fst |
| 148 | +
|
| 149 | + Returns: |
| 150 | + Fst: fst |
| 151 | + """ |
| 152 | + return pynutil.insert(f"{self.name} {{ ") + fst + pynutil.insert(" }") |
| 153 | + |
| 154 | + def delete_tokens(self, fst) -> "pynini.FstLike": |
| 155 | + """ |
| 156 | + Deletes class name wrap around output of given fst |
| 157 | +
|
| 158 | + Args: |
| 159 | + fst: input fst |
| 160 | +
|
| 161 | + Returns: |
| 162 | + Fst: fst |
| 163 | + """ |
| 164 | + res = ( |
| 165 | + pynutil.delete(f"{self.name}") |
| 166 | + + delete_space |
| 167 | + + pynutil.delete("{") |
| 168 | + + delete_space |
| 169 | + + fst |
| 170 | + + delete_space |
| 171 | + + pynutil.delete("}") |
| 172 | + ) |
| 173 | + return res @ pynini.cdrewrite(pynini.cross("\u00a0", " "), "", "", NEMO_SIGMA) |
0 commit comments