diff --git a/src/script_translator.cc b/src/script_translator.cc new file mode 100644 index 0000000..95b0862 --- /dev/null +++ b/src/script_translator.cc @@ -0,0 +1,178 @@ +/* + * table_translator.cc + * Copyright (C) 2023 Shewer Lu + * + * Distributed under terms of the MIT license. + */ + +#include +#include +#include +#include + +#include +#include + +#include + +#include "translator.h" +namespace { + +using namespace rime; + +class LScriptTranslator : public ScriptTranslator { + public: + LScriptTranslator(const Ticket& ticket, Lua* lua); + virtual bool Memorize(const CommitEntry& commit_entry); + bool memorize(const CommitEntry& commit_entry); + bool update_entry(const DictEntry& index, + int commits, + const string& new_entory_prefix); + GET_(engine, Engine*); + ACCESS_(memorize_callback, an); + + // ScriptTranslator member + ACCESS_(spelling_hints, int); // ok + ACCESS_(always_show_comments, bool); // ok + ACCESS_(max_homophones, int); // ok + GET_(enable_correction, bool); // ok + void set_enable_correction(bool); + // TranslatorOptions + void set_contextual_suggestions(bool); + SET_(delimiters, string&); + + + protected: + Lua* lua_; + an memorize_callback_ = {}; + string schema_id_; +}; + +bool LScriptTranslator::Memorize(const CommitEntry& commit_entry) { + if (!memorize_callback_ || memorize_callback_->type() != LUA_TFUNCTION) { + return ScriptTranslator::Memorize(commit_entry); + } + + auto r = lua_->call, LScriptTranslator*, const CommitEntry&>( + memorize_callback_, this, commit_entry); + if (!r.ok()) { + auto e = r.get_err(); + LOG(ERROR) << "LScriptTranslator of " << name_space_ + << ": memorize_callback error(" << e.status << "): " << e.e; + return false; + } + return r.get(); +} + + +bool LScriptTranslator::memorize(const CommitEntry& commit_entry) { + return ScriptTranslator::Memorize(commit_entry); +} + +LScriptTranslator::LScriptTranslator(const Ticket& ticket, Lua* lua) + : lua_(lua), ScriptTranslator(ticket), schema_id_(ticket.schema->schema_id()){ +} + +void LScriptTranslator::set_enable_correction(bool enable) { + enable_correction_ = enable; + if (corrector_ || !enable_correction_) + return; + + if (auto* corrector = Corrector::Require("corrector")) { + Schema schema = Schema(schema_id_); + Ticket ticket( &schema, name_space_); + corrector_.reset(corrector->Create(ticket)); + } +} +void LScriptTranslator::set_contextual_suggestions(bool enable) { + contextual_suggestions_ = enable; + if (poet_ || !contextual_suggestions_) + return; + Config* config = engine_->schema()->config(); + poet_.reset(new Poet(language(), config, Poet::LeftAssociateCompare)); +} + + +bool LScriptTranslator::update_entry(const DictEntry& entry, + int commits, + const string& new_entory_prefix) { + if (user_dict_ && user_dict_->loaded()) + return user_dict_->UpdateEntry(entry, commits, new_entory_prefix); + + return false; +} + + +namespace ScriptTranslatorReg { +using T = LScriptTranslator; + +static const luaL_Reg funcs[] = { + {NULL, NULL}, +}; + +static const luaL_Reg methods[] = { + {"query", WRAPMEM(T, Query)}, // string, segment + {"start_session", WRAPMEM(T, StartSession)}, + {"finish_session", WRAPMEM(T, FinishSession)}, + {"discard_session", WRAPMEM(T, DiscardSession)}, + WMEM(memorize), // delegate TableTransaltor::Momorize + WMEM(update_entry), // delegate UserDictionary::UpdateEntry + {NULL, NULL}, +}; + +static const luaL_Reg vars_get[] = { + Get_WMEM(memorize_callback), // an callback + + // ScriptTranslator member + Get_WMEM(max_homophones), // int + Get_WMEM(spelling_hints), // int + Get_WMEM(always_show_comments), // bool + Get_WMEM(enable_correction), // bool + // TranslatorOptions + Get_WMEM(delimiters), // string& + Get_WMEM(tag), // string + Get_WMEM(enable_completion), // bool + Get_WMEM(contextual_suggestions), // bool + Get_WMEM(strict_spelling), // bool + Get_WMEM(initial_quality), // double + {NULL, NULL}, +}; + +static const luaL_Reg vars_set[] = { + Set_WMEM(memorize_callback), // an callback + + // ScriptTranslator member + Set_WMEM(max_homophones), // int + Set_WMEM(spelling_hints), // int + Set_WMEM(always_show_comments), // bool + Set_WMEM(enable_correction), // bool + // TranslatorOptions + Set_WMEM(delimiters), // string& + Set_WMEM(tag), // string + Set_WMEM(enable_completion), // bool + Set_WMEM(contextual_suggestions), // bool + Set_WMEM(strict_spelling), // bool + Set_WMEM(initial_quality), // double + {NULL, NULL}, +}; + +void reg_Component(lua_State* L) { + lua_getglobal(L, "Component"); + if (lua_type(L, -1) != LUA_TTABLE) { + LOG(ERROR) << "table of _G[\"Component\"] not found."; + } + else { + int index = lua_absindex(L, -1); + lua_pushcfunction(L, raw_make_translator); + lua_setfield(L, index, "ScriptTranslator"); + } + lua_pop(L, 1); +} + +} // namespace StriptTranslatorReg +} // namespace + +void LUAWRAPPER_LOCAL script_translator_init(lua_State* L) { + EXPORT(ScriptTranslatorReg, L); + ScriptTranslatorReg::reg_Component(L); +} diff --git a/src/types_ext.cc b/src/types_ext.cc index e270696..ad636f7 100644 --- a/src/types_ext.cc +++ b/src/types_ext.cc @@ -14,15 +14,17 @@ #include #include #include "table_translator.h" +#include "script_translator.h" #include "lib/lua_export_type.h" #include "lib/luatype_boost_optional.h" - +#include "translator.h" #include using namespace rime; - namespace { +using ns::optional; + template using void_t = void; template @@ -312,7 +314,6 @@ namespace UserDbReg{ } namespace ComponentReg{ - using LT= LTableTranslator; using P = Processor; using S = Segmentor; using T = Translator; @@ -344,40 +345,12 @@ namespace ComponentReg{ } }; - template - int raw_make(lua_State *L){ - int n = lua_gettop(L); - if (3 > n || 4 < n) - return 0; - - C_State C; - Ticket ticket( - LuaType::todata(L, 1), - LuaType::todata(L, -2, &C), - LuaType::todata(L, -1, &C) - ); - DLOG(INFO) << "check Ticket:" << ticket.klass << "@" <::todata(L, 2) ); //overwrite schema - Lua* lua= Lua::from_state(L); - //an obj = New(Lua::from_state(L), ticket); - an obj = New(ticket, lua); - if (obj) { - LuaType>::pushdata(L, obj); - return 1; - } - else { - //LOG(ERROR) << "error creating " << typeid(O).name() << ": '" << ticket.klass << "'"; - return 0; - } - }; static const luaL_Reg funcs[] = { {"Processor", raw_create

}, {"Segmentor" , raw_create}, {"Translator", raw_create}, {"Filter", raw_create}, - {"TableTranslator", raw_make}, { NULL, NULL }, }; @@ -391,6 +364,7 @@ namespace ComponentReg{ } void table_translator_init(lua_State *L); +void script_translator_init(lua_State *L); void LUAWRAPPER_LOCAL types_ext_init(lua_State *L) { EXPORT(ProcessorReg, L); @@ -400,6 +374,8 @@ void LUAWRAPPER_LOCAL types_ext_init(lua_State *L) { EXPORT(ReverseLookupDictionaryReg, L); EXPORT(DbAccessorReg, L); EXPORT(UserDbReg, L); - table_translator_init(L); ComponentReg::init(L); + // add LtableTranslator ScriptTranslator in Component + table_translator_init(L); + script_translator_init(L); }