shewer / librime-lua-script

57 stars 6 forks source link

有可能调用rime的词库文件用于英文单词(带中文释义注释)输入吗? #12

Closed zhuangzhemin closed 2 years ago

zhuangzhemin commented 2 years ago

我试了另外一个思路,先按dict.yaml格式创建词库,用中文释义作为text,英文单词作为code,然后用lua_filter识别到这些candidate,把dict的code处理成candidate的text,把dict的text处理成candidate的comment。这样可以在dict文件里面添加词频信息,经常打的单词的优先级也会调高。

首先遇到的问题是candidate没有code信息,只能把code(word)添加到dict的text(中文释义)部分,变成code:text \t code的格式,然后lua脚本再去分解text得到英文单词和中文释义。 现在遇到的问题是,在rime里面,除非是只有英文词库,否则长单词会被rime拆分成很多端,比如anothe会被优先拆解成a no th e或者ano the,而不是能补全提示到another。 image

只有英文词库时能自动补全: image

请问这种情况有什么办法解决吗?谢谢! 我用的lua代码如下:

function ecdict_filter(input, env)
  local text, comment
  local cache = {}
  for cand in input:iter() do
    text, comment = cand.text:match("^(.*)::(.*)")
    if text then
      yield(Candidate("ECDICT", 1, text:len(), text, truncate_comment(comment)))
    else
      table.insert(cache, cand)
    end
    for i, cand in ipairs(cache) do
      yield(cand)
    end
  end
end

*.scheme.yaml文件调用ecdict脚本的配置:

  translators:
    - punct_translator
    ...
    - table_translator@ecdict
  filters:
    - lua_filter@ecdict_filter
    - simplifier
    - uniquifier
ecdict:                           # 英文单词(中文释义提示), 搭配lua脚本使用
  dictionary: ecdict
  enable_completion: false        # 是否启用英文输入联想补全
  initial_quality: 1
zhuangzhemin commented 2 years ago

问题解决了。主要是要设置enable_sentence: falseenable_completion: true,这两项之前没处理对。

ecdict:                           # 英文单词(中文释义提示), 搭配lua脚本使用
  enable_encoder: false
  encode_commit_history: false
  enable_sentence: false           # 不开启造句
  enable_completion: true        # 启用英文输入联想补全
  dictionary: ecdict
  initial_quality: -1

另外我把lua脚本也更新了,加上之前用输入字符前两个字母确定候选词小写、首字母大写、全大写状态的功能。

