From fc87bd9090b0b8a128a1aed5c58b868cdcb4baa9 Mon Sep 17 00:00:00 2001 From: ksqsf Date: Mon, 8 Jan 2024 15:32:04 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E7=B0=A1=E5=BF=AB=E7=A2=BC=E6=8F=90?= =?UTF-8?q?=E7=A4=BA=E6=BF=BE=E9=8F=A1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lua/moran_quick_code_hint.lua | 62 +++++++++++++++++++++++++++++++++++ moran.defaults.yaml | 6 +++- moran.schema.yaml | 2 ++ 3 files changed, 69 insertions(+), 1 deletion(-) create mode 100644 lua/moran_quick_code_hint.lua diff --git a/lua/moran_quick_code_hint.lua b/lua/moran_quick_code_hint.lua new file mode 100644 index 0000000..5e2383d --- /dev/null +++ b/lua/moran_quick_code_hint.lua @@ -0,0 +1,62 @@ +local Module = {} + +function Module.init(env) + env.enable_quick_code_hint = env.engine.schema.config:get_bool("moran/enable_quick_code_hint") + log.error("moran_quick_code_hint: init " .. tostring(env.enable_quick_code_hint)) + if env.enable_quick_code_hint then + -- The user might have changed it. + local dict = env.engine.schema.config:get_string("fixed/dictionary") + log.error("moran_quick_code_hint: using dict" .. dict) + env.quick_code_hint_reverse = ReverseLookup(dict) + log.error("moran_quick_code_hint: enabled") + else + env.quick_code_hint_reverse = nil + log.error("moran_quick_code_hint: disabled") + end + + env.quick_code_indicator = env.engine.schema.config:get_string("moran/quick_code_indicator") + env.enable_aux_hint = env.engine.schema.config:get_bool("moran/enable_aux_hint") +end + +function Module.fini(env) + env.quick_code_hint_reverse = nil +end + +function Module.func(translation, env) + if (not env.enable_quick_code_hint) or env.quick_code_hint_reverse == nil then + for cand in translation:iter() do + yield(cand) + end + return + end + + -- Look up if the "geniune" candidate is already in the qc dict + local indicator = env.quick_code_indicator + if env.enable_aux_hint and indicator == "" then + indicator = "⚡" + end + for cand in translation:iter() do + local gcand = cand:get_genuine() + local word = gcand.text + local all_codes = env.quick_code_hint_reverse:lookup(word) + if all_codes then + local codes = {} + for code in all_codes:gmatch("%S+") do + if #code < 4 then + table.insert(codes, code) + end + end + if #codes > 0 then + -- do not show two indicators + if gcand.comment == indicator then + gcand.comment = gcand.comment .. table.concat(codes, " ") + else + gcand.comment = gcand.comment .. indicator .. table.concat(codes, " ") + end + end + end + yield(cand) + end +end + +return Module diff --git a/moran.defaults.yaml b/moran.defaults.yaml index d45185d..da7d37a 100644 --- a/moran.defaults.yaml +++ b/moran.defaults.yaml @@ -60,9 +60,13 @@ patch: #defer: 5 # 延遲多少位?若不設置该项,則默認值是推遲到第二頁 show_hint: true # 若讓全,則提示簡碼打法 - # 輔助碼提示 + # 單字輔助碼提示 enable_aux_hint: false + # 簡快碼提示(包括字和詞) + # 例如 輸入 yy te er 英特爾 會提示「⚡yte」 + enable_quick_code_hint: true + enable_language_model: grammar: language: zh-hant-t-essay-bgw diff --git a/moran.schema.yaml b/moran.schema.yaml index 0727305..ca1cc7e 100644 --- a/moran.schema.yaml +++ b/moran.schema.yaml @@ -75,6 +75,7 @@ engine: filters: - lua_filter@*moran_reorder_filter - lua_filter@*moran_aux_hint + - lua_filter@*moran_quick_code_hint - simplifier@emoji - charset_filter@gbk+emoji - charset_filter@big5+emoji @@ -272,6 +273,7 @@ moran: # 兜底默認值;以防用戶不提供任何值 quick_code_indicator: "⚡️" enable_word_filter: false enable_aux_hint: false + enable_quick_code_hint: false ijrq: enable: true #defer: 5