Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add TableTranslator , ScriptTranslator in Component #287

Merged
merged 16 commits into from
Feb 2, 2024
59 changes: 59 additions & 0 deletions sample/lua/ltable_tran.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
#! /usr/bin/env lua
--
-- ltable_tran.lua
-- Copyright (C) 2023 Shewer Lu <shewer@gmail.com>
--
-- Distributed under terms of the MIT license.
-- callback return bool

-- 簡單 callback ,
local function callback(table_tran, commit_entry)
if table_tran.engine.context:get_option("update_user_dict") then
return table_tran:memorize(commit_entry) -- delegate super::Memorize()
end
return false
end
--
local function callback2(tran,commits)
if table_tran.engine.context:get_option("update_user_dict") then
for i, dictentry in ipairs(commits:get()) do
if (true) then
tran:update_userdict(dictentry, 1, "") -- do update user_dict prefix_str = ""
elseif (true) then
tran:update_userdict(dictentry, -1, "") -- do delete user_dict prefix_str = ""
else
tran:update_userdict(dictentry, 0, "") -- do nothing prefix_str = ""
end
end
return true
end
return false
end



local M = {}
function M.init(env)
env.tran = Component.TableTranslator(env.engine, Schema('cangjie5'), "translator", "")
env.tran.memorize_callback = callback
env.engine.context.option_update_notifier:connect(
function (ctx,name)
if name == "completion" then
env.tran.enable_completion = ctx:get_option(name) -- completion
elseif name == "disable_userdict" then
env.tran.disable_userdict = ctx:get_option(name) -- Query 屏蔽 user_dict
end
end)
end
function M.fini(env)
end
function M.func(inp, seg, env)
local translation = env.tran:query(inp,seg)
if translatation then
for cand in translation:iter() do
yield(cand)
end
end
end

return M
83 changes: 83 additions & 0 deletions sample/lua/script_translator.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
#! /usr/bin/env lua
--
-- script_translator.lua
-- Copyright (C) 2023 Shewer Lu <shewer@gmail.com>
--
-- Distributed under terms of the MIT license.
--

--[[

''' custom.yaml'
patch:
engine/translators/+:
- lua_translator@*table_translator@translator
- lua_translator@*script_translator@cangjie
````
------- methods return
env.tran:start_session false function()
env.tran:finish_session false function()
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)

------- vars_set
env.tran.spelling_hints = int >0
env.tran.initial_quality = double
env.tran.contextual_suggestions = boolean
env.tran.enable_completion = boolean
env.tran.always_show_comments = boolean
env.tran.strict_spelling = boolean
env.tran.max_homophones = int
env.tran.memorize_callback = function(self, commit_entry)
env.tran.enable_correction = boolean
env.tran.tag = string
env.tran.delimiters = string

------- vars_get
res = env.tran.spelling_hints 0 number
res = env.tran.initial_quality 0.0 number
res = env.tran.contextual_suggestions false boolean
res = env.tran.enable_completion true boolean
res = env.tran.always_show_comments false boolean
res = env.tran.strict_spelling false boolean
res = env.tran.max_homophones 1 number
res = env.tran.memorize_callback function: 0x557a0c15ef40 function
res = env.tran.enable_correction false boolean
res = env.tran.tag abc string
res = env.tran.delimiters ' string

--]]
local M={}
local function simple_callback(self, commits)
local context = self.engine.context
if true then
return self:memorize(commits)
end
end
local function callback(self, commits) -- self : env.tran commits : list
local context = self.engine.context
for i, entry in ipairs(commits:get()) do
self:update_entry(entry,0,"") -- do nothing to userdict
-- self:update_entry(entry,1,"") -- update entry to userdict
-- self:update_entry(entry,-1,"") -- delete entry to userdict
end
end
function M.init(env)
env.tran = Component.ScriptTranslator(env.engine, env.name_space, "table")
env.tran.memorize_callback = simple_callback
end

function M.fini(env)
end

function M.func(inp, seg, env)
local t = env.tran:query(inp,seg)
if not t then return end
for cand in t:iter() do
yield(cand)
end
end

return M
85 changes: 85 additions & 0 deletions sample/lua/table_translator.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
#! /usr/bin/env lua
--
-- table_translator.lua
-- Copyright (C) 2023 Shewer Lu <shewer@gmail.com>
--
-- Distributed under terms of the MIT license.
--
--[[
--
''' custom.yaml'
patch:
engine/translators/+:
- lua_translator@*table_translator@translator
- lua_translator@*script_translator@cangjie
````

------- methods
env.tran:finish_session bool function()
env.tran:start_session bool function()
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)

------- 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.enable_completion = false boolean
env.tran.delimiters = false string
env.tran.strict_spelling = boolean
env.tran.contextual_suggestions = boolean
env.tran.initial_quality = double
env.tran.always_show_comments = boolean
env.tran.tag = string

------- vars_get 取值
res = env.tran.max_homophones 1 number
res = env.tran.spelling_hints 0 number
res = env.tran.enable_correction false boolean
res = env.tran.memorize_callback function: 0x562c732947e0 function
res = env.tran.enable_completion true boolean
res = env.tran.delimiters ' string
res = env.tran.strict_spelling false boolean
res = env.tran.contextual_suggestions false boolean
res = env.tran.initial_quality 0.0 number
res = env.tran.always_show_comments false boolean
res = env.tran.tag cangjie string


--]]

