From 6192b47a86e49aff33bd31fc8c19c02bcb7cdf23 Mon Sep 17 00:00:00 2001 From: pengzhendong <275331498@qq.com> Date: Thu, 11 Jun 2026 14:15:37 +0800 Subject: [PATCH] fix: preserve spaces as token boundaries in English TN tagger MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The English TN normalizer previously deleted all input spaces before tagging (delete(" ").star + tagger.star), which destroyed token boundaries and caused "4x faster" to be misparsed as a single measure token producing "four times degrees Fahrenheit aster" (wenet-e2e/wetext#15). Refactor the tagger composition to preserve spaces as token boundaries (NeMo-style), using closure(punct) + classify + closure(punct) as token units with delete(SPACE) | punct as inter-token separators. Also remove single-letter f from unit_alternatives.tsv since 4°F is the correct Fahrenheit input format, not 4f. Lower range tagger weight to 1.0 so "4x" is matched as range ("four times") rather than serial ("four x"), and fraction to 0.99 to preserve "3/4" as "three quarters". --- tn/english/data/measure/unit_alternatives.tsv | 1 - tn/english/normalizer.py | 47 +++++++++++-------- tn/english/test/data/normalizer.txt | 2 +- 3 files changed, 28 insertions(+), 22 deletions(-) diff --git a/tn/english/data/measure/unit_alternatives.tsv b/tn/english/data/measure/unit_alternatives.tsv index 77e4057a..a28c956b 100644 --- a/tn/english/data/measure/unit_alternatives.tsv +++ b/tn/english/data/measure/unit_alternatives.tsv @@ -3,7 +3,6 @@ bq becquerel cd candela da dalton eb exabyte -f degree Fahrenheit gb gigabyte g gram gl gigaliter diff --git a/tn/english/normalizer.py b/tn/english/normalizer.py index 2d0d313c..eecd0543 100644 --- a/tn/english/normalizer.py +++ b/tn/english/normalizer.py @@ -13,8 +13,9 @@ # See the License for the specific language governing permissions and # limitations under the License. +import pynini from importlib_resources import files -from pynini.lib.pynutil import add_weight, delete +from pynini.lib import pynutil from tn.english.rules.cardinal import Cardinal from tn.english.rules.date import Date @@ -59,24 +60,30 @@ def build_tagger_and_verbalizer(self): whitelist = WhiteList() rang = Range(date=date, time=time) - tagger = ( - add_weight(cardinal.tagger, 1.0) - | add_weight(ordinal.tagger, 1.0) - | add_weight(word.tagger, 100) - | add_weight(date.tagger, 0.99) - | add_weight(decimal.tagger, 1.0) - | add_weight(fraction.tagger, 1.0) - | add_weight(time.tagger, 1.00) - | add_weight(measure.tagger, 1.00) - | add_weight(money.tagger, 1.00) - | add_weight(telephone.tagger, 1.00) - | add_weight(electronic.tagger, 1.00) - | add_weight(serial.tagger, 1.01) - | add_weight(whitelist.tagger, 1.00) - | add_weight(rang.tagger, 1.01) - | add_weight(punctuation.tagger, 2.00) - ).optimize() + (add_weight(punctuation.tagger, 2.00).plus | self.DELETE_SPACE) - self.tagger = (delete(" ").star + tagger.star) @ self.build_rule(delete(" "), r="[EOS]") + classify = ( + pynutil.add_weight(cardinal.tagger, 1.0) + | pynutil.add_weight(ordinal.tagger, 1.0) + | pynutil.add_weight(word.tagger, 100) + | pynutil.add_weight(date.tagger, 0.99) + | pynutil.add_weight(decimal.tagger, 1.0) + | pynutil.add_weight(fraction.tagger, 0.99) + | pynutil.add_weight(time.tagger, 1.00) + | pynutil.add_weight(measure.tagger, 1.00) + | pynutil.add_weight(money.tagger, 1.00) + | pynutil.add_weight(telephone.tagger, 1.00) + | pynutil.add_weight(electronic.tagger, 1.00) + | pynutil.add_weight(serial.tagger, 1.01) + | pynutil.add_weight(whitelist.tagger, 1.00) + | pynutil.add_weight(rang.tagger, 1.0) + ).optimize() + + punct = pynutil.add_weight(punctuation.tagger, 2.00) + token = pynini.closure(punct) + classify + pynini.closure(punct) + separator = pynutil.delete(self.SPACE) | punct + graph = ( + self.DELETE_SPACE + token + pynini.closure(separator + token) + self.DELETE_SPACE + ) | punct + self.tagger = graph.optimize() @ self.build_rule(pynutil.delete(" "), r="[EOS]") classify = ( cardinal.verbalizer @@ -103,4 +110,4 @@ def build_tagger_and_verbalizer(self): classify + (punct.plus | self.INSERT_SPACE) | punct + (punct.plus | self.DELETE_SPACE) ).star - self.verbalizer = verbalizer @ self.build_rule(delete(" "), r="[EOS]") + self.verbalizer = verbalizer @ self.build_rule(pynutil.delete(" "), r="[EOS]") diff --git a/tn/english/test/data/normalizer.txt b/tn/english/test/data/normalizer.txt index 2deb920d..c3bff093 100644 --- a/tn/english/test/data/normalizer.txt +++ b/tn/english/test/data/normalizer.txt @@ -1,5 +1,5 @@ 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". +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 hundred 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 to 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. Try searching for 'Toyota' or 'Investment' => Try searching for 'Toyota' or 'Investment'