-
-
Notifications
You must be signed in to change notification settings - Fork 274
/
nodejieba.cpp
179 lines (153 loc) · 5.81 KB
/
nodejieba.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
#include "nodejieba.h"
#include "cppjieba/Jieba.hpp"
#include "cppjieba/KeywordExtractor.hpp"
NAN_METHOD(load) {
if(info.Length() != 5) {
info.GetReturnValue().Set(Nan::New<v8::Boolean>(false));
return;
}
Nan::Utf8String dictPath(Nan::To<v8::String>((info[0])).ToLocalChecked());
Nan::Utf8String modelPath(Nan::To<v8::String>((info[1])).ToLocalChecked());
Nan::Utf8String userDictPath(Nan::To<v8::String>((info[2])).ToLocalChecked());
Nan::Utf8String idfPath(Nan::To<v8::String>((info[3])).ToLocalChecked());
Nan::Utf8String stopWordsPath(Nan::To<v8::String>((info[4])).ToLocalChecked());
v8::Isolate *isolate = v8::Isolate::GetCurrent();
cppjieba::Jieba* _jieba_handle = Nan::GetIsolateData<cppjieba::Jieba>(isolate);
delete _jieba_handle;
_jieba_handle = new cppjieba::Jieba(*dictPath,
*modelPath,
*userDictPath,
*idfPath,
*stopWordsPath);
Nan::SetIsolateData<cppjieba::Jieba>(isolate, _jieba_handle);
info.GetReturnValue().Set(Nan::New<v8::Boolean>(true));
}
NAN_METHOD(insertWord) {
if(info.Length() < 1) {
info.GetReturnValue().Set(Nan::New<v8::Boolean>(false));
return;
}
string word = *(Nan::Utf8String(Nan::To<v8::String>((info[0])).ToLocalChecked()));
string tag = "x";
if(info.Length() > 1) {
tag = *(Nan::Utf8String(Nan::To<v8::String>((info[1])).ToLocalChecked()));
}
v8::Isolate *isolate = v8::Isolate::GetCurrent();
cppjieba::Jieba* _jieba_handle = Nan::GetIsolateData<cppjieba::Jieba>(isolate);
assert(_jieba_handle);
if(!_jieba_handle->InsertUserWord(word, tag)) {
info.GetReturnValue().Set(Nan::New<v8::Boolean>(false));
return;
}
info.GetReturnValue().Set(Nan::New<v8::Boolean>(true));
}
NAN_METHOD(cut) {
if (info.Length() == 0) {
info.GetReturnValue().Set(Nan::New<v8::Boolean>(false));
return;
}
string sentence = *(Nan::Utf8String(Nan::To<v8::String>((info[0])).ToLocalChecked()));
vector<string> words;
bool useHMM = false;
if (info.Length() > 1) {
useHMM = *Nan::To<v8::Boolean>((info[1])).ToLocalChecked();
}
v8::Isolate *isolate = v8::Isolate::GetCurrent();
cppjieba::Jieba* _jieba_handle = Nan::GetIsolateData<cppjieba::Jieba>(isolate);
_jieba_handle->Cut(sentence, words, useHMM);
Local<Array> outArray;
WrapVector(words, outArray);
info.GetReturnValue().Set(outArray);
}
NAN_METHOD(cutHMM) {
if (info.Length() == 0) {
info.GetReturnValue().Set(Nan::New<v8::Boolean>(false));
return;
}
string sentence = *(Nan::Utf8String(Nan::To<v8::String>((info[0])).ToLocalChecked()));
vector<string> words;
v8::Isolate *isolate = v8::Isolate::GetCurrent();
cppjieba::Jieba* _jieba_handle = Nan::GetIsolateData<cppjieba::Jieba>(isolate);
_jieba_handle->CutHMM(sentence, words);
Local<Array> outArray;
WrapVector(words, outArray);
info.GetReturnValue().Set(outArray);
}
NAN_METHOD(cutAll) {
if (info.Length() == 0) {
info.GetReturnValue().Set(Nan::New<v8::Boolean>(false));
return;
}
string sentence = *(Nan::Utf8String(Nan::To<v8::String>((info[0])).ToLocalChecked()));
vector<string> words;
v8::Isolate *isolate = v8::Isolate::GetCurrent();
cppjieba::Jieba* _jieba_handle = Nan::GetIsolateData<cppjieba::Jieba>(isolate);
_jieba_handle->CutAll(sentence, words);
Local<Array> outArray;
WrapVector(words, outArray);
info.GetReturnValue().Set(outArray);
}
NAN_METHOD(cutForSearch) {
if (info.Length() == 0) {
info.GetReturnValue().Set(Nan::New<v8::Boolean>(false));
return;
}
string sentence = *(Nan::Utf8String(Nan::To<v8::String>((info[0])).ToLocalChecked()));
vector<string> words;
bool useHMM = false;
if (info.Length() > 1) {
useHMM = *Nan::To<v8::Boolean>((info[1])).ToLocalChecked();
}
v8::Isolate *isolate = v8::Isolate::GetCurrent();
cppjieba::Jieba* _jieba_handle = Nan::GetIsolateData<cppjieba::Jieba>(isolate);
_jieba_handle->CutForSearch(sentence, words, useHMM);
Local<Array> outArray;
WrapVector(words, outArray);
info.GetReturnValue().Set(outArray);
}
NAN_METHOD(cutSmall) {
if (info.Length() < 2) {
info.GetReturnValue().Set(Nan::New<v8::Boolean>(false));
return;
}
string sentence = *(Nan::Utf8String(Nan::To<v8::String>((info[0])).ToLocalChecked()));
vector<string> words;
size_t word_len_limit = Nan::To<int32_t>((info[1])).FromJust();
v8::Isolate *isolate = v8::Isolate::GetCurrent();
cppjieba::Jieba* _jieba_handle = Nan::GetIsolateData<cppjieba::Jieba>(isolate);
_jieba_handle->CutSmall(sentence, words, word_len_limit);
Local<Array> outArray;
WrapVector(words, outArray);
info.GetReturnValue().Set(outArray);
}
NAN_METHOD(tag) {
if (info.Length() == 0) {
info.GetReturnValue().Set(Nan::New<v8::Boolean>(false));
return;
}
vector<pair<string, string> > words;
string sentence = *(Nan::Utf8String(Nan::To<v8::String>((info[0])).ToLocalChecked()));
v8::Isolate *isolate = v8::Isolate::GetCurrent();
cppjieba::Jieba* _jieba_handle = Nan::GetIsolateData<cppjieba::Jieba>(isolate);
assert(_jieba_handle);
_jieba_handle->Tag(sentence, words);
Local<Array> outArray;
WrapPairVector(words, outArray);
info.GetReturnValue().Set(outArray);
}
NAN_METHOD(extract) {
if (info.Length() != 2) {
info.GetReturnValue().Set(Nan::New<v8::Boolean>(false));
return;
}
string sentence = *(Nan::Utf8String(Nan::To<v8::String>((info[0])).ToLocalChecked()));
size_t topN = Nan::To<int32_t>((info[1])).FromJust();
vector<pair<string, double> > words;
v8::Isolate *isolate = v8::Isolate::GetCurrent();
cppjieba::Jieba* _jieba_handle = Nan::GetIsolateData<cppjieba::Jieba>(isolate);
assert(_jieba_handle);
_jieba_handle->extractor.Extract(sentence, words, topN);
Local<Array> outArray;
WrapPairVector(words, outArray);
info.GetReturnValue().Set(outArray);
}