From d96d2b2ad3ef60ce26d6dd43ba9de15ec5b0f14f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Xingchen=20Song=28=E5=AE=8B=E6=98=9F=E8=BE=B0=29?= Date: Wed, 5 Jun 2024 17:21:44 +0800 Subject: [PATCH] [tn] english tn, support range (#233) * [tn] english tn, support range * [tn] english tn, support range * [tn] english tn, support range --- tn/english/normalizer.py | 9 +- tn/english/rules/range.py | 132 ++++++++++++++++++++++++++++ tn/english/test/data/normalizer.txt | 1 + tn/english/test/data/range.txt | 1 + tn/english/test/range_test.py | 28 ++++++ tn/processor.py | 2 +- 6 files changed, 170 insertions(+), 3 deletions(-) create mode 100644 tn/english/rules/range.py create mode 100644 tn/english/test/data/range.txt create mode 100644 tn/english/test/range_test.py diff --git a/tn/english/normalizer.py b/tn/english/normalizer.py index 4fbf6e2..62e2ecb 100644 --- a/tn/english/normalizer.py +++ b/tn/english/normalizer.py @@ -27,6 +27,7 @@ from tn.english.rules.electronic import Electronic from tn.english.rules.whitelist import WhiteList from tn.english.rules.punctuation import Punctuation +from tn.english.rules.range import Range from pynini.lib.pynutil import add_weight, delete from importlib_resources import files @@ -54,6 +55,7 @@ def build_tagger(self): word = add_weight(Word().tagger, 100) whitelist = add_weight(WhiteList().tagger, 1.00) punct = add_weight(Punctuation().tagger, 2.00) + rang = add_weight(Range().tagger, 1.01) # TODO(xcsong): add roman tagger = punct.star + \ (cardinal | ordinal | word @@ -61,7 +63,8 @@ def build_tagger(self): | time | measure | money | telephone | electronic | whitelist - | punct).optimize() + (punct.plus | self.DELETE_SPACE) + | punct + | rang).optimize() + (punct.plus | self.DELETE_SPACE) # delete the last space self.tagger = tagger.star @ self.build_rule(delete(' '), r='[EOS]') @@ -79,6 +82,7 @@ def build_verbalizer(self): electronic = Electronic().verbalizer whitelist = WhiteList().verbalizer punct = Punctuation().verbalizer + rang = Range().verbalizer verbalizer = \ (cardinal | ordinal | word | date | decimal @@ -87,6 +91,7 @@ def build_verbalizer(self): | telephone | electronic | whitelist - | punct).optimize() + punct.ques + self.INSERT_SPACE + | punct + | rang).optimize() + punct.ques + self.INSERT_SPACE self.verbalizer = verbalizer.star @ self.build_rule(delete(' '), r='[EOS]') diff --git a/tn/english/rules/range.py b/tn/english/rules/range.py new file mode 100644 index 0000000..90d5d3e --- /dev/null +++ b/tn/english/rules/range.py @@ -0,0 +1,132 @@ +# Copyright (c) 2022, NVIDIA CORPORATION. All rights reserved. +# Copyright (c) 2024, WENET COMMUNITY. Xingchen Song (sxc19@tsinghua.org.cn). +# +# 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 tn.processor import Processor +from tn.english.rules.cardinal import Cardinal +from tn.english.rules.time import Time +from tn.english.rules.date import Date + + +class Range(Processor): + + def __init__(self, deterministic: bool = False): + """ + Args: + deterministic: if True will provide a single transduction option, + for False multiple transduction are generated (used for audio-based normalization) + """ + super().__init__('range', ordertype="en_tn") + self.deterministic = deterministic + self.build_tagger() + self.build_verbalizer() + + def build_tagger(self): + """ + Finite state transducer for verbalizing range, e.g. + 2-3 => range { value "two to three" } + """ + cardinal = Cardinal(deterministic=True).graph_with_and + time = Time(deterministic=self.deterministic) + time = time.tagger @ time.verbalizer + date = Date(deterministic=self.deterministic) + date = date.tagger @ date.verbalizer + delete_space = pynini.closure(pynutil.delete(" "), 0, 1) + + approx = pynini.cross("~", "approximately") + + # TIME + time_graph = time + delete_space + pynini.cross( + "-", " to ") + delete_space + time + self.graph = time_graph | (approx + time) + + # YEAR + date_year_four_digit = (self.DIGIT**4 + + pynini.closure(pynini.accep("s"), 0, 1)) @ date + date_year_two_digit = (self.DIGIT**2 + + pynini.closure(pynini.accep("s"), 0, 1)) @ date + year_to_year_graph = (date_year_four_digit + delete_space + + pynini.cross("-", " to ") + delete_space + + (date_year_four_digit | date_year_two_digit | + (self.DIGIT**2 @ cardinal))) + mid_year_graph = pynini.accep("mid") + pynini.cross( + "-", " ") + (date_year_four_digit | date_year_two_digit) + + self.graph |= year_to_year_graph + self.graph |= mid_year_graph + + # ADDITION + range_graph = cardinal + pynini.closure( + pynini.cross("+", " plus ") + cardinal, 1) + range_graph |= cardinal + pynini.closure( + pynini.cross(" + ", " plus ") + cardinal, 1) + range_graph |= approx + cardinal + range_graph |= cardinal + (pynini.cross("...", " ... ") + | pynini.accep(" ... ")) + cardinal + + if not self.deterministic: + # cardinal ---- + cardinal_to_cardinal_graph = ( + cardinal + delete_space + + pynini.cross("-", pynini.union(" to ", " minus ")) + + delete_space + cardinal) + + range_graph |= cardinal_to_cardinal_graph | ( + cardinal + delete_space + pynini.cross(":", " to ") + + delete_space + cardinal) + + # MULTIPLY + for x in [" x ", "x"]: + range_graph |= cardinal + pynini.cross( + x, pynini.union(" by ", " times ")) + cardinal + + # 40x -> "40 times" ("40 x" cases is covered in serial) + for x in [" x", "x"]: + range_graph |= cardinal + pynini.cross(x, " times") + + # 5x to 7x-> five to seven x/times + range_graph |= (cardinal + pynutil.delete(x) + + pynini.union(" to ", "-", " - ") + cardinal + + pynini.cross(x, pynini.union(" x", " times"))) + + for x in ["*", " * "]: + range_graph |= cardinal + pynini.closure( + pynini.cross(x, " times ") + cardinal, 1) + + # supports "No. 12" -> "Number 12" + range_graph |= ((pynini.cross(pynini.union("NO", "No"), "Number") + | pynini.cross("no", "number")) + + pynini.closure(pynini.union(". ", " "), 0, 1) + + cardinal) + + for x in ["/", " / "]: + range_graph |= cardinal + pynini.closure( + pynini.cross(x, " divided by ") + cardinal, 1) + + # 10% to 20% -> ten to twenty percent + range_graph |= ( + cardinal + pynini.closure( # noqa + pynini.cross("%", " percent") | pynutil.delete("%"), 0, 1) + + # noqa + pynini.union(" to ", "-", " - ") + cardinal + # noqa + pynini.cross("%", " percent")) # noqa + + self.graph |= range_graph + + final_graph = pynutil.insert( + "value: \"") + self.graph + pynutil.insert("\"") + self.tagger = self.add_tokens(final_graph) diff --git a/tn/english/test/data/normalizer.txt b/tn/english/test/data/normalizer.txt index aafff26..1cc460c 100644 --- a/tn/english/test/data/normalizer.txt +++ b/tn/english/test/data/normalizer.txt @@ -1,3 +1,4 @@ this is 12th game, number 256, 2024-05-06, 2021-03-07 31.990 billion. ¾ people like chattts, let's eat at 03:43 p.m. run 10 km, give me $12.345 please, call 123-123-5678-1 Mt Hill "HAHAHA" billion 4 March => this is twelfth game, number two hundred and fifty six, the sixth of may twenty twenty four, the seventh of march twenty twenty one thirty one point nine nine oh billion. three quarters people like chattts, let' s eat at three forty three PM run ten kilometers, give me twelve point three four five dollars please, call one two three, one two three, five six seven eight, one Mt Hill" HAHAHA" billion the fourth of march The National Map, accessed April 1, 2011" Site Description of Koppers Co. From the quartet's recording" Jefferson Friedman: Quartets,"" String Quartet no, Riots again broke out, Atassi resigned, and Syrian independence was deferred until after World War II. 1988 (1988) ( 1988) ( 1988). Starling, Arthur E.( 1988 ). this is 12th game, number 256, 2024-05-06, 2021-03-07 31.990 billion. 3/4 people like chattts Retrieved December 2011. Information on Album" Thepodule.com"" Biography by Amy Hanson". => The National Map, accessed the first of april , twenty eleven" Site Description of Koppers company From the quartet' s recording" Jefferson Friedman: Quartets,"" String Quartet no, Riots again broke out, Atassi resigned, and Syrian independence was deferred until after World War two nineteen eighty eight( nineteen eighty eight )( nineteen eighty eight )( nineteen eighty eight). Starling, Arthur E.( nineteen eighty eight). this is twelfth game, number two fifty six, the sixth of may twenty twenty four, the seventh of march twenty twenty one thirty one point nine nine oh billion. three quarters people like chattts Retrieved december twenty eleven. Information on Album" Thepodule dot com"" Biography by Amy Hanson". .345" and ".456" "9.456" or 6.7890" => point three four five" and". four hundred and fifty six" " nine point four five six" or six point seven eight nine oh" +The museum is open Mon.-Sun. children of 3-4 years 123 The plan will help you lose 3-4 pounds the first week, and 1-2 pounds the weeks thereafter. => The museum is open Monday.- Sunday. children of three to four years one hundred and twenty three The plan will help you lose three to four pounds the first week, and one to two pounds the weeks thereafter. diff --git a/tn/english/test/data/range.txt b/tn/english/test/data/range.txt new file mode 100644 index 0000000..a4ce8ab --- /dev/null +++ b/tn/english/test/data/range.txt @@ -0,0 +1 @@ +2-3 => two to three diff --git a/tn/english/test/range_test.py b/tn/english/test/range_test.py new file mode 100644 index 0000000..df08e5f --- /dev/null +++ b/tn/english/test/range_test.py @@ -0,0 +1,28 @@ +# Copyright (c) 2024 Xingchen Song (sxc19@tsinghua.org.cn) +# +# 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 tn.english.rules.range import Range +from tn.english.test.utils import parse_test_case + + +class TestRange: + + range = Range(deterministic=False) + range_cases = parse_test_case('data/range.txt') + + @pytest.mark.parametrize("written, spoken", range_cases) + def test_range(self, written, spoken): + assert self.range.normalize(written) == spoken diff --git a/tn/processor.py b/tn/processor.py index 9145638..0505f37 100644 --- a/tn/processor.py +++ b/tn/processor.py @@ -101,7 +101,7 @@ def build_fst(self, prefix, cache_dir, overwrite_cache): self.verbalizer.optimize().write(verbalizer_path) logging.info("done") logging.info("fst path: {}".format(tagger_path)) - logging.info(" {}".format(tagger_path)) + logging.info(" {}".format(verbalizer_path)) def tag(self, input): if len(input) == 0: