Skip to content

Commit 351ff15

Browse files
committed
feat: add 亿级 support and strip commas for Chinese TN (#223, #284)
- Extend Cardinal number FST to support 亿级 (9-13 digit numbers) using two 4-digit blocks to prevent ambiguity with shorter numbers - Strip commas between digits in Measure rule for western-formatted numbers like 299,792,458米 - Refactor Cardinal: extract four_nonzero/four_any helpers, remove redundant add_weight and rmpunct from number patterns Examples: 299,792,458米 => 两亿九千九百七十九万二千四百五十八米 100000001 => 一亿零一 1,000,000米 => 一百万米
1 parent 0fb5b04 commit 351ff15

1 file changed

Lines changed: 24 additions & 17 deletions

File tree

tn/chinese/rules/cardinal.py

Lines changed: 24 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -36,35 +36,42 @@ def build_tagger(self):
3636
dot = string_file(get_abs_path("chinese/data/number/dot.tsv"))
3737

3838
rmzero = delete("0") | delete("0")
39-
rmpunct = delete(",").ques
4039
digits = zero | digit
4140
self.digits = digits
4241

43-
# 11 => 十一
42+
# 11 => 十一, 10 => 十
4443
ten = teen + insert("十") + (digit | rmzero)
45-
# 11 => 一十一
44+
# 21 => 二十一, 30 => 三十
4645
tens = digit + insert("十") + (digit | rmzero)
47-
# 111, 101, 100
46+
# 111 => 一百一十一, 101 => 一百零一, 100 => 一百
4847
hundred = digit + insert("百") + (tens | (zero + digit) | rmzero**2)
49-
# 1111, 1011, 1001, 1000
50-
thousand = digit + insert("千") + rmpunct + (hundred | (zero + tens) | (rmzero + zero + digit) | rmzero**3)
51-
# 10001111, 1001111, 101111, 11111, 10111, 10011, 10001, 10000
48+
# 1111 => 一千一百一十一, 1001 => 一千零一, 1000 => 一千
49+
thousand = digit + insert("千") + (hundred | (zero + tens) | (rmzero + zero + digit) | rmzero**3)
50+
51+
# exactly 4 input digits, not all zero
52+
four_nonzero = thousand | (zero + hundred) | (rmzero + zero + tens) | (rmzero**2 + zero + digit)
53+
# exactly 4 input digits, may be all zero
54+
four_any = four_nonzero | rmzero**4
55+
56+
# 万级: 1,0000 ~ 9999,9999 (5~8位)
5257
ten_thousand = (
5358
(thousand | hundred | ten | digit)
5459
+ insert("万")
55-
+ (
56-
thousand
57-
| (zero + rmpunct + hundred)
58-
| (rmzero + rmpunct + zero + tens)
59-
| (rmzero + rmpunct + rmzero + zero + digit)
60-
| rmzero**4
61-
)
60+
+ four_any
61+
)
62+
63+
# 亿级: 1,0000,0000 ~ 9999,9999,9999 (9~13位)
64+
hundred_million = (
65+
(thousand | hundred | ten | digit)
66+
+ insert("亿")
67+
+ (four_nonzero + insert("万") + four_any | rmzero**4 + four_nonzero | rmzero**8)
6268
)
6369

64-
# 1.11, 1.01
65-
number = digits | ten | hundred | thousand | ten_thousand
70+
number = digits | ten | hundred | thousand | ten_thousand | hundred_million
6671
number = sign.ques + number + (dot + digits.plus).ques
67-
number @= self.build_rule(cross("二百", "两百") | cross("二千", "两千") | cross("二万", "两万"), "[BOS]").optimize()
72+
number @= self.build_rule(
73+
cross("二百", "两百") | cross("二千", "两千") | cross("二万", "两万") | cross("二亿", "两亿"), "[BOS]"
74+
).optimize()
6875
percent = insert("百分之") + number + delete("%")
6976
self.number = accep("约").ques + accep("人均").ques + (number | percent)
7077

0 commit comments

Comments
 (0)