Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make module context-aware to be included in electron #173

Merged
merged 1 commit into from
Nov 12, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 11 additions & 11 deletions lib/index.cpp
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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