local function ecdict_filter(input, env)
  local context = env.engine.context
  local input_text = context:get_preedit().text
  local separator = " 🔎"
  local text, comment
  for cand in input:iter() do
    text, comment = cand.text:match("^(.*)::(.*)")
    if text then
      text = sync_case(input_text, text)
      comment = truncate_comment(separator .. comment)
      yield(Candidate("ECDICT",1,#text,text,comment))
    else
      yield(cand)
    end
  end
end

现在的遗留问题是dict文件内容不够美观,中文释义部分多了"code::"的前缀。

name: ECDICT
version: "V1.0"
description: "英文单词(中文释义提示), 搭配lua脚本使用"
sort: by_weight
use_preset_vocabulary: false
...

a::[ei]; 第一个字母 A; 一个; 第一的\r\n art. [计] 累加器, 加法器, 地址, 振幅, 模拟, 区域, 面积, 汇编, 组件, 异步 a
A-road::['ei'rәjd]; <英>A级公路,主车道 A-road
a.m.::上午, 午前    a.m.
AA::[ɔ:]; [计] 绝对地址, 农业自动化, 自动应答\n [医] 各, 各个 AA
AAA::[医]; 急性焦虑发作    AAA
aaah::abbr. American Association for the Advancement of the Humanities 美国人权促进会  aaah
Aachen::['ɑ:kәn]; 亚琛[德意志联邦共和国西部城市]  Aachen
aah::[ɑ:]; abbr.[军] Armored Artillery Howitzer, 装甲榴弹炮\n abbr.[军] Advanced Attack Helicopter, 先进攻击直升机    aah
aardvark::['ɑ:dvɑ:k]; n. 土豚 aardvark
aargh::[ɑ:];  啊     aargh
aaron::['eәrәn]; n. 亚伦(男子名);[圣经]亚伦(摩西之兄, 犹太教的第一祭司长) aaron

按这个方法配置的rime方案已经更新到我的github 👉 https://github.com/zhuangzhemin/rime

zhuangzhemin commented 2 years ago

我在librime那边提了一个issues请求在dict.yaml文件中添加comment列。有大佬建议用reverse_lookup查找的方式实现。 我按照这个方法,用reverse_lookup查找的方式,改了一份Rime配置在我的github,已经可以实现用*reverse.bin查找、标识中文释义作为英文词汇的comment的需求。

https://github.com/rime/librime/issues/538#issuecomment-1092707778

engine:
  processors:
    - lua_processor@ecdict_processor               # 非ASCII模式下,使用右shift键切换中英/英文模式
    - ascii_composer
  translators:
    - punct_translator
    - script_translator
    - table_translator@flypy_phrases
    - table_translator@wiki_zh
    - table_translator@custom_phrase_completion
    - table_translator@custom_phrase_no_completion
    - reverse_lookup_translator
    - table_translator@ecdict
  filters:
    - reverse_lookup_filter@ecdict_reverse_lookup  # 反查英文单词/词组的中文释义,作为comment显示
    - lua_filter@ecdict_filter                     # 控制中英混合输入或者纯英文输入,以及截断过长的comment
    - simplifier
    - uniquifier
ecdict:                           # 英文单词
  enable_encoder: false
  encode_commit_history: false
  enable_sentence: false
  enable_completion: true         # 是否启用英文输入联想补全
  enable_user_dict: true
  dictionary: ecdict
  initial_quality: 1

ecdict_reverse_lookup:            # 查询英文单词/词组的中文释义, 作为Comment进行提示
  overwrite_comment: true
  dictionary: ECDICT_reverse
  comment_format:
    - xform/\\n/ /
    - xform/^/ /
    # - xform/^(.{1,80}).*$/$1/   # 截断过长的comment,用xform截断中文comment最后一个字符可能会出现乱码, 改成用lua_filter处理
shewer commented 2 years ago

问题解决了。主要是要设置enable_sentence: falseenable_completion: true,这两项之前没处理对。

ecdict:                           # 英文单词(中文释义提示), 搭配lua脚本使用
  enable_encoder: false
  encode_commit_history: false
  enable_sentence: false           # 不开启造句
  enable_completion: true        # 启用英文输入联想补全
  dictionary: ecdict
  initial_quality: -1

另外我把lua脚本也更新了,加上之前用输入字符前两个字母确定候选词小写、首字母大写、全大写状态的功能。

local function ecdict_filter(input, env)
  local context = env.engine.context
  local input_text = context:get_preedit().text
  local separator = " 🔎"
  local text, comment
  for cand in input:iter() do
    text, comment = cand.text:match("^(.*)::(.*)")
    if text then
      text = sync_case(input_text, text)
      comment = truncate_comment(separator .. comment)
      yield(Candidate("ECDICT",1,#text,text,comment))
    else
      yield(cand)
    end
  end
end

现在的遗留问题是dict文件内容不够美观,中文释义部分多了"code::"的前缀。

name: ECDICT
version: "V1.0"
description: "英文单词(中文释义提示), 搭配lua脚本使用"
sort: by_weight
use_preset_vocabulary: false
...

a::[ei]; 第一个字母 A; 一个; 第一的\r\n art. [计] 累加器, 加法器, 地址, 振幅, 模拟, 区域, 面积, 汇编, 组件, 异步   a
A-road::['ei'rәjd]; <英>A级公路,主车道   A-road
a.m.::上午, 午前  a.m.
AA::[ɔ:]; [计] 绝对地址, 农业自动化, 自动应答\n [医] 各, 各个   AA
AAA::[医]; 急性焦虑发作  AAA
aaah::abbr. American Association for the Advancement of the Humanities 美国人权促进会    aaah
Aachen::['ɑ:kәn]; 亚琛[德意志联邦共和国西部城市]    Aachen
aah::[ɑ:]; abbr.[军] Armored Artillery Howitzer, 装甲榴弹炮\n abbr.[军] Advanced Attack Helicopter, 先进攻击直升机  aah
aardvark::['ɑ:dvɑ:k]; n. 土豚   aardvark
aargh::[ɑ:];  啊   aargh
aaron::['eәrәn]; n. 亚伦(男子名);[圣经]亚伦(摩西之兄, 犹太教的第一祭司长)   aaron

按这个方法配置的rime方案已经更新到我的github point_right https://github.com/zhuangzhemin/rime

dict.yaml 可以設定columns 這樣就可以把 code text 調換

columns: 定義碼表以Tab分隔出的各列,可設text【文本】、code【碼】、weight【權重】、stem【造詞碼】

columns: [ code , text ]