Skip to content

Commit a29897d

Browse files
authored
Add TableTranslator , ScriptTranslator in Component (hchunhui#287)
* add Component.TableTranslator Signed-off-by: shewer <shewer@gmail.com> * add LTableTranslator * 移除 table_translator.h Signed-off-by: shewer <shewer@gmail.com> * 移除 table_translator.h Signed-off-by: shewer <shewer@gmail.com> * 加入 LScriptTranslator Signed-off-by: shewer <shewer@gmail.com> * fixed boost::optional error Signed-off-by: shewer <shewer@gmail.com> * fix memory dump : memories_callback 初始化 Signed-off-by: shewer <shewer@gmail.com> * add sample Signed-off-by: shewer <shewer@gmail.com> * fixd code Signed-off-by: shewer <shewer@gmail.com> * clang-format 排版 Signed-off-by: shewer <shewer@gmail.com> * fixed sample/ script code * 補上 *engine Signed-off-by: shewer <shewer@gmail.com> * add dict user_dict members of Translator Signed-off-by: shewer <shewer@gmail.com> * refactoring code Signed-off-by: shewer <shewer@gmail.com> --------- Signed-off-by: shewer <shewer@gmail.com>
1 parent 265e1cc commit a29897d

7 files changed

+652
-6
lines changed

sample/lua/script_translator.lua

+81
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
#! /usr/bin/env lua
2+
--
3+
-- script_translator.lua
4+
-- Copyright (C) 2023 Shewer Lu <shewer@gmail.com>
5+
--
6+
-- Distributed under terms of the MIT license.
7+
--
8+
9+
--[[
10+
11+
''' custom.yaml'
12+
patch:
13+
engine/translators/+:
14+
- lua_translator@*table_translator@translator
15+
- lua_translator@*script_translator@cangjie
16+
````
17+
------- methods return
18+
env.tran:start_session false function()
19+
env.tran:finish_session false function()
20+
env.tran:discard_session false function()
21+
env.tran:query false function(inp, seg)
22+
env.tran:memorize false function(commit_entrys)
23+
env.tran:update_entry false function(entry, state, prefix_str)
24+
env.tran.memorize_callback = function(self, commit_entry)
25+
------- vars_set
26+
env.tran.spelling_hints = int >0
27+
env.tran.initial_quality = double
28+
env.tran.contextual_suggestions = boolean
29+
env.tran.enable_completion = boolean
30+
env.tran.always_show_comments = boolean
31+
env.tran.strict_spelling = boolean
32+
env.tran.max_homophones = int
33+
env.tran.enable_correction = boolean
34+
env.tran.tag = string
35+
env.tran.delimiters = string
36+
37+
------- vars_get
38+
res = env.tran.spelling_hints 0 number
39+
res = env.tran.initial_quality 0.0 number
40+
res = env.tran.contextual_suggestions false boolean
41+
res = env.tran.enable_completion true boolean
42+
res = env.tran.always_show_comments false boolean
43+
res = env.tran.strict_spelling false boolean
44+
res = env.tran.max_homophones 1 number
45+
res = env.tran.enable_correction false boolean
46+
res = env.tran.tag abc string
47+
res = env.tran.delimiters ' string
48+
49+
--]]
50+
local M={}
51+
local function simple_callback(self, commits)
52+
local context = self.engine.context
53+
if true then
54+
return self:memorize(commits)
55+
end
56+
end
57+
local function callback(self, commits) -- self : env.tran commits : list
58+
local context = self.engine.context
59+
for i, entry in ipairs(commits:get()) do
60+
self:update_entry(entry,0,"") -- do nothing to userdict
61+
-- self:update_entry(entry,1,"") -- update entry to userdict
62+
-- self:update_entry(entry,-1,"") -- delete entry to userdict
63+
end
64+
end
65+
function M.init(env)
66+
env.tran = Component.ScriptTranslator(env.engine, env.name_space, "script_translator")
67+
env.tran:memorize_callback(simple_callback)
68+
end
69+
70+
function M.fini(env)
71+
end
72+
73+
function M.func(inp, seg, env)
74+
local t = env.tran:query(inp,seg)
75+
if not t then return end
76+
for cand in t:iter() do
77+
yield(cand)
78+
end
79+
end
80+
81+
return M

sample/lua/table_translator.lua

+84
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
#! /usr/bin/env lua
2+
--
3+
-- table_translator.lua
4+
-- Copyright (C) 2023 Shewer Lu <shewer@gmail.com>
5+
--
6+
-- Distributed under terms of the MIT license.
7+
--
8+
--[[
9+
--
10+
''' custom.yaml'
11+
patch:
12+
engine/translators/+:
13+
- lua_translator@*table_translator@translator
14+
- lua_translator@*script_translator@cangjie
15+
````
16+
17+
------- methods
18+
env.tran:finish_session bool function()
19+
env.tran:start_session bool function()
20+
env.tran:discard_session bool function()
21+
env.tran:query translation function(inp, seg)
22+
env.tran:memorize bool function(commit_entrys)
23+
env.tran:update_entry bool function(entry)
24+
25+
------- vars_set 設定值
26+
env.tran.max_homophones = number
27+
env.tran.spelling_hints = number
28+
env.tran.enable_correction = boolean
29+
env.tran.memorize_callback = function(self, commits)
30+
env.tran.enable_completion = false boolean
31+
env.tran.delimiters = false string
32+
env.tran.strict_spelling = boolean
33+
env.tran.contextual_suggestions = boolean
34+
env.tran.initial_quality = double
35+
env.tran.always_show_comments = boolean
36+
env.tran.tag = string
37+
38+
------- vars_get 取值
39+
res = env.tran.max_homophones 1 number
40+
res = env.tran.spelling_hints 0 number
41+
res = env.tran.enable_correction false boolean
42+
res = env.tran.enable_completion true boolean
43+
res = env.tran.delimiters ' string
44+
res = env.tran.strict_spelling false boolean
45+
res = env.tran.contextual_suggestions false boolean
46+
res = env.tran.initial_quality 0.0 number
47+
res = env.tran.always_show_comments false boolean
48+
res = env.tran.tag cangjie string
49+
50+
51+
--]]
52+
53+
local M={}
54+
local function simple_callback(self, commits)
55+
local context = self.engine.context
56+
if true then
57+
return self:memorize(commits)
58+
end
59+
end
60+
local function callback(self, commits) -- self : env.tran commits : list
61+
local context = self.engine.context
62+
for i, entry in ipairs(commits:get()) do
63+
self:update_entry(entry, 0,"") -- do nothing to userdict
64+
-- self:update_entry(entry,1,"") -- update entry to userdict
65+
-- self:update_entry(entry,-1,"") -- delete entry to userdict
66+
end
67+
end
68+
function M.init(env)
69+
env.tran = Component.TableTranslator(env.engine, env.name_space, "table_translator")
70+
env.tran:memorize_callback(simple_callback)
71+
end
72+
73+
function M.fini(env)
74+
end
75+
76+
function M.func(inp, seg, env)
77+
local t = env.tran:query(inp,seg)
78+
if not t then return end
79+
for cand in t:iter() do
80+
yield(cand)
81+
end
82+
end
83+
84+
return M

sample/luna_pinyin.custom.yaml

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
patch:
2+
engine/translators/+:
3+
- lua_translator@*table_translator@translator
4+
- lua_translator@*script_translator@cangjie
5+
6+

src/script_translator.cc

+178
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,178 @@
1+
/*
2+
* table_translator.cc
3+
* Copyright (C) 2023 Shewer Lu <shewer@gmail.com>
4+
*
5+
* Distributed under terms of the MIT license.
6+
*/
7+
8+
#include <rime/gear/script_translator.h>
9+
#include <rime/algo/syllabifier.h>
10+
#include <rime/dict/corrector.h>
11+
#include <rime/gear/poet.h>
12+
13+
14+
#include "translator.h"
15+
16+
using namespace rime;
17+
18+
namespace {
19+
namespace ScriptTranslatorReg {
20+
21+
class LScriptTranslator : public ScriptTranslator {
22+
public:
23+
LScriptTranslator(const Ticket& ticket, Lua* lua)
24+
: ScriptTranslator(ticket), lua_(lua) {};
25+
26+
virtual bool Memorize(const CommitEntry& commit_entry);
27+
bool memorize(const CommitEntry& commit_entry);
28+
bool update_entry(const DictEntry& index,
29+
int commits, const string& new_entory_prefix);
30+
31+
SET_(memorize_callback, an<LuaObj>);
32+
bool memorize_callback();
33+
34+
// TranslatorOptions
35+
SET_(contextual_suggestions, bool);
36+
SET_(delimiters, string&);
37+
SET_(preedit_formatter, Projection&);
38+
SET_(comment_formatter, Projection&);
39+
bool reload_user_dict_disabling_patterns(an<ConfigList>);
40+
41+
// ScriptTranslator member
42+
ACCESS_(spelling_hints, int);
43+
ACCESS_(always_show_comments, bool);
44+
ACCESS_(max_homophones, int);
45+
GET_(enable_correction, bool);
46+
void set_enable_correction(bool);
47+
48+
protected:
49+
Lua* lua_;
50+
an<LuaObj> memorize_callback_;
51+
void init_correction();
52+
};
53+
54+
using T = LScriptTranslator;
55+
56+
bool T::memorize_callback() {
57+
return (memorize_callback_) ? true : false;
58+
}
59+
60+
bool T::memorize(const CommitEntry& commit_entry) {
61+
return ScriptTranslator::Memorize(commit_entry);
62+
}
63+
64+
bool T::Memorize(const CommitEntry& commit_entry) {
65+
if (!memorize_callback_) {
66+
return memorize(commit_entry);
67+
}
68+
69+
auto r = lua_->call<bool, an<LuaObj>, LScriptTranslator*, const CommitEntry&>(
70+
memorize_callback_, this, commit_entry);
71+
if (!r.ok()) {
72+
auto e = r.get_err();
73+
LOG(ERROR) << "LScriptTranslator of " << name_space_
74+
<< ": memorize_callback error(" << e.status << "): " << e.e;
75+
return false;
76+
}
77+
return r.get();
78+
}
79+
80+
void T::init_correction() {
81+
if (auto* corrector = Corrector::Require("corrector")) {
82+
Ticket ticket(engine_, name_space_);
83+
corrector_.reset(corrector->Create(ticket));
84+
}
85+
}
86+
87+
void T::set_enable_correction(bool enable) {
88+
if (enable_correction_ = enable && !corrector_)
89+
init_correction();
90+
}
91+
92+
bool T::update_entry(const DictEntry& entry,
93+
int commits, const string& new_entory_prefix) {
94+
if (user_dict_ && user_dict_->loaded())
95+
return user_dict_->UpdateEntry(entry, commits, new_entory_prefix);
96+
97+
return false;
98+
}
99+
100+
bool T::reload_user_dict_disabling_patterns(an<ConfigList> cl) {
101+
return cl ? user_dict_disabling_patterns_.Load(cl) : false;
102+
}
103+
104+
static const luaL_Reg funcs[] = {
105+
{NULL, NULL},
106+
};
107+
108+
static const luaL_Reg methods[] = {
109+
{"query", WRAPMEM(T, Query)}, // string, segment
110+
{"start_session", WRAPMEM(T, StartSession)},
111+
{"finish_session", WRAPMEM(T, FinishSession)},
112+
{"discard_session", WRAPMEM(T, DiscardSession)},
113+
WMEM(memorize), // delegate TableTransaltor::Momorize
114+
WMEM(update_entry), // delegate UserDictionary::UpdateEntry
115+
WMEM(reload_user_dict_disabling_patterns),
116+
Set_WMEM(memorize_callback), // an<LuaObj> callback function
117+
{NULL, NULL},
118+
};
119+
120+
static const luaL_Reg vars_get[] = {
121+
Get_WMEM(name_space), // string
122+
Set_WMEM(memorize_callback), // an<LuaObj> callback function
123+
// ScriptTranslator member
124+
Get_WMEM(max_homophones), // int
125+
Get_WMEM(spelling_hints), // int
126+
Get_WMEM(always_show_comments), // bool
127+
Get_WMEM(enable_correction), // bool
128+
//TranslatorOptions
129+
Get_WMEM(delimiters), // string&
130+
Get_WMEM(tag), // string
131+
Get_WMEM(enable_completion), // bool
132+
Get_WMEM(contextual_suggestions), // bool
133+
Get_WMEM(strict_spelling), // bool
134+
Get_WMEM(initial_quality), // double
135+
Get_WMEM(preedit_formatter), // Projection&
136+
Get_WMEM(comment_formatter), // Projection&
137+
// Memory
138+
Get_WMEM(dict),
139+
Get_WMEM(user_dict),
140+
{NULL, NULL},
141+
};
142+
143+
static const luaL_Reg vars_set[] = {
144+
// ScriptTranslator member
145+
Set_WMEM(max_homophones), // int
146+
Set_WMEM(spelling_hints), // int
147+
Set_WMEM(always_show_comments), // bool
148+
Set_WMEM(enable_correction), // bool
149+
// TranslatorOptions
150+
Set_WMEM(delimiters), // string&
151+
Set_WMEM(tag), // string
152+
Set_WMEM(enable_completion), // bool
153+
Set_WMEM(contextual_suggestions), // bool
154+
Set_WMEM(strict_spelling), // bool
155+
Set_WMEM(initial_quality), // double
156+
Set_WMEM(preedit_formatter), // Projection&
157+
Set_WMEM(comment_formatter), // Projection&
158+
{NULL, NULL},
159+
};
160+
161+
void reg_Component(lua_State* L) {
162+
lua_getglobal(L, "Component");
163+
if (lua_type(L, -1) != LUA_TTABLE) {
164+
LOG(ERROR) << "table of _G[\"Component\"] not found.";
165+
} else {
166+
lua_pushcfunction(L, raw_make_translator<T>);
167+
lua_setfield(L, -2, "ScriptTranslator");
168+
}
169+
lua_pop(L, 1);
170+
}
171+
172+
} // namespace ScriptTranslatorReg
173+
} // namespace
174+
175+
void LUAWRAPPER_LOCAL script_translator_init(lua_State* L) {
176+
EXPORT(ScriptTranslatorReg, L);
177+
ScriptTranslatorReg::reg_Component(L);
178+
}

0 commit comments

Comments
 (0)