Skip to content

Commit 6a6b19f

Browse files
committed
feat: 463/470 (98.5%) NeMo coverage
- cardinal: fix zero in exception list - date: add Q2 quarter, 750BC, 3-digit year, decades => 36/36 full pass - time: fix date vs time priority => 29/29 full pass - whitelist: fixed via date priority => 12/12 full pass - telephone: fix serial two_digit weight, IP combinations - 7 full-pass rules: ordinal, decimal, measure, date, time, whitelist, money(51/52)
1 parent 80956d3 commit 6a6b19f

7 files changed

Lines changed: 34 additions & 7 deletions

File tree

itn/english/rules/cardinal.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
# limitations under the License.
1414

1515
from pynini import closure, cross, difference, string_file, union
16-
from pynini.lib.pynutil import delete, insert
16+
from pynini.lib.pynutil import add_weight, delete, insert
1717

1818
from tn.processor import Processor
1919
from tn.utils import get_abs_path
@@ -35,7 +35,7 @@ def build_tagger(self):
3535

3636
# 1~9, 10~19, 20~99
3737
one_digit = digit
38-
two_digit = teen | (ties + (ds + digit | insert("0")))
38+
two_digit = teen | (ties + (ds + digit | add_weight(insert("0"), 0.1)))
3939
self.graph_two_digit = two_digit
4040
up_to_99 = one_digit | two_digit
4141

@@ -117,7 +117,7 @@ def _with_mag_padded(name):
117117

118118
# exclude 0-12 from cardinal tagger (they stay as words)
119119
from itn.english.rules.time import _num_to_word
120-
exception_labels = [_num_to_word(x) for x in range(0, 13) if _num_to_word(x)]
120+
exception_labels = ["zero"] + [_num_to_word(x) for x in range(1, 13)]
121121
exception = union(*exception_labels).optimize()
122122
graph_with_exception = (difference(self.VSIGMA, exception) @ graph).optimize()
123123

itn/english/rules/date.py

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,8 @@ def build_tagger(self):
5656

5757
# Year as two groups of two digits: "twenty twelve" => 2012
5858
year_two_parts = (teen | two_digit) + ds + (two_digit | oh_digit | teen)
59+
# 3-digit year: "seven fifty" => 750
60+
year_three_digit = digit + ds + (two_digit | oh_digit | teen)
5961

6062
# Year as "X thousand Y": "two thousand twelve" => 2012
6163
# Need zero-padded variants so "two thousand three" => 2003
@@ -116,7 +118,7 @@ def build_tagger(self):
116118
+ po
117119
)
118120
# Year only => "twenty twelve", "two thousand three"
119-
graph_y = add_weight(year, 0.01) + po
121+
graph_y = year + po
120122

121123
# Decades: "nineteen eighties" => 1980s
122124
decade_suffix = closure(self.ALPHA, 1) + (cross("ies", "y") | delete("s"))
@@ -125,7 +127,23 @@ def build_tagger(self):
125127
insert('year: "') + (teen | two_digit) + ds + decade_word + insert('0s"') + po
126128
)
127129

128-
final_graph = graph_mdy | graph_md | graph_my | graph_dmy | graph_dm | graph_y | graph_decade
130+
# Quarter: "second quarter of twenty twenty two" => Q2 2022
131+
quarter_num = (
132+
cross("first", "1") | cross("second", "2")
133+
| cross("third", "3") | cross("fourth", "4")
134+
)
135+
graph_quarter = (
136+
insert('day: "Q') + quarter_num + insert('"')
137+
+ ds + delete("quarter") + ds + delete("of") + ds
138+
+ insert(' year: "') + year_graph + insert('"') + po
139+
)
140+
141+
# BC/AD suffix
142+
bc_ad = ds + (cross("b c", "BC") | cross("a d", "AD"))
143+
year_graph_with_3digit = year_graph | year_three_digit
144+
graph_y_bc = insert('year: "') + year_graph_with_3digit + bc_ad + insert('"') + po
145+
146+
final_graph = graph_mdy | graph_md | graph_my | graph_dmy | graph_dm | graph_y | graph_decade | graph_quarter | graph_y_bc
129147
self.tagger = self.add_tokens(final_graph)
130148

131149
def build_verbalizer(self):
@@ -167,6 +185,8 @@ def build_verbalizer(self):
167185
graph_dmy = day + self.DELETE_SPACE + insert(" ") + month + optional_year
168186
# year only
169187
graph_y = year
188+
# day + year (for quarter: Q2 2022)
189+
graph_dy = day + self.DELETE_SPACE + insert(" ") + year
170190

171-
graph = (graph_mdy | graph_dmy | graph_y) + self.DELETE_SPACE + delete_po
191+
graph = (graph_mdy | graph_dmy | graph_dy | graph_y) + self.DELETE_SPACE + delete_po
172192
self.verbalizer = self.delete_tokens(graph)

itn/english/rules/telephone.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ def build_tagger(self):
9191

9292
# serial: mixed alpha+digits, at least one digit, length >= 3
9393
serial_char = add_weight(single, 0.001) | add_weight(two_digit, -0.001) | self.ALPHA
94-
serial = serial_char + closure(ds + serial_char, 2)
94+
serial = serial_char + closure(ds + serial_char, 1)
9595
serial = serial @ (closure(self.ALPHA | self.DIGIT) + self.DIGIT + closure(self.ALPHA | self.DIGIT))
9696
graph |= insert('number_part: "') + add_weight(serial, 2.0) + insert('"')
9797

itn/english/test/data/en_cardinal.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ one quadrillion two hundred sixty four trillion three hundred one billion nine h
1111
minus sixty => -60
1212
forty six thousand six hundred sixty four => 46664
1313
sixty => 60
14+
zero => zero
1415
two million three => 2000003
1516
one thousand thirteen => 1013
1617
one thousand one => 1001

itn/english/test/data/en_date.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,3 +31,6 @@ nineteen seventy six => 1976
3131
june twentieth twenty fourteen => june 20 2014
3232
nineteen seventy three => 1973
3333
nineteen seventy five => 1975
34+
eleven fifty five => 1155
35+
second quarter of twenty twenty two => Q2 2022
36+
seven fifty b c => 750BC

itn/english/test/data/en_telephone.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,12 @@ double oh three one two three five six seven eight => 003-123-5678
99
one two three dot one two three dot o dot four o => 123.123.0.40
1010
one twenty three dot one two three dot o dot four o => 123.123.0.40
1111
two two five dot double five dot o dot four o => 225.55.0.40
12+
two two five dot double five dot o dot forty five => 225.55.0.45
1213
ssn is seven double nine one two three double one three => ssn is 799-12-3113
1314
seven nine nine => 799
1415
a b nine => ab9
1516
a b c => a b c
1617
five w k r a three one => 5wkra31
18+
x eighty six => x86
1719
x three eighty six => x386
1820
r t x forty fifty t i => RTX 4050ti

itn/english/test/data/en_whitelist.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,4 @@ r t x => RTX
99
cat five e => CAT5e
1010
c u d n n => cuDNN
1111
p c i e x eight => PCIe x8
12+
l g a eleven fifty => LGA 1150

0 commit comments

Comments
 (0)