Skip to content

Commit

Permalink
- 新增后缀检索功能
Browse files Browse the repository at this point in the history
  • Loading branch information
Saimoecat committed Dec 3, 2024
1 parent 1b8e95f commit 657fa9d
Show file tree
Hide file tree
Showing 10 changed files with 4,022 additions and 597 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
/trash/
/recorder/baidu.txt
/recorder/recorder_*.txt
/recorder/suffix.txt
zrm.userdb.txt
*.iml
weasel.custom.yaml
Expand Down
153 changes: 128 additions & 25 deletions lua/desc_filter.lua
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ end


--获取目录
local function get_path ()
local function get_pin_path ()
local path = rime_api:get_user_data_dir() .. "/recorder/pin.txt"
local a = io.open(path, "r")
a = a and
Expand All @@ -158,6 +158,17 @@ local function get_path ()
return path
end

--获取目录
local function get_suffix_path ()
local path = rime_api:get_user_data_dir() .. "/recorder/suffix.txt"
local a = io.open(path, "r")
a = a and
a:close()
or
io.open(path, "w+"):close()
return path
end

-- 分组
local function split(inputstr, sep)
local t = {}
Expand All @@ -168,7 +179,7 @@ local function split(inputstr, sep)
end

-- 置顶候选词
local function pinCandidate (env,inp,cand)
local function specialCandidate (env,inp,cand,tage)
env.countIndex = env.countIndex + 1

-- 还原preedit
Expand All @@ -184,7 +195,7 @@ local function pinCandidate (env,inp,cand)
end

local res = comment(cand)
cand.comment = res .. "📌"
cand.comment = res .. tage

