Skip to content

Commit

Permalink
refactor: Lua 模块化
Browse files Browse the repository at this point in the history
删除 `rime.lua`,拆分到 `lua/` 文件夹内。
方案中的 `- lua_xxx@xxx` 修改为 `- lua_xxx@*xxx`(加一个星号)。
  • Loading branch information
iDvel committed May 4, 2023
1 parent d33e193 commit a34c46a
Show file tree
Hide file tree
Showing 19 changed files with 379 additions and 392 deletions.
16 changes: 8 additions & 8 deletions double_pinyin.schema.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ switches:
# 输入引擎
engine:
processors:
- lua_processor@select_character # 以词定字
- lua_processor@*select_character # 以词定字
- ascii_composer
- recognizer
- key_binder
Expand All @@ -69,17 +69,17 @@ engine:
translators:
- punct_translator
- script_translator
- lua_translator@date_translator # 时间、日期、星期
- lua_translator@*date_translator # 时间、日期、星期
- table_translator@custom_phrase # 自定义短语 custom_phrase_double.txt
- table_translator@melt_eng # 英文输入
- reverse_lookup_translator@liangfen # 反查,两分拼字
- lua_translator@unicode # Unicode
- lua_translator@*unicode # Unicode
filters:
- simplifier@emoji # Emoji
- simplifier@traditionalize # 简繁切换
# - lua_filter@is_in_user_dict # 为用户词典中(输入过)的内容结尾加上一个星号 *
- lua_filter@reduce_english_filter # 降低部分英语单词在候选项的位置
- uniquifier # 去重
- simplifier@emoji # Emoji
- simplifier@traditionalize # 简繁切换
# - lua_filter@*is_in_user_dict # 为用户词典中(输入过)的内容结尾加上一个星号 *
- lua_filter@*reduce_english_filter # 降低部分英语单词在候选项的位置
- uniquifier # 去重


# Lua 配置: 日期、时间、星期、ISO 8601、时间戳的触发关键字
Expand Down
16 changes: 8 additions & 8 deletions double_pinyin_flypy.schema.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ switches:
# 输入引擎
engine:
processors:
- lua_processor@select_character # 以词定字
- lua_processor@*select_character # 以词定字
- ascii_composer
- recognizer
- key_binder
Expand All @@ -69,17 +69,17 @@ engine:
translators:
- punct_translator
- script_translator
- lua_translator@date_translator # 时间、日期、星期
- lua_translator@*date_translator # 时间、日期、星期
- table_translator@custom_phrase # 自定义短语 custom_phrase_double.txt
- table_translator@melt_eng # 英文输入
- reverse_lookup_translator@liangfen # 反查,两分拼字
- lua_translator@unicode # Unicode
- lua_translator@*unicode # Unicode
filters:
- simplifier@emoji # Emoji
- simplifier@traditionalize # 简繁切换
# - lua_filter@is_in_user_dict # 为用户词典中(输入过)的内容结尾加上一个星号 *
- lua_filter@reduce_english_filter # 降低部分英语单词在候选项的位置
- uniquifier # 去重
- simplifier@emoji # Emoji
- simplifier@traditionalize # 简繁切换
# - lua_filter@*is_in_user_dict # 为用户词典中(输入过)的内容结尾加上一个星号 *
- lua_filter@*reduce_english_filter # 降低部分英语单词在候选项的位置
- uniquifier # 去重


# Lua 配置: 日期、时间、星期、ISO 8601、时间戳的触发关键字
Expand Down
16 changes: 8 additions & 8 deletions double_pinyin_mspy.schema.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ switches:
# 输入引擎
engine:
processors:
- lua_processor@select_character # 以词定字
- lua_processor@*select_character # 以词定字
- ascii_composer
- recognizer
- key_binder
Expand All @@ -69,17 +69,17 @@ engine:
translators:
- punct_translator
- script_translator
- lua_translator@date_translator # 时间、日期、星期
- lua_translator@*date_translator # 时间、日期、星期
- table_translator@custom_phrase # 自定义短语 custom_phrase_double.txt
- table_translator@melt_eng # 英文输入
- reverse_lookup_translator@liangfen # 反查,两分拼字
- lua_translator@unicode # Unicode
- lua_translator@*unicode # Unicode
filters:
- simplifier@emoji # Emoji
- simplifier@traditionalize # 简繁切换
# - lua_filter@is_in_user_dict # 为用户词典中(输入过)的内容结尾加上一个星号 *
- lua_filter@reduce_english_filter # 降低部分英语单词在候选项的位置
- uniquifier # 去重
- simplifier@emoji # Emoji
- simplifier@traditionalize # 简繁切换
# - lua_filter@*is_in_user_dict # 为用户词典中(输入过)的内容结尾加上一个星号 *
- lua_filter@*reduce_english_filter # 降低部分英语单词在候选项的位置
- uniquifier # 去重


