feat: pin_cand_filter.lua 支持用 > 分割含有空格的词汇 #586

This commit is contained in:
Dvel 2024-02-03 13:24:00 +08:00
parent 1534c28301
commit c865232561
2 changed files with 57 additions and 42 deletions

View File

@ -10,10 +10,6 @@ local function is_in_list(list, str)
return false, 0
end
local function starts_with(a, b)
return string.sub(a, 1, string.len(b)) == b
end
local M = {}
function M.init(env)
@ -35,9 +31,9 @@ function M.init(env)
-- 'l 了 啦' → M.pin_cands["l"] = {"了", "啦"}
-- 'ta 他 她 它' → M.pin_cands["ta"] = {"他", "她", "它"}
--
-- 无空格词汇,如 `nihao 你好` → M.pin_cands["nihao"] = {"你好"}
-- 无空格的键,如 `nihao 你好` → M.pin_cands["nihao"] = {"你好"}
--
-- 包含空格的词汇,同时生成简码的拼写(最后一个空格后的首字母),如:
-- 包含空格的的键,同时生成简码的拼写(最后一个空格后的首字母),如:
-- 'ni hao 你好 拟好' → M.pin_cands["nihao"] = {"你好", "拟好"}
-- → M.pin_cands["nih"] = {"你好", "拟好"}
--
@ -61,12 +57,18 @@ function M.init(env)
M.pin_cands = {}
for i = 0, list.size - 1 do
local preedit, texts = list:get_value_at(i).value:match("([^\t]+)\t(.+)")
-- use #text to match both nil and empty value
if #preedit > 0 and #texts > 0 then
-- 按照 " > " 或 " " 分割词汇
local delimiter = "\0"
if texts:find(" > ") then
texts = texts:gsub(" > ", delimiter)
else
texts = texts:gsub(" ", delimiter)
end
-- 按照键生成完整的拼写
local preedit_no_spaces = preedit:gsub(" ", "")
M.pin_cands[preedit_no_spaces] = {}
-- 按照配置生成完整的拼写
for text in texts:gmatch("%S+") do
for text in texts:gmatch("[^" .. delimiter .. "]+") do
table.insert(M.pin_cands[preedit_no_spaces], text)
end
-- 额外处理包含空格的 preedit增加最后一个拼音的首字母和 zh, ch, sh 的简码
@ -78,7 +80,7 @@ function M.init(env)
-- 只在没有明确定义此简码时才生成,已有的追加,没有的直接赋值
if not set[p1] then
if M.pin_cands[p1] ~= nil then
for text in texts:gmatch("%S+") do
for text in texts:gmatch("[^" .. delimiter .. "]+") do
table.insert(M.pin_cands[p1], text)
end
else
@ -91,7 +93,7 @@ function M.init(env)
-- 只在没有明确定义此简码时才生成,已有的追加,没有的直接赋值
if not set[p2] then
if M.pin_cands[p2] ~= nil then
for text in texts:gmatch("%S+") do
for text in texts:gmatch("[^" .. delimiter .. "]+") do
table.insert(M.pin_cands[p2], text)
end
else
@ -112,21 +114,33 @@ function M.func(input, env)
return
end
-- 当前输入框的 preedit未经过方案 translator/preedit_format 转换
-- 输入 nihaoshij 则为 nihaoshij选择了「你好」后变成 你好shij
local full_preedit = env.engine.context:get_preedit().text
-- 非汉字部分的 preedit如 shij
local letter_only_preedit = string.gsub(full_preedit, "[^a-zA-Z]", "")
-- 是否正在选词(已经选择了至少一个字词,如 `你好shij` 这种状态)
-- local isSelecting = full_preedit ~= letter_only_preedit
local pined = {} -- 提升的候选项
local others = {} -- 其余候选项
local pined_count = 0
for cand in input:iter() do
local preedit_no_spaces = cand.preedit:gsub(" ", "")
local cand_preedit_no_spaces = cand.preedit:gsub(" ", "")
-- 跳过不需要处理的部分,这样期望置顶的字词在句子开头及输入到一半时也会被置顶
if M.pin_cands[preedit_no_spaces] == nil then
-- 无关的输入直接 break
if string.find(letter_only_preedit, "^" .. cand_preedit_no_spaces) == nil then
yield(cand)
goto continue
break
end
local texts = M.pin_cands[preedit_no_spaces]
if texts then
local texts = M.pin_cands[cand_preedit_no_spaces]
-- 跳过不需要处理的部分,对后续的候选项排序
if texts == nil then
yield(cand)
else
-- 给 pined 几个空字符串占位元素,后面直接 pined[idx] = cand 确保 pined 与 texts 顺序一致
if #pined < #texts then
for _ = 1, #texts do
@ -134,17 +148,17 @@ function M.func(input, env)
end
end
-- 处理简繁转换后的问题
local candtext = cand.text
local cand_text = cand.text
if cand:get_dynamic_type() == "Shadow" then
-- handle cands converted by simplifier
local originalCand = cand:get_genuine()
if #originalCand.text == #candtext then
-- 笑|😄 candtext = 😄; 麼|么 candtext = 麼;
candtext = originalCand.text
if #originalCand.text == #cand_text and not is_in_list({ "", "", "", "", "", "", "", "" }, cand.text) then
-- 笑|😄 cand_text = 😄; 麼|么 cand_text = 麼;
cand_text = originalCand.text
end
end
-- 要置顶的放到 pined 中,其余的放到 others
local ok, idx = is_in_list(texts, candtext)
local ok, idx = is_in_list(texts, cand_text)
if ok then
pined[idx] = cand
pined_count = pined_count + 1
@ -155,12 +169,7 @@ function M.func(input, env)
if pined_count == #texts or #others > 50 then
break
end
else
table.insert(others, cand)
break
end
::continue::
end
-- yield pined others 及后续的候选项

