改善 reduce_english_filter() 效率 (#69)

This commit is contained in:
Shewer Lu 2023-02-13 21:37:23 +08:00 committed by GitHub
parent 0d4f1e3727
commit 117d779105
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -194,26 +194,34 @@ end
-- 感谢大佬 @[Shewer Lu](https://github.com/shewer) 指点 -- 感谢大佬 @[Shewer Lu](https://github.com/shewer) 指点
function reduce_english_filter(input, env) function reduce_english_filter(input, env)
local config = env.engine.schema.config local config = env.engine.schema.config
local idx = config:get_int(env.name_space .. "/idx") -- 要插入的位置 -- load data
local words = {} -- 要过滤的词 if not env.idx then
local list = config:get_list(env.name_space .. "/words") env.idx = config:get_int(env.name_space .. "/idx") -- 要插入的位置
for i = 0, list.size - 1 do
table.insert(words, list:get_value_at(i).value)
end end
if not env.words then
local l = {} env.words = {} -- 要过滤的词 ,使用hash table
local code = env.engine.context.input -- 当前编码 -- for old version
local list_size = config:get_list_size(env.name_space .. "/words")
for i = 0, list.size - 1 do
local word = config:get_string(env.name_space .. "/words/@" .. i)
env.words[word]=true
end
end
-- filter start
local code = env.engine.context.input
if env.words[code] then
-- 交換 1 2 順序
local l ={}
for cand in input:iter() do for cand in input:iter() do
table.insert(l, cand) table.insert(l, cand)
if #l >=2 then break end
end end
for _, word in ipairs(words) do for i=#l,1,-1 do
if (code == word) then yield(l[i])
first_element = table.remove(l, 1)
table.insert(l, 2, first_element)
break
end end
end end
for _, cand in ipairs(l) do -- yield other
for cand in input:iter() do
yield(cand) yield(cand)
end end
end end