Skip to content

Commit 290f30b

Browse files
committed
feat(jieba): support weight value in extract
1 parent b70fa5c commit 290f30b

File tree

5 files changed

+28
-10
lines changed

5 files changed

+28
-10
lines changed

packages/jieba/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ edition = "2018"
88
crate-type = ["cdylib"]
99

1010
[dependencies]
11-
jieba-rs = { version = "0.5", features = ["default-dict", "tfidf", "textrank"] }
11+
jieba-rs = { version = "0.6.0-alpha.1", features = ["default-dict", "tfidf", "textrank"] }
1212
napi = { version = "0.4"}
1313
napi-derive = { version = "0.4" }
1414
once_cell = "1.4"

packages/jieba/__tests__/jieba.spec.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,13 @@ test('extract should be equal to nodejieba', (t) => {
1818
'今天纽约的天气真好啊,京华大酒店的张尧经理吃了一只北京烤鸭。后天纽约的天气不好,昨天纽约的天气也不好,北京烤鸭真好吃'
1919
const topn = 3
2020
t.deepEqual(
21-
extract(sentence, topn),
22-
nodejieba.extract(sentence, topn).map((t) => t.word),
21+
extract(sentence, topn).map((t) => ({
22+
keyword: t.keyword,
23+
weight: typeof t.weight,
24+
})),
25+
nodejieba.extract(sentence, topn).map((t) => ({
26+
keyword: t.word,
27+
weight: typeof t.weight,
28+
})),
2329
)
2430
})

packages/jieba/index.d.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,11 @@ export interface TagResult {
88
tag: string
99
}
1010

11+
export interface Keyword {
12+
keyword: string
13+
weight: number
14+
}
15+
1116
export function tag(sentence: string | Buffer, hmm?: boolean): TagResult[]
1217

13-
export function extract(sentence: string | Buffer, topn: number, allowedPos?: string[]): string[]
18+
export function extract(sentence: string | Buffer, topn: number, allowedPos?: string[]): Keyword[]

packages/jieba/index.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ module.exports = {
2626

2727
extract: function extract(sentence, topn, allowedPos = []) {
2828
const input = Buffer.isBuffer(sentence) ? sentence : Buffer.from(sentence)
29-
const output = native.extract(input, topn, allowedPos.join(','))
30-
return output.split(',')
29+
return native.extract(input, topn, allowedPos.join(','))
3130
},
3231
}

packages/jieba/src/lib.rs

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ extern crate napi_derive;
55

66
use jieba_rs::{Jieba, KeywordExtract, TFIDF};
77
use napi::{
8-
CallContext, Env, Error, JsBoolean, JsBuffer, JsNumber, JsString, JsUndefined, Module, Result,
9-
Status,
8+
CallContext, Env, Error, JsBoolean, JsBuffer, JsNumber, JsObject, JsString, JsUndefined, Module,
9+
Result, Status,
1010
};
1111
use once_cell::sync::OnceCell;
1212
use std::convert::TryInto;
@@ -133,7 +133,7 @@ fn tag(ctx: CallContext) -> Result<JsString> {
133133
}
134134

135135
#[js_function(3)]
136-
fn extract(ctx: CallContext) -> Result<JsString> {
136+
fn extract(ctx: CallContext) -> Result<JsObject> {
137137
let sentence = ctx.get::<JsBuffer>(0)?;
138138
let topn = ctx.get::<JsNumber>(1)?;
139139
let allowed_pos = ctx
@@ -159,8 +159,16 @@ fn extract(ctx: CallContext) -> Result<JsString> {
159159
topn,
160160
allowed_pos,
161161
);
162+
let mut js_tags = ctx.env.create_array_with_length(tags.len())?;
162163

163-
ctx.env.create_string(tags.join(",").as_str())
164+
for (index, t) in tags.iter().enumerate() {
165+
let mut tag_value = ctx.env.create_object()?;
166+
tag_value.set_named_property("keyword", ctx.env.create_string(t.keyword.as_str())?)?;
167+
tag_value.set_named_property("weight", ctx.env.create_double(t.weight)?)?;
168+
js_tags.set_index(index, tag_value)?;
169+
}
170+
171+
Ok(js_tags)
164172
}
165173

166174
#[js_function]

0 commit comments

Comments
 (0)