Skip to content

Commit

Permalink
加入 LScriptTranslator
Browse files Browse the repository at this point in the history
Signed-off-by: shewer <[email protected]>
  • Loading branch information
shewer committed Oct 25, 2023
1 parent 6451c9d commit dba5d1b
Show file tree
Hide file tree
Showing 2 changed files with 186 additions and 32 deletions.
178 changes: 178 additions & 0 deletions src/script_translator.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,178 @@
/*
* table_translator.cc
* Copyright (C) 2023 Shewer Lu <[email protected]>
*
* Distributed under terms of the MIT license.
*/

#include <rime/gear/script_translator.h>
#include <rime/algo/syllabifier.h>
#include <rime/dict/corrector.h>
#include <rime/gear/poet.h>

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

#include <boost/regex.hpp>

#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<LuaObj>);

// 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<LuaObj> 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<bool, an<LuaObj>, 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<LuaObj> 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<LuaObj> 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<LScriptTranslator>);
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);
}
40 changes: 8 additions & 32 deletions src/types_ext.cc
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,17 @@
#include <rime/dict/reverse_lookup_dictionary.h>
#include <rime/dict/user_db.h>
#include "table_translator.h"
#include "script_translator.h"

#include "lib/lua_export_type.h"
#include "lib/luatype_boost_optional.h"

#include "translator.h"
#include <utility>
using namespace rime;

namespace {

using ns::optional;

template<typename> using void_t = void;

template<typename T, typename = void>
Expand Down Expand Up @@ -312,7 +314,6 @@ namespace UserDbReg{
}

namespace ComponentReg{
using LT= LTableTranslator;
using P = Processor;
using S = Segmentor;
using T = Translator;
Expand Down Expand Up @@ -344,40 +345,12 @@ namespace ComponentReg{
}
};

template <typename O>
int raw_make(lua_State *L){
int n = lua_gettop(L);
if (3 > n || 4 < n)
return 0;

C_State C;
Ticket ticket(
LuaType<Engine *>::todata(L, 1),
LuaType<string>::todata(L, -2, &C),
LuaType<string>::todata(L, -1, &C)
);
DLOG(INFO) << "check Ticket:" << ticket.klass << "@" <<ticket.name_space ;
if ( n == 4 )
ticket.schema = &(LuaType<Schema &>::todata(L, 2) ); //overwrite schema
Lua* lua= Lua::from_state(L);
//an<O> obj = New<O>(Lua::from_state(L), ticket);
an<O> obj = New<O>(ticket, lua);
if (obj) {
LuaType<an<O>>::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<P>},
{"Segmentor" , raw_create<S>},
{"Translator", raw_create<T>},
{"Filter", raw_create<F>},
{"TableTranslator", raw_make<LT>},
{ NULL, NULL },
};

Expand All @@ -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);
Expand All @@ -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);
}

0 comments on commit dba5d1b

Please sign in to comment.