style: Lua 统一缩进 4 空格

This commit is contained in:
Dvel 2024-02-08 18:39:49 +08:00
parent 1ffdce6a84
commit efcb4e2bdd
17 changed files with 1054 additions and 1012 deletions

View File

@ -42,36 +42,48 @@
-- puts(INFO,__FILE__(),__LINE__(),__FUNC__() , ...)
--
-- global variable
function __FILE__(n) n=n or 2 return debug.getinfo(n,'S').source end
function __LINE__(n) n=n or 2 return debug.getinfo(n, 'l').currentline end
function __FUNC__(n) n=n or 2 return debug.getinfo(n, 'n').name end
INFO="log"
WARN="warn"
ERROR="error"
DEBUG="trace"
CONSOLE="console"
function __FILE__(n)
n = n or 2
return debug.getinfo(n, 'S').source
end
function __LINE__(n)
n = n or 2
return debug.getinfo(n, 'l').currentline
end
function __FUNC__(n)
n = n or 2
return debug.getinfo(n, 'n').name
end
INFO = "log"
WARN = "warn"
ERROR = "error"
DEBUG = "trace"
CONSOLE = "console"
local function tran_msg(...)
local msg="\t"
for i,k in next, {...} do msg = msg .. ": " .. tostring(k) end
local msg = "\t"
for i, k in next, { ... } do msg = msg .. ": " .. tostring(k) end
return msg
end
local function puts( tag , ...)
local function puts(tag, ...)
if type(tag) ~= "string" then return end
if INFO and tag:match("^" .. INFO) then
(log and log.info or print)( tag .. tran_msg(...))
(log and log.info or print)(tag .. tran_msg(...))
elseif WARN and tag:match("^" .. WARN) then
(log and log.warning or print)(tag .. tran_msg(...))
elseif ERROR and tag:match("^" .. ERROR) then
(log and log.error or print)(tag .. tran_msg(...))
elseif DEBUG and tag:match("^" .. DEBUG) then
(log and log.error or print)(tag .. tran_msg(...))
elseif CONSOLE and tag:match( "^" .. CONSOLE ) then
( print)( tag .. tran_msg(...))
elseif CONSOLE and tag:match("^" .. CONSOLE) then
(print)(tag .. tran_msg(...))
else
return
end

View File

@ -1,4 +1,3 @@
local drop_list = require("cold_word_drop.drop_words")
local hide_list = require("cold_word_drop.hide_words")
local turndown_freq_list = require("cold_word_drop.turndown_freq_words")

View File

@ -27,29 +27,29 @@ function metatable_chk(tab)
end
end
table.eachi = function (tab, func)
table.eachi = function(tab, func)
for i = 1, #tab do
func(tab[i], i)
end
return tab
end
table.eacha = function (tab, func)
table.eacha = function(tab, func)
for i, v in ipairs(tab) do
func(v, i)
end
return tab
end
table.each = function (tab, func)
table.each = function(tab, func)
for k, v in pairs(tab) do
func(v, k)
end
return tab
end
table.find_index = function (tab, elm, ...)
table.find_index = function(tab, elm, ...)
local _, i = table.find(tab, elm, ...)
return i
end
table.find = function (tab, elm, func)
table.find = function(tab, elm, func)
for i, v in ipairs(tab) do
if elm == v then
return v, i
@ -57,17 +57,17 @@ table.find = function (tab, elm, func)
end
end
table.find_with_func = function (tab, elm, ...)
table.find_with_func = function(tab, elm, ...)
local i, v = table.find(tab, elm)
end
table.delete = function (tab, elm, ...)
table.delete = function(tab, elm, ...)
local index = table.find_index(tab, elm)
return index and table.remove(tab, index)
end
table.find_all = function (tab, elm, ...)
table.find_all = function(tab, elm, ...)
local tmptab = setmetatable({}, { __index = table })
local _func = (type(elm) == "function" and elm) or function (v, k, ...) return v == elm end
local _func = (type(elm) == "function" and elm) or function(v, k, ...) return v == elm end
for k, v in pairs(tab) do
if _func(v, k, ...) then
tmptab:insert(v)
@ -77,7 +77,7 @@ table.find_all = function (tab, elm, ...)
end
table.select = table.find_all
table.reduce = function (tab, func, arg)
table.reduce = function(tab, func, arg)
local new, old = arg, arg
for i, v in ipairs(tab) do
new, old = func(v, new)
@ -85,17 +85,17 @@ table.reduce = function (tab, func, arg)
return new, arg
end
table.map = function (tab, func)
table.map = function(tab, func)
local newtab = setmetatable({}, { __index = table })
func = func or function (v, i) return v, i end
func = func or function(v, i) return v, i end
for i, v in ipairs(tab) do
newtab[i] = func(v, i)
end
return newtab
end
table.map_hash = function (tab, func) -- table to list of array { key, v}
table.map_hash = function(tab, func) -- table to list of array { key, v}
local newtab = setmetatable({}, { __index = table })
func = func or function (k, v) return { k, v } end
func = func or function(k, v) return { k, v } end
for k, v in pairs(tab) do
newtab:insert(func(k, v))
end

