#include "nodejieba.h" #include "utils.h" #include "cppjieba/Jieba.hpp" #include "cppjieba/KeywordExtractor.hpp" #include "cppjieba/TextRankExtractor.hpp" NodeJieba::NodeJieba(Napi::Env env, Napi::Object exports) { DefineAddon(exports, { InstanceMethod("load", &NodeJieba::load), InstanceMethod("cut", &NodeJieba::cut), InstanceMethod("cutAll", &NodeJieba::cutAll), InstanceMethod("cutHMM", &NodeJieba::cutHMM), InstanceMethod("cutForSearch", &NodeJieba::cutForSearch), InstanceMethod("cutSmall", &NodeJieba::cutSmall), InstanceMethod("tag", &NodeJieba::tag), InstanceMethod("extract", &NodeJieba::extract), InstanceMethod("textRankExtract", &NodeJieba::textRankExtract), InstanceMethod("insertWord", &NodeJieba::insertWord) }); } Napi::Value NodeJieba::load(const Napi::CallbackInfo& info) { if (info.Length() != 5) { return Napi::Boolean::New(info.Env(), false); } std::string dictPath = info[0].As(); std::string modelPath = info[1].As(); std::string userDictPath = info[2].As(); std::string idfPath = info[3].As(); std::string stopWordsPath = info[4].As(); delete _jieba_handle; _jieba_handle = new cppjieba::Jieba(dictPath, modelPath, userDictPath, idfPath, stopWordsPath); delete _text_rank_extractor_handle; _text_rank_extractor_handle = new cppjieba ::TextRankExtractor(dictPath, modelPath, stopWordsPath, userDictPath); return Napi::Boolean::New(info.Env(), true); } Napi::Value NodeJieba::insertWord(const Napi::CallbackInfo& info) { if(info.Length() < 1) { return Napi::Boolean::New(info.Env(), false); } std::string word = info[0].As(); std::string tag = "x"; if(info.Length() > 1) { tag = info[1].As(); } if( !_jieba_handle ){ Napi::Error::New(info.Env(), "Before calling any other function you have to call load() first").ThrowAsJavaScriptException(); } if(!_jieba_handle->InsertUserWord(word, tag)) { return Napi::Boolean::New(info.Env(), false); } return Napi::Boolean::New(info.Env(), true); } Napi::Value NodeJieba::cut(const Napi::CallbackInfo& info) { if (info.Length() == 0) { return Napi::Boolean::New(info.Env(), false); } std::string sentence = info[0].As(); bool useHMM = false; if (info.Length() > 1) { useHMM = info[1].As(); } if( !_jieba_handle ){ Napi::Error::New(info.Env(), "Before calling any other function you have to call load() first").ThrowAsJavaScriptException(); } std::vector words; _jieba_handle->Cut(sentence, words, useHMM); Napi::Array outArray; WrapVector(info.Env(), words, outArray); return outArray; } Napi::Value NodeJieba::cutHMM(const Napi::CallbackInfo& info) { if (info.Length() == 0) { return Napi::Boolean::New(info.Env(), false); } std::string sentence = info[0].As(); if( !_jieba_handle ){ Napi::Error::New(info.Env(), "Before calling any other function you have to call load() first").ThrowAsJavaScriptException(); } std::vector words; _jieba_handle->CutHMM(sentence, words); Napi::Array outArray; WrapVector(info.Env(), words, outArray); return outArray; } Napi::Value NodeJieba::cutAll(const Napi::CallbackInfo& info) { if (info.Length() == 0) { return Napi::Boolean::New(info.Env(), false); } std::string sentence = info[0].As(); if( !_jieba_handle ){ Napi::Error::New(info.Env(), "Before calling any other function you have to call load() first").ThrowAsJavaScriptException(); } std::vector words; _jieba_handle->CutAll(sentence, words); Napi::Array outArray; WrapVector(info.Env(), words, outArray); return outArray; } Napi::Value NodeJieba::cutForSearch(const Napi::CallbackInfo& info) { if (info.Length() == 0) { return Napi::Boolean::New(info.Env(), false); } std::string sentence = info[0].As(); bool useHMM = false; if (info.Length() > 1) { useHMM = info[1].As(); } if( !_jieba_handle ){ Napi::Error::New(info.Env(), "Before calling any other function you have to call load() first").ThrowAsJavaScriptException(); } std::vector words; _jieba_handle->CutForSearch(sentence, words, useHMM); Napi::Array outArray; WrapVector(info.Env(), words, outArray); return outArray; } Napi::Value NodeJieba::cutSmall(const Napi::CallbackInfo& info) { if (info.Length() < 2) { return Napi::Boolean::New(info.Env(), false); } std::string sentence = info[0].As(); size_t word_len_limit = info[1].As().Int32Value(); if( !_jieba_handle ){ Napi::Error::New(info.Env(), "Before calling any other function you have to call load() first").ThrowAsJavaScriptException(); } std::vector words; _jieba_handle->CutSmall(sentence, words, word_len_limit); Napi::Array outArray; WrapVector(info.Env(), words, outArray); return outArray; } Napi::Value NodeJieba::tag(const Napi::CallbackInfo& info) { if (info.Length() == 0) { return Napi::Boolean::New(info.Env(), false); } std::string sentence = info[0].As(); if( !_jieba_handle ){ Napi::Error::New(info.Env(), "Before calling any other function you have to call load() first").ThrowAsJavaScriptException(); } std::vector > words; _jieba_handle->Tag(sentence, words); Napi::Array outArray; WrapPairVector(info.Env(), words, outArray); return outArray; } Napi::Value NodeJieba::extract(const Napi::CallbackInfo& info) { if (info.Length() != 2) { return Napi::Boolean::New(info.Env(), false); } std::string sentence = info[0].As(); size_t topN = info[1].As().Int32Value(); if( !_jieba_handle ){ Napi::Error::New(info.Env(), "Before calling any other function you have to call load() first").ThrowAsJavaScriptException(); } std::vector > words; _jieba_handle->extractor.Extract(sentence, words, topN); Napi::Array outArray; WrapPairVector(info.Env(), words, outArray); return outArray; } Napi::Value NodeJieba::textRankExtract(const Napi::CallbackInfo& info) { if (info.Length() != 2) { return Napi::Boolean::New(info.Env(), false); } std::string sentence = info[0].As(); size_t topN = info[1].As().Int32Value(); if( !_text_rank_extractor_handle ){ Napi::Error::New(info.Env(), "Before calling any other function you have to call load() first").ThrowAsJavaScriptException(); } std::vector > words; _text_rank_extractor_handle->Extract(sentence, words, topN); Napi::Array outArray; WrapPairVector(info.Env(), words, outArray); return outArray; }