Skip to content

Commit

Permalink
万象更新
Browse files Browse the repository at this point in the history
  • Loading branch information
amzxyz committed Feb 8, 2025
1 parent 74010b8 commit fad1230
Showing 1 changed file with 19 additions and 30 deletions.
49 changes: 19 additions & 30 deletions lua/demote_derive.lua
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
--万象为了降低1位辅助码权重保证分词,提升2位辅助码权重为了4码高效,但是有些时候单字超越了词组,如自然码中:jmma 睑 剑麻,于是调序 剑麻 睑
--abbrev的时候通过辅助码匹配提权单字放在双字后面第一位
local M = {}
function M.run_fuzhu(cand, env, initial_comment)

-- 获取辅助码的处理逻辑
function M.run_fuzhu(cand, initial_comment)
local final_comment = nil
local fuzhu_comments = {}

Expand All @@ -27,27 +29,19 @@ function M.run_fuzhu(cand, env, initial_comment)
return final_comment or "" -- 确保返回最终值
end

-- **初始化函数**
function M.init(env)
local config = env.engine.schema.config
env.settings = {
fuzhu_type = config:get_string("pro_comment_format/fuzhu_type") or ""
}
end

function M.func(input, env)
local context = env.engine.context
local input_code = context.input -- 获取输入码

-- 只有当输入码长度等于 4 时才处理
if utf8.len(input_code) ~= 4 then
-- 只有当输入码长度为 3 或 4 时才处理
if utf8.len(input_code) < 3 or utf8.len(input_code) > 4 then
for cand in input:iter() do
yield(cand) -- 直接按原顺序输出
end
return
end

local candidates = {} -- 存储前 10 个符合条件的候选词
local candidates = {} -- 存储符合条件的候选词
local others = {} -- 存储剩余的候选词
local single_char_cands = {} -- 存储单字候选
local double_char_cands = {} -- 存储双字候选
Expand All @@ -58,8 +52,8 @@ function M.func(input, env)
end

-- **修改 `get_comment()`,使用 `M.run_fuzhu()` 获取辅助码**
local function get_comment(cand, env)
return M.run_fuzhu(cand, env, cand.comment or "")
local function get_comment(cand)
return M.run_fuzhu(cand, cand.comment or "")
end

-- 获取输入码的后两个字符
Expand All @@ -71,16 +65,11 @@ function M.func(input, env)
local len = utf8.len(cand.text)

if len == 2 and not is_alnum(cand.text) then
if count < 10 then
table.insert(double_char_cands, cand) -- 只存前 10 个双字词
else
table.insert(others, cand) -- 超过 10 个的,按原顺序放入 others
end
count = count + 1
table.insert(double_char_cands, cand) -- 存储双字候选
elseif len == 1 and not is_alnum(cand.text) then
table.insert(single_char_cands, cand) -- 存储单字
table.insert(single_char_cands, cand) -- 存储单字候选
else
table.insert(others, cand) -- 不符合长度要求或是字母/数字的,按原顺序存储
table.insert(others, cand) -- 不符合条件的候选词存入 others
end
end

Expand All @@ -89,8 +78,8 @@ function M.func(input, env)
local moved_single = nil

for _, cand in ipairs(single_char_cands) do
local comment = get_comment(cand, env) -- **调用 `M.run_fuzhu()` 解析辅助码**
local comment = get_comment(cand) -- **调用 `M.run_fuzhu()` 解析辅助码**

-- **使用 `,` 逗号分割辅助码,并检查是否有一个片段匹配**
local matched = false
for segment in comment:gmatch("[^,]+") do
Expand All @@ -101,23 +90,23 @@ function M.func(input, env)
end

if matched then
moved_single = cand -- 记录这个单字
moved_single = cand -- 记录匹配的单字
else
table.insert(reordered_singles, cand)
table.insert(reordered_singles, cand) -- 不匹配的单字放入重新排序的列表
end
end

-- 输出双字词
-- 输出双字候选
for _, cand in ipairs(double_char_cands) do
yield(cand)
end

-- 如果找到匹配的单字,先输出双字词,再放到单字的第一位
-- 如果找到匹配的单字,先输出双字候选,再放到单字的第一位
if moved_single then
yield(moved_single)
end

-- 输出其余单字
-- 输出其余单字候选
for _, cand in ipairs(reordered_singles) do
yield(cand)
end
Expand All @@ -128,4 +117,4 @@ function M.func(input, env)
end
end

return M
return M

0 comments on commit fad1230

Please sign in to comment.