-
Notifications
You must be signed in to change notification settings - Fork 43
/
number.lua
108 lines (98 loc) · 2.67 KB
/
number.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
--[[
number_translator: 将 `/` + 阿拉伯数字 翻译为大小写汉字
--]]
local confs = {
{
comment = " 大写",
numeral = { [0] = "零", "壹", "贰", "叁", "肆", "伍", "陆", "柒", "捌", "玖" },
place = { [0] = "", "拾", "佰", "仟" },
group = { [0] = "", "万", "亿", "万亿", "亿亿" }
},
{
comment = " 小写",
numeral = { [0] = "零", "一", "二", "三", "四", "五", "六", "七", "八", "九" },
place = { [0] = "", "十", "百", "千" },
group = { [0] = "", "万", "亿", "万亿", "亿亿" }
},
{
comment = " 序数",
numeral = { [0] = "〇", "一", "二", "三", "四", "五", "六", "七", "八", "九" }
},
{
comment = " 大寫",
numeral = { [0] = "零", "壹", "貳", "參", "肆", "伍", "陸", "柒", "捌", "玖" },
place = { [0] = "", "拾", "佰", "仟" },
group = { [0] = "", "萬", "億", "兆", "京" }
},
{
comment = " 小寫",
numeral = { [0] = "零", "一", "二", "三", "四", "五", "六", "七", "八", "九" },
place = { [0] = "", "十", "百", "千" },
group = { [0] = "", "萬", "億", "兆", "京" }
},
{
comment = " 序數",
numeral = { [0] = "〇", "一", "二", "三", "四", "五", "六", "七", "八", "九" }
}
}
local function read_seg(conf, n)
local s = ""
local i = 0
local zf = true
while string.len(n) > 0 do
local d = tonumber(string.sub(n, -1, -1))
if conf.place == nil then
s = conf.numeral[d] .. s
elseif d == 1 and i == 1 and string.len(n) == 1 then
s = conf.place[i] .. s
elseif d ~= 0 then
s = conf.numeral[d] .. conf.place[i] .. s
zf = false
else
if not zf then
s = conf.numeral[0] .. s
end
zf = true
end
i = i + 1
n = string.sub(n, 1, -2)
end
return i < 4, s
end
local function read_number(conf, n)
local s = ""
local i = 0
local zf = false
n = string.gsub(n, "^0+", "")
if n == "" then
return conf.numeral[0]
end
while string.len(n) > 0 do
local zf2, r = read_seg(conf, string.sub(n, -4, -1))
if r ~= "" then
if conf.group == nil then
s = r .. s
elseif zf and s ~= "" then
s = r .. conf.group[i] .. conf.numeral[0] .. s
else
s = r .. conf.group[i] .. s
end
end
zf = zf2
i = i + 1
n = string.sub(n, 1, -5)
end
return s
end
local function translator(input, seg)
if string.sub(input, 1, 1) == "/" then
local n = string.sub(input, 2)
if tonumber(n) ~= nil then
for _, conf in ipairs(confs) do
local r = read_number(conf, n)
yield(Candidate("number", seg.start, seg._end, r, conf.comment))
end
end
end
end
return translator