local function user_dict_exists_(str,dir) --输入字符串和文件路径,在路径指向文件中逐行查找完全对应的字符串,如果已存在则返回true,适用于小狼毫判断词条是否已经存在 local file = io.open(dir, "r") for line in file:lines() do if line == str then file:close() return true end end file:close() return false end local function custom_en(input, seg, env) if (string.sub(input, -1) == "_") then local inpu = string.gsub(input, "[_]+$", "") local unconfirm = inpu .. "\t" .. inpu:gsub("[^%a]+",""):lower() .. "\t100000" if (string.len(inpu) > 1 and string.sub(input, 1, 1) ~= "_") then path = "D:\\Rime\\dist\\custom_en.dict.yaml" -- for Windows users -- path = "/Users/xxx/Rime/dist/custom_en.dict.yaml" -- for Linux or Mac users if(user_dict_exists_(unconfirm,path))then local file = io.open(path, "r+") local content = file:read("*all") file:close() if (string.sub(input, -2) == "__") then content = content:gsub("\n" .. unconfirm:gsub("([%%%(%)%[%]%-*+?%.%^])", "%%%1"), "") file = io.open(path, "w+") file:write(content) file:close() yield(Candidate("pin", seg.start, seg._end, inpu," 已删除 输入__可重新添加")) else yield(Candidate("pin", seg.start, seg._end, inpu, " _删除")) end else if (string.sub(input, -2) == "__") then local file = io.open(path, "a") file:write("\n" .. unconfirm) file:close() yield(Candidate("pin", seg.start, seg._end, inpu, " 已保存为用户词")) else yield(Candidate("pin", seg.start, seg._end, inpu, " _保存")) end end end end end return custom_en