hchunhui / librime-lua

Extending RIME with Lua scripts
BSD 3-Clause "New" or "Revised" License
310 stars 43 forks source link

librime-lua #177 使用 leveldb 製作 英文字典 lua_translator@ecdict #194

Open shewer opened 2 years ago

shewer commented 2 years ago

rime 的字典不適合, 因為上屏順序是 字元長度優先 而leveldb 內建 排序為一般字元排序,特別適合查英文字典 ecdict.lua lua_translator@ecdict leveldb.lua 建立 leveldb pool 避免重覆開啓 db, ecdict db https://github.com/shewer/librime-lua-script/releases/download/ecdict_leveldb/ecdict.zip

local leveldb=require 'leveldb'
local M={}
function M.init(env)
  local file = rime_api.get_user_data_dir() .. "/ecdict"
  env.dict= leveldb.open( file,'dict')
  env.quality = 1.5
end
function M.fini(env)
end
local function gen_cand(word,info,seg,env)
    local info= load( "return ".. info)()
    local comment= info. phonetic .. info.translation
    local cand=Candidate('english',seg.start,seg._end,word, comment)
    cand.preedit = word
    cand.quality = env.quality  
    return cand
end
function M.func(inp,seg,env)
   for word,info in env.dict:query(inp):iter() do
       local cand= gen_cand(word,info,seg,env)
       yield(cand)
   end
end
return M
local db_pool_={}
local function opendb(fn,dbname) 
  dbname= dbname or ""
  local db = db_pool_[fn]
  if not db then 
    db = LevelDb(fn,dbname) 
    if not db then return nil end
    db_pool_[fn] = db
  end
  if not db:loaded() then db:open() end
  return db
end
local function closedb(db)
  db:close()
  for k,v in next , db_pool_ do 
    if  v == db then 
      db_pool_[k] = nil
    end
  end
end
local function pool()
  local tab = {}
  for k,v in next,db_pool_ do
    table.insert(tab, ("%s:%s(%s)"):format(k,v,v:loaded() and "opened" or "closed" ))
  end
  return tab
end
return {
  open=opendb, 
  -- close=closedb,
  puol= pool,
}
shewer commented 2 years ago

安裝 1 copy ecdict.lua /lua/ecdict.lua 2 copy leveldb.lua /lua/leveldb.lua 3 unzip ecdict.zip 4 echo 'ecdict = require "ecdict" ' >> rime.lua 5 patch: engine/translators/@next: lua_translator@ecdict

qq420100523 commented 1 year ago

安裝 1 copy ecdict.lua /lua/ecdict.lua 2 copy leveldb.lua /lua/leveldb.lua 3 unzip ecdict.zip 4 echo 'ecdict = require "ecdict" ' >> rime.lua 5 patch: engine/translators/@next: lua_translator@ecdict

大佬好,我又来了,你分享的这个脚本,我也不生效,我根据你教的log解决了文件夹读取问题:

local leveldb=require("leveldb")
-- 这个方法把rime_api.get_user_data_dir()提取出来就可以读取到目录
-- /Users/zengzw/Library/Rime/ecdict
function getEcdictFile()
    return rime_api.get_user_data_dir()  .. package.config:sub(1,1) .. "ecdict"
end
local M={}
function M.init(env)
  -- rime_api.get_user_data_dir()放在这里会在日志里会报
  -- E20230202 20:38:57.604226 52898 level_db.cc:277] Error opening db 'dict': NotFound: ./ecdict/LOCK: No such file or directory
  local file = getEcdictFile()
  env.dict= leveldb.open(file,'dict')
  env.quality = 1.5
end
function M.fini(env)
end
local function gen_cand(word,info,seg,env)
    local info= load( "return ".. info)()
    local comment= info.phonetic .. info.translation
    local cand=Candidate('english',seg.start,seg._end,word, comment)
    cand.preedit = word
    cand.quality = env.quality
    return cand
end
function M.func(inp,seg,env)
   for word,info in env.dict:query(inp):iter() do
       local cand= gen_cand(word,info,seg,env)
       yield(cand)
   end
end
return M

但是这个英文插件还是不生效,在ecdict/LOG文件有日志打印:

2023/02/02-22:42:30.048405 0x7ff846853640 Recovering log #725
2023/02/02-22:42:30.111214 0x7ff846853640 Delete type=3 #724
2023/02/02-22:42:30.111283 0x7ff846853640 Delete type=0 #725
shewer commented 1 year ago

這只是大綱 及注意事項, 請用 librime-lua-script 中的

你的版本是新版的 rime_api.get_user_data_dir() 是支援的 ,

qq420100523 commented 1 year ago

這只是大綱 及注意事項, 請用 librime-lua-script 中的

你的版本是新版的 rime_api.get_user_data_dir() 是支援的 ,

明白了