Skip to content

Commit

Permalink
🏗️ Use LuaNativeObjects
Browse files Browse the repository at this point in the history
  • Loading branch information
Freed-Wu committed Dec 8, 2024
1 parent ad01525 commit d8ad68c
Show file tree
Hide file tree
Showing 8 changed files with 93 additions and 126 deletions.
55 changes: 0 additions & 55 deletions jieba.c

This file was deleted.

24 changes: 11 additions & 13 deletions jieba.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,21 @@

#define DEFAULT_BUFFER_SIZE 1024

using namespace std;
using cppjieba::Jieba, cppjieba::Word, std::string, std::vector;

static cppjieba::Jieba *jieba;

extern "C" void init(struct jieba_path jieba_path) {
jieba = new cppjieba::Jieba{jieba_path.dict_path, jieba_path.model_path,
jieba_path.user_dict_path, jieba_path.idf_path,
jieba_path.stop_word_path};
extern "C" jieba jieba_new(char *dict_path, char *model_path,
char *user_dict_path, char *idf_path,
char *stop_word_path) {
return reinterpret_cast<jieba>(new Jieba{
dict_path, model_path, user_dict_path, idf_path, stop_word_path});
}

extern "C" char **cut(const char *str, bool hmm) {
extern "C" char **jieba_cut(jieba jieba, const char *str, bool hmm) {
vector<string> words;
vector<cppjieba::Word> jiebawords;
vector<Word> jiebawords;
string s = str;

(*jieba).Cut(s, words, hmm);
reinterpret_cast<Jieba *>(jieba)->Cut(s, words, hmm);
char **results = (char **)malloc(sizeof(char *) * DEFAULT_BUFFER_SIZE);
char **p = results;
for (auto word : words)
Expand All @@ -27,7 +26,6 @@ extern "C" char **cut(const char *str, bool hmm) {
return results;
}

extern "C" void deinit() {
delete jieba;
jieba = nullptr;
extern "C" void jieba_delete(jieba jieba) {
delete reinterpret_cast<Jieba *>(jieba);
}
15 changes: 5 additions & 10 deletions jieba.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,11 @@
#include <sys/cdefs.h>
__BEGIN_DECLS

struct jieba_path {
const char *dict_path;
const char *model_path;
const char *user_dict_path;
const char *idf_path;
const char *stop_word_path;
};
void init(struct jieba_path);
char **cut(const char *str, bool hmm);
void deinit();
typedef void *jieba;
void *jieba_new(char *dict_path, char *model_path, char *user_dict_path,
char *idf_path, char *stop_word_path);
char **jieba_cut(jieba jieba, const char *str, bool hmm);
void jieba_delete(jieba jieba);

__END_DECLS
#endif /* jieba.h */
21 changes: 21 additions & 0 deletions jieba.nobj.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
-- luacheck: ignore 113
---@diagnostic disable: undefined-global
c_module "jieba" {
use_globals = true,
include "jieba.h",
object "jieba" {
constructor {
c_call "void *" "jieba_new" { "char *", "dict_path",
"char *", "model_path", "char *", "user_dict_path",
"char *", "idf_path", "char *", "stop_word_path" },
},
destructor "delete" {
c_method_call "void" "jieba_delete" {}
},
method "cut" {
c_method_call "char **" "jieba_cut" {
"const char *", "str", "bool", "hmm"
}
}
},
}
17 changes: 17 additions & 0 deletions lua/jieba/nvim/config.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
-- luacheck: ignore 112 113
---@diagnostic disable: undefined-global
local dict_dir = vim.fs.joinpath(
vim.fs.dirname(vim.fs.dirname(debug.getinfo(1).source:match("@?(.*)"))),
"dict"
)

return {
hmm = true,
paths = {
vim.fs.joinpath(dict_dir, "jieba.dict.utf8"),
vim.fs.joinpath(dict_dir, "hmm_model.utf8"),
vim.fn.has("win32") == 0 and "/dev/null" or "nul",
vim.fs.joinpath(dict_dir, "idf.utf8"),
vim.fs.joinpath(dict_dir, "stop_words.utf8"),
}
}
20 changes: 8 additions & 12 deletions lua/jieba/nvim/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,10 @@
-- SOFTWARE.

local M = {}
local jieba = require("jieba.nvim.utils")
local config = require("jieba.nvim.config")
local ut = require("jieba.utils")

if not jieba.is_init then
require("jieba").init(jieba.jieba_path)
jieba.is_init = true
end

local jieba = require("jieba").jieba(unpack(config.paths))
local str_match = string.match
local sub = ut.sub
local len = vim.api.nvim_strwidth
Expand Down Expand Up @@ -412,7 +408,7 @@ local function navigate(primary_index_func, secondary_index_func, backward, buff
-- unwrap the row and col from the cursor position
local row, col = cursor_pos[1], cursor_pos[2]
if row == sentinel_row then
pt = parse_tokens(jieba.cut(buffer[row], jieba.hmm))
pt = parse_tokens(jieba.cut(buffer[row], config.hmm))
pt = stack_merge(pt)
col = primary_index_func(pt, col)

Expand All @@ -435,23 +431,23 @@ local function navigate(primary_index_func, secondary_index_func, backward, buff
return { row, col }
end
-- similar steps for when row is not the sentinel_row
pt = parse_tokens(jieba.cut(buffer[row], jieba.hmm))
pt = parse_tokens(jieba.cut(buffer[row], config.hmm))
pt = stack_merge(pt)
col = primary_index_func(pt, col)
if col ~= nil then
return { row, col }
end
row = row + row_step
while row ~= sentinel_row do
pt = parse_tokens(jieba.cut(buffer[row], jieba.hmm))
pt = parse_tokens(jieba.cut(buffer[row], config.hmm))
pt = stack_merge(pt)
col = secondary_index_func(pt)
if col ~= nil then
return { row, col }
end
row = row + row_step
end
pt = parse_tokens(jieba.cut(buffer[row], jieba.hmm))
pt = parse_tokens(jieba.cut(buffer[row], config.hmm))
pt = stack_merge(pt)
col = secondary_index_func(pt)
if col == nil then
Expand Down Expand Up @@ -549,7 +545,7 @@ end
M.select_w = function()
local cursor_pos = vim.api.nvim_win_get_cursor(0)
local current_line = Lines[cursor_pos[1]]
local line = parse_tokens(jieba.cut(current_line, jieba.hmm))
local line = parse_tokens(jieba.cut(current_line, config.hmm))
print(line)
line = stack_merge(line)
local _, start, row = index_tokens(line, cursor_pos[2])
Expand All @@ -573,7 +569,7 @@ end

-- TODO: 高亮当前光标下的词
-- local function hightlight_under_curosr()
-- local line = parse_tokens(jieba.cut(vim.api.nvim_get_current_line(), jieba.hmm))
-- local line = parse_tokens(jieba.cut(vim.api.nvim_get_current_line(), config.hmm))
-- line = stack_merge(line)
-- local cursor_pos = vim.api.nvim_win_get_cursor(0)
-- local _, start, row = index_tokens(line, cursor_pos[2] + 1)
Expand Down
32 changes: 0 additions & 32 deletions lua/jieba/nvim/utils.lua

This file was deleted.

35 changes: 31 additions & 4 deletions xmake.lua
Original file line number Diff line number Diff line change
@@ -1,12 +1,39 @@
-- luacheck: ignore 111 113
-- luacheck: ignore 111 113 143
---@diagnostic disable: undefined-global
---@diagnostic disable: undefined-field
add_rules("mode.debug", "mode.release")

rule("lua-native-object")
do
set_extensions(".nobj.lua")
before_buildcmd_file(function(target, batchcmds, sourcefile, opt)
-- get c source file for lua-native-object
local dirname = path.join(target:autogendir(), "rules", "lua-native-object")
local sourcefile_c = path.join(dirname, path.basename(sourcefile) .. ".c")

-- add objectfile
local objectfile = target:objectfile(sourcefile_c)
table.insert(target:objectfiles(), objectfile)

-- add commands
batchcmds:show_progress(opt.progress, "${color.build.object}compiling.nobj.lua %s", sourcefile)
batchcmds:mkdir(path.directory(sourcefile_c))
batchcmds:vrunv("native_objects.lua",
{ "-outpath", dirname, "-gen", "lua", path(sourcefile) })
batchcmds:compile(sourcefile_c, objectfile)

-- add deps
batchcmds:add_depfiles(sourcefile)
batchcmds:set_depmtime(os.mtime(objectfile))
batchcmds:set_depcache(target:dependfile(objectfile))
end)
end

target("jieba")
do
add_rules("luarocks.module")
add_files("*.c", "*.cpp")
add_includedirs("cppjieba/include", "cppjieba/deps/limonp/include")
add_includedirs(".", "cppjieba/include", "cppjieba/deps/limonp/include")
add_rules("luarocks.module", "lua-native-object")
add_files("*.cpp", "*.nobj.lua")
add_links("stdc++")
before_build(
-- luacheck: ignore 212/target
Expand Down

0 comments on commit d8ad68c

Please sign in to comment.