Skip to content

Commit

Permalink
suport Projection([ConfigList| string of table]) , proj:load([ConfigL…
Browse files Browse the repository at this point in the history
…ist| string of table])

Signed-off-by: shewer <[email protected]>
  • Loading branch information
shewer committed Sep 25, 2024
2 parents eb56e73 + fa6563c commit bd1d579
Show file tree
Hide file tree
Showing 22 changed files with 1,976 additions and 442 deletions.
10 changes: 6 additions & 4 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
set(LUA_VERSION "lua" CACHE STRING "lua version")
if(NOT EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/thirdparty/lua5.4/lua.h")
find_package(PkgConfig)
if(PkgConfig_FOUND)
foreach(pkg lua lua54 lua53 lua52 luajit lua51)
pkg_check_modules(LUA ${pkg})
foreach(pkg ${LUA_VERSION} lua54 lua53 lua52 luajit lua51)
pkg_check_modules(LUA IMPORTED_TARGET GLOBAL ${pkg})
if(LUA_FOUND)
break()
endif()
endforeach()
endif()
if(LUA_FOUND)
set(LUA_TARGET PkgConfig::LUA)
include_directories(${LUA_INCLUDE_DIRS})
else()
message(FATAL_ERROR "Lua not found, consider using `bash travis-install.sh` to download.")
message(FATAL_ERROR "Lua not found, consider using `bash action-install.sh` to download.")
endif()
else()
message(STATUS "Using in-tree lua source")
Expand Down Expand Up @@ -44,5 +46,5 @@ endif()

set(plugin_name "rime-lua" PARENT_SCOPE)
set(plugin_objs $<TARGET_OBJECTS:rime-lua-objs> PARENT_SCOPE)
set(plugin_deps ${LUA_LIBRARIES} ${rime_library} ${rime_gears_library} PARENT_SCOPE)
set(plugin_deps ${LUA_TARGET} ${rime_library} ${rime_gears_library} PARENT_SCOPE)
set(plugin_modules "lua" PARENT_SCOPE)
22 changes: 12 additions & 10 deletions sample/lua/charset.lua
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,16 @@

-- 帮助函数(可跳过)
local charset = {
["CJK"] = { first = 0x4E00, last = 0x9FFF }, -- CJK Unified Ideographs - https://unicode.org/charts/PDF/U4E00.pdf
["ExtA"] = { first = 0x3400, last = 0x4DBF }, -- CJK Unified Ideographs Extension A - https://unicode.org/charts/PDF/U3400.pdf
["ExtB"] = { first = 0x20000, last = 0x2A6DF }, -- CJK Unified Ideographs Extension B - https://unicode.org/charts/PDF/U20000.pdf
["ExtC"] = { first = 0x2A700, last = 0x2B73F }, -- CJK Unified Ideographs Extension C - https://unicode.org/charts/PDF/U2A700.pdf
["ExtD"] = { first = 0x2B740, last = 0x2B81F }, -- CJK Unified Ideographs Extension D - https://unicode.org/charts/PDF/U2B740.pdf
["ExtE"] = { first = 0x2B820, last = 0x2CEAF }, -- CJK Unified Ideographs Extension E - https://unicode.org/charts/PDF/U2B820.pdf
["ExtF"] = { first = 0x2CEB0, last = 0x2EBEF }, -- CJK Unified Ideographs Extension F - https://unicode.org/charts/PDF/U2CEB0.pdf
["ExtG"] = { first = 0x30000, last = 0x3134A }, -- CJK Unified Ideographs Extension G - https://unicode.org/charts/PDF/U30000.pdf
["Compat"] = { first = 0x2F800, last = 0x2FA1F } -- CJK Compatibility Ideographs Supplement - https://unicode.org/charts/PDF/U2F800.pdf
["CJK"] = { first = 0x4E00, last = 0x9FFF }, -- CJK Unified Ideographs - https://unicode.org/charts/PDF/U4E00.pdf
["ExtA"] = { first = 0x3400, last = 0x4DBF }, -- CJK Unified Ideographs Extension A - https://unicode.org/charts/PDF/U3400.pdf
["ExtB"] = { first = 0x20000, last = 0x2A6DF }, -- CJK Unified Ideographs Extension B - https://unicode.org/charts/PDF/U20000.pdf
["ExtC"] = { first = 0x2A700, last = 0x2B73F }, -- CJK Unified Ideographs Extension C - https://unicode.org/charts/PDF/U2A700.pdf
["ExtD"] = { first = 0x2B740, last = 0x2B81F }, -- CJK Unified Ideographs Extension D - https://unicode.org/charts/PDF/U2B740.pdf
["ExtE"] = { first = 0x2B820, last = 0x2CEAF }, -- CJK Unified Ideographs Extension E - https://unicode.org/charts/PDF/U2B820.pdf
["ExtF"] = { first = 0x2CEB0, last = 0x2EBEF }, -- CJK Unified Ideographs Extension F - https://unicode.org/charts/PDF/U2CEB0.pdf
["ExtG"] = { first = 0x30000, last = 0x3134A }, -- CJK Unified Ideographs Extension G - https://unicode.org/charts/PDF/U30000.pdf
["Compat"] = { first = 0xF900, last = 0xFAFF }, -- CJK Compatibility Ideographs - https://unicode.org/charts/PDF/UF900.pdf
["CompatSupp"] = { first = 0x2F800, last = 0x2FA1F } -- CJK Compatibility Ideographs Supplement - https://unicode.org/charts/PDF/U2F800.pdf
}

local function exists(single_filter, text)
Expand All @@ -48,7 +49,8 @@ local function is_cjk_ext(c)
return is_charset("ExtA")(c) or is_charset("ExtB")(c) or
is_charset("ExtC")(c) or is_charset("ExtD")(c) or
is_charset("ExtE")(c) or is_charset("ExtF")(c) or
is_charset("ExtG")(c) or is_charset("Compat")(c)
is_charset("ExtG")(c) or is_charset("Compat")(c) or
is_charset("CompatSupp")(c)
end

--[[
Expand Down
11 changes: 10 additions & 1 deletion sample/lua/expand_translator.lua
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,20 @@ local function memoryCallback(memory, commit)
return true
end

local function memoryCallback1(commit)
for i, dictentry in ipairs(commit:get()) do
commits:update_entry(dictentry, 1, "")
end
--commits:update(1) --
end


local function init(env)
env.mem = Memory(env.engine,env.engine.schema) -- ns= "translator"
-- env.mem = Memory(env.engine,env.engine.schema, env.name_space )
-- env.mem = Memory(env.engine,Schema("cangjie5") ) -- ns= "translator-
env.mem:memorize(function(commit) memoryCallback(env.mem, commit) end)
--env.mem:memorize(function(commit) memoryCallback(env.mem, commit) end)
env.mem:memorize(memoryCallback1)
-- or use
-- schema = Schema("cangjie5") -- schema_id
-- env.mem = Memory(env.engine, schema, "translator")
Expand Down
152 changes: 83 additions & 69 deletions sample/lua/number.lua
Original file line number Diff line number Diff line change
Expand Up @@ -3,92 +3,106 @@ number_translator: 将 `/` + 阿拉伯数字 翻译为大小写汉字
--]]

local confs = {
{
comment = " 大写",
number = { [0] = "", "", "", "", "", "", "", "", "", "" },
suffix = { [0] = "", "", "", "" },
suffix2 = { [0] = "", "", "亿", "万亿", "亿亿" }
},
{
comment = " 小写",
number = { [0] = "", "", "", "", "", "", "", "", "", "" },
suffix = { [0] = "", "", "", "" },
suffix2 = { [0] = "", "", "亿", "万亿", "亿亿" }
},
{
comment = " 大寫",
number = { [0] = "", "", "", "", "", "", "", "", "", "" },
suffix = { [0] = "", "", "", "" },
suffix2 = { [0] = "", "", "", "萬億", "億億" }
},
{
comment = " 小寫",
number = { [0] = "", "", "", "", "", "", "", "", "", "" },
suffix = { [0] = "", "", "", "" },
suffix2 = { [0] = "", "", "", "萬億", "億億" }
},
{
comment = " 大写",
numeral = { [0] = "", "", "", "", "", "", "", "", "", "" },
place = { [0] = "", "", "", "" },
group = { [0] = "", "", "亿", "万亿", "亿亿" }
},
{
comment = " 小写",
numeral = { [0] = "", "", "", "", "", "", "", "", "", "" },
place = { [0] = "", "", "", "" },
group = { [0] = "", "", "亿", "万亿", "亿亿" }
},
{
comment = " 序数",
numeral = { [0] = "", "", "", "", "", "", "", "", "", "" }
},
{
comment = " 大寫",
numeral = { [0] = "", "", "", "", "", "", "", "", "", "" },
place = { [0] = "", "", "", "" },
group = { [0] = "", "", "", "", "" }
},
{
comment = " 小寫",
numeral = { [0] = "", "", "", "", "", "", "", "", "", "" },
place = { [0] = "", "", "", "" },
group = { [0] = "", "", "", "", "" }
},
{
comment = " 序數",
numeral = { [0] = "", "", "", "", "", "", "", "", "", "" }
}
}

local function read_seg(conf, n)
local s = ""
local i = 0
local zf = true
local s = ""
local i = 0
local zf = true

while string.len(n) > 0 do
local d = tonumber(string.sub(n, -1, -1))
if d ~= 0 then
s = conf.number[d] .. conf.suffix[i] .. s
zf = false
else
if not zf then
s = conf.number[0] .. s
end
zf = true
while string.len(n) > 0 do
local d = tonumber(string.sub(n, -1, -1))
if conf.place == nil then
s = conf.numeral[d] .. s
elseif d == 1 and i == 1 and string.len(n) == 1 then
s = conf.place[i] .. s
elseif d ~= 0 then
s = conf.numeral[d] .. conf.place[i] .. s
zf = false
else
if not zf then
s = conf.numeral[0] .. s
end
i = i + 1
n = string.sub(n, 1, -2)
end
zf = true
end
i = i + 1
n = string.sub(n, 1, -2)
end

return i < 4, s
return i < 4, s
end

local function read_number(conf, n)
local s = ""
local i = 0
local zf = false
local s = ""
local i = 0
local zf = false

n = string.gsub(n, "^0+", "")
n = string.gsub(n, "^0+", "")

if n == "" then
return conf.number[0]
end
if n == "" then
return conf.numeral[0]
end

while string.len(n) > 0 do
local zf2, r = read_seg(conf, string.sub(n, -4, -1))
if r ~= "" then
if zf and s ~= "" then
s = r .. conf.suffix2[i] .. conf.number[0] .. s
else
s = r .. conf.suffix2[i] .. s
end
while string.len(n) > 0 do
local zf2, r = read_seg(conf, string.sub(n, -4, -1))
if r ~= "" then
if conf.group == nil then
s = r .. s
elseif zf and s ~= "" then
s = r .. conf.group[i] .. conf.numeral[0] .. s
else
s = r .. conf.group[i] .. s
end
zf = zf2
i = i + 1
n = string.sub(n, 1, -5)
end
return s
end
zf = zf2
i = i + 1
n = string.sub(n, 1, -5)
end
return s
end

local function translator(input, seg)
if string.sub(input, 1, 1) == "/" then
local n = string.sub(input, 2)
if tonumber(n) ~= nil then
for _, conf in ipairs(confs) do
local r = read_number(conf, n)
yield(Candidate("number", seg.start, seg._end, r, conf.comment))
end
if string.sub(input, 1, 1) == "/" then
local n = string.sub(input, 2)
if tonumber(n) ~= nil then
for _, conf in ipairs(confs) do
local r = read_number(conf, n)
yield(Candidate("number", seg.start, seg._end, r, conf.comment))
end
end
end
end
end

return translator
86 changes: 86 additions & 0 deletions sample/lua/script_translator.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
#! /usr/bin/env lua
--
-- script_translator.lua
-- Copyright (C) 2023 Shewer Lu <[email protected]>
--
-- 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)
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
env.tran.max_homophones = int
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
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
res = env.tran.max_homophones 1 number
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, "script_translator")
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)
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
Loading

0 comments on commit bd1d579

Please sign in to comment.