View File

@ -146,22 +146,22 @@ pin_cand_filter:
# 单编码
- q 去 千
- w 我 万 往
- e 呃
- e 呃 🤔
- r 让 人
- t 他 她 它 祂
- y 与 于
# - u 在 custom_phrase 置顶
# - i 在 custom_phrase 置顶
- o 哦
- o 哦 😮
- p 片 篇
- a 啊
- a 啊 😲 😦 😧
- s 是 时 使 式
- d 的 地 得
- f 发 放 分
- g 个 各
- h 和 或
- j 及 将 即 既 继
- k 可
- k 可 🉑
- l 了 啦 喽 嘞
- z 在 再 自
- x 想 像 向
@ -170,12 +170,12 @@ pin_cand_filter:
- b 吧 把 呗 百
- n 那 哪 拿 呐
- m 吗 嘛 呣
# 单字
# 常用单字
- qing 请
- qu 去
- wo 我
- wei 为
- er 而 儿 二
- er 而 儿 二 2
- en 嗯
- rang 让
- ta 他 她 它 祂
@ -186,12 +186,16 @@ pin_cand_filter:
- yao 要
- ye 也
- shi 是 时 使 式
- suo 所
- shang 上 ⬆️ ↑
- shuo 说
- de 的 地 得
- dan 但
- dou 都
- dao 到 倒
- dian 点
- dian 点 ·
- dang 当
- dui 对
- dui 对 ⭕ ✅ ✔
- fa 发
- ge 个 各
- gang 刚
@ -199,12 +203,12 @@ pin_cand_filter:
- huo 或
- hui 会
- hai 还
- hao 好
- hao 好 👌 🙆‍♂️ 🙆‍♀️
- ji 及 即 既
- jiu 就
- jiang 将
- ke 可
- kan 看
- ke 可 🉑
- kan 看 👀
- kai 开
- le 了
- la 啦 拉
@ -216,11 +220,11 @@ pin_cand_filter:
- zhen 真
- zui 最
- zheng 正
- zuo 做 坐 左
- zuo 做 坐 左 ⬅️ ←
- ze 则
- xiang 想 像 向
- xian 先
- xia 下
- xia 下 ⬇️ ↓
- xing 行
- cai 才
- cong 从
@ -232,18 +236,20 @@ pin_cand_filter:
- bie 别
- bi 比
- bing 并
- na 那 哪 拿
- na 那 哪 拿
- ni 你
- ma 吗 嘛 妈
- mei 没
- mai 买 卖
# ta、na
- ta men 他们 她们 它们
- tm 他们 她们 它们
- ta de 他的 她的 它的
- td 他的 她的 它的
- ta men de 他们的 她们的 它们的
- na er 那儿 哪儿
- na ge 那个 哪个
- ng 那个 哪个
- ng 那个 哪个 拿个
- na xie 那些 哪些
- na li 那里 哪里
- na bian 那边 哪边