From dba1fc598788315ef55e9404ab98766548cfc1d6 Mon Sep 17 00:00:00 2001 From: Tarrex Date: Sun, 14 May 2023 21:09:32 +0800 Subject: [PATCH] refactor: refactor date translator (#265) --- lua/date_translator.lua | 102 +++++++++++++++++++++++----------------- lua/unicode.lua | 2 +- 2 files changed, 59 insertions(+), 45 deletions(-) diff --git a/lua/date_translator.lua b/lua/date_translator.lua index 9e49529..14689af 100644 --- a/lua/date_translator.lua +++ b/lua/date_translator.lua @@ -1,67 +1,81 @@ -- 日期时间 -- 提高权重的原因:因为在方案中设置了大于 1 的 initial_quality,导致 rq sj xq dt ts 产出的候选项在所有词语的最后。 +local formats = { + date = { + '%Y-%m-%d', + '%Y/%m/%d', + '%Y.%m.%d', + '%Y%m%d', + '%Y 年 ' .. tostring(tonumber(os.date('%m'))) .. ' 月 ' .. tostring(tonumber(os.date('%d'))) .. ' 日' + }, + time = { + '%H:%M', + '%H:%M:%S' + }, + datetime = { + '%Y-%m-%dT%H:%M:%S+08:00', + '%Y%m%d%H%M%S' + }, + week = { + '星期%s', + '礼拜%s', + '周%s' + }, + timestamp = { + '%d' + } +} + local function date_translator(input, seg, env) if not env.date then - local config = env.engine.schema.config - env.name_space = env.name_space:gsub("^*", "") - env.date = config:get_string(env.name_space .. "/date") or "rq" - env.time = config:get_string(env.name_space .. "/time") or "sj" - env.week = config:get_string(env.name_space .. "/week") or "xq" - env.datetime = config:get_string(env.name_space .. "/datetime") or "dt" - env.timestamp = config:get_string(env.name_space .. "/timestamp") or "ts" + local config = env.engine.schema.config + env.name_space = env.name_space:gsub('^*', '') + env.date = config:get_string(env.name_space .. '/date') or 'rq' + env.time = config:get_string(env.name_space .. '/time') or 'sj' + env.week = config:get_string(env.name_space .. '/week') or 'xq' + env.datetime = config:get_string(env.name_space .. '/datetime') or 'dt' + env.timestamp = config:get_string(env.name_space .. '/timestamp') or 'ts' + end + + local current_time = os.time() + + local yield_cand = function(type, text) + local cand = Candidate(type, seg.start, seg._end, text, '') + cand.quality = 100 + yield(cand) end -- 日期 if (input == env.date) then - local cand = Candidate("date", seg.start, seg._end, os.date("%Y-%m-%d"), "") - cand.quality = 100 - yield(cand) - local cand = Candidate("date", seg.start, seg._end, os.date("%Y/%m/%d"), "") - cand.quality = 100 - yield(cand) - local cand = Candidate("date", seg.start, seg._end, os.date("%Y.%m.%d"), "") - cand.quality = 100 - yield(cand) - local cand = Candidate("date", seg.start, seg._end, os.date("%Y 年 " .. tostring(tonumber(os.date("%m"))) .. " 月 " .. tostring(tonumber(os.date("%m"))) .. " 日"), "") - cand.quality = 100 - yield(cand) + for _, fmt in ipairs(formats.date) do + yield_cand('date', os.date(fmt, current_time)) + end end -- 时间 if (input == env.time) then - local cand = Candidate("time", seg.start, seg._end, os.date("%H:%M"), "") - cand.quality = 100 - yield(cand) - local cand = Candidate("time", seg.start, seg._end, os.date("%H:%M:%S"), "") - cand.quality = 100 - yield(cand) + for _, fmt in ipairs(formats.time) do + yield_cand('time', os.date(fmt, current_time)) + end end -- 星期 if (input == env.week) then - local weakTab = {'日', '一', '二', '三', '四', '五', '六'} - local cand = Candidate("week", seg.start, seg._end, "星期" .. weakTab[tonumber(os.date("%w") + 1)], "") - cand.quality = 100 - yield(cand) - local cand = Candidate("week", seg.start, seg._end, "礼拜" .. weakTab[tonumber(os.date("%w") + 1)], "") - cand.quality = 100 - yield(cand) - local cand = Candidate("week", seg.start, seg._end, "周" .. weakTab[tonumber(os.date("%w") + 1)], "") - cand.quality = 100 - yield(cand) + local week_tab = { '日', '一', '二', '三', '四', '五', '六' } + for _, fmt in ipairs(formats.week) do + local text = week_tab[tonumber(os.date('%w', current_time) + 1)] + yield_cand('week', string.format(fmt, text)) + end end -- ISO 8601/RFC 3339 的时间格式 (固定东八区)(示例 2022-01-07T20:42:51+08:00) if (input == env.datetime) then - local cand = Candidate("datetime", seg.start, seg._end, os.date("%Y-%m-%dT%H:%M:%S+08:00"), "") - cand.quality = 100 - yield(cand) - local cand = Candidate("time", seg.start, seg._end, os.date("%Y%m%d%H%M%S"), "") - cand.quality = 100 - yield(cand) + for _, fmt in ipairs(formats.datetime) do + yield_cand('datetime', os.date(fmt, current_time)) + end end -- 时间戳(十位数,到秒,示例 1650861664) if (input == env.timestamp) then - local cand = Candidate("datetime", seg.start, seg._end, os.time(), "") - cand.quality = 100 - yield(cand) + for _, fmt in ipairs(formats.timestamp) do + yield_cand('timestamp', string.format(fmt, current_time)) + end end -- -- 显示内存 -- local cand = Candidate("date", seg.start, seg._end, ("%.f"):format(collectgarbage('count')), "") diff --git a/lua/unicode.lua b/lua/unicode.lua index 3e24d28..10f1aca 100644 --- a/lua/unicode.lua +++ b/lua/unicode.lua @@ -7,7 +7,7 @@ local function unicode(input, seg, env) local code = tonumber(ucodestr, 16) local text = utf8.char(code) yield(Candidate("unicode", seg.start, seg._end, text, string.format("U%x", code))) - if #ucodestr < 5 then + if code < 0x10000 then for i = 0, 15 do local text = utf8.char(code * 16 + i) yield(Candidate("unicode", seg.start, seg._end, text, string.format("U%x~%x", code, i)))