-- 判断是否有空码
if (env.countIndex == 1 and env.engine.context.caret_pos == #inp) then
Expand Down Expand Up @@ -297,11 +308,19 @@ local function ordinaryCandidate (index,env,inp,cand)
end

cand.preedit = preedit
if (index == 1 and #groups == 1) then
env.oneFlag = true
-- 去掉符号
local first = groups[1]
first = string.gsub(first, " ", "")
first = string.gsub(first, "«", "")
first = string.gsub(first, "»", "")
if (first == inp) then
env.oneFlag = true
end
end


if (flag == false or #groups > 1 or env.oneFlag ) then
return true
else
Expand All @@ -310,23 +329,12 @@ local function ordinaryCandidate (index,env,inp,cand)

end

---
---初始化
---@param env object 上下文对象
---
local function init(env)
env.countIndex = 0
env.oneFlag = false
end

local function filter(input, env)
env.countIndex = 0
env.oneFlag = false
local inp = env.engine.context.input
-- 查询pin库
local function checkPin(env,inp)
local pinList = {}

-- 查询pin库
local path = get_path()
local path = get_pin_path()
local file = io.open(path, "r")
if not file then return end
local content = file:read("*all")
Expand All @@ -340,32 +348,127 @@ local function filter(input, env)

-- 加入置顶集合
for _,each in ipairs(parts) do
env.countIndex = env.countIndex + 1
local pinCand = Candidate("pin", 0, #inp, each, "")
pinCand.preedit = part2
yield(pinCandidate(env,inp,pinCand))
yield(specialCandidate(env,inp,pinCand,"📌"))
table.insert(pinList, each)
end
end
end
--关闭文件
file:close()

return pinList
end

-- 查询suffix库
local function checkSuffix(env,inp,pinList)
local suffixList = {}

-- 查询pin库
local path = get_suffix_path()
local file = io.open(path, "r")
if not file then return end
local content = file:read("*all")
file:seek("set")
--循环解析
for line in file:lines() do
if line:find("^" .. inp .. "\t") then
-- 分组
local part1, part2, part3, part4, part5 = string.match(line, "(.*)\t(.*)\t(.*)\t(.*)\t(.*)")

-- 最后输入
local lasttext = env.engine.context.commit_history:latest_text()

-- 检查前缀
if (lasttext == part3) then
-- 排出pin置顶词
local pinFlag = true
if (#pinList > 0) then
for _,each in ipairs(pinList) do
if (each == part3) then
pinFlag = false
break
end
end
end

if (pinFlag) then
local suffixCand = {}
suffixCand.text = part4
suffixCand.preedit = part2
suffixCand.weight = part5
table.insert(suffixList, suffixCand)
end
end
end
end

-- 按照 weight 属性降序排序
table.sort(suffixList, function(a, b)
return a.weight > b.weight
end)

-- 生成候选
for _,each in ipairs(suffixList) do
local suffixCand = Candidate("suffix", 0, #inp, each.text, "")
suffixCand.preedit = each.preedit
yield(specialCandidate(env,inp,suffixCand,""))
end

--关闭文件
file:close()

return suffixList
end

---
---初始化
---@param env object 上下文对象
---
local function init(env)
env.countIndex = 0
env.oneFlag = false
end

local function filter(input, env)
env.countIndex = 0
env.oneFlag = false
local inp = env.engine.context.input


-- 查询pin库
local pinList = checkPin(env,inp)

local suffixList = checkSuffix(env,inp,pinList)

local index = 0

-- 检查是否有置顶词
for cand in input:iter() do
local pinFlag = true
local candFlag = true
-- 检查pin置顶词
if (#pinList > 0) then
for _,each in ipairs(pinList) do
if (each == cand.text) then
pinFlag = false
candFlag = false
break
end
end
end

-- 检查suffix后缀词
if (#suffixList > 0) then
for _,each in ipairs(suffixList) do
if (each.text == cand.text) then
candFlag = false
break
end
end
end

if (pinFlag) then
-- 普通词
if (candFlag) then
index = index + 1
local ordinary = ordinaryCandidate(index,env,inp,cand)
if (ordinary) then
Expand All @@ -374,7 +477,7 @@ local function filter(input, env)
end
end

--清空无编码
--清空无编码
local composition = env.engine.context.composition
if(not composition:empty()) then
local segment = composition:back()
Expand Down
100 changes: 100 additions & 0 deletions lua/suffix_processor.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
--忽略字符
local listArray = { "", "", "", "", "", "", "", "", "¥", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "×", "§", "", "·", "", "÷",
"", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "",
"", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", ""," " }


-- 获取文件路径​​​​​​​​​​​​​​​​
local get_path = function()
local path = rime_api:get_user_data_dir() .. "/recorder/suffix.txt"
local a = io.open(path, "r")
a = a and
a:close()
or
io.open(path, "w+"):close()
return path
end
-- 添加内容到文件末尾
local append_new_line = function(lct,path)
local file = io.open(path, "r")
if not file then return end
local content = file:read("*all")
file:seek("set")
local append_line = lct .. "\t1"
for line in file:lines() do
if line:find("^" .. lct .. "\t") then
content = content:gsub(line .. "\n", "")
append_line = lct .. "\t" .. (line:match("([0-9]+)") + 1)
break
end
end
file:close()
io.open(path, "w+"):write(append_line):close()
io.open(path, "a"):write("\n" .. content):close()
end
return {
init = function(env)
--上次提交
env.laststr = ""
-- 监听提交
env.notifier = env.engine.context.commit_notifier:connect(function(context)
if (env.engine.schema.schema_id == "zrm") then
local flag = true
-- 本次提交
local lct = context:get_commit_text()
-- 忽略字符
for i, v in ipairs(listArray) do
if (string.find(env.laststr, v) ~= nil) then
flag = false
break
elseif (string.find(lct, v) ~= nil) then
flag = false
break
end
end

local input = context.input
if (input == nil or input == "") then
flag = false
elseif (env.lastpreedit == nil or env.lastpreedit == "") then
flag = false
elseif (env.laststr == nil or env.laststr == "") then
flag = false
elseif (lct == nil or lct == "") then
flag = false
end

if (flag) then
local str = input .. "\t" .. env.lastpreedit .. "\t" .. env.laststr .. "\t" .. lct
local path = get_path()
append_new_line(str,path)
end
end
end)
end,
func = function (key_event, env)
local engine = env.engine
local context = engine.context
local input = context.input
local text = context:get_script_text()
local composition = context.composition
local segment = nil
if (not composition:empty()) then
segment = composition:back()
end

-- 还原preedit
local preeditArray = { "¹", "²", "³", "", "", "", "", "", "", "" }
for i, v in ipairs(preeditArray) do
text = string.gsub(text, v, "")
end

--最后输入
local lasttext = env.engine.context.commit_history:latest_text()
if (lasttext ~= "") then
env.laststr = lasttext
env.lastpreedit = text
end
return 2
end
}
5 changes: 3 additions & 2 deletions opencc/others.txt
Original file line number Diff line number Diff line change
Expand Up @@ -810,9 +810,10 @@ rime rime 㞢
八次方 八次方 ⁸
九次方 九次方 ⁹
十次方 十次方 ¹⁰
版权 版全 ©
度 度 °
摄氏度 摄氏度 °C
华氏度 华氏度 °F
摄氏度 摄氏度
华氏度 华氏度
点 点 · 丶
间隔号 间隔号 ·
连接号 连接号 —
Expand Down
6 changes: 3 additions & 3 deletions zrm.custom.yaml
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
patch:

"engine/processors/@before 2":
#chord_composer # 实现并击按键处理
lua_processor@*chord_composer_processor # 实现并击按键处理
chord_composer # 实现并击按键处理
#lua_processor@*chord_composer_processor # 实现并击按键处理

chord_composer:
finish_chord_on_first_key_release: true #有一键被释放时立刻触发合成
Expand Down Expand Up @@ -266,7 +266,7 @@ patch:
__patch:
# Rx: 语言模型 {
- patch/+:
#__include: grammar:/huayu
__include: grammar:/huayu
# }


Expand Down
1 change: 1 addition & 0 deletions zrm.schema.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ engine:
- recognizer #此处理器用于识别输入的字符和词组。它可以识别您输入的文本,然后将其转换为相应的候选词。
- key_binder #此处理器主要负责键盘按键的绑定。它可以将特定的按键绑定到特定的功能或命令上。
- lua_processor@*recorder_processor
- lua_processor@*suffix_processor
- lua_processor@*jane_processor
- lua_processor@*main_processor
- speller #此处理器用于拼写检查。它可以检查您输入的文本是否正确,并提供正确的拼写建议。
Expand Down
2 changes: 1 addition & 1 deletion zrm_fu.dict.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -355,7 +355,7 @@ $ $_
: _:
; _;
‘ _'
© _@
_@
¥ _$
“ _/
” _\
Expand Down
Loading

0 comments on commit 657fa9d

Please sign in to comment.