Skip to content

对象接口

librime-lua 封装了 librime C++ 对象到 lua 中供脚本访问。需注意随着项目的开发,以下文档可能是不完整或过时的,敬请各位参与贡献文档。

Engine

可通过 env.engine 获得。

属性:

属性名类型解释
schema
contextContext
active_engine

方法:

方法名参数返回值解释
process_key
compose
commit_text(text)text: string上屏 text 字符串
apply_schema

Context

输入编码上下文。

可通过 env.engine.context 获得。 ( *W : 觸發 Compose() )

属性:

属性名类型解释
compositionComposition
inputstring*W 正在输入的编码字符串
caret_posnumber*W 脱字符位置(以raw input中的ASCII字符数量标记)
commit_notifierNotifier
select_notifierNotifier
update_notifierNotifier
delete_notifierNotifier
option_update_notifierOptionUpdateNotifier选项改变通知,使用 connect 方法接收通知
property_update_notifierPropertyUpdateNotifier
unhandled_key_notifierKeyEventNotifier

方法:

方法名参数返回值解释
commit上屏选中的候选词
get_commit_textstring
get_script_textstring按音节分割
get_preeditPreedit
is_composingboolean是否正在输入(输入字符串非空或候选词菜单非空)
has_menuboolean是否有候选词(选项菜单)
get_selected_candidateCandidate返回选中的候选词
push_input(text)text: string*W 在caret_pos位置插入指定的text编码字符串,caret_pos跟隨右移
pop_input(num)num: numberboolean*W 在caret_pos位置往左删除num指定数量的编码字符串,caret_pos跟隨左移
delete_input
clear*W 清空正在输入的编码字符串及候选词
select(index)index: numberboolean选择第index个候选词(序号从0开始)
confirm_current_selection确认选择当前高亮选择的候选词(默认为第0个)
delete_current_selectionboolean删除当前高亮选择的候选词(自造词组从词典中删除;固有词则删除用户输入词频)(returning true doesn't mean anything is deleted for sure)
https://github.com/rime/librime/.../src/context.cc#L125-L137
confirm_previous_selection
reopen_previous_selection *W
clear_previous_segment
reopen_previous_segment *W
clear_non_confirmed_composition
refresh_non_confirmed_composition *W
set_option
get_option
set_property(key, value)key: string
value: string
可以用于存储上下文信息(可配合 property_update_notifier 使用)
get_property(key)key: stringstring
clear_transient_options

Preedit

属性:

属性名类型解释
text
caret_pos
sel_start
sel_end

方法:

方法名参数返回值解释

Composition

用户编写的“作品”。(通过此对象,可间接获得“菜单menu”、“候选词candidate”、“片段segment”相关信息)

可通过 env.engine.context.composition 获得。

属性:

属性名类型解释

方法:

方法名参数返回值解释
emptyboolean尚未开始编写(无输入字符串、无候选词)
backSegment获得队尾(input字符串最右侧)的 Segment 对象
pop_back去掉队尾的 Segment 对象
push_back在队尾添加一个 Segment对象
has_finished_composition
get_promptstring获得队尾的 Segment 的 prompt 字符串(prompt 为显示在 caret 右侧的提示,比如菜单、预览输入结果等)
toSegmentation

e.g.

lua
local composition = env.engine.context.composition

if(not composition:empty()) then
  -- 获得队尾的 Segment 对象
  local segment = composition:back()

  -- 获得选中的候选词序号
  local selected_candidate_index = segment.selected_index

  -- 获取 Menu 对象
  local menu = segment.menu

  -- 获得(已加载)候选词数量
  local loaded_candidate_count = menu:candidate_count()
end

Segmentation

在分词处理流程 Segmentor 中存储 Segment 并把其传递给 Translator 进行下一步翻译处理。

作为第一个参数传入以注册的 lua_segmentor。

或通过以下方法获得:

lua
local composition = env.engine.context.composition
local segmentation = composition:toSegmentation()

librime 定义 - https://github.com/rime/librime/blob/5c36fb74ccdff8c91ac47b1c221bd7e559ae9688/src/segmentation.cc#L28

属性:

属性名类型解释
inputstring活动中的原始(未preedit)输入编码

方法:

方法名参数返回值解释
emptyboolean是否包含 Segment 或 Menu
backSegment队尾(对应input最右侧的输入字符)的 Segment
pop_backSegment移除队列最后的 Segment
reset_lengthsize_t保留 n 個 Segment
add_segment(seg)seg: Segment添加 Segment
(librime v1.7.3:如果已包含 Segment 且起始位置相同,会取较长的Segment 并且合并 Segment.tags)
forwardboolean新增 一個 kVoid 的 Segment(start_pos = 前一個 end_pos , end_pos = start_pos)
trim摘除队列最末位的0长度 Segment (0长度 Segment 用于语句流输入法中标记已确认kConfirmed但未上屏的 Segment 结束,用于开启一个新的 Segment)
has_finished_segmentationboolean
get_current_start_positionnumber
get_current_end_positionnumber
get_current_segment_lengthnumber
get_confirmed_positionnumber属性 input 中已经确认(处理完)的长度
(通过判断 status 为 kSelectedkConfirmed 的 Segment 的 _end 来判断 confirmed_position)
https://github.com/rime/librime/.../src/segmentation.cc#L127

e.g.

txt
                         | 你hao‸a
env.engine.context.input | "nihaoa"
Segmentation.input       | "nihao"
get_confirmed_position   | 2

Segment

分词片段。触发 translator 时作为第二个参数传递给注册好的 lua_translator。

或者以下方法获得: (在 filter 以外的场景使用)

lua
local composition = env.engine.context.composition
if(not composition:empty()) then
  local segment = composition:back()
end

构造方法:Segment(start_pos, end_pos)

  1. start_pos: 首码在输入字符串中的位置
  2. end_pos: 尾码在输入字符串中的位置

属性:

属性名类型解释
statusstring1. kVoid - (默认)
2. kGuess
3. kSelected - 大于此状态才会被视为选中
4. kConfirmed
start
_start
_end
length
tagsSet标签
menu
selected_index
promptstring输入编码以右的提示字符串
image

方法:

方法名参数返回值解释
clear
close
reopen
has_tag
get_candidate_at(index)index: number 序号0开始Candidate
get_selected_candidateCandidate

Schema

方案。可以通过 env.engine.schema 获得。

构造方法:Schema(schema_id)

  1. schema_id: string

属性:

属性名类型解释
schema_idstring方案编号
schema_namestring方案名称
configConfig方案配置
page_sizenumber每页最大候选词数
select_keys选词按键(不一定是数字键,视输入方案而定)

方法:

方法名参数返回值解释

Config

(方案的)配置。可以通过 env.engine.schema.config 获得

属性:

属性名类型解释

方法:

方法名参数返回值解释
load_from_file
save_to_file
is_null(conf_path)conf_path: string
is_value
is_list(conf_path)conf_path: stringboolean1. 存在且为 ConfigList 返回 true
2. 存在且不为 ConfigList 返回 false
3. 不存在返回 true ⚠️
is_map
get_bool
get_int
get_double
get_string(conf_path)conf_path: stringstring根据配置路径 conf_path 获取配置的字符串值
set_bool
set_int
set_double
set_string(path, str)path: string
str: string
get_item
set_item(path, item)path: string
item: ConfigItem
get_value
get_list(conf_path)conf_path: stringConfigList不存在或不为 ConfigList 时返回 nil
get_map(conf_path)conf_path: stringConfigMap不存在或不为 ConfigMap 时返回 nil
set_value(path, value)path: string
value: ConfigValue
set_list
set_map
get_list_size

ConfigMap

属性:

属性名类型解释
sizenumber
typestring如:“kMap”
element轉換成ConfigItem

方法:

方法名参数返回值解释
set
get(key)key: stringConfigItem
get_value(key)key: stringConfigValue
has_keyboolean
clear
emptyboolean
keystable

ConfigList

属性:

属性名类型解释
sizenumber
typestring如:“kList”
element轉換成ConfigItem

方法:

方法名参数返回值解释
get_at(index)index: number
(下标从0开始)
ConfigItem
get_value_at(index)index: number
(下标从0开始)
ConfigValue
set_at
append
insert
clear
empty
resize

ConfigValue

继承 ConfigItem

构造方法:ConfigValue(str)

  1. str: 值(可通过 get_string 获得)

属性:

属性名类型解释
valuestring
typestring如:“kScalar”
element轉換成ConfigItem

方法:

方法名参数返回值解释
get_boolboolint子集,所以也可以用get_int来取得bool
get_int
get_double
set_bool
set_int
set_double
get_string
set_string

ConfigItem

属性:

属性名类型解释
typestring1. "kNull"
2. "kScalar"
3. "kList"
4. "kMap"
empty

方法:

方法名参数返回值解释
get_value当 type == "kScalar" 时使用
get_list当 type == "kList" 时使用
get_map当 type == "kMap" 时使用

KeyEvent

按键事件对象。

当一般按键被按下、修饰键被按下或释放时均会产生按键事件(KeyEvent),触发 processor,此时 KeyEvent 会被作为第一个参数传递给已注册的 lua_processor。

  • 一般按键按下时:生成该按键的keycode,此时保持按下状态的所有修饰键(Ctrl、Alt、Shift等)以bitwise OR形式储存于modifier中
  • 修饰键被按下时:生成该修饰键的keycode,此时保持按下状态的所有修饰键(包括新近按下的这个修饰键)以bitwise OR形式储存于modifier中
  • 修饰键被释放时:生成该修饰符的keycode,此时仍保持按下状态的所有修饰键外加一个通用的 kRelease 以bitwise OR形式储存于modifier中。

属性:

属性名类型解释
keycodenumber按键值,除ASCII字符外按键值与字符codepoint并不相等
modifier当前处于按下状态的修饰键或提示有修饰键刚刚被抬起的kRelease

方法:

方法名参数返回值解释
shiftboolean触发事件时,shift是否被按下
ctrlboolean触发事件时,ctrl是否被按下
altboolean触发事件时,alt/option是否被按下
caps
(CapsLk)
boolean
superboolean触发事件时,win/command是否被按下
releaseboolean是否因为修饰键被抬起release而触发事件
repr
(representation)
string修饰键(含release)+按键名(若没有按键名,则显示4位或6位十六进制X11按键码位 ≠ Unicode)
eq(key)
(equal)
key: KeyEventboolean两个 KeyEvent 是否“相等”
lt(key)
(less than)
key: KeyEventboolean对象小于参数时为 true

KeySequence

形如{按键1}{修饰键2+按键2}的一串按键、组合键序列。一对花括号内的为一组组合键;序列有先后顺序

属性:

属性名类型解释

方法:

方法名参数返回值解释
parse
repr
toKeyEvent

Candidate 候选词

Candidate 缺省为 SimpleCandidate(选中后不会更新用户字典)

构造方法:Candidate(type, start, end, text, comment)

  1. type: 来源和类别标记
  2. start: 分词开始
  3. end: 分词结束
  4. text: 候选词内容
  5. comment: 注释

属性:

属性名类型解释
typestring候选词来源和类别标记,如:“user_phrase”、“phrase”、“punct”、“simplified”
1. "user_phrase": 用户字典(随用户输入而更新)
2. "phrase"
3. "punct": 来源有两 "engine/segmentors/punct_segmentor" 或 "symbols:/patch/recognizer/patterns/punct"
4. "simplified"
5. "completion": 编码未完整。see https://github.com/rime/librime/.../src/rime/gear/table_translator.cc#L77
6...
startnumber
_startnumber编码开始位置,如:“好” 在 “ni hao” 中的 _start=2
_endnumber编码结束位置,如:“好” 在 “ni hao” 中的 _end=5
qualitynumber结果展示排名权重
textstring候选词内容
commentstring註解(name_space/comment_format)
image
preeditstring得到当前候选词预处理后的输入编码(如形码映射字根、音码分音节加变音符,如:"ni hao")(name_space/preedit_format)

方法:

方法名参数返回值解释
get_dynamic_typestring1. "Phrase": Phrase
2. "Simple": SimpleCandidate
3. "Shadow": ShadowCandidate
4. "Uniquified": UniquifiedCandidate
5. "Other"
get_genuineCandidate
get_genuinestable: <number, Candidate>
to_shadow_candidate
to_uniquified_candidate
append
to_phrasePhrase可能为 nil

ShadowCandidate 衍生扩展词

https://github.com/hchunhui/librime-lua/pull/162

ShadowCandidate(典型地,simplifier 繁简转换产生的新候选词皆为ShadowCandidate

构造方法:ShadowCandidate(cand, type, text, comment, inherit_comment)

  1. cand
  2. type
  3. text
  4. comment
  5. inherit_comment: (可选)

Phrase 词组

Phrase(选择后会更新相应的用户字典)

构造方法:Phrase(memory, type, start, end, entry)

  1. memory: Memory
  2. type: string
  3. start: number
  4. end: number
  5. entry: DictEntry

属性:

属性名类型解释
language
type
start
_start
_end
quality
text
comment
preedit
weight
code
entry

方法:

方法名参数返回值解释
toCandidate

UniquifiedCandidate 去重合并候选词

https://github.com/hchunhui/librime-lua/pull/162

UniqifiedCandidate(cand, type, text, comment) (典型地,uniqifier 合并重复候选词之后形成的唯一候选词即为UniqifiedCandidate

Set

构造方法:Set(table)

  1. table: 列表

ex: local set_tab = Set({'a','b','c','c'}) # set_tab = {a=true,b=true, c=true}

属性:

属性名类型解释

方法:

方法名参数返回值解释
empty
__index
__add
__sub
__mul
__set

属性:

属性名类型解释

方法:

方法名参数返回值解释
add_translation
prepare
get_candidate_at
candidate_count
empty

Opencc

构造方法:Opencc(filename)

  1. filename: string

属性:

属性名类型解释

方法:

方法名参数返回值解释
convert

ReverseDb / ReverseLookup

反查

构造方法:ReverseDb(file_name)

  1. file_name: 反查字典文件路径。 如: build/terra_pinyin.reverse.bin

e.g.

lua
local pyrdb = ReverseDb("build/terra_pinyin.reverse.bin")

属性:

属性名类型解释

方法:

方法名参数返回值解释
lookup

ReverseLookup (ver #177)

构造方法:ReverseLookup(dict_name)

  1. dict_name: 字典名。 如: luna_pinyin

属性:

属性名类型解释

方法:

方法名参数返回值解释
lookup(key)key: stringstring如:ReverseLookup("luna_pinyin"):lookup("百") == "bai bo"
lookup_stems

CommitEntry

继承 DictEntry

属性:

属性名类型解释

方法:

方法名参数返回值解释
get

DictEntry

构造方法:DictEntry()

librime 定义:https://github.com/rime/librime/blob/ae848c47adbe0411d4b7b9538e4a1aae45352c31/include/rime/impl/vocabulary.h#L33

属性:

属性名类型解释
textstring词,如:“好”
commentstring剩下的编码,如:preedit "h", text "好", comment "~ao"
preeditstring如:“h”
weightnumber如:“-13.998352335763”
commit_countnumber如:“2”
custom_codestring词编码(根据特定规则拆分,以" "(空格)连接,如:拼音中以音节拆分),如:“hao”、“ni hao”
remaining_code_lengthnumber(预测的结果中)未输入的编码,如:preedit "h", text "好", comment "~ao", remaining_code_length “2”
codeCode

方法:

方法名参数返回值解释

Code

构造方法:Code()

属性:

属性名类型解释

方法:

方法名参数返回值解释
push(inputCode)rime::SyllableId
(librime中定义的类型)
printstring

Memory

提供来操作 dict(字典、固态字典、静态字典)和 user_dict(用户字典、动态字典)的接口

构造方法:Memory(engine, schema, name_space)

  1. engine: Engine
  2. schema: Schema
  3. name_space: string (可选,默认为空)
  • Memory 字典中有userdb 須要在function fini(env) 中執行 env.mem:disconnect() 關閉userdb 避免記憶泄露

e.g.

lua
env.mem = Memory(env.engine, env.engine.schema)  --  ns = "translator"
-- env.mem = Memory(env.engine, env.engine.schema, env.name_space)  
-- env.mem = Memory(env.engine, Schema("cangjie5")) --  ns = "translator-
-- env.mem = Memory(env.engine, Schema("cangjie5"), "translator")

构造流程:https://github.com/rime/librime/blob/3451fd1eb0129c1c44a08c6620b7956922144850/src/gear/memory.cc#L51

  1. 加载 schema 中指定的字典(dictionary)
    (包括:"{name_space}/dictionary"、"{name_space}/prism"、"{name_space}/packs")
  2. 加在 schema 中指定的用户字典(user_dict)
    (前提:{name_space}/enable_user_dict 为 true)
    (包括:"{name_space}/user_dict" 或 "{name_space}/dictionary")
    (后缀:"*.userdb.txt")
  3. 添加通知事件监听(commit_notifier、delete_notifier、unhandled_key_notifier)

属性:

属性名类型解释

方法:

方法名参数返回值解释
dict_lookup(input, predictive, limit)input: string
predictive: boolean
limit: number
boolean 是否有结果查询
user_lookup(input, predictive)input: string
predictive: boolean
memorize(callback)callback: function
(回调参数:CommitEntry)
当用户字典候选词被选中时触发回调。
decode(code)code: Codetable: <number, string>
iter_dict配合 for ... end 获得 DictEntry
iter_user配合 for ... end 获得 DictEntry
update_userdict(entry, commits, prefix)entry: DictEntry
commits: number
prefix: string
boolean

使用案例:https://github.com/hchunhui/librime-lua/blob/67ef681a9fd03262c49cc7f850cc92fc791b1e85/sample/lua/expand_translator.lua#L32

e.g.

lua
-- 遍历

local input = "hello"
local mem = Memory(env.engine, env.engine.schema) 
mem:dict_lookup(input, true, 100)
-- 遍历字典
for entry in mem:iter_dict() do
 print(entry.text)
end

mem:user_lookup(input, true)
-- 遍历用户字典
for entry in mem:iter_user() do
 print(entry.text)
end
lua
-- 监听 & 更新

env.mem = Memory(env.engine, env.engine.schema) 
env.mem:memorize(function(commit) 
  for i,dictentry in ipairs(commit:get()) do
    log.info(dictentry.text .. " " .. dictentry.weight .. " " .. dictentry.comment .. "")
    -- memory:update_userdict(dictentry, 0, "") -- do nothing to userdict
    -- memory:update_userdict(dictentry, 1, "") -- update entry to userdict
    -- memory:update_userdict(dictentry, -1, "") -- delete entry to userdict
    --[[
      用户字典形式如:
      ```txt
      # Rime user dictionary
      #@/db_name	luna_pinyin.userdb
      #@/db_type	userdb
      #@/rime_version	1.5.3
      #@/tick	693
      #@/user_id	aaaaaaaa-bbbb-4c62-b0b0-ccccccccccc
      wang shang 	网上	c=1 d=0.442639 t=693
      wang shi zhi duo shao 	往事知多少	c=1 d=0.913931 t=693
      wang xia 	往下	c=1 d=0.794534 t=693
      wei 	未	c=1 d=0.955997 t=693
      ```
    --]]
  end
end

Projection

可以用于处理 candidate 的 comment 的转换

构造:Projection()

属性:

属性名类型解释
Projection([ConfigListstring of table])
方法:
方法名参数返回值解释
load(rules)rules: ConfigList-加载转换规则
apply(str,[ret_org_str])str: string, ret_org_str: boolstring转换字符串: 預設轉換失敗返回 空字串, ret_org_str: true 返回原字串

使用参考: https://github.com/hchunhui/librime-lua/pull/102

lua
local config = env.engine.schema.config
-- load ConfigList form path
local proedit_fmt_list = conifg:get_list("translastor/preedit_format")
-- create Projection obj
local p1 = Projection()
-- load convert rules
p1:load(proedit_fmt_list)
-- convert string
local str_raw = "abcdefg"
local str_preedit = p1:apply(str)

-- new example
  local p2 = Projection(config:get_list('translator/preedit_format'))
  local p3 = Projection({'xlit/abc/ABC/', 'xlit/ABC/xyz/'})
   p3:apply(str,[true])

Component

調用 processor, segmentor, translator, filter 組件,可在lua script中再重組。 參考範例: librime-lua/sample/lua/component_test.lua

属性:

属性名类型解释

方法:

方法名参数返回值解释
Processorengine, [schema, ]name_space, prescriptionProcessor如:Component.Processor(env.engine, "", "ascii_composer"), Component.Processor(env.engine, Schema('cangjie5'), "", 'ascii_composer)(使用Schema: cangjie5 config)
Segmentor同上Segmentor
Translator同上Translator`Component.Translator(env.engine, '', 'table_translator')
Filter同上FilterComponent.Filter(env.engine, '', 'uniquility')

Processor

属性:

属性名类型解释
name_spacestring取出instance name_space #212

方法:

方法名参数返回值解释
process_key_eventKeyEvent0-20:kReject 1:kAccepted 2:Noop,參考engine.cc

Segmentator

属性:

属性名类型解释
name_spacestring取出instance name_space #212

方法:

方法名参数返回值解释
proceedSegmentationbool參考engine.cc

Translator

属性:

属性名类型解释
name_spacestring取出instance name_space #212

方法:

方法名参数返回值解释
querystring: input, segmetTranslation 參考engine.cc

Filter

属性:

属性名类型解释
name_spacestring取出instance name_space #212

方法:

方法名参数返回值解释
applytranslation,candsTranslation參考engine.cc

Notifier

接收通知

通知类型:

  1. commit_notifier
  2. select_notifier
  3. update_notifier
  4. delete_notifier

属性:

属性名类型解释

方法: notifier connect

方法名参数返回值解释
connect(func[,group])func: function grup: intNotifier 新增 group 依 gorup 順序通知 0,1,...connect(func) 排在最後)使用 notifier 時必須在解構時 disconnect()

e.g.

lua
-- ctx: Context
function init(env)
  env.notifier = env.engine.context.commit_notifier:connect(function(ctx)
  -- your code ...
end)
end
function fini(env)
   env.notifier:disconnect()
end

OptionUpdateNotifier

同 Notifier

e.g.

lua
-- ctx: Context
-- name: string
env.engine.context.option_update_notifier:connect(function(ctx, name)
  -- your code ...
end)

PropertyUpdateNotifier

同 Notifier

e.g.

lua
-- ctx: Context
-- name: string
env.engine.context.property_update_notifier:connect(function(ctx, name)
  -- your code ...
end)

KeyEventNotifier

同 Notifier

e.g.

lua
-- ctx: Context
-- key: KeyEvent
env.engine.context.unhandled_key_notifier:connect(function(ctx, key)
  -- your code ...
end)

Connection

属性:

属性名类型解释

方法:

方法名参数返回值解释
disconnect

log

记录日志到日志文件

日志位置:https://github.com/rime/home/wiki/RimeWithSchemata#關於調試

  • 【中州韻】 /tmp/rime.ibus.*
  • 【小狼毫】 %TEMP%\rime.weasel.*
  • 【鼠鬚管】 $TMPDIR/rime.squirrel.*
  • 各發行版的早期版本 用戶資料夾/rime.log

属性:

属性名类型解释

方法:

方法名参数返回值解释
info
warning
error

rime_api

属性:

属性名类型解释

方法:

方法名参数返回值解释
get_rime_versionstringlibrime 版本
get_shared_data_dirstring程序目录\data
get_user_data_dirstring用户目录
get_sync_dirstring用户资料同步目录
get_distribution_namestring如:“小狼毫”
get_distribution_code_namestring如:“Weasel”
get_distribution_versionstring发布版本号
get_user_id

CommitRecord

CommitRecord : 參考 librime/src/rime/ engine.cc commit_history.h

  • commit_text => {type: 'raw', text: string}
  • commit => {type: cand.type, text: cand.text}
  • reject => {type: 'thru', text: ascii code}

属性:

属性名类型解释
typestring
textstring

CommitHistory

engine 在 commit commit_text 會將 資料存入 commit_history, reject且屬於ascii範圍時存入ascii 此api 除了可以取出 CommitRecord 還可以在lua中推入commit_record 參考: librime/src/rime/gear/history_translator

属性:

属性名类型解释
sizenumbermax_size <=20

方法:

方法名参数返回值解释
push(KeyEvent), (composition, ctx.input) (cand.type, cand.text)推入 CommitRecord
backCommitRecord取出最後一個 CommitRecord
to_tablelua table of CommitRecord轉出 lua table of CommitRecord
iterreverse_iter
reprstring格式 [type]text[type]text....
latest_textstring取出最後一個CommitRecord.text
emptybool
clearsize=0
pop_back移除最後一個CommitRecord
lua
-- 將comit cand.type == "table" 加入 translation
local T={}
function T.func(inp, seg, env)
  if not seg.has_tag('histroy') then return end

  for r_iter, commit_record in context.commit_history:iter() do
    if commit_record.type == "table" then
       yield(Candidate(commit_record.type, seg.start, seg._end, commit_record.text, "commit_history"))
    end
  end
end
return T

DbAssessor

支援 leveldb:query(prefix_key)

methods: obj of LevelDb

方法名参数返回值解释
resetnonebooljump to begin
jumpprefix of key:stringbooljump to first of prefix_key
iternoneiter_func,self範例: for k, v in da:iter() do print(k, v) end

LevelDb ( 不可用於已開啓的userdb, 專用於 librime-lua key-value db)

便於調用大型資料庫且不佔用 lua 動態記憶

新建 leveldb

方法名参数返回值解释
LevelDbdbname:stringobj of LevelDblocal db = LevelDb('ecdict') -- opendb :user_data_dir/ecdict

物件方法

方法名参数返回值解释
opennonebool
open_read_onlynonebool禁用 earse ,update
closenonebool
loadednonebool
queryprefix of key:stringobj of DbAccessor查找 prefix key
fetchkey:stringvalue:string or nil查找 value
updatekey:string,value:stringbool
erasekey:stringbool

範例:

lua
 -- 建議加入 db_pool 可避免無法開啓已開啓DB
 _db_pool= _db_pool or {}
 local function wrapLevelDb(dbname, mode)
   _db_pool[dbname] = _db_pool[dbname] or LevelDb(dbname)
   local db = _db_pool[dbname]
   if db and not db:loaded() then
      if mode then
        db:open()
      else 
        db:open_read_only()
      end
      return db
   end
 end
 
 local db = wrapLevelDb('ecdict') -- open_read_only
 -- local db = wrapLevelDb('ecdictu', true) -- open
 local da = db:query('the') -- return obj of DbAccessor
 for k, v in da:iter() do print(k, v) end