From 7ec5675724189db37bcc51b6ede8d15f5b515e33 Mon Sep 17 00:00:00 2001 From: derek0x <372765@qq.com> Date: Fri, 17 May 2024 15:56:55 +0800 Subject: [PATCH 1/2] =?UTF-8?q?ctrl=20+=20shift=20+=20j=20=20=20=E7=BC=96?= =?UTF-8?q?=E7=A0=81=E9=9A=90=E8=97=8F=E7=9A=84=E6=83=85=E5=86=B5=E4=B8=8B?= =?UTF-8?q?=EF=BC=8C=E5=8F=AA=E9=9A=90=E8=97=8F=E6=8B=BC=E9=9F=B3=E4=B8=8D?= =?UTF-8?q?=E9=9A=90=E8=97=8F=E7=BC=96=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lua/new_spelling.lua | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/lua/new_spelling.lua b/lua/new_spelling.lua index cce5e12..cda3d6a 100644 --- a/lua/new_spelling.lua +++ b/lua/new_spelling.lua @@ -170,7 +170,8 @@ local function get_tricomment(cand, env) if spll_raw ~= '' then if env.engine.context:get_option("new_hide_pinyin") then -- return xform(spll_raw:gsub('%[(.-,.-),.+%]', '[%1]')) - return xform(spll_raw:gsub('%[(.-),.+%]', '[%1]')) + -- return xform(spll_raw:gsub('%[(.-),.+%]', '[%1]')) + return xform(spll_raw:gsub('%[(.-),(.-),(.-)%]', '[%1'..' · '..'%2]')) else return xform(spll_raw:gsub('%[(.-),(.-),(.-),(.-)%]', '[%1'..' · '..'%2'..' · '..'%3]')) end -- Gitee From 4f832eb4d031ce56e0290dbb5d1b316deb6ef0c7 Mon Sep 17 00:00:00 2001 From: derek0x <372765@qq.com> Date: Sat, 18 May 2024 07:32:49 +0800 Subject: [PATCH 2/2] =?UTF-8?q?=E6=94=AF=E6=8C=81=E5=A4=9A=E9=87=8D?= =?UTF-8?q?=E5=88=87=E6=8D=A2=EF=BC=9A=20=20=20-=20=E5=85=B3=E9=97=AD=20?= =?UTF-8?q?=20=20-=20=E5=AD=97=E6=A0=B9=20=EF=BC=88default=EF=BC=89=20=20?= =?UTF-8?q?=20-=20=E5=AD=97=E6=A0=B9=20+=20=E7=BC=96=E7=A0=81=20=20=20-=20?= =?UTF-8?q?=E5=AD=97=E6=A0=B9=20+=20=E7=BC=96=E7=A0=81=20+=20=E6=8B=BC?= =?UTF-8?q?=E9=9F=B3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lua/lib/basic.lua | 113 +++++++++++++++++++++++++++++++++++++ lua/new_spelling.lua | 64 ++++++++++++++++++--- lua/spelling_processor.lua | 10 ++++ wubi_ci.schema.yaml | 37 ++++++++---- 4 files changed, 204 insertions(+), 20 deletions(-) create mode 100644 lua/spelling_processor.lua diff --git a/lua/lib/basic.lua b/lua/lib/basic.lua index 98da0b6..2008a8c 100644 --- a/lua/lib/basic.lua +++ b/lua/lib/basic.lua @@ -2,6 +2,7 @@ local basic = {} package.loaded[...] = basic +basic.encoder = {} function basic.index(table, item) for k, v in pairs(table) do @@ -47,3 +48,115 @@ function basic.utf8sub(str, first, ...) return string.sub(str, fstoff, lstoff) end +function basic.encoder.parse_formula(formula) + if type(formula) ~= 'string' or formula:gsub('%u%l', '') ~= '' then + return + end + local rule = {} + local A, a, U, u, Z, z = ('AaUuZz'):byte(1, -1) + for m in formula:gmatch('%u%l') do + local upper, lower = m:byte(1, 2) + local char_idx = upper < U and upper - A + 1 or upper - Z - 1 + local code_idx = lower < u and lower - a + 1 or lower - z - 1 + rule[#rule + 1] = {char_idx, code_idx} + end + return rule +end + +function basic.encoder.load_settings(setting) + -- 注意到公式同则规则同,可通过 f2r 在 rt 中作引用定义,以节省资源。 + local ft, f2r, rt = {}, {}, {} + for _, t in ipairs(setting) do + if t.length_equal then + ft[t.length_equal] = t.formula + elseif t.length_in_range then + local min, max = table.unpack(t.length_in_range) + for l = min, max do ft[l] = t.formula end + end + end + -- setting 中的 length 不一定连续且一般不包括 1,所以不能用 ipairs()。 + for k, f in pairs(ft) do + local rule = basic.encoder.parse_formula(f) + if not rule then return end + if not f2r[f] then f2r[f] = rule end + rt[k] = f2r[f] + end + return rt +end + +function basic.switch_option(name, context) + context:set_option(name, not context:get_option(name)) +end + +function basic.cycle_options(options, env, reverse) + local context = env.engine.context + if #options == 0 then return 0 end + if #options == 1 then + basic.switch_option(options[1], context) + return 1 + end + local state + for k, v in ipairs(options) do + if context:get_option(v) then + context:set_option(v, false) + state = (reverse and (k - 1) or (k + 1)) % #options + if state == 0 then state = #options end + break + end + end + local k = state or options.save or options.default or 1 + context:set_option(options[k], true) + return k +end + +-- Set an option in 'options' if no one is set yet. +function basic.init_options(options, context) + for k, v in ipairs(options) do if context:get_option(v) then return end end + local k = state or options.save or options.default or 1 + context:set_option(options[k], true) +end + +-- Generate a processor that cycle a group of options with a key. +-- For now only works when composing. +function basic.make_option_cycler(options, cycle_key_config_path, + switch_key_config_path, reverse) + local processor, cycle_key, switch_key = {} + processor.init = function(env) + local config = env.engine.schema.config + basic.init_options(options, env.engine.context) + cycle_key = config:get_string(cycle_key_config_path) + switch_key = config:get_string(switch_key_config_path) + end + processor.func = function(key, env) + local context = env.engine.context + if context:is_composing() and key:repr() == cycle_key then + local state = basic.cycle_options(options, env, reverse) + if state > 1 then options.save = state end + return 1 + elseif context:is_composing() and key:repr() == switch_key then + -- 选项状态可能在切换方案时被重置,因此需检测更新。但是不能在 filter.init + -- 中检测,因为得到的似乎是重置之前的状态,说明组件初始化先于状态重置。为 + -- 经济计,仅在手动切换开关时检测。 + -- https://github.com/rime/librime/issues/449 + -- Todo: 对于较新的 librime-lua,尝试利用 option_update_notifier 更新 + -- options.save + for k, v in ipairs(options) do + if context:get_option(v) then + if k > 1 then options.save = k end + end + end + local k = options.save or options.default + -- Consider the 1st options as OFF state. + if context:get_option(options[1]) then + context:set_option(options[1], false) + context:set_option(options[k], true) + else + context:set_option(options[k], false) + context:set_option(options[1], true) + end + return 1 + end + return 2 -- kNoop + end + return processor +end \ No newline at end of file diff --git a/lua/new_spelling.lua b/lua/new_spelling.lua index cda3d6a..89acb21 100644 --- a/lua/new_spelling.lua +++ b/lua/new_spelling.lua @@ -163,19 +163,18 @@ local function get_en_code(s, spll_rvdb) end end + local function get_tricomment(cand, env) local ctext = cand.text if utf8.len(ctext) == 1 then local spll_raw = env.spll_rvdb:lookup(ctext) - if spll_raw ~= '' then - if env.engine.context:get_option("new_hide_pinyin") then - -- return xform(spll_raw:gsub('%[(.-,.-),.+%]', '[%1]')) - -- return xform(spll_raw:gsub('%[(.-),.+%]', '[%1]')) - return xform(spll_raw:gsub('%[(.-),(.-),(.-)%]', '[%1'..' · '..'%2]')) - else - return xform(spll_raw:gsub('%[(.-),(.-),(.-),(.-)%]', '[%1'..' · '..'%2'..' · '..'%3]')) - end - end + if spll_raw == '' then return end + return env.engine.context:get_option('spelling.lv1') and + xform(spll_raw:gsub('%[(.-),.*%]', '[%1]')) or + env.engine.context:get_option('spelling.lv2') and + xform(spll_raw:gsub('%[(.-,.-),.*%]', '[%1]')) or + env.engine.context:get_option('spelling.lv3') and + xform(spll_raw:gsub('%[(.-,.-,.-),.*%]', '[%1]')) else local spelling = spell_phrase(ctext, env.spll_rvdb) if spelling ~= '' then @@ -236,7 +235,54 @@ local function get_horizontal_style(filename,item) end end +local function generate_candidate(cand, comment) + local type = cand:get_dynamic_type() + + if type == 'Shadow' then + if ShadowCandidate then + cand = cand:to_shadow_candidate(cand.type, cand.text, comment, true) + else + cand = Candidate(cand.type, cand.text, comment) + end + elseif type == 'Uniquified' then + if UniquifiedCandidate then + cand = cand:to_uniquified_candidate(cand.type, cand.text, comment, true) + else + cand = Candidate(cand.type, cand.text, comment) + end + else + cand.comment = comment + end + return cand +end + local function filter(input, env) + if env.engine.context:get_option('spelling.off') then + for cand in input:iter() do yield(cand) end + return + end + + for cand in input:iter() do + if cand.type == 'simplified' and env.name_space == 'spelling_reverse' then + local comment = (get_tricomment(cand, env) or '') .. cand.comment + cand = generate_candidate(cand, comment) + else + local add_comment = cand.type == 'punct' and + env.code_rvdb:lookup(cand.text) or cand.type ~= + 'sentence' and get_tricomment(cand, env) + if add_comment and add_comment ~= '' then + -- 混输和反查中的非 completion 类型,原注释为空或主词典的编码。 + -- 为免重复冗长,直接以新增注释替换之。前提是后者非空。 + cand.comment = cand.type ~= 'completion' and + ((env.name_space == 'hmsp' and + env.is_mixtyping) or + (env.name_space == 'hmsp_for_rvlk')) and + add_comment or add_comment .. cand.comment + end + end + yield(cand) + end + local codetext=env.engine.context.input -- 获取编码 local script_text=env.engine.context:get_script_text() local hide_pinyin=env.engine.context:get_option("new_hide_pinyin") diff --git a/lua/spelling_processor.lua b/lua/spelling_processor.lua new file mode 100644 index 0000000..7217e19 --- /dev/null +++ b/lua/spelling_processor.lua @@ -0,0 +1,10 @@ +local basic = require('lib/basic') +-- options 要与方案保持一致 +local options = { 'spelling.off', 'spelling.lv1', 'spelling.lv2', 'spelling.lv3' } +options.default = 4 + +local processor = basic.make_option_cycler(options, + 'spelling/lua/cycle_key', + 'spelling/lua/switch_key') + +return processor diff --git a/wubi_ci.schema.yaml b/wubi_ci.schema.yaml index c722dd9..8e08efa 100644 --- a/wubi_ci.schema.yaml +++ b/wubi_ci.schema.yaml @@ -26,18 +26,18 @@ switches: - name: zh_trad reset: 0 states: [ 简,繁 ] - - name: new_spelling - reset: 1 - states: [ 隐,显 ] - - name: rvl_zhuyin - reset: 1 - states: [ 形,拼 ] +# - name: new_spelling +# reset: 1 +# states: [ 隐,显 ] +# - name: rvl_zhuyin +# reset: 1 +# states: [ 形,拼 ] - name: GB2312 reset: 1 states: [ 扩,常 ] - - name: new_hide_pinyin - reset: 1 - states: [ 音,哑 ] +# - name: new_hide_pinyin +# reset: 1 +# states: [ 音,哑 ] - name: single_char reset: 0 states: [ 词,单 ] @@ -47,6 +47,13 @@ switches: - name: full_shape reset: 0 states: [ 半,全 ] + - options: + - spelling.off + - spelling.lv1 + - spelling.lv2 + - spelling.lv3 + reset: 1 + states: [ (-)关三重, (-)拆, (-)拆+编, (-)拆+编+音 ] engine: filters: - lua_filter@new_spelling@spelling @@ -58,6 +65,7 @@ engine: processors: - ascii_composer + - lua_processor@spelling_processor - lua_processor@submit_text_processor - lua_processor@switch_processor - recognizer @@ -210,6 +218,13 @@ simplifier: lua_reverse_db: spelling: wb_spelling code: wb_spelling +spelling: + tags: [ abc, encode, punct ] + lua: + switch_key: 'Control+c' + cycle_key: 'Shift+Control+C' + # 注意写法:Shift 在前,Control 在后。 + recognizer: import_preset: default patterns: @@ -238,8 +253,8 @@ key_binder: - {toggle: zh_trad, accept: "Control+Shift+4", when: always} - {toggle: zh_trad, accept: "Control+Shift+dollar", when: always} - {toggle: zh_trad, accept: "Control+Shift+F", when: always} - - {toggle: new_spelling, accept: "Control+Shift+H", when: always} - - {toggle: new_hide_pinyin, accept: "Control+Shift+J", when: always} +# - {toggle: new_spelling, accept: "Control+Shift+H", when: always} +# - {toggle: new_hide_pinyin, accept: "Control+Shift+J", when: always} - {toggle: single_char, accept: "Control+Shift+K", when: always} - {toggle: GB2312, accept: "Control+Shift+U", when: always} # 回车清空编码 -- Gitee