From a34c46ad34673d535dc1df4ef208ad4c7e3baf80 Mon Sep 17 00:00:00 2001 From: Dvel Date: Fri, 5 May 2023 01:41:21 +0800 Subject: [PATCH] =?UTF-8?q?refactor:=20Lua=20=E6=A8=A1=E5=9D=97=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 删除 `rime.lua`,拆分到 `lua/` 文件夹内。 方案中的 `- lua_xxx@xxx` 修改为 `- lua_xxx@*xxx`(加一个星号)。 --- double_pinyin.schema.yaml | 16 +- double_pinyin_flypy.schema.yaml | 16 +- double_pinyin_mspy.schema.yaml | 16 +- double_pinyin_ziguang.schema.yaml | 16 +- lua/date_translator.lua | 79 +++++++ lua/is_in_user_dict.lua | 11 + lua/long_word_filter.lua | 43 ++++ lua/reduce_english_filter.lua | 45 ++++ lua/select_character.lua | 83 +++++++ lua/t9_preedit.lua | 15 ++ lua/unicode.lua | 19 ++ lua/v_filter.lua | 32 +++ others/recipes/all_dicts.recipe.yaml | 15 +- others/recipes/cn_dicts.recipe.yaml | 7 +- others/recipes/en_dicts.recipe.yaml | 4 +- others/recipes/full.recipe.yaml | 17 +- others/recipes/opencc.recipe.yaml | 4 +- rime.lua | 313 --------------------------- rime_ice.schema.yaml | 20 +- 19 files changed, 379 insertions(+), 392 deletions(-) create mode 100644 lua/date_translator.lua create mode 100644 lua/is_in_user_dict.lua create mode 100644 lua/long_word_filter.lua create mode 100644 lua/reduce_english_filter.lua create mode 100644 lua/select_character.lua create mode 100644 lua/t9_preedit.lua create mode 100644 lua/unicode.lua create mode 100644 lua/v_filter.lua delete mode 100644 rime.lua diff --git a/double_pinyin.schema.yaml b/double_pinyin.schema.yaml index 47bc046..5834835 100644 --- a/double_pinyin.schema.yaml +++ b/double_pinyin.schema.yaml @@ -51,7 +51,7 @@ switches: # 输入引擎 engine: processors: - - lua_processor@select_character # 以词定字 + - lua_processor@*select_character # 以词定字 - ascii_composer - recognizer - key_binder @@ -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、时间戳的触发关键字 diff --git a/double_pinyin_flypy.schema.yaml b/double_pinyin_flypy.schema.yaml index 76e5f3a..43120a3 100644 --- a/double_pinyin_flypy.schema.yaml +++ b/double_pinyin_flypy.schema.yaml @@ -51,7 +51,7 @@ switches: # 输入引擎 engine: processors: - - lua_processor@select_character # 以词定字 + - lua_processor@*select_character # 以词定字 - ascii_composer - recognizer - key_binder @@ -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、时间戳的触发关键字 diff --git a/double_pinyin_mspy.schema.yaml b/double_pinyin_mspy.schema.yaml index 4b15742..4ae53e5 100644 --- a/double_pinyin_mspy.schema.yaml +++ b/double_pinyin_mspy.schema.yaml @@ -51,7 +51,7 @@ switches: # 输入引擎 engine: processors: - - lua_processor@select_character # 以词定字 + - lua_processor@*select_character # 以词定字 - ascii_composer - recognizer - key_binder @@ -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、时间戳的触发关键字 diff --git a/double_pinyin_ziguang.schema.yaml b/double_pinyin_ziguang.schema.yaml index 050868f..3a70080 100644 --- a/double_pinyin_ziguang.schema.yaml +++ b/double_pinyin_ziguang.schema.yaml @@ -51,7 +51,7 @@ switches: # 输入引擎 engine: processors: - - lua_processor@select_character # 以词定字 + - lua_processor@*select_character # 以词定字 - ascii_composer - recognizer - key_binder @@ -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、时间戳的触发关键字 diff --git a/lua/date_translator.lua b/lua/date_translator.lua new file mode 100644 index 0000000..97076d2 --- /dev/null +++ b/lua/date_translator.lua @@ -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 diff --git a/lua/is_in_user_dict.lua b/lua/is_in_user_dict.lua new file mode 100644 index 0000000..cabbc44 --- /dev/null +++ b/lua/is_in_user_dict.lua @@ -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 diff --git a/lua/long_word_filter.lua b/lua/long_word_filter.lua new file mode 100644 index 0000000..eb34b50 --- /dev/null +++ b/lua/long_word_filter.lua @@ -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 diff --git a/lua/reduce_english_filter.lua b/lua/reduce_english_filter.lua new file mode 100644 index 0000000..c049ab1 --- /dev/null +++ b/lua/reduce_english_filter.lua @@ -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 diff --git a/lua/select_character.lua b/lua/select_character.lua new file mode 100644 index 0000000..315d7ff --- /dev/null +++ b/lua/select_character.lua @@ -0,0 +1,83 @@ +-- 以词定字 +-- 来源 https://github.com/BlindingDark/rime-lua-select-character +-- 删除了默认按键 [ ],和方括号翻页冲突,需要在 key_binder 下指定才能生效 +local function utf8_sub(s, i, j) + i = i or 1 + j = j or -1 + + if i < 1 or j < 1 then + local n = utf8.len(s) + if not n then + return nil + end + if i < 0 then + i = n + 1 + i + end + if j < 0 then + j = n + 1 + j + end + if i < 0 then + i = 1 + elseif i > n then + i = n + end + if j < 0 then + j = 1 + elseif j > n then + j = n + end + end + + if j < i then + return "" + end + + i = utf8.offset(s, i) + j = utf8.offset(s, j + 1) + + if i and j then + return s:sub(i, j - 1) + elseif i then + return s:sub(i) + else + return "" + end +end + +local function first_character(s) + return utf8_sub(s, 1, 1) +end + +local function last_character(s) + return utf8_sub(s, -1, -1) +end + +local function select_character(key, env) + local engine = env.engine + local context = engine.context + local commit_text = context:get_commit_text() + local config = engine.schema.config + + -- local first_key = config:get_string('key_binder/select_first_character') or 'bracketleft' + -- local last_key = config:get_string('key_binder/select_last_character') or 'bracketright' + local first_key = config:get_string('key_binder/select_first_character') + local last_key = config:get_string('key_binder/select_last_character') + + if (key:repr() == first_key and commit_text ~= "") then + engine:commit_text(first_character(commit_text)) + context:clear() + + return 1 -- kAccepted + end + + if (key:repr() == last_key and commit_text ~= "") then + engine:commit_text(last_character(commit_text)) + context:clear() + + return 1 -- kAccepted + end + + return 2 -- kNoop +end + +return select_character diff --git a/lua/t9_preedit.lua b/lua/t9_preedit.lua new file mode 100644 index 0000000..635331c --- /dev/null +++ b/lua/t9_preedit.lua @@ -0,0 +1,15 @@ +-- 九宫格专用 +-- 拼写规则通过 xlit 转写: xlit/abcdefghijklmnopqrstuvwxyz/22233344455566677778889999/ +-- 然后通过此 Lua 将输入框的数字转为对应的拼音或英文 +local function t9_preedit(input, env) + for cand in input:iter() do + if (string.find(cand.text, "%w+") ~= nil) then + cand:get_genuine().preedit = cand.text + else + cand:get_genuine().preedit = cand.comment + end + yield(cand) + end +end + +return t9_preedit diff --git a/lua/unicode.lua b/lua/unicode.lua new file mode 100644 index 0000000..3e24d28 --- /dev/null +++ b/lua/unicode.lua @@ -0,0 +1,19 @@ +-- Unicode +-- 示例:输入 U62fc 得到「拼」 +-- 复制自: https://github.com/shewer/librime-lua-script/blob/main/lua/component/unicode.lua +local function unicode(input, seg, env) + local ucodestr = seg:has_tag("unicode") and input:match("U(%x+)") + if ucodestr and #ucodestr > 1 then + 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 + 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))) + end + end + end +end + +return unicode diff --git a/lua/v_filter.lua b/lua/v_filter.lua new file mode 100644 index 0000000..858695b --- /dev/null +++ b/lua/v_filter.lua @@ -0,0 +1,32 @@ +-- v 模式,单个字符优先 +-- 因为设置了英文翻译器的 initial_quality 大于 1,导致输入「va」时,候选项是「van vain …… ā á ǎ à」 +-- 把候选项应改为「ā á ǎ à …… van vain」,让单个字符的排在前面 +-- 感谢改进 @[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."}) + -- 仅当当前输入以 v 开头,并且编码长度为 2,才进行处理 + if (string.len(code) == 2 and string.find(code, "^v")) then + local l = {} + for cand in input:iter() do + -- 特殊情况处理 + if (env.v_spec_arr[cand.text]) then + yield(cand) + -- 候选项为单个字符的,提到前面来。 + elseif (utf8.len(cand.text) == 1) then + yield(cand) + else + table.insert(l, cand) + end + end + for _, cand in ipairs(l) do + yield(cand) + end + else + for cand in input:iter() do + yield(cand) + end + end +end + +return v_filter diff --git a/others/recipes/all_dicts.recipe.yaml b/others/recipes/all_dicts.recipe.yaml index da33054..738a792 100644 --- a/others/recipes/all_dicts.recipe.yaml +++ b/others/recipes/all_dicts.recipe.yaml @@ -6,15 +6,6 @@ recipe: description: >- 所有词库安装、更新 install_files: >- - cn_dicts/8105.dict.yaml - cn_dicts/41448.dict.yaml - cn_dicts/base.dict.yaml - cn_dicts/ext.dict.yaml - cn_dicts/tencent.dict.yaml - cn_dicts/others.dict.yaml - en_dicts/en.dict.yaml - en_dicts/en_ext.dict.yaml - en_dicts/cn_en.dict.yaml - opencc/emoji.json - opencc/emoji.txt - opencc/others.txt + cn_dicts/*.* + en_dicts/*.* + opencc/*.* diff --git a/others/recipes/cn_dicts.recipe.yaml b/others/recipes/cn_dicts.recipe.yaml index a2c173b..99be13d 100644 --- a/others/recipes/cn_dicts.recipe.yaml +++ b/others/recipes/cn_dicts.recipe.yaml @@ -6,9 +6,4 @@ recipe: description: >- 拼音词库安装、更新 install_files: >- - cn_dicts/8105.dict.yaml - cn_dicts/41448.dict.yaml - cn_dicts/base.dict.yaml - cn_dicts/ext.dict.yaml - cn_dicts/tencent.dict.yaml - cn_dicts/others.dict.yaml + cn_dicts/*.* diff --git a/others/recipes/en_dicts.recipe.yaml b/others/recipes/en_dicts.recipe.yaml index 0e5d689..dcfcf40 100644 --- a/others/recipes/en_dicts.recipe.yaml +++ b/others/recipes/en_dicts.recipe.yaml @@ -6,6 +6,4 @@ recipe: description: >- 英文词库安装、更新 install_files: >- - en_dicts/en.dict.yaml - en_dicts/en_ext.dict.yaml - en_dicts/cn_en.dict.yaml + en_dicts/*.* diff --git a/others/recipes/full.recipe.yaml b/others/recipes/full.recipe.yaml index 7d17965..3edca59 100644 --- a/others/recipes/full.recipe.yaml +++ b/others/recipes/full.recipe.yaml @@ -6,18 +6,10 @@ recipe: description: >- 完整安装、更新 install_files: >- - cn_dicts/8105.dict.yaml - cn_dicts/41448.dict.yaml - cn_dicts/base.dict.yaml - cn_dicts/ext.dict.yaml - cn_dicts/tencent.dict.yaml - cn_dicts/others.dict.yaml - en_dicts/en.dict.yaml - en_dicts/en_ext.dict.yaml - en_dicts/cn_en.dict.yaml - opencc/emoji.json - opencc/emoji.txt - opencc/others.txt + cn_dicts/*.* + en_dicts/*.* + opencc/*.* + lua/*.* default.yaml squirrel.yaml rime_ice.schema.yaml @@ -32,6 +24,5 @@ install_files: >- liangfen.dict.yaml melt_eng.schema.yaml melt_eng.dict.yaml - rime.lua custom_phrase.txt zh-hans-t-essay-bgw.gram diff --git a/others/recipes/opencc.recipe.yaml b/others/recipes/opencc.recipe.yaml index 5af7577..1633883 100644 --- a/others/recipes/opencc.recipe.yaml +++ b/others/recipes/opencc.recipe.yaml @@ -6,6 +6,4 @@ recipe: description: >- opencc(Emoji)安装、更新 install_files: >- - opencc/emoji.json - opencc/emoji.txt - opencc/others.txt + opencc/*.* diff --git a/rime.lua b/rime.lua deleted file mode 100644 index feeb5f2..0000000 --- a/rime.lua +++ /dev/null @@ -1,313 +0,0 @@ --- Rime Lua 扩展 https://github.com/hchunhui/librime-lua --- 文档 https://github.com/hchunhui/librime-lua/wiki/Scripting -------------------------------------------------------------- --- 日期时间 --- 提高权重的原因:因为在方案中设置了大于 1 的 initial_quality,导致 rq sj xq dt ts 产出的候选项在所有词语的最后。 -function date_translator(input, seg, env) - local config = env.engine.schema.config - local date = config:get_string(env.name_space .. "/date") or "rq" - local time = config:get_string(env.name_space .. "/time") or "sj" - local week = config:get_string(env.name_space .. "/week") or "xq" - local datetime = config:get_string(env.name_space .. "/datetime") or "dt" - local timestamp = config:get_string(env.name_space .. "/timestamp") or "ts" - -- 日期 - if (input == 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 == 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 == 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 == 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 == 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 -------------------------------------------------------------- --- 以词定字 --- https://github.com/BlindingDark/rime-lua-select-character --- 删除了默认按键,需要在 key_binder 下设置 -local function utf8_sub(s, i, j) - i = i or 1 - j = j or -1 - - if i < 1 or j < 1 then - local n = utf8.len(s) - if not n then - return nil - end - if i < 0 then - i = n + 1 + i - end - if j < 0 then - j = n + 1 + j - end - if i < 0 then - i = 1 - elseif i > n then - i = n - end - if j < 0 then - j = 1 - elseif j > n then - j = n - end - end - - if j < i then - return "" - end - - i = utf8.offset(s, i) - j = utf8.offset(s, j + 1) - - if i and j then - return s:sub(i, j - 1) - elseif i then - return s:sub(i) - else - return "" - end -end - -local function first_character(s) - return utf8_sub(s, 1, 1) -end - -local function last_character(s) - return utf8_sub(s, -1, -1) -end - -function select_character(key, env) - local engine = env.engine - local context = engine.context - local commit_text = context:get_commit_text() - local config = engine.schema.config - - -- local first_key = config:get_string('key_binder/select_first_character') or 'bracketleft' - -- local last_key = config:get_string('key_binder/select_last_character') or 'bracketright' - local first_key = config:get_string('key_binder/select_first_character') - local last_key = config:get_string('key_binder/select_last_character') - - if (key:repr() == first_key and commit_text ~= "") then - engine:commit_text(first_character(commit_text)) - context:clear() - - return 1 -- kAccepted - end - - if (key:repr() == last_key and commit_text ~= "") then - engine:commit_text(last_character(commit_text)) - context:clear() - - return 1 -- kAccepted - end - - return 2 -- kNoop -end -------------------------------------------------------------- --- 长词优先(提升「西安」「提案」「图案」「饥饿」等词汇的优先级) --- 感谢&参考于: https://github.com/tumuyan/rime-melt --- 修改:不提升英文和中英混输的 -function long_word_filter(input, env) - -- 提升 count 个词语,插入到第 idx 个位置,默认 2、4。 - local config = env.engine.schema.config - local count = config:get_int(env.name_space .. "/count") or 2 - local idx = config:get_int(env.name_space .. "/idx") or 4 - - 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 < idx) then - i = i + 1 - firstWordLength = leng - yield(cand) - elseif ((leng > firstWordLength) and (done < 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 == 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 -------------------------------------------------------------- --- 降低部分英语单词在候选项的位置 --- https://dvel.me/posts/make-rime-en-better/#短单词置顶的问题 --- 感谢大佬 @[Shewer Lu](https://github.com/shewer) 指点 -function reduce_english_filter(input, env) - local config = env.engine.schema.config - -- load data - if not env.idx then - env.idx = config:get_int(env.name_space .. "/idx") -- 要插入的位置 - end - if not env.words then - 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 -------------------------------------------------------------- --- v 模式,单个字符优先 --- 因为设置了英文翻译器的 initial_quality 大于 1,导致输入「va」时,候选项是「van vain …… ā á ǎ à」 --- 把候选项应改为「ā á ǎ à …… van vain」,让单个字符的排在前面 --- 感谢改进 @[t123yh](https://github.com/t123yh) @[Shewer Lu](https://github.com/shewer) -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."}) - -- 仅当当前输入以 v 开头,并且编码长度为 2,才进行处理 - if (string.len(code) == 2 and string.find(code, "^v")) then - local l = {} - for cand in input:iter() do - -- 特殊情况处理 - if (env.v_spec_arr[cand.text]) then - yield(cand) - -- 候选项为单个字符的,提到前面来。 - elseif (utf8.len(cand.text) == 1) then - yield(cand) - else - table.insert(l, cand) - end - end - for _, cand in ipairs(l) do - yield(cand) - end - else - for cand in input:iter() do - yield(cand) - end - end -end -------------------------------------------------------------- --- iRime 九宫格专用,将输入框的数字转为对应的拼音或英文 -function irime_t9_preedit(input, env) - for cand in input:iter() do - if (string.find(cand.text, "%w+") ~= nil) then - cand:get_genuine().preedit = cand.text - else - cand:get_genuine().preedit = cand.comment - end - yield(cand) - end -end -------------------------------------------------------------- --- Unicode 输入 --- 复制自: https://github.com/shewer/librime-lua-script/blob/main/lua/component/unicode.lua -function unicode(input, seg, env) - local ucodestr = seg:has_tag("unicode") and input:match("U(%x+)") - if ucodestr and #ucodestr > 1 then - 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 - 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))) - end - end - end -end -------------------------------------------------------------- --- 为用户词典中(输入过)的内容结尾加上一个星号 * -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 diff --git a/rime_ice.schema.yaml b/rime_ice.schema.yaml index d441504..616c42d 100644 --- a/rime_ice.schema.yaml +++ b/rime_ice.schema.yaml @@ -42,7 +42,7 @@ switches: # 输入引擎 engine: processors: - - lua_processor@select_character # 以词定字 + - lua_processor@*select_character # 以词定字 - ascii_composer - recognizer - key_binder @@ -60,19 +60,19 @@ engine: translators: - punct_translator - script_translator - - lua_translator@date_translator # 时间、日期、星期 + - lua_translator@*date_translator # 时间、日期、星期 - table_translator@custom_phrase # 自定义短语 custom_phrase.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@v_filter # v 模式 symbols 优先(否则是英文优先) - # - lua_filter@is_in_user_dict # 为用户词典中(输入过)的内容结尾加上一个星号 * - - lua_filter@reduce_english_filter # 降低部分英语单词在候选项的位置 - - lua_filter@long_word_filter # 长词优先 - - uniquifier # 去重 + - simplifier@emoji # Emoji + - simplifier@traditionalize # 简繁切换 + # - lua_filter@*is_in_user_dict # 为用户词典中(输入过)的内容结尾加上一个星号 * + - lua_filter@*v_filter # v 模式 symbols 优先(否则是英文优先) + - lua_filter@*reduce_english_filter # 降低部分英语单词在候选项的位置 + - lua_filter@*long_word_filter # 长词优先 + - uniquifier # 去重 # Lua 配置: 日期、时间、星期、ISO 8601、时间戳的触发关键字