2023-05-12 19:42:30 +02:00
|
|
|
|
-- 来源 https://github.com/yanhuacuo/98wubi-tables > http://98wb.ysepan.com/
|
2024-01-29 11:51:45 +01:00
|
|
|
|
-- 数字、金额大写
|
2024-01-14 16:58:37 +01:00
|
|
|
|
-- 触发前缀默认为 recognizer/patterns/number 的第 2 个字符,即 R
|
2023-05-12 19:42:30 +02:00
|
|
|
|
|
|
|
|
|
local function splitNumPart(str)
|
2024-02-08 11:39:49 +01:00
|
|
|
|
local part = {}
|
|
|
|
|
part.int, part.dot, part.dec = string.match(str, "^(%d*)(%.?)(%d*)")
|
|
|
|
|
return part
|
2023-05-12 19:42:30 +02:00
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
local function GetPreciseDecimal(nNum, n)
|
2024-02-08 11:39:49 +01:00
|
|
|
|
if type(nNum) ~= "number" then nNum = tonumber(nNum) end
|
|
|
|
|
n = n or 0;
|
|
|
|
|
n = math.floor(n)
|
|
|
|
|
if n < 0 then n = 0 end
|
|
|
|
|
local nDecimal = 10 ^ n
|
|
|
|
|
local nTemp = math.floor(nNum * nDecimal);
|
|
|
|
|
local nRet = nTemp / nDecimal;
|
|
|
|
|
return nRet;
|
2023-05-12 19:42:30 +02:00
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
local function decimal_func(str, posMap, valMap)
|
2024-02-08 11:39:49 +01:00
|
|
|
|
local dec
|
|
|
|
|
posMap = posMap or { [1] = "角", [2] = "分", [3] = "厘", [4] = "毫" }
|
|
|
|
|
valMap = valMap or { [0] = "零", "壹", "贰", "叁", "肆", "伍", "陆", "柒", "捌", "玖" }
|
|
|
|
|
if #str > 4 then dec = string.sub(tostring(str), 1, 4) else dec = tostring(str) end
|
|
|
|
|
dec = string.gsub(dec, "0+$", "")
|
2023-05-12 19:42:30 +02:00
|
|
|
|
|
2024-02-08 11:39:49 +01:00
|
|
|
|
if dec == "" then return "整" end
|
|
|
|
|
|
|
|
|
|
local result = ""
|
|
|
|
|
for pos = 1, #dec do
|
|
|
|
|
local val = tonumber(string.sub(dec, pos, pos))
|
|
|
|
|
if val ~= 0 then result = result .. valMap[val] .. posMap[pos] else result = result .. valMap[val] end
|
|
|
|
|
end
|
|
|
|
|
result = result:gsub(valMap[0] .. valMap[0], valMap[0])
|
|
|
|
|
return result:gsub(valMap[0] .. valMap[0], valMap[0])
|
2023-05-12 19:42:30 +02:00
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
-- 把数字串按千分位四位数分割,进行转换为中文
|
2024-02-08 11:39:49 +01:00
|
|
|
|
local function formatNum(num, t)
|
|
|
|
|
local digitUnit, wordFigure
|
|
|
|
|
local result = ""
|
|
|
|
|
num = tostring(num)
|
|
|
|
|
if tonumber(t) < 1 then digitUnit = { "", "十", "百", "千" } else digitUnit = { "", "拾", "佰", "仟" } end
|
|
|
|
|
if tonumber(t) < 1 then
|
|
|
|
|
wordFigure = { "〇", "一", "二", "三", "四", "五", "六", "七", "八", "九" }
|
|
|
|
|
else
|
|
|
|
|
wordFigure = { "零", "壹", "贰", "叁", "肆", "伍", "陆", "柒", "捌", "玖" }
|
|
|
|
|
end
|
|
|
|
|
if string.len(num) > 4 or tonumber(num) == 0 then return wordFigure[1] end
|
|
|
|
|
local lens = string.len(num)
|
|
|
|
|
for i = 1, lens do
|
|
|
|
|
local n = wordFigure[tonumber(string.sub(num, -i, -i)) + 1]
|
|
|
|
|
if n ~= wordFigure[1] then result = n .. digitUnit[i] .. result else result = n .. result end
|
|
|
|
|
end
|
|
|
|
|
result = result:gsub(wordFigure[1] .. wordFigure[1], wordFigure[1])
|
|
|
|
|
result = result:gsub(wordFigure[1] .. "$", "")
|
|
|
|
|
result = result:gsub(wordFigure[1] .. "$", "")
|
2023-05-12 19:42:30 +02:00
|
|
|
|
|
2024-02-08 11:39:49 +01:00
|
|
|
|
return result
|
2023-05-12 19:42:30 +02:00
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
-- 数值转换为中文
|
2024-02-08 11:39:49 +01:00
|
|
|
|
local function number2cnChar(num, flag, digitUnit, wordFigure) --flag=0中文小写反之为大写
|
2024-02-23 09:06:29 +01:00
|
|
|
|
local result = ""
|
|
|
|
|
|
|
|
|
|
if tonumber(flag) < 1 then
|
|
|
|
|
digitUnit = digitUnit or { [1] = "万", [2] = "亿" }
|
|
|
|
|
wordFigure = wordFigure or { [1] = "〇", [2] = "一", [3] = "十", [4] = "元" }
|
|
|
|
|
else
|
|
|
|
|
digitUnit = digitUnit or { [1] = "万", [2] = "亿" }
|
|
|
|
|
wordFigure = wordFigure or { [1] = "零", [2] = "壹", [3] = "拾", [4] = "元" }
|
|
|
|
|
end
|
|
|
|
|
local lens = string.len(num)
|
|
|
|
|
if lens < 5 then
|
|
|
|
|
result = formatNum(num, flag)
|
|
|
|
|
elseif lens < 9 then
|
|
|
|
|
result = formatNum(string.sub(num, 1, -5), flag) .. digitUnit[1] .. formatNum(string.sub(num, -4, -1), flag)
|
|
|
|
|
elseif lens < 13 then
|
|
|
|
|
result = formatNum(string.sub(num, 1, -9), flag) ..
|
2024-02-08 11:39:49 +01:00
|
|
|
|
digitUnit[2] ..
|
2024-02-23 09:06:29 +01:00
|
|
|
|
formatNum(string.sub(num, -8, -5), flag) .. digitUnit[1] .. formatNum(string.sub(num, -4, -1), flag)
|
2024-02-08 11:39:49 +01:00
|
|
|
|
else
|
2024-02-23 09:06:29 +01:00
|
|
|
|
result = ""
|
2024-02-08 11:39:49 +01:00
|
|
|
|
end
|
2023-05-12 19:42:30 +02:00
|
|
|
|
|
2024-02-23 09:06:29 +01:00
|
|
|
|
result = result:gsub("^" .. wordFigure[1], "")
|
|
|
|
|
result = result:gsub(wordFigure[1] .. digitUnit[1], "")
|
|
|
|
|
result = result:gsub(wordFigure[1] .. digitUnit[2], "")
|
|
|
|
|
result = result:gsub(wordFigure[1] .. wordFigure[1], wordFigure[1])
|
|
|
|
|
result = result:gsub(wordFigure[1] .. "$", "")
|
|
|
|
|
if lens > 4 then result = result:gsub("^" .. wordFigure[2] .. wordFigure[3], wordFigure[3]) end
|
|
|
|
|
if result ~= "" then result = result .. wordFigure[4] else result = "数值超限!" end
|
|
|
|
|
|
2024-02-08 11:39:49 +01:00
|
|
|
|
return result
|
2023-05-12 19:42:30 +02:00
|
|
|
|
end
|
|
|
|
|
|
2024-02-08 11:39:49 +01:00
|
|
|
|
local function number2zh(num, t)
|
|
|
|
|
local result, wordFigure
|
|
|
|
|
result = ""
|
|
|
|
|
if tonumber(t) < 1 then
|
|
|
|
|
wordFigure = { "〇", "一", "二", "三", "四", "五", "六", "七", "八", "九" }
|
|
|
|
|
else
|
|
|
|
|
wordFigure = { "零", "壹", "贰", "叁", "肆", "伍", "陆", "柒", "捌", "玖" }
|
|
|
|
|
end
|
|
|
|
|
if tostring(num) == nil then return "" end
|
|
|
|
|
for pos = 1, string.len(num) do
|
|
|
|
|
result = result .. wordFigure[tonumber(string.sub(num, pos, pos) + 1)]
|
|
|
|
|
end
|
|
|
|
|
result = result:gsub(wordFigure[1] .. wordFigure[1], wordFigure[1])
|
|
|
|
|
return result:gsub(wordFigure[1] .. wordFigure[1], wordFigure[1])
|
2023-05-12 19:42:30 +02:00
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
local function number_translatorFunc(num)
|
2024-02-08 11:39:49 +01:00
|
|
|
|
local numberPart = splitNumPart(num)
|
|
|
|
|
local result = {}
|
|
|
|
|
if numberPart.dot ~= "" then
|
|
|
|
|
table.insert(result,
|
|
|
|
|
{ number2cnChar(numberPart.int, 0, { "万", "亿" }, { "〇", "一", "十", "点" }) .. number2zh(numberPart.dec, 0),
|
|
|
|
|
"〔数字小写〕" })
|
|
|
|
|
table.insert(result,
|
|
|
|
|
{ number2cnChar(numberPart.int, 1, { "萬", "億" }, { "〇", "一", "十", "点" }) .. number2zh(numberPart.dec, 1),
|
|
|
|
|
"〔数字大写〕" })
|
|
|
|
|
else
|
|
|
|
|
table.insert(result, { number2cnChar(numberPart.int, 0, { "万", "亿" }, { "〇", "一", "十", "" }), "〔数字小写〕" })
|
|
|
|
|
table.insert(result, { number2cnChar(numberPart.int, 1, { "萬", "億" }, { "零", "壹", "拾", "" }), "〔数字大写〕" })
|
|
|
|
|
end
|
|
|
|
|
table.insert(result,
|
|
|
|
|
{ number2cnChar(numberPart.int, 0) ..
|
|
|
|
|
decimal_func(numberPart.dec, { [1] = "角", [2] = "分", [3] = "厘", [4] = "毫" },
|
|
|
|
|
{ [0] = "〇", "一", "二", "三", "四", "五", "六", "七", "八", "九" }), "〔金额小写〕" })
|
2024-07-27 12:28:27 +02:00
|
|
|
|
|
|
|
|
|
local number2cnCharInt = number2cnChar(numberPart.int, 1)
|
|
|
|
|
local number2cnCharDec = decimal_func(numberPart.dec, { [1] = "角", [2] = "分", [3] = "厘", [4] = "毫" }, { [0] = "零", "壹", "贰", "叁", "肆", "伍", "陆", "柒", "捌", "玖" })
|
2024-09-12 04:09:31 +02:00
|
|
|
|
if string.len(numberPart.int) > 4 and number2cnCharInt:find('^拾[壹贰叁肆伍陆柒捌玖]?') and number2cnCharInt:find('[万亿]') then -- 简易地规避 utf8 匹配问题
|
|
|
|
|
local number2cnCharInt_var = number2cnCharInt:gsub('^拾', '壹拾')
|
|
|
|
|
table.insert(result, { number2cnCharInt_var .. number2cnCharDec , "〔金额大写〕"})
|
|
|
|
|
-- 会计书写要求 https://github.com/iDvel/rime-ice/issues/989
|
2024-07-27 12:28:27 +02:00
|
|
|
|
end
|
2024-09-12 04:09:31 +02:00
|
|
|
|
-- table.insert(result, { number2cnCharInt .. number2cnCharDec , "〔金额大写〕"})
|
2024-02-08 11:39:49 +01:00
|
|
|
|
return result
|
2023-05-12 19:42:30 +02:00
|
|
|
|
end
|
|
|
|
|
|
2024-01-14 16:58:37 +01:00
|
|
|
|
local function number_translator(input, seg, env)
|
2024-02-08 11:39:49 +01:00
|
|
|
|
-- 获取 recognizer/patterns/number 的第 2 个字符作为触发前缀
|
|
|
|
|
env.number_keyword = env.number_keyword or
|
2024-02-23 09:06:29 +01:00
|
|
|
|
env.engine.schema.config:get_string('recognizer/patterns/number'):sub(2, 2)
|
2023-05-12 19:42:30 +02:00
|
|
|
|
local str, num, numberPart
|
2024-01-14 16:58:37 +01:00
|
|
|
|
if env.number_keyword ~= '' and input:sub(1, 1) == env.number_keyword then
|
2023-05-12 19:42:30 +02:00
|
|
|
|
str = string.gsub(input, "^(%a+)", "")
|
|
|
|
|
numberPart = number_translatorFunc(str)
|
2024-04-25 14:53:39 +02:00
|
|
|
|
if str and #str > 0 and #numberPart > 0 then
|
2023-05-12 19:42:30 +02:00
|
|
|
|
for i = 1, #numberPart do
|
|
|
|
|
yield(Candidate(input, seg.start, seg._end, numberPart[i][1], numberPart[i][2]))
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
-- print(#number_translatorFunc(3355.433))
|
|
|
|
|
return number_translator
|