hchunhui / librime-lua

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

Opencc 相关接口方法无法获得转换后的所有结果 #327

Closed boomker closed 7 months ago

boomker commented 7 months ago

我的需求: https://github.com/hchunhui/librime-lua/pull/147#issuecomment-1259704959 -- lua 脚本

  local opencc_emoji = Opencc("emoji.json")
  local bb2 = {opencc_emoji:convert("开心")}
  log.info(bb2)

-- 结果: { "开心" } -- 期望:{ "开心" ,"😁", "🥳"}


另外 convert_word 方法貌似没有任何输出。 我的环境:macOS 14.4, librime 1.11.0, @shewer

shewer commented 7 months ago

很久沒用 , 在 rime_api_console 下測試 ok
一對多 用 convert_word(word) return table : 轉換字詞 random_convert_text(str) : 轉換整句字串 ,亂數取用 一對多的字詞 一對一 convert or convert_text(str) : 轉換整句字串

os: linux data: https://github.com/rtransformation/rime-opencc_emoji_symbols/blob/master (es.text , es.json)

app: rime_api_console

user_data_dir : ./opencc (es.txt es.json)

es = Opencc('es.json')
for i,v in next, es:convert_word('手机')  do
     print(i, v)
end

image

boomker commented 7 months ago

rime_api_console,这个从哪里获取 我这边测试还是没看有输出, 是否和 librime 版本有关系

shewer commented 7 months ago

local opencc_emoji = Opencc("emoji.json") local bb2 = {opencc_emoji:convert("开心")} log.info(bb2) -- ?

  local opencc_emoji = Opencc("emoji.json")
  local bb2 = opencc_emoji:convert_word("开心")}
  if bb2 then 
     for i,v in  ipairs(bb2) do  
          log.info(v)
     end
  else
      log.info("not found")
  end
boomker commented 7 months ago

经过我多次测试, 发现问题在于:

    for cand in input:iter() do
            local cand_text = cand.text
            opencc_emoji:convert_word(cand_text)  -- 这里引用`cand_text` (候选项 text 属性)时会崩溃
            ...
        end
shewer commented 7 months ago
local cand_text = cand.text
local words = opencc_emoji:convert_word(cand_text)  or { cand_text } -- 解決 nil 問題
...