This repository has been archived by the owner on Feb 1, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
75 lines (67 loc) · 2.36 KB
/
main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#include <algorithm>
#include <cppjieba/Jieba.hpp>
#include <cstddef>
// WARNING: 这个头文件会导致库没法使用
// #include <lauxlib.h>
#include <string>
#include <thread>
#include <vector>
#include "lua.hpp"
extern "C" {
#include <dlfcn.h>
}
constexpr char const *const DICT_PATH = "data/jieba.dict.utf8";
constexpr char const *const HMM_PATH = "data/hmm_model.utf8";
constexpr char const *const USER_DICT_PATH = "data/user.dict.utf8";
constexpr char const *const IDF_PATH = "data/idf.utf8";
constexpr char const *const STOP_WORD_PATH = "data/stop_words.utf8";
int getPos(const std::string &, size_t, bool);
cppjieba::Jieba &getJieba() {
// INFO: 这里会有内存泄漏吗?不会,Lua 的模块是没法 unload 的
Dl_info dl_info;
dladdr((void *)getJieba, &dl_info);
std::string path(dl_info.dli_fname);
path = path.substr(0, path.find_last_of('/') + 1);
static cppjieba::Jieba jieba(path + DICT_PATH, path + HMM_PATH, path + USER_DICT_PATH, path + IDF_PATH,
path + STOP_WORD_PATH);
return jieba;
}
int getPos(const std::string &line, size_t pos, bool isRight) {
std::vector<std::string> segList;
auto & jieba = getJieba();
jieba.Cut(line, segList, true);
if(isRight) {
size_t curPos = 0;
for(auto const &item: segList) {
curPos += item.length();
if(curPos > pos) {
// 跳过空格
while(line[curPos] == ' ' && curPos < line.length()) ++curPos;
return curPos;
};
}
}
size_t curPos = line.length();
for(auto beg = segList.rbegin(); curPos >= pos && curPos; ++beg) { curPos -= beg->length(); }
// 跳过空格
while(line[curPos] == ' ' && curPos > 0) --curPos;
return curPos;
}
extern "C" int l_pos(lua_State *L) {
auto line_data = luaL_checkstring(L, 1);
std::string line(line_data);
auto pos = luaL_checkinteger(L, 2);
auto isRight = luaL_checkinteger(L, 3) != 0;
auto result = getPos(line, pos, isRight);
lua_pushinteger(L, result);
return 1;
}
static luaL_Reg jieba_move[] = { { "getPos", l_pos }, { NULL, NULL } };
extern "C" int luaopen_libjiebamove(lua_State *L) {
std::thread t([]() {
getJieba();
});
t.detach();
luaL_newlib(L, jieba_move);
return 1;
}