Skip to content

Commit

Permalink
refactor set_memorize_callback() memorize_callback (#331)
Browse files Browse the repository at this point in the history
* add Component.TableTranslator

Signed-off-by: shewer <[email protected]>

* add LTableTranslator

* 移除 table_translator.h

Signed-off-by: shewer <[email protected]>

* 移除 table_translator.h

Signed-off-by: shewer <[email protected]>

* 加入 LScriptTranslator

Signed-off-by: shewer <[email protected]>

* fixed boost::optional error

Signed-off-by: shewer <[email protected]>

* fix memory dump : memories_callback 初始化

Signed-off-by: shewer <[email protected]>

* add sample

Signed-off-by: shewer <[email protected]>

* fixd  code

Signed-off-by: shewer <[email protected]>

* clang-format 排版

Signed-off-by: shewer <[email protected]>

* fixed sample/  script code

* 補上 *engine

Signed-off-by: shewer <[email protected]>

* add dict user_dict members of Translator

Signed-off-by: shewer <[email protected]>

* refactoring code

Signed-off-by: shewer <[email protected]>

* fixed ScriptTranslatorReg::vars_get: {memorize_call}

Signed-off-by: shewer <[email protected]>

* using optional : memorize_callback()

Signed-off-by: shewer <[email protected]>

* fixed format

Signed-off-by: shewer <[email protected]>

* add raw_set_memorize_callback([nil|function|other data]) return bool: nil(reset), function

Signed-off-by: shewer <[email protected]>

* reduce include head file in translator.h

Signed-off-by: shewer <[email protected]>

* update sample sclipt (table_translator.lua script_translator.lua)

Signed-off-by: shewer <[email protected]>

* add vars_get  "lang_name" to support langname check

Signed-off-by: shewer <[email protected]>

---------

Signed-off-by: shewer <[email protected]>
  • Loading branch information
shewer authored May 2, 2024
1 parent c85440f commit cb1152c
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 30 deletions.
9 changes: 7 additions & 2 deletions sample/lua/script_translator.lua
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,12 @@ env.tran:discard_session false function()
env.tran:query false function(inp, seg)
env.tran:memorize false function(commit_entrys)
env.tran:update_entry false function(entry, state, prefix_str)
env.tran.memorize_callback = function(self, commit_entry)
env.tran:set_memorize_callback bool function(commit_entry)
------- vars_set
env.tran.spelling_hints = int >0
env.tran.initial_quality = double
env.tran.contextual_suggestions = boolean
env.tran.memorize_callback = [function | nil]
env.tran.enable_completion = boolean
env.tran.always_show_comments = boolean
env.tran.strict_spelling = boolean
Expand All @@ -38,6 +39,7 @@ env.tran.delimiters = string
res = env.tran.spelling_hints 0 number
res = env.tran.initial_quality 0.0 number
res = env.tran.contextual_suggestions false boolean
env.tran.memorize_callback function|nil
res = env.tran.enable_completion true boolean
res = env.tran.always_show_comments false boolean
res = env.tran.strict_spelling false boolean
Expand All @@ -64,7 +66,10 @@ local function callback(self, commits) -- self : env.tran commits : list
end
function M.init(env)
env.tran = Component.ScriptTranslator(env.engine, env.name_space, "script_translator")
env.tran:memorize_callback(simple_callback)
env.tran:set_memorize_callback(simple_callback)
--env.tran:set_memorize_callback() -- reset callback
--env.tran.memorize_callback= function(simple_callback)
--env.tran.memorize_callback= nil -- reset callback
end

function M.fini(env)
Expand Down
9 changes: 7 additions & 2 deletions sample/lua/table_translator.lua
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,13 @@ env.tran:discard_session bool function()
env.tran:query translation function(inp, seg)
env.tran:memorize bool function(commit_entrys)
env.tran:update_entry bool function(entry)
env.tran:set_memorize_callback bool function(commit_entry)
------- vars_set 設定值
env.tran.max_homophones = number
env.tran.spelling_hints = number
env.tran.enable_correction = boolean
env.tran.memorize_callback = function(self, commits)
env.tran.memorize_callback = function(commits)|nil
env.tran.enable_completion = false boolean
env.tran.delimiters = false string
env.tran.strict_spelling = boolean
Expand All @@ -39,6 +40,7 @@ env.tran.tag = string
res = env.tran.max_homophones 1 number
res = env.tran.spelling_hints 0 number
res = env.tran.enable_correction false boolean
env.tran.memorize_callback function|nil
res = env.tran.enable_completion true boolean
res = env.tran.delimiters ' string
res = env.tran.strict_spelling false boolean
Expand Down Expand Up @@ -67,7 +69,10 @@ local function callback(self, commits) -- self : env.tran commits : list
end
function M.init(env)
env.tran = Component.TableTranslator(env.engine, env.name_space, "table_translator")
env.tran:memorize_callback(simple_callback)
env.tran:set_memorize_callback(simple_callback)
--env.tran:set_memorize_callback() -- reset callback
--env.tran.memorize_callback= function(simple_callback)
--env.tran.memorize_callback= nil -- reset callback
end

function M.fini(env)
Expand Down
26 changes: 17 additions & 9 deletions src/script_translator.cc
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@
#include <rime/dict/corrector.h>
#include <rime/gear/poet.h>

#include <rime/dict/dictionary.h>
#include <rime/dict/user_dictionary.h>
#include <rime/language.h>

#include "translator.h"

Expand All @@ -29,7 +32,8 @@ namespace ScriptTranslatorReg {
int commits, const string& new_entory_prefix);

SET_(memorize_callback, an<LuaObj>);
bool memorize_callback();
optional<an<LuaObj>> memorize_callback();
string lang_name() { return language_->name();};

// TranslatorOptions
SET_(contextual_suggestions, bool);
Expand All @@ -53,8 +57,10 @@ namespace ScriptTranslatorReg {

using T = LScriptTranslator;

bool T::memorize_callback() {
return (memorize_callback_) ? true : false;
optional<an<LuaObj>> T::memorize_callback() {
if (memorize_callback_)
return memorize_callback_;
return {};
}

bool T::memorize(const CommitEntry& commit_entry) {
Expand All @@ -67,7 +73,7 @@ namespace ScriptTranslatorReg {
}

auto r = lua_->call<bool, an<LuaObj>, LScriptTranslator*, const CommitEntry&>(
memorize_callback_, this, commit_entry);
memorize_callback_, this, commit_entry);
if (!r.ok()) {
auto e = r.get_err();
LOG(ERROR) << "LScriptTranslator of " << name_space_
Expand Down Expand Up @@ -113,14 +119,15 @@ namespace ScriptTranslatorReg {
WMEM(memorize), // delegate TableTransaltor::Momorize
WMEM(update_entry), // delegate UserDictionary::UpdateEntry
WMEM(reload_user_dict_disabling_patterns),
Set_WMEM(memorize_callback), // an<LuaObj> callback function
{"set_memorize_callback", raw_set_memorize_callback<T>}, // an<LuaObj> callback function
{NULL, NULL},
};

static const luaL_Reg vars_get[] = {
Get_WMEM(name_space), // string
Set_WMEM(memorize_callback), // an<LuaObj> callback function
// ScriptTranslator member
Get_WMEM(name_space), // string
Get_WMEM(lang_name), // string
Get_WMEM(memorize_callback), // an<LuaObj> callback function
// ScriptTranslator member
Get_WMEM(max_homophones), // int
Get_WMEM(spelling_hints), // int
Get_WMEM(always_show_comments), // bool
Expand All @@ -141,12 +148,13 @@ namespace ScriptTranslatorReg {
};

static const luaL_Reg vars_set[] = {
{"memorize_callback", raw_set_memorize_callback<T>}, // an<LuaObj> callback function
// ScriptTranslator member
Set_WMEM(max_homophones), // int
Set_WMEM(spelling_hints), // int
Set_WMEM(always_show_comments), // bool
Set_WMEM(enable_correction), // bool
// TranslatorOptions
// TranslatorOptions
Set_WMEM(delimiters), // string&
Set_WMEM(tag), // string
Set_WMEM(enable_completion), // bool
Expand Down
28 changes: 19 additions & 9 deletions src/table_translator.cc
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@
#include <rime/schema.h>
#include <rime/engine.h>

#include <rime/dict/dictionary.h>
#include <rime/dict/user_dictionary.h>
#include <rime/language.h>

#include "translator.h"

using namespace rime;
Expand All @@ -29,7 +33,8 @@ namespace TableTranslatorReg {
int commits, const string& new_entory_prefix);

SET_(memorize_callback, an<LuaObj>);
bool memorize_callback();
optional<an<LuaObj>> memorize_callback();
string lang_name() const { return language_->name();};

// TranslatorOptions
void set_contextual_suggestions(bool);
Expand Down Expand Up @@ -60,9 +65,12 @@ namespace TableTranslatorReg {

using T = LTableTranslator;

bool T::memorize_callback() {
return (memorize_callback_) ? true : false;
optional<an<LuaObj>> T::memorize_callback() {
if (memorize_callback_)
return memorize_callback_;
return {};
}

bool T::memorize(const CommitEntry& commit_entry) {
return TableTranslator::Memorize(commit_entry);
}
Expand All @@ -73,18 +81,18 @@ namespace TableTranslatorReg {
}

auto r = lua_->call<bool, an<LuaObj>, LTableTranslator*, const CommitEntry&>(
memorize_callback_, this, commit_entry);
memorize_callback_, this, commit_entry);
if (!r.ok()) {
auto e = r.get_err();
LOG(ERROR) << "LTableTranslator of " << name_space_
<< ": memorize_callback error(" << e.status << "): " << e.e;
<< ": memorize_callback error(" << e.status << "): " << e.e;
return false;
}
return r.get();
}

bool T::update_entry(const DictEntry& entry,
int commits, const string& new_entory_prefix) {
int commits, const string& new_entory_prefix) {
if (user_dict_ && user_dict_->loaded())
return user_dict_->UpdateEntry(entry, commits, new_entory_prefix);

Expand Down Expand Up @@ -151,14 +159,15 @@ namespace TableTranslatorReg {
WMEM(memorize), // delegate TableTransaltor::Momorize
WMEM(update_entry), // delegate UserDictionary::UpdateEntry
WMEM(reload_user_dict_disabling_patterns),
Set_WMEM(memorize_callback), // an<LuaObj> callback function
{"set_memorize_callback", raw_set_memorize_callback<T>}, // an<LuaObj> callback function
{NULL, NULL},
};

static const luaL_Reg vars_get[] = {
// class translator member
Get_WMEM(name_space), // string
Get_WMEM(memorize_callback), // an<LuaObj> callback function
Get_WMEM(name_space), // string
Get_WMEM(lang_name), // string
Get_WMEM(memorize_callback), // an<LuaObj> callback function
// TabletTranslator member
Get_WMEM(enable_charset_filter), // bool
Get_WMEM(enable_encoder), // bool
Expand All @@ -183,6 +192,7 @@ namespace TableTranslatorReg {
};

static const luaL_Reg vars_set[] = {
{"memorize_callback", raw_set_memorize_callback<T>}, // an<LuaObj> callback function
// TableTranslator member
Set_WMEM(enable_charset_filter), // bool
Set_WMEM(enable_encoder), // bool
Expand Down
35 changes: 27 additions & 8 deletions src/translator.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,9 @@
#ifndef _LUA_TRANSLATOR_H
#define _LUA_TRANSLATOR_H

#include <rime/common.h>
#include <rime/engine.h>
#include <rime/schema.h>
#include <rime/config.h>
#include <rime/dict/dictionary.h>
#include <rime/dict/user_dictionary.h>
#include <rime/ticket.h>
#include "lib/lua_export_type.h"
#include "optional.h"

#define SET_(name, type) \
void set_##name(const type data) { \
Expand Down Expand Up @@ -56,14 +52,37 @@ int raw_make_translator(lua_State* L){
if ( n == 4 )
ticket.schema = &(LuaType<rime::Schema &>::todata(L, 2) ); //overwrite schema
Lua* lua= Lua::from_state(L);
rime::an<O> obj = rime::New<O>(ticket, lua);
std::shared_ptr<O> obj = rime::New<O>(ticket, lua);
if (obj) {
LuaType<rime::an<O>>::pushdata(L, obj);
LuaType<std::shared_ptr<O>>::pushdata(L, obj);
return 1;
}
else {
//LOG(ERROR) << "error creating " << typeid(O).name() << ": '" << ticket.klass << "'";
return 0;
}
};

template <typename O>
int raw_set_memorize_callback(lua_State *L) {
bool res = false;
auto t = LuaType<std::shared_ptr<O>>::todata(L, 1);
const int type = (1 < lua_gettop(L)) ? lua_type(L, 2) : LUA_TNIL;
if (type == LUA_TNIL) {
// reset memorize_callback
LOG(INFO) << typeid(*t).name() <<" of " << t->name_space() << ": reset memorize_callback";
t->set_memorize_callback({});
res = true;
}
else if (type == LUA_TFUNCTION) {
t->set_memorize_callback( LuaObj::todata(L, 2));
res = true;
}
else {
LOG(WARNING) << typeid(*t).name() <<" of " << t->name_space()
<< ": set memorize_callback '?' (function expected, got " << lua_typename(L, type) <<")";
}
lua_pushboolean(L, res);
return 1;
}
#endif /* !_LUA_TRANSLATOR_H */

0 comments on commit cb1152c

Please sign in to comment.