-
Notifications
You must be signed in to change notification settings - Fork 46
/
Copy pathexpand_translator.lua
70 lines (60 loc) · 2.29 KB
/
expand_translator.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
--[[
in cangjie5.schema.yaml
schema:
schema_id: cangjie5
engine:
translators:
- lua_translator@expand_translator
expand_translator:
wildcard: "*"
// you must add wildcard to speller, otherwise the Rime won't take it as normal input;
speller:
alphabet: zyxwvutsrqponmlkjihgfedcba*
]]
local function memoryCallback(memory, 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
end
return true
end
local function init(env)
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:memorize(function(commit) memoryCallback(env.mem, commit) end)
-- or use
-- schema = Schema("cangjie5") -- schema_id
-- env.mem = Memory(env.engine, schema, "translator")
config = env.engine.schema.config
namespace = 'expand_translator'
env.wildcard = config:get_string(namespace .. '/wildcard')
-- or try get config like this
-- schema = Schema("cangjie5") -- schema_id
-- config = schema.config
log.info("expand_translator Initilized!")
end
local function translate(inp,seg,env)
if string.match(inp,env.wildcard) then
local tail = string.match(inp, '[^'.. env.wildcard .. ']+$') or ''
inp = string.match(inp, '^[^' ..env.wildcard .. ']+')
env.mem:dict_lookup(inp,true, 100) -- expand_search
for dictentry in env.mem:iter_dict()
do
local codetail = string.match(dictentry.comment,tail .. '$') or ''
if tail ~= nil and codetail == tail then
local code = env.mem:decode(dictentry.code)
codeComment = table.concat(code, ",")
local ph = Phrase(env.mem,"expand_translator", seg.start, seg._end, dictentry)
ph.comment = codeComment
yield(ph:toCandidate())
-- you can also use Candidate Simply, but it cannot be recognized by memorize, memorize callback won't be called
-- yield(Candidate("type",seg.start,seg.end,dictentry.text, codeComment ))
end
end
end
end
return {init = init, func = translate}