# Lua 配置: 日期、时间、星期、ISO 8601、时间戳的触发关键字
Expand Down
16 changes: 8 additions & 8 deletions double_pinyin_ziguang.schema.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ switches:
# 输入引擎
engine:
processors:
- lua_processor@select_character # 以词定字
- lua_processor@*select_character # 以词定字
- ascii_composer
- recognizer
- key_binder
Expand All @@ -69,17 +69,17 @@ engine:
translators:
- punct_translator
- script_translator
- lua_translator@date_translator # 时间、日期、星期
- lua_translator@*date_translator # 时间、日期、星期
- table_translator@custom_phrase # 自定义短语 custom_phrase_double.txt
- table_translator@melt_eng # 英文输入
- reverse_lookup_translator@liangfen # 反查,两分拼字
- lua_translator@unicode # Unicode
- lua_translator@*unicode # Unicode
filters:
- simplifier@emoji # Emoji
- simplifier@traditionalize # 简繁切换
# - lua_filter@is_in_user_dict # 为用户词典中(输入过)的内容结尾加上一个星号 *
- lua_filter@reduce_english_filter # 降低部分英语单词在候选项的位置
- uniquifier # 去重
- simplifier@emoji # Emoji
- simplifier@traditionalize # 简繁切换
# - lua_filter@*is_in_user_dict # 为用户词典中(输入过)的内容结尾加上一个星号 *
- lua_filter@*reduce_english_filter # 降低部分英语单词在候选项的位置
- uniquifier # 去重


# Lua 配置: 日期、时间、星期、ISO 8601、时间戳的触发关键字
Expand Down
79 changes: 79 additions & 0 deletions lua/date_translator.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
-- 日期时间
-- 提高权重的原因:因为在方案中设置了大于 1 的 initial_quality,导致 rq sj xq dt ts 产出的候选项在所有词语的最后。
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"
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"))) .. os.date(" 月 %d 日"), "")
cand.quality = 100
yield(cand)
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)
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)
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)
end
-- 时间戳(十位数,到秒,示例 1650861664)
if (input == env.timestamp) then
local cand = Candidate("datetime", seg.start, seg._end, os.time(), "")
cand.quality = 100
yield(cand)
end
-- -- 显示内存
-- local cand = Candidate("date", seg.start, seg._end, ("%.f"):format(collectgarbage('count')), "")
-- cand.quality = 100
-- yield(cand)
-- if input == "xxx" then
-- collectgarbage()
-- local cand = Candidate("date", seg.start, seg._end, "collectgarbage()", "")
-- cand.quality = 100
-- yield(cand)
-- end
end

return date_translator
11 changes: 11 additions & 0 deletions lua/is_in_user_dict.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
-- 为用户词典中(输入过)的内容结尾加上一个星号 *
local function is_in_user_dict(input, env)
for cand in input:iter() do
if (string.find(cand.type, "user")) then
cand.comment = cand.comment .. '*'
end
yield(cand)
end
end

return is_in_user_dict
43 changes: 43 additions & 0 deletions lua/long_word_filter.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
-- 长词优先(提升「西安」「提案」「图案」「饥饿」等词汇的优先级)
-- 感谢&参考于: https://github.com/tumuyan/rime-melt
-- 修改:不提升英文和中英混输的
local function long_word_filter(input, env)
-- 提升 count 个词语,插入到第 idx 个位置,默认 2、4。
if not env.idx then
local config = env.engine.schema.config
env.name_space = env.name_space:gsub("^*", "")
env.idx = config:get_int(env.name_space .. "/idx") or 4
env.count = config:get_int(env.name_space .. "/count") or 2
end

local l = {}
local firstWordLength = 0 -- 记录第一个候选词的长度,提前的候选词至少要比第一个候选词长
local done = 0 -- 记录筛选了多少个词条(只提升 count 个词的权重)
local i = 1
for cand in input:iter() do
-- 找到要提升的词
local leng = utf8.len(cand.text)
if (firstWordLength < 1 or i < env.idx) then
i = i + 1
firstWordLength = leng
yield(cand)
elseif ((leng > firstWordLength) and (done < env.count)) and (string.find(cand.text, "[%w%p%s]+") == nil) then
yield(cand)
done = done + 1
else
table.insert(l, cand)
end
-- 找齐了或者 l 太大了,就不找了
if (done == env.count) or (#l > 50) then
break
end
end
for _, cand in ipairs(l) do
yield(cand)
end
for cand in input:iter() do
yield(cand)
end
end

return long_word_filter
45 changes: 45 additions & 0 deletions lua/reduce_english_filter.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
-- 降低部分英语单词在候选项的位置
-- https://dvel.me/posts/make-rime-en-better/#短单词置顶的问题
-- 感谢大佬 @[Shewer Lu](https://github.com/shewer) 指点
local function reduce_english_filter(input, env)
-- load data
if not env.idx then
local config = env.engine.schema.config
env.name_space = env.name_space:gsub("^*", "")
env.idx = config:get_int(env.name_space .. "/idx") -- 要插入的位置
env.words = {} -- 要过滤的词
local list = config:get_list(env.name_space .. "/words")
for i = 0, list.size - 1 do
local word = list:get_value_at(i).value
env.words[word] = true
end
end

-- filter start
local code = env.engine.context.input
if env.words[code] then
local pending_cands = {}
local index = 0
for cand in input:iter() do
index = index + 1
if string.lower(cand.text) == code then
table.insert(pending_cands, cand)
else
yield(cand)
end
if index >= env.idx + #pending_cands - 1 then
for _, cand in ipairs(pending_cands) do
yield(cand)
end
break
end
end
end

-- yield other
for cand in input:iter() do
yield(cand)
end
end

return reduce_english_filter
Loading

0 comments on commit a34c46a

Please sign in to comment.