Skip to content

Commit

Permalink
Merge pull request #6 from lost-melody/main
Browse files Browse the repository at this point in the history
  • Loading branch information
forFudan committed May 22, 2023
2 parents c0c0479 + 2f03f2c commit 0588051
Show file tree
Hide file tree
Showing 5 changed files with 154 additions and 5 deletions.
133 changes: 133 additions & 0 deletions beta/schema/lua/yuhao/yuhao_embeded_cands.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
-- 将要被返回的過濾器對象
local embeded_cands_filter = {}

--[[
# xxx.schema.yaml
switches:
- name: embeded_cands
states: [ 普通, 嵌入 ]
reset: 1
engine:
filters:
- lua_filter@*smyh.embeded_cands
key_binder:
bindings:
- { when: always, accept: "Control+Shift+E", toggle: embeded_cands }
--]]

-- 讀取 schema.yaml 開關設置:
local option_name = "embeded_cands"
local embeded_cands = nil

function embeded_cands_filter.init(env)
local handler = function(ctx, name)
-- 通知回調, 當改變選項值時更新暫存的值
if name == option_name then
embeded_cands = ctx:get_option(name)
if embeded_cands == nil then
-- 當選項不存在時默認爲啟用狀態
embeded_cands = true
end
end
end
-- 初始化爲選項實際值, 如果設置了 reset, 則會再次觸發 handler
handler(env.engine.context, option_name)
-- 注册通知回調
env.engine.context.option_update_notifier:connect(handler)
end

-- 過濾器
function embeded_cands_filter.func(input, env)
if not embeded_cands then
for cand in input:iter() do
yield(cand)
end
return
end

-- 要顯示的候選數量
local page_size = env.engine.schema.page_size
-- 暫存當前頁候選, 然后批次送出
local page_cands = {}
-- 暫存索引, 首選和預編輯文本
local index, first_cand, preedit = 0, nil, ""

-- 迭代器
local iter, obj = input:iter()
-- 迭代由翻譯器輸入的候選列表
local next = iter(obj)
while next do
-- 頁索引自增, 滿足 1 <= index <= page_size
index = index + 1
-- 當前遍歷候選項
local cand = next

if index == 1 then
-- 把首選捉出來
first_cand = cand:get_genuine()
end

-- 修改首選的預编輯文本, 這会作爲内嵌編碼顯示到輸入處
if index == 1 then
-- 首選和編碼
-- 這裏的if块可以直接改成一個 preedit = preedit..cand.text
if string.len(cand.text) <= string.len("四個漢字") then
-- 四字以内, "漢字code"
preedit = cand.text..first_cand.preedit
else
-- 四字以上, "code四字以上詞"
preedit = first_cand.preedit..cand.text
end
elseif index <= page_size then
-- 當前頁余下候選項, 形如 "2.漢字"
-- 組合顯示爲 "首選code 2.次選 3.三選 ..."
preedit = preedit.." "..tostring(index).."."..cand.text
end

-- 如果候選有提示且不以 "~" 開頭(补全提示), 識别爲反查提示
if string.len(cand.comment) ~= 0 and string.sub(cand.comment, 1, 1) ~= "~" then
if index == 1 then
-- 首選後額外增加一空格, 以将輸入編碼與反查分隔
preedit = preedit.." "
end
preedit = preedit..cand.comment
end
-- 存入首選
table.insert(page_cands, cand)

-- 遍歷完一頁候選後, 刷新預編輯文本
if index == page_size then
first_cand.preedit = preedit
-- 将暫存的一頁候選批次送出
for _, c in ipairs(page_cands) do
yield(c)
end
-- 清空暫存
first_cand, preedit = nil, ""
page_cands = {}
end

-- 當前候選處理完畢, 查詢下一個
next = iter(obj)

-- 如果當前暫存候選不足page_size但没有更多候選, 則需要刷新預編輯並送出
if not next and index < page_size then
first_cand.preedit = preedit
-- 将暫存的前三候選批次送出
for _, c in ipairs(page_cands) do
yield(c)
end
-- 清空暫存
first_cand, preedit = nil, ""
page_cands = {}
end

-- 下一頁, index歸零
index = index % page_size
end
end

function embeded_cands_filter.fini(env)
end

