Skip to content

Commit

Permalink
feat: 簡快碼提示濾鏡
Browse files Browse the repository at this point in the history
  • Loading branch information
ksqsf committed Jan 8, 2024
1 parent ab8473d commit fc87bd9
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 1 deletion.
62 changes: 62 additions & 0 deletions lua/moran_quick_code_hint.lua
Original file line number Diff line number Diff line change
@@ -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
6 changes: 5 additions & 1 deletion moran.defaults.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions moran.schema.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit fc87bd9

Please sign in to comment.