Skip to content

Commit

Permalink
⚡ Use init() once to speed up
Browse files Browse the repository at this point in the history
  • Loading branch information
Freed-Wu committed Nov 21, 2024
1 parent 6865e26 commit 763926e
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 6 deletions.
10 changes: 8 additions & 2 deletions jieba.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

#include "jieba.h"

static int _cut(lua_State *L) {
static int _init(lua_State *L) {
struct jieba_path jieba_path;
lua_getfield(L, 3, "dict_path");
jieba_path.dict_path = lua_tostring(L, -1);
Expand All @@ -14,8 +14,13 @@ static int _cut(lua_State *L) {
jieba_path.idf_path = lua_tostring(L, -1);
lua_getfield(L, 3, "stop_word_path");
jieba_path.stop_word_path = lua_tostring(L, -1);
init(jieba_path);
return 0;
}

static int _cut(lua_State *L) {
lua_newtable(L);
char **results = cut(lua_tostring(L, 1), lua_toboolean(L, 2), jieba_path);
char **results = cut(lua_tostring(L, 1), lua_toboolean(L, 2));
int i = 1;
while (*results) {
lua_pushstring(L, *results++);
Expand All @@ -25,6 +30,7 @@ static int _cut(lua_State *L) {
}

static const luaL_Reg functions[] = {
{"init", _init},
{"cut", _cut},
{NULL, NULL},
};
Expand Down
10 changes: 8 additions & 2 deletions jieba.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,24 @@

using namespace std;

extern "C" char **cut(const char *str, bool hmm, struct jieba_path jieba_path) {
static cppjieba::Jieba jieba;

extern "C" void init(struct jieba_path jieba_path) {
cppjieba::Jieba 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" char **cut(const char *str, bool hmm) {
vector<string> words;
vector<cppjieba::Word> jiebawords;
string s = str;

jieba.Cut(s, words, hmm);
char **results = new char *[DEFAULT_BUFFER_SIZE] {};
char **results = (char **)malloc(sizeof(char *) * DEFAULT_BUFFER_SIZE);
char **p = results;
for (auto word : words)
*p++ = strdup(word.c_str());
*p = NULL;
return results;
}
3 changes: 2 additions & 1 deletion jieba.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ struct jieba_path {
const char *idf_path;
const char *stop_word_path;
};
char **cut(const char *str, bool hmm, struct jieba_path);
void init(struct jieba_path);
char **cut(const char *str, bool hmm);

__END_DECLS
#endif /* jieba.h */
2 changes: 2 additions & 0 deletions lua/jieba/nvim/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ local M = {}
local jieba = require("jieba.nvim.utils")
local ut = require("jieba.utils")

require("jieba").init(jieba.jieba_path)

local str_match = string.match
local sub = ut.sub
local len = vim.api.nvim_strwidth
Expand Down
3 changes: 2 additions & 1 deletion lua/jieba/nvim/utils.lua
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,10 @@ end
---@param hmm boolean
---@return string[]
local function cut(str, hmm)
return require 'jieba'.cut(str, hmm, jieba_path)
return require 'jieba'.cut(str, hmm)
end

return {
jieba_path = jieba_path,
cut = cut
}

0 comments on commit 763926e

Please sign in to comment.