View File

@ -1,4 +1,3 @@
require('cold_word_drop.string')
require("cold_word_drop.metatable")
-- local puts = require("tools/debugtool")

View File

@ -3,38 +3,39 @@
-- string.utf8_len = utf8.len
-- string.utf8_offset= utf8.offset
-- string.utf8_sub= utf8.sub
function string.split( str, sp,sp1)
sp =type(sp) == "string" and sp or " "
function string.split(str, sp, sp1)
sp = type(sp) == "string" and sp or " "
if #sp == 0 then
sp= "([%z\1-\127\194-\244][\128-\191]*)"
sp = "([%z\1-\127\194-\244][\128-\191]*)"
elseif #sp == 1 then
sp= "[^" .. (sp=="%" and "%%" or sp) .. "]*"
sp = "[^" .. (sp == "%" and "%%" or sp) .. "]*"
else
sp1= sp1 or "^"
str=str:gsub(sp,sp1)
sp= "[^".. sp1 .. "]*"
sp1 = sp1 or "^"
str = str:gsub(sp, sp1)
sp = "[^" .. sp1 .. "]*"
end
local tab= {}
local tab = {}
for v in str:gmatch(sp) do
table.insert(tab,v)
table.insert(tab, v)
end
return tab
end
function utf8.gsub(str,si,ei)
local function index(ustr,i)
return i>=0 and ( ustr:utf8_offset(i) or ustr:len() +1 )
or ( ustr:utf8_offset(i) or 1 )
function utf8.gsub(str, si, ei)
local function index(ustr, i)
return i >= 0 and (ustr:utf8_offset(i) or ustr:len() + 1)
or (ustr:utf8_offset(i) or 1)
end
local u_si= index(str,si)
local u_si = index(str, si)
ei = ei or str:utf8_len()
ei = ei >=0 and ei +1 or ei
local u_ei= index(str, ei ) -1
return str:sub(u_si,u_ei)
ei = ei >= 0 and ei + 1 or ei
local u_ei = index(str, ei) - 1
return str:sub(u_si, u_ei)
end
string.utf8_len= utf8.len
string.utf8_offset=utf8.offset
string.utf8_sub= utf8.gsub
string.utf8_len = utf8.len
string.utf8_offset = utf8.offset
string.utf8_sub = utf8.gsub
return true

View File

