Skip to content

Commit

Permalink
Merge pull request #173 from McShelby/McShelby/issue172
Browse files Browse the repository at this point in the history
Make module context-aware to be included in electron
  • Loading branch information
yanyiwu committed Nov 12, 2020
2 parents 425f1c8 + 348fe83 commit b0cf403
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 62 deletions.
22 changes: 11 additions & 11 deletions lib/index.cpp
@@ -1,24 +1,24 @@
#include "nodejieba.h"

void init(Local<Object> exports) {
Nan::Set(exports,Nan::New<v8::String>("load").ToLocalChecked(),
NAN_MODULE_INIT(init) {
Nan::Set(target,Nan::New<v8::String>("load").ToLocalChecked(),
Nan::GetFunction(Nan::New<FunctionTemplate>(load)).ToLocalChecked());
Nan::Set(exports,Nan::New<v8::String>("cut").ToLocalChecked(),
Nan::Set(target,Nan::New<v8::String>("cut").ToLocalChecked(),
Nan::GetFunction(Nan::New<FunctionTemplate>(cut)).ToLocalChecked());
Nan::Set(exports,Nan::New<v8::String>("cutAll").ToLocalChecked(),
Nan::Set(target,Nan::New<v8::String>("cutAll").ToLocalChecked(),
Nan::GetFunction(Nan::New<FunctionTemplate>(cutAll)).ToLocalChecked());
Nan::Set(exports,Nan::New<v8::String>("cutHMM").ToLocalChecked(),
Nan::Set(target,Nan::New<v8::String>("cutHMM").ToLocalChecked(),
Nan::GetFunction(Nan::New<FunctionTemplate>(cutHMM)).ToLocalChecked());
Nan::Set(exports,Nan::New<v8::String>("cutForSearch").ToLocalChecked(),
Nan::Set(target,Nan::New<v8::String>("cutForSearch").ToLocalChecked(),
Nan::GetFunction(Nan::New<FunctionTemplate>(cutForSearch)).ToLocalChecked());
Nan::Set(exports,Nan::New<v8::String>("cutSmall").ToLocalChecked(),
Nan::Set(target,Nan::New<v8::String>("cutSmall").ToLocalChecked(),
Nan::GetFunction(Nan::New<FunctionTemplate>(cutSmall)).ToLocalChecked());
Nan::Set(exports,Nan::New<v8::String>("tag").ToLocalChecked(),
Nan::Set(target,Nan::New<v8::String>("tag").ToLocalChecked(),
Nan::GetFunction(Nan::New<FunctionTemplate>(tag)).ToLocalChecked());
Nan::Set(exports,Nan::New<v8::String>("extract").ToLocalChecked(),
Nan::Set(target,Nan::New<v8::String>("extract").ToLocalChecked(),
Nan::GetFunction(Nan::New<FunctionTemplate>(extract)).ToLocalChecked());
Nan::Set(exports,Nan::New<v8::String>("insertWord").ToLocalChecked(),
Nan::Set(target,Nan::New<v8::String>("insertWord").ToLocalChecked(),
Nan::GetFunction(Nan::New<FunctionTemplate>(insertWord)).ToLocalChecked());
}

NODE_MODULE(nodejieba, init)
NAN_MODULE_WORKER_ENABLED(NODE_GYP_MODULE_NAME, init)
82 changes: 33 additions & 49 deletions lib/nodejieba.cpp
Expand Up @@ -3,8 +3,6 @@
#include "cppjieba/Jieba.hpp"
#include "cppjieba/KeywordExtractor.hpp"

cppjieba::Jieba* global_jieba_handle;

NAN_METHOD(load) {
if(info.Length() != 5) {
info.GetReturnValue().Set(Nan::New<v8::Boolean>(false));
Expand All @@ -17,12 +15,15 @@ NAN_METHOD(load) {
Nan::Utf8String idfPath(Nan::To<v8::String>((info[3])).ToLocalChecked());
Nan::Utf8String stopWordsPath(Nan::To<v8::String>((info[4])).ToLocalChecked());

delete global_jieba_handle;
global_jieba_handle = new cppjieba::Jieba(*dictPath,
*modelPath,
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));
}
Expand All @@ -41,49 +42,18 @@ NAN_METHOD(insertWord) {
tag = *(Nan::Utf8String(Nan::To<v8::String>((info[1])).ToLocalChecked()));
}

assert(global_jieba_handle);
v8::Isolate *isolate = v8::Isolate::GetCurrent();
cppjieba::Jieba* _jieba_handle = Nan::GetIsolateData<cppjieba::Jieba>(isolate);
assert(_jieba_handle);

if(!global_jieba_handle->InsertUserWord(word, tag)) {
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;
//
// assert(global_jieba_handle);
// if (info.Length() >= 2) {
// string method = *(String::Utf8Value(info[1]->ToString()));
// if ("MP" == method) {
// global_jieba_handle->Cut(sentence, words, false);
// if (info.Length() == 3) {
// global_jieba_handle->CutSmall(sentence, words, info[2]->Int32Value());
// }
// } else if ("HMM" == method) {
// global_jieba_handle->CutHMM(sentence, words);
// } else if ("MIX" == method) {
// global_jieba_handle->Cut(sentence, words, true);
// } else {
// global_jieba_handle->Cut(sentence, words, true);
// }
// } else {
// global_jieba_handle->Cut(sentence, words, true);
// }
//
// Local<Array> outArray;
// WrapVector(words, outArray);
//
// info.GetReturnValue().Set(outArray);
//}

NAN_METHOD(cut) {
if (info.Length() == 0) {
info.GetReturnValue().Set(Nan::New<v8::Boolean>(false));
Expand All @@ -95,7 +65,9 @@ NAN_METHOD(cut) {
if (info.Length() > 1) {
useHMM = *Nan::To<v8::Boolean>((info[1])).ToLocalChecked();
}
global_jieba_handle->Cut(sentence, words, useHMM);
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);
Expand All @@ -108,7 +80,9 @@ NAN_METHOD(cutHMM) {
}
string sentence = *(Nan::Utf8String(Nan::To<v8::String>((info[0])).ToLocalChecked()));
vector<string> words;
global_jieba_handle->CutHMM(sentence, 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);
Expand All @@ -121,7 +95,9 @@ NAN_METHOD(cutAll) {
}
string sentence = *(Nan::Utf8String(Nan::To<v8::String>((info[0])).ToLocalChecked()));
vector<string> words;
global_jieba_handle->CutAll(sentence, 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);
Expand All @@ -138,7 +114,9 @@ NAN_METHOD(cutForSearch) {
if (info.Length() > 1) {
useHMM = *Nan::To<v8::Boolean>((info[1])).ToLocalChecked();
}
global_jieba_handle->CutForSearch(sentence, words, useHMM);
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);
Expand All @@ -152,7 +130,9 @@ NAN_METHOD(cutSmall) {
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();
global_jieba_handle->CutSmall(sentence, words, word_len_limit);
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);
Expand All @@ -166,8 +146,10 @@ NAN_METHOD(tag) {

vector<pair<string, string> > words;
string sentence = *(Nan::Utf8String(Nan::To<v8::String>((info[0])).ToLocalChecked()));
assert(global_jieba_handle);
global_jieba_handle->Tag(sentence, words);
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);
Expand All @@ -185,8 +167,10 @@ NAN_METHOD(extract) {
size_t topN = Nan::To<int32_t>((info[1])).FromJust();
vector<pair<string, double> > words;

assert(global_jieba_handle);
global_jieba_handle->extractor.Extract(sentence, words, topN);
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);
Expand Down
5 changes: 3 additions & 2 deletions package.json
Expand Up @@ -9,7 +9,7 @@
"main": "./index.js",
"typings": "types/index.d.ts",
"engines": {
"node": ">= 0.10.0",
"node": ">= 9.3.0",
"iojs": ">= 1.0.0"
},
"repository": {
Expand Down Expand Up @@ -38,7 +38,8 @@
},
"scripts": {
"test": "node test/demo.js && node test/load_dict_demo.js && mocha --timeout 10s -R spec test/test.js && mocha --timeout 10s -R spec test/load_dict_test.js",
"install": "node-pre-gyp install --fallback-to-build"
"install": "node-pre-gyp install --fallback-to-build",
"rebuild": "node-pre-gyp rebuild"
},
"binary": {
"module_name": "nodejieba",
Expand Down

0 comments on commit b0cf403

Please sign in to comment.