Skip to content

Commit

Permalink
add: api nodejieba.textRankExtract
Browse files Browse the repository at this point in the history
  • Loading branch information
JabinGP committed Jan 9, 2022
1 parent ca51c4c commit 7a240a0
Show file tree
Hide file tree
Showing 10 changed files with 78 additions and 2 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,4 @@ t.*
node_modules
npm-debug.log
.idea
package-lock.json
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,13 @@ console.log(nodejieba.extract("升职加薪,当上CEO,走上人生巅峰。"
// { word: '升职', weight: 10.8561552143 },
// { word: '加薪', weight: 10.642581114 },
// { word: '巅峰', weight: 9.49395840471 } ]

console.log(nodejieba.textRankExtract("升职加薪,当上CEO,走上人生巅峰。", topN));
//[ { word: '当上', weight: 1 },
// { word: '不用', weight: 0.9898479330698993 },
// { word: '多久', weight: 0.9851260595435759 },
// { word: '加薪', weight: 0.9830464899847804 },
// { word: '升职', weight: 0.9802777682279076 } ]
```

更详细的其他用法请看 [test/demo.js](test/demo.js)
Expand Down
7 changes: 7 additions & 0 deletions README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,13 @@ console.log(nodejieba.extract("升职加薪,当上CEO,走上人生巅峰。"
// { word: '升职', weight: 10.8561552143 },
// { word: '加薪', weight: 10.642581114 },
// { word: '巅峰', weight: 9.49395840471 } ]

console.log(nodejieba.textRankExtract("升职加薪,当上CEO,走上人生巅峰。", topN));
//[ { word: '当上', weight: 1 },
// { word: '不用', weight: 0.9898479330698993 },
// { word: '多久', weight: 0.9851260595435759 },
// { word: '加薪', weight: 0.9830464899847804 },
// { word: '升职', weight: 0.9802777682279076 } ]
```

See details in [test/demo.js](test/demo.js)
Expand Down
1 change: 1 addition & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ wrapWithDictLoad("cutForSearch");
wrapWithDictLoad("cutSmall");
wrapWithDictLoad("tag");
wrapWithDictLoad("extract");
wrapWithDictLoad("textRankExtract");
wrapWithDictLoad("insertWord");

module.exports = exports;
27 changes: 27 additions & 0 deletions lib/nodejieba.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

#include "cppjieba/Jieba.hpp"
#include "cppjieba/KeywordExtractor.hpp"
#include "cppjieba/TextRankExtractor.hpp"

NodeJieba::NodeJieba(Napi::Env env, Napi::Object exports) {
DefineAddon(exports, {
Expand All @@ -15,6 +16,7 @@ NodeJieba::NodeJieba(Napi::Env env, Napi::Object exports) {
InstanceMethod("cutSmall", &NodeJieba::cutSmall),
InstanceMethod("tag", &NodeJieba::tag),
InstanceMethod("extract", &NodeJieba::extract),
InstanceMethod("textRankExtract", &NodeJieba::textRankExtract),
InstanceMethod("insertWord", &NodeJieba::insertWord)
});
}
Expand All @@ -37,6 +39,13 @@ Napi::Value NodeJieba::load(const Napi::CallbackInfo& info) {
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);
}

Expand Down Expand Up @@ -189,3 +198,21 @@ Napi::Value NodeJieba::extract(const Napi::CallbackInfo& info) {
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<Napi::String>();
size_t topN = info[1].As<Napi::Number>().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<std::pair<std::string, double> > words;
_text_rank_extractor_handle->Extract(sentence, words, topN);
Napi::Array outArray;
WrapPairVector(info.Env(), words, outArray);
return outArray;
}
4 changes: 3 additions & 1 deletion lib/nodejieba.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

#include <napi.h>

namespace cppjieba{ class Jieba; }
namespace cppjieba{ class Jieba; class TextRankExtractor; }

class NodeJieba : public Napi::Addon<NodeJieba> {
public:
Expand All @@ -18,9 +18,11 @@ class NodeJieba : public Napi::Addon<NodeJieba> {
Napi::Value cutSmall(const Napi::CallbackInfo& info);
Napi::Value tag(const Napi::CallbackInfo& info);
Napi::Value extract(const Napi::CallbackInfo& info);
Napi::Value textRankExtract(const Napi::CallbackInfo& info);
Napi::Value insertWord(const Napi::CallbackInfo& info);

cppjieba::Jieba* _jieba_handle{nullptr};
cppjieba::TextRankExtractor* _text_rank_extractor_handle{nullptr};
};

#endif // NODEJIBEA_SRC_NODEJIEBA_H
3 changes: 3 additions & 0 deletions test/demo.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ var topN = 5;
result = nodejieba.extract(sentence, topN);
console.log(result);

result = nodejieba.textRankExtract(sentence, topN);
console.log(result);

result = nodejieba.cut("男默女泪");
console.log(result);
nodejieba.insertWord("男默女泪");
Expand Down
24 changes: 24 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,30 @@ describe("nodejieba", function() {
}]);
});

it('nodejieba.textRankExtract(sentence, 5)', function() {
nodejieba.textRankExtract(sentence, 5).should.eql([
{
"weight": 1,
"word": "当上"
},
{
"weight": 0.9898479330698993,
"word": "不用"
},
{
"weight": 0.9851260595435759,
"word": "多久"
},
{
"weight": 0.9830464899847804,
"word": "加薪"
},
{
"weight": 0.9802777682279076,
"word": "升职"
}]);
});

it('nodejieba.cut("红掌拨清波")', function() {
nodejieba.cut("红掌拨清波").should.eql([
'红掌',
Expand Down
1 change: 1 addition & 0 deletions types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ declare module "nodejieba" {
export function cutForSearch(sentence: string, strict?: boolean): string[];
export function tag(sentence: string): TagResult[];
export function extract(sentence: string, threshold: number): ExtractResult[];
export function textRankExtract(sentence: string, threshold: number): ExtractResult[];
export function insertWord(sentence: string): boolean;
export function cutSmall(sentence: string, small: number): boolean;
}
5 changes: 4 additions & 1 deletion typescript_demo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,14 @@ const topN = 5;
result = nodejieba.extract(sentence, topN);
console.log(result);

result = nodejieba.textRankExtract(sentence, topN);
console.log(result);

result = nodejieba.cut("男默女泪");
console.log(result);
nodejieba.insertWord("男默女泪");
result = nodejieba.cut("男默女泪");
console.log(result);

result = nodejieba.cutSmall("南京市长江大桥", 3);
console.log(result);
console.log(result);

0 comments on commit 7a240a0

Please sign in to comment.