-
Notifications
You must be signed in to change notification settings - Fork 47
Expand file tree
/
Copy pathcomponent_test.lua
More file actions
224 lines (203 loc) · 5.18 KB
/
component_test.lua
File metadata and controls
224 lines (203 loc) · 5.18 KB
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
#! /usr/bin/env lua
--
-- rime.lua
-- Copyright (C) 2022 Shewer Lu <shewer@gmail.com>
--
-- Distributed under terms of the MIT license.
--
--[[
Component.Processor(engine, [schema,] ns, <klass>@<ns>)
Component.Segmentor(engine, [schema,] ns, <klass>@<ns>)
Component.Translator(engine, [schema,] ns, <klass>@<ns>)
Component.Filter(env.engine, [schema,] ns, <klass>@<ns>)
ex:
-- return an<Translator> load config from <self schema_id>.schema:/translator
Component.Translator(env.engine,"","script_translator")
-- return an<Translator> load config from luna_pinyin.schema:/translator
Component.Translator(env.engine,Schema(luna_pinyin"),"","script_translator")
-- rime.lua
require 'component_text'
init_processor=P
init_segmentor=S
m_translator=T
init_filter=F
test.schema.yaml
engine:
processors:
- lua_processor@(init_processor
segmentors:
- lua_segmentor@init_segmentor
translators:
- lua_translator@m_translator
filters:
- lua_filter@init_filter
--]]
local P={}
local S={}
local T={}
local F={}
function P.init(env)
print( "** init Processor",env.name_space )
env.ncomps={
"ascii_composer",
"recognizer",
"key_binder",
"speller",
"punctuator",
"selector",
"navigator",
}
env.comps={}
for i,v in ipairs(env.ncomps) do
local comp=Component.Processor(env.engine,"",v)
assert(comp,"failed : create processor of " .. v)
table.insert(env.comps, Component.Processor(env.engine,"",v))
end
-- option: editor_mode change editor_mode
env.neditors={
"express_editor",
"fluid_editor",
}
local function gen_editor(env)
local mode = env.engine.context:get_option("editor_mode") and 2 or 1
print("** " ,env.neditors[mode])
env.editor= Component.Processor(env.engine,"",env.neditors[mode])
end
gen_editor(env)
env.notifier = env.engine.context.option_update_notifier:connect(
function(ctx,name)
if name =="editor_mode" then
gen_editor(env)
end
end)
end
function P.fini(env)
env.notifier:disconnect()
end
function P.func(key,env)
local context = env.engine.context
local key = KeyEvent(key:repr() )
local Rejected,Accepted,Noop= 0,1,2
for i,v in ipairs(env.comps) do
res = v:process_key_event(key)
if res == Rejected or res == Accepted then
return res
end
end
return env.editor:process_key_event(key)
end
function S.init(env)
print( "** init segmentor",env.name_space )
env.ncomps = {
"ascii_segmentor",
"matcher",
"abc_segmentor",
"punct_segmentor",
"fallback_segmentor",
}
env.comps ={}
for i,v in ipairs(env.ncomps) do
local comp=Component.Segmentor(env.engine,"",v)
assert(comp,"failed : create segmentor of " .. v)
table.insert(env.comps,comp)
end
end
function S.fini(env)
end
function S.func(segs,env)
local res=true
for i,v in ipairs(env.comps) do
res = v:proceed(segs)
if not res then
break
end
end
return res
end
function T.init(env)
print( "** init translator",env.name_space )
env.tran1 = Component.Translator(env.engine,"","table_translator@translator")
assert(env.tran1, "failed: tran1 of table_translator ")
local luna= Schema("luna_pinyin")
luna.config:set_string("translator/initial_quality", "1.5")
env.tran2 = Component.Translator(env.engine,luna,"translator","script_translator")
assert(env.tran1, "failed: tran2 of stript_translator from luna_pinyin.schema")
end
function T.fini(env)
end
function T.func(input,seg,env)
local comp_cand1
local t1= env.tran1:query(input,seg)
for cand in t1:iter() do
if cand.type == "compoletion" then
ccomp_cand1 = cand
break
end
yield(cand)
end
local comp_cand2
local t2 = env.tran2:query(input,reg)
for cand in t2:iter() do
if cand.type == "compoletion" then
ccomp_cand2 = cand
break
end
yield(cand)
end
yield(comp_cand1)
for cand in t1:iter() do
yield(cand)
end
yield(comp_cand2)
for cand in t2:iter() do
yield(cand)
end
end
function F.init(env)
print( "** init filter",env.name_space )
env.ncomps={
"simplifier",
"single_char_filter",
"uniquifier"
}
env.comps={}
for i,v in ipairs(env.ncomps) do
local comp= Component.Filter(env.engine, "" , v)
assert( comp , "failed : create filter comp: " .. v)
table.insert(env.comps, comp)
end
-- reversedb test
env.reversedb= ReverseLookup("luna_pinyin")
assert(env.reversedb, "failed create reversedb : luna_pinyin")
assert(env.reversedb:lookup("百")=="bai bo", "luna_pinyin reverse lookup not match")
end
function F.fini(env)
end
function F.tags_match(seg,env)
env.applyed={}
env.napplyed={}
for i,v in ipairs(env.comps) do
if v:applies_to_segment(seg) then
table.insert(env.napplyed,env.ncomps[i])
table.insert(env.applyed,v)
end
end
return true
end
-- new args cands for subfilter:apply(inp,cands)
function F.func(inp,env,cands)
for i,v in ipairs(env.applyed) do
inp = v:apply(inp,cands)
end
for cand in inp:iter() do
local code = env.reversedb:lookup(cand.text)
local comment = cand.comment .. "-" .. code .. "-" .. cand.quality
local c = cand:get_genuine()
c.comment = comment
yield(cand)
end
end
init_processor=P
init_segmentor=S
m_translator=T
init_filter=F