return embeded_cands_filter
5 changes: 3 additions & 2 deletions beta/schema/rime.lua
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ yuhao_char_only = yuhao_char_filter.yuhao_char_only
yuhao_single_char_only_for_full_code = require("yuhao/yuhao_single_char_only_for_full_code")
yuhao_postpone_full_code = require("yuhao/yuhao_postpone_full_code")
yuhao_helper = require("yuhao/yuhao_helper")
temp = require("yuhao/yuhao_chaifen")
local temp = require("yuhao/yuhao_chaifen")
yuhao_chaifen = temp.filter
yuhao_chaifen_processor = temp.processor
yuhao_chaifen_processor = temp.processor
yuhao_embeded_cands = require("yuhao.yuhao_embeded_cands")
7 changes: 6 additions & 1 deletion beta/schema/yuhao.schema.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,9 @@ switches:
reset: 0
states: [繁保持, 繁转简]
# Control+Shift+J
- name: embeded_cands
reset: 0
states: [普通, 嵌入候选]
- name: ascii_punct
states: [。,, .,]
reset: 0
Expand Down Expand Up @@ -97,6 +100,7 @@ engine:
- simplifier@traditionalize
- simplifier@simplify
- lua_filter@yuhao_chaifen
- lua_filter@yuhao_embeded_cands
# - simplifier@chaifen
- uniquifier

Expand Down Expand Up @@ -219,6 +223,7 @@ key_binder:
- { when: always, accept: "Control+period", toggle: ascii_punct }
- { when: always, accept: "Control+Shift+J", toggle: simplification }
- { when: always, accept: "Control+Shift+F", toggle: traditionalization }
- { when: always, accept: "Control+Shift+E", toggle: embeded_cands }
- { when: always, accept: "Control+Shift+O", toggle: yuhao_char_only } # 常用詞過濾
- { when: always, accept: "Control+Shift+I", toggle: yuhao_char_first } # 常用詞前置
- { when: has_menu, accept: "0", toggle: yuhao_char_only } # 常用詞過濾
Expand Down Expand Up @@ -254,4 +259,4 @@ style:
preedit_type: preview

# menu:
# page_size: 5
# page_size: 5
7 changes: 6 additions & 1 deletion beta/schema/yuhao_tradition.schema.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,9 @@ switches:
states: [繁保持, 繁转简]
reset: 0
# Control+Shift+J
- name: embeded_cands
reset: 0
states: [普通, 嵌入候选]
- name: ascii_punct
states: [。,, .,]
reset: 0
Expand Down Expand Up @@ -91,6 +94,7 @@ engine:
- lua_filter@yuhao_postpone_full_code
- simplifier@simplify
- lua_filter@yuhao_chaifen
- lua_filter@yuhao_embeded_cands
# - simplifier@chaifen
- uniquifier

Expand Down Expand Up @@ -204,6 +208,7 @@ key_binder:
- { when: always, accept: "Control+period", toggle: ascii_punct }
- { when: always, accept: "Control+Shift+J", toggle: simplification }
# - { when: always, accept: "Control+Shift+F", toggle: traditionalization }
- { when: always, accept: "Control+Shift+E", toggle: embeded_cands }
- { when: always, accept: "Control+Shift+O", toggle: yuhao_char_only } # 常用詞過濾
- { when: always, accept: "Control+Shift+I", toggle: yuhao_char_first } # 常用詞前置
- { when: has_menu, accept: "0", toggle: yuhao_char_only } # 常用詞過濾
Expand Down Expand Up @@ -238,4 +243,4 @@ style:
preedit_type: preview

# menu:
# page_size: 5
# page_size: 5
7 changes: 6 additions & 1 deletion beta/schema/yuhao_tradition_tw.schema.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@ switches:
states: [繁保持, 繁转简]
reset: 0
# Control+Shift+J
- name: embeded_cands
reset: 0
states: [普通, 嵌入候选]
- name: ascii_punct
states: [。,, .,]
reset: 0
Expand Down Expand Up @@ -87,6 +90,7 @@ engine:
- lua_filter@yuhao_postpone_full_code
- simplifier@simplify
- lua_filter@yuhao_chaifen
- lua_filter@yuhao_embeded_cands
# - simplifier@chaifen
- uniquifier

Expand Down Expand Up @@ -193,6 +197,7 @@ key_binder:
- { when: always, accept: "Control+period", toggle: ascii_punct }
- { when: always, accept: "Control+Shift+J", toggle: simplification }
# - { when: always, accept: "Control+Shift+F", toggle: traditionalization }
- { when: always, accept: "Control+Shift+E", toggle: embeded_cands }
- { when: always, accept: "Control+Shift+O", toggle: yuhao_char_only } # 常用詞過濾
- { when: always, accept: "Control+Shift+I", toggle: yuhao_char_first } # 常用詞前置
- { when: has_menu, accept: "0", toggle: yuhao_char_only } # 常用詞過濾
Expand Down Expand Up @@ -227,4 +232,4 @@ style:
preedit_type: preview

# menu:
# page_size: 5
# page_size: 5

0 comments on commit 0588051

Please sign in to comment.