local M={}
local function simple_callback(self, commits)
local context = self.engine.context
if true then
return self:memorize(commits)
end
end
local function callback(self, commits) -- self : env.tran commits : list
local context = self.engine.context
for i, entry in ipairs(commits:get()) do
self:update_entry(entry, 0,"") -- do nothing to userdict
-- self:update_entry(entry,1,"") -- update entry to userdict
-- self:update_entry(entry,-1,"") -- delete entry to userdict
end
end
function M.init(env)
env.tran = Component.TableTranslator(env.engine, env.name_space, "table")
env.tran.memorize_callback = simple_callback
end

function M.fini(env)
end

function M.func(inp, seg, env)
local t = env.tran:query(inp,seg)
if not t then return end
for cand in t:iter() do
yield(cand)
end
end

return M
6 changes: 6 additions & 0 deletions sample/luna_pinyin.custom.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
patch:
engine/translators/+:
- lua_translator@*table_translator@translator
- lua_translator@*script_translator@cangjie


7 changes: 6 additions & 1 deletion src/lib/lua.cc
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,12 @@ LuaObj::LuaObj(lua_State *L, int i) : L_(L) {
lua_pushvalue(L, i);
id_ = luaL_ref(L, LUA_REGISTRYINDEX);
}

int LuaObj::type() {
lua_rawgeti(L_, LUA_REGISTRYINDEX, id_);
int type = lua_type(L_, -1);
lua_pop(L_, 1);
return type;
}
LuaObj::~LuaObj() {
luaL_unref(L_, LUA_REGISTRYINDEX, id_);
}
Expand Down
1 change: 1 addition & 0 deletions src/lib/lua.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ class LuaObj {

static void pushdata(lua_State *L, std::shared_ptr<LuaObj> &o);
static std::shared_ptr<LuaObj> todata(lua_State *L, int i);
int type();

private:
LuaObj(lua_State *L, int i);
Expand Down
28 changes: 22 additions & 6 deletions src/lib/luatype_boost_optional.h
shewer marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -1,23 +1,39 @@
#ifndef LUATYPE_BOOST_OPTIONAL_H
#define LUATYPE_BOOST_OPTIONAL_H

#include "lua_templates.h"
#if __cplusplug >= 201703L || _MSVC_VER >=201703L
#include <optional>
#else
#include <boost/optional.hpp>
#endif // C++17
//
#include "lua_templates.h"



namespace ns {
#if __cplusplug >= 201703L || _MSVC_VER >=201703L
using std::optional;
#else
using boost::optional;
#endif// C++17

}// namespace

template<typename T>
struct LuaType<boost::optional<T>> {
static void pushdata(lua_State *L, boost::optional<T> o) {
struct LuaType<ns::optional<T>> {
static void pushdata(lua_State *L, ns::optional<T> o) {
if (o)
LuaType<T>::pushdata(L, *o);
else
lua_pushnil(L);
}

static boost::optional<T> &todata(lua_State *L, int i, C_State *C) {
static ns::optional<T> &todata(lua_State *L, int i, C_State *C) {
if (lua_type(L, i) == LUA_TNIL)
return C->alloc<boost::optional<T>>();
return C->alloc<ns::optional<T>>();
else
return C->alloc<boost::optional<T>>(LuaType<T>::todata(L, i, C));
return C->alloc<ns::optional<T>>(LuaType<T>::todata(L, i, C));
}
};

Expand Down
Loading