Skip to content

Commit 8be2f91

Browse files
committed
fix: preserve spaces as token boundaries in English TN tagger
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.
1 parent cb2fb03 commit 8be2f91

3 files changed

Lines changed: 28 additions & 22 deletions

File tree

tn/english/data/measure/unit_alternatives.tsv

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ bq becquerel
33
cd candela
44
da dalton
55
eb exabyte
6-
f degree Fahrenheit
76
gb gigabyte
87
g gram
98
gl gigaliter

tn/english/normalizer.py

Lines changed: 27 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,9 @@
1313
# See the License for the specific language governing permissions and
1414
# limitations under the License.
1515

16+
import pynini
1617
from importlib_resources import files
17-
from pynini.lib.pynutil import add_weight, delete
18+
from pynini.lib import pynutil
1819

1920
from tn.english.rules.cardinal import Cardinal
2021
from tn.english.rules.date import Date
@@ -59,24 +60,30 @@ def build_tagger_and_verbalizer(self):
5960
whitelist = WhiteList()
6061
rang = Range(date=date, time=time)
6162

62-
tagger = (
63-
add_weight(cardinal.tagger, 1.0)
64-
| add_weight(ordinal.tagger, 1.0)
65-
| add_weight(word.tagger, 100)
66-
| add_weight(date.tagger, 0.99)
67-
| add_weight(decimal.tagger, 1.0)
68-
| add_weight(fraction.tagger, 1.0)
69-
| add_weight(time.tagger, 1.00)
70-
| add_weight(measure.tagger, 1.00)
71-
| add_weight(money.tagger, 1.00)
72-
| add_weight(telephone.tagger, 1.00)
73-
| add_weight(electronic.tagger, 1.00)
74-
| add_weight(serial.tagger, 1.01)
75-
| add_weight(whitelist.tagger, 1.00)
76-
| add_weight(rang.tagger, 1.01)
77-
| add_weight(punctuation.tagger, 2.00)
78-
).optimize() + (add_weight(punctuation.tagger, 2.00).plus | self.DELETE_SPACE)
79-
self.tagger = (delete(" ").star + tagger.star) @ self.build_rule(delete(" "), r="[EOS]")
63+
classify = (
64+
pynutil.add_weight(cardinal.tagger, 1.0)
65+
| pynutil.add_weight(ordinal.tagger, 1.0)
66+
| pynutil.add_weight(word.tagger, 100)
67+
| pynutil.add_weight(date.tagger, 0.99)
68+
| pynutil.add_weight(decimal.tagger, 1.0)
69+
| pynutil.add_weight(fraction.tagger, 1.0)
70+
| pynutil.add_weight(time.tagger, 1.00)
71+
| pynutil.add_weight(measure.tagger, 1.00)
72+
| pynutil.add_weight(money.tagger, 1.00)
73+
| pynutil.add_weight(telephone.tagger, 1.00)
74+
| pynutil.add_weight(electronic.tagger, 1.00)
75+
| pynutil.add_weight(serial.tagger, 1.01)
76+
| pynutil.add_weight(whitelist.tagger, 1.00)
77+
| pynutil.add_weight(rang.tagger, 1.01)
78+
).optimize()
79+
80+
punct = pynutil.add_weight(punctuation.tagger, 2.00)
81+
token = pynini.closure(punct) + classify + pynini.closure(punct)
82+
separator = pynutil.delete(self.SPACE) | punct
83+
graph = (
84+
self.DELETE_SPACE + token + pynini.closure(separator + token) + self.DELETE_SPACE
85+
) | punct
86+
self.tagger = graph.optimize() @ self.build_rule(pynutil.delete(" "), r="[EOS]")
8087

8188
classify = (
8289
cardinal.verbalizer
@@ -103,4 +110,4 @@ def build_tagger_and_verbalizer(self):
103110
classify + (punct.plus | self.INSERT_SPACE)
104111
| punct + (punct.plus | self.DELETE_SPACE)
105112
).star
106-
self.verbalizer = verbalizer @ self.build_rule(delete(" "), r="[EOS]")
113+
self.verbalizer = verbalizer @ self.build_rule(pynutil.delete(" "), r="[EOS]")

tn/english/test/data/normalizer.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
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
2-
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".
2+
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".
33
.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"
44
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.
55
Try searching for 'Toyota' or 'Investment' => Try searching for 'Toyota' or 'Investment'

0 commit comments

Comments
 (0)