|
| 1 | +# Copyright (c) 2022, NVIDIA CORPORATION. All rights reserved. |
| 2 | +# Copyright (c) 2024, WENET COMMUNITY. Xingchen Song (sxc19@tsinghua.org.cn). |
| 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 pynini |
| 17 | +from pynini.lib import pynutil |
| 18 | + |
| 19 | +from tn.processor import Processor |
| 20 | +from tn.english.rules.cardinal import Cardinal |
| 21 | +from tn.english.rules.time import Time |
| 22 | +from tn.english.rules.date import Date |
| 23 | + |
| 24 | + |
| 25 | +class Range(Processor): |
| 26 | + |
| 27 | + def __init__(self, deterministic: bool = False): |
| 28 | + """ |
| 29 | + Args: |
| 30 | + deterministic: if True will provide a single transduction option, |
| 31 | + for False multiple transduction are generated (used for audio-based normalization) |
| 32 | + """ |
| 33 | + super().__init__('range', ordertype="en_tn") |
| 34 | + self.deterministic = deterministic |
| 35 | + self.build_tagger() |
| 36 | + self.build_verbalizer() |
| 37 | + |
| 38 | + def build_tagger(self): |
| 39 | + """ |
| 40 | + Finite state transducer for verbalizing range, e.g. |
| 41 | + 2-3 => range { value "two to three" } |
| 42 | + """ |
| 43 | + cardinal = Cardinal(deterministic=True).graph_with_and |
| 44 | + time = Time(deterministic=self.deterministic) |
| 45 | + time = time.tagger @ time.verbalizer |
| 46 | + date = Date(deterministic=self.deterministic) |
| 47 | + date = date.tagger @ date.verbalizer |
| 48 | + delete_space = pynini.closure(pynutil.delete(" "), 0, 1) |
| 49 | + |
| 50 | + approx = pynini.cross("~", "approximately") |
| 51 | + |
| 52 | + # TIME |
| 53 | + time_graph = time + delete_space + pynini.cross( |
| 54 | + "-", " to ") + delete_space + time |
| 55 | + self.graph = time_graph | (approx + time) |
| 56 | + |
| 57 | + # YEAR |
| 58 | + date_year_four_digit = (self.DIGIT**4 + |
| 59 | + pynini.closure(pynini.accep("s"), 0, 1)) @ date |
| 60 | + date_year_two_digit = (self.DIGIT**2 + |
| 61 | + pynini.closure(pynini.accep("s"), 0, 1)) @ date |
| 62 | + year_to_year_graph = (date_year_four_digit + delete_space + |
| 63 | + pynini.cross("-", " to ") + delete_space + |
| 64 | + (date_year_four_digit | date_year_two_digit | |
| 65 | + (self.DIGIT**2 @ cardinal))) |
| 66 | + mid_year_graph = pynini.accep("mid") + pynini.cross( |
| 67 | + "-", " ") + (date_year_four_digit | date_year_two_digit) |
| 68 | + |
| 69 | + self.graph |= year_to_year_graph |
| 70 | + self.graph |= mid_year_graph |
| 71 | + |
| 72 | + # ADDITION |
| 73 | + range_graph = cardinal + pynini.closure( |
| 74 | + pynini.cross("+", " plus ") + cardinal, 1) |
| 75 | + range_graph |= cardinal + pynini.closure( |
| 76 | + pynini.cross(" + ", " plus ") + cardinal, 1) |
| 77 | + range_graph |= approx + cardinal |
| 78 | + range_graph |= cardinal + (pynini.cross("...", " ... ") |
| 79 | + | pynini.accep(" ... ")) + cardinal |
| 80 | + |
| 81 | + if not self.deterministic: |
| 82 | + # cardinal ---- |
| 83 | + cardinal_to_cardinal_graph = ( |
| 84 | + cardinal + delete_space + |
| 85 | + pynini.cross("-", pynini.union(" to ", " minus ")) + |
| 86 | + delete_space + cardinal) |
| 87 | + |
| 88 | + range_graph |= cardinal_to_cardinal_graph | ( |
| 89 | + cardinal + delete_space + pynini.cross(":", " to ") + |
| 90 | + delete_space + cardinal) |
| 91 | + |
| 92 | + # MULTIPLY |
| 93 | + for x in [" x ", "x"]: |
| 94 | + range_graph |= cardinal + pynini.cross( |
| 95 | + x, pynini.union(" by ", " times ")) + cardinal |
| 96 | + |
| 97 | + # 40x -> "40 times" ("40 x" cases is covered in serial) |
| 98 | + for x in [" x", "x"]: |
| 99 | + range_graph |= cardinal + pynini.cross(x, " times") |
| 100 | + |
| 101 | + # 5x to 7x-> five to seven x/times |
| 102 | + range_graph |= (cardinal + pynutil.delete(x) + |
| 103 | + pynini.union(" to ", "-", " - ") + cardinal + |
| 104 | + pynini.cross(x, pynini.union(" x", " times"))) |
| 105 | + |
| 106 | + for x in ["*", " * "]: |
| 107 | + range_graph |= cardinal + pynini.closure( |
| 108 | + pynini.cross(x, " times ") + cardinal, 1) |
| 109 | + |
| 110 | + # supports "No. 12" -> "Number 12" |
| 111 | + range_graph |= ((pynini.cross(pynini.union("NO", "No"), "Number") |
| 112 | + | pynini.cross("no", "number")) + |
| 113 | + pynini.closure(pynini.union(". ", " "), 0, 1) + |
| 114 | + cardinal) |
| 115 | + |
| 116 | + for x in ["/", " / "]: |
| 117 | + range_graph |= cardinal + pynini.closure( |
| 118 | + pynini.cross(x, " divided by ") + cardinal, 1) |
| 119 | + |
| 120 | + # 10% to 20% -> ten to twenty percent |
| 121 | + range_graph |= ( |
| 122 | + cardinal + pynini.closure( # noqa |
| 123 | + pynini.cross("%", " percent") | pynutil.delete("%"), 0, 1) |
| 124 | + + # noqa |
| 125 | + pynini.union(" to ", "-", " - ") + cardinal + # noqa |
| 126 | + pynini.cross("%", " percent")) # noqa |
| 127 | + |
| 128 | + self.graph |= range_graph |
| 129 | + |
| 130 | + final_graph = pynutil.insert( |
| 131 | + "value: \"") + self.graph + pynutil.insert("\"") |
| 132 | + self.tagger = self.add_tokens(final_graph) |
0 commit comments