@ -9,7 +9,7 @@ local function splitNumPart(str)
end
local function GetPreciseDecimal(nNum, n)
if type(nNum) ~= "number" then nNum =tonumber(nNum) end
if type(nNum) ~= "number" then nNum = tonumber(nNum) end
n = n or 0;
n = math.floor(n)
if n < 0 then n = 0 end
@ -21,98 +21,131 @@ end
local function decimal_func(str, posMap, valMap)
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
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+$", "")
if dec == "" then return "" end
local result = ""
for pos =1, #dec do
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
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])
result = result:gsub(valMap[0] .. valMap[0], valMap[0])
return result:gsub(valMap[0] .. valMap[0], valMap[0])
end
-- 把数字串按千分位四位数分割,进行转换为中文
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
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
result=result:gsub(wordFigure[1]..wordFigure[1] ,wordFigure[1])
result=result:gsub(wordFigure[1].."$","") result=result:gsub(wordFigure[1].."$","")
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] .. "$", "")
return result
end
-- 数值转换为中文
local function number2cnChar(num,flag,digitUnit,wordFigure) --flag=0中文小写反之为大写
local st,result
num=tostring(num) result=""
local num1,num2=math.modf(num)
if tonumber(num2)==0 then
local function number2cnChar(num, flag, digitUnit, wordFigure) --flag=0中文小写反之为大写
local st, result
num = tostring(num)
result = ""
local num1, num2 = math.modf(num)
if tonumber(num2) == 0 then
if tonumber(flag) < 1 then
digitUnit = digitUnit or {[1]="";[2]="亿"} wordFigure = wordFigure or {[1]=""; [2]=""; [3]=""; [4]=""}
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]=""}
digitUnit = digitUnit or { [1] = "", [2] = "亿" }
wordFigure = wordFigure or { [1] = "", [2] = "", [3] = "", [4] = "" }
end
local lens = string.len(num1)
if lens < 5 then
result = formatNum(num1, flag)
elseif lens < 9 then
result = formatNum(string.sub(num1, 1, -5), flag) .. digitUnit[1] .. formatNum(string.sub(num1, -4, -1), flag)
elseif lens < 13 then
result = formatNum(string.sub(num1, 1, -9), flag) ..
digitUnit[2] ..
formatNum(string.sub(num1, -8, -5), flag) .. digitUnit[1] .. formatNum(string.sub(num1, -4, -1), flag)
else
result = ""
end
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
else
return "数值超限!"
end
local lens=string.len(num1)
if lens<5 then result=formatNum(num1,flag) elseif lens<9 then result=formatNum(string.sub(num1,1,-5),flag) .. digitUnit[1].. formatNum(string.sub(num1,-4,-1),flag)
elseif lens<13 then result=formatNum(string.sub(num1,1,-9),flag) .. digitUnit[2] .. formatNum(string.sub(num1,-8,-5),flag) .. digitUnit[1] .. formatNum(string.sub(num1,-4,-1),flag) else result="" end
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
else return "数值超限!" end
return result
end
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)]
local function number2zh(num, t)
local result, wordFigure
result = ""
if tonumber(t) < 1 then
wordFigure = { "", "", "", "", "", "", "", "", "", "" }
else
wordFigure = { "", "", "", "", "", "", "", "", "", "" }
end
result=result:gsub(wordFigure[1] .. wordFigure[1],wordFigure[1])
return result:gsub(wordFigure[1] .. wordFigure[1],wordFigure[1])
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])
end
local function number_translatorFunc(num)
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),"〔数字大写〕"})
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,{"", ""},{"","","",""}),"〔数字大写〕"})
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]=""; ""; ""; "" ;""; ""; ""; ""; ""; ""}),"〔金额小写〕"})
table.insert(result,{number2cnChar(numberPart.int,1)..decimal_func(numberPart.dec,{[1]=""; [2]=""; [3]=""; [4]=""},{[0]=""; ""; ""; "" ;""; ""; ""; ""; ""; ""}),"〔金额大写〕"})
table.insert(result,
{ number2cnChar(numberPart.int, 0) ..
decimal_func(numberPart.dec, { [1] = "", [2] = "", [3] = "", [4] = "" },
{ [0] = "", "", "", "", "", "", "", "", "", "" }), "〔金额小写〕" })
table.insert(result,
{ number2cnChar(numberPart.int, 1) ..
decimal_func(numberPart.dec, { [1] = "", [2] = "", [3] = "", [4] = "" },
{ [0] = "", "", "", "", "", "", "", "", "", "" }), "〔金额大写〕" })
return result
end
local function number_translator(input, seg, env)
-- 获取 recognizer/patterns/number 的第 2 个字符作为触发前缀
env.number_keyword = env.number_keyword or env.engine.schema.config:get_string('recognizer/patterns/number'):sub(2, 2)
env.number_keyword = env.number_keyword or
env.engine.schema.config:get_string('recognizer/patterns/number'):sub(2, 2)
local str, num, numberPart
if env.number_keyword ~= '' and input:sub(1, 1) == env.number_keyword then
str = string.gsub(input, "^(%a+)", "")

View File

@ -95,7 +95,6 @@ function f.init(env)
-- f.update_dict_entry(edit, no_search_string)
end
end)
end
-- function f.update_dict_entry(s, code)
@ -233,7 +232,6 @@ function f.func(input, env)
yield(cand)
end
end
end
function f.fini(env)

View File

@ -4,7 +4,7 @@
-- 感谢改进 @[t123yh](https://github.com/t123yh) @[Shewer Lu](https://github.com/shewer)
local function v_filter(input, env)
local code = env.engine.context.input -- 当前编码
env.v_spec_arr = env.v_spec_arr or Set({"0", "1", "2", "3", "4", "5", "6", "7", "8", "9","Vs."})
env.v_spec_arr = env.v_spec_arr or Set({ "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "Vs." })
-- 仅当当前输入以 v 开头,并且编码长度为 2才进行处理
if (string.len(code) == 2 and string.find(code, "^v")) then
local l = {}