@ohos-ports/nodejieba 0.0.0-stage → 3.5.8-beta.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +360 -0
- package/LICENSE +21 -0
- package/README.md +270 -2
- package/binding.gyp +63 -0
- package/build/Release/nodejieba.node +0 -0
- package/index.js +77 -0
- package/lib/index.cpp +3 -0
- package/lib/nodejieba.cpp +218 -0
- package/lib/nodejieba.h +28 -0
- package/lib/utils.h +47 -0
- package/package.json +62 -4
- package/submodules/cppjieba/CHANGELOG.md +305 -0
- package/submodules/cppjieba/deps/limonp/CHANGELOG.md +160 -0
- package/submodules/cppjieba/deps/limonp/include/limonp/ArgvContext.hpp +70 -0
- package/submodules/cppjieba/deps/limonp/include/limonp/Closure.hpp +206 -0
- package/submodules/cppjieba/deps/limonp/include/limonp/Colors.hpp +31 -0
- package/submodules/cppjieba/deps/limonp/include/limonp/Condition.hpp +38 -0
- package/submodules/cppjieba/deps/limonp/include/limonp/Config.hpp +103 -0
- package/submodules/cppjieba/deps/limonp/include/limonp/ForcePublic.hpp +7 -0
- package/submodules/cppjieba/deps/limonp/include/limonp/LocalVector.hpp +139 -0
- package/submodules/cppjieba/deps/limonp/include/limonp/Logging.hpp +90 -0
- package/submodules/cppjieba/deps/limonp/include/limonp/NonCopyable.hpp +21 -0
- package/submodules/cppjieba/deps/limonp/include/limonp/StdExtension.hpp +157 -0
- package/submodules/cppjieba/deps/limonp/include/limonp/StringUtil.hpp +386 -0
- package/submodules/cppjieba/dict/README.md +31 -0
- package/submodules/cppjieba/dict/hmm_model.utf8 +34 -0
- package/submodules/cppjieba/dict/idf.utf8 +258826 -0
- package/submodules/cppjieba/dict/jieba.dict.utf8 +348982 -0
- package/submodules/cppjieba/dict/pos_dict/char_state_tab.utf8 +6653 -0
- package/submodules/cppjieba/dict/pos_dict/prob_emit.utf8 +166 -0
- package/submodules/cppjieba/dict/pos_dict/prob_start.utf8 +259 -0
- package/submodules/cppjieba/dict/pos_dict/prob_trans.utf8 +5222 -0
- package/submodules/cppjieba/dict/stop_words.utf8 +1534 -0
- package/submodules/cppjieba/dict/user.dict.utf8 +4 -0
- package/submodules/cppjieba/include/cppjieba/DictTrie.hpp +285 -0
- package/submodules/cppjieba/include/cppjieba/FullSegment.hpp +93 -0
- package/submodules/cppjieba/include/cppjieba/HMMModel.hpp +129 -0
- package/submodules/cppjieba/include/cppjieba/HMMSegment.hpp +190 -0
- package/submodules/cppjieba/include/cppjieba/Jieba.hpp +169 -0
- package/submodules/cppjieba/include/cppjieba/KeywordExtractor.hpp +153 -0
- package/submodules/cppjieba/include/cppjieba/MPSegment.hpp +137 -0
- package/submodules/cppjieba/include/cppjieba/MixSegment.hpp +109 -0
- package/submodules/cppjieba/include/cppjieba/PosTagger.hpp +77 -0
- package/submodules/cppjieba/include/cppjieba/PreFilter.hpp +54 -0
- package/submodules/cppjieba/include/cppjieba/QuerySegment.hpp +89 -0
- package/submodules/cppjieba/include/cppjieba/SegmentBase.hpp +46 -0
- package/submodules/cppjieba/include/cppjieba/SegmentTagged.hpp +23 -0
- package/submodules/cppjieba/include/cppjieba/TextRankExtractor.hpp +190 -0
- package/submodules/cppjieba/include/cppjieba/Trie.hpp +200 -0
- package/submodules/cppjieba/include/cppjieba/Unicode.hpp +227 -0
- package/test/load_dict_test.js +14 -0
- package/test/missing_binding_test.js +42 -0
- package/test/test.js +366 -0
- package/test/testdata/userdict.utf8 +1 -0
- package/types/index.d.ts +30 -0
|
@@ -0,0 +1,227 @@
|
|
|
1
|
+
#ifndef CPPJIEBA_UNICODE_H
|
|
2
|
+
#define CPPJIEBA_UNICODE_H
|
|
3
|
+
|
|
4
|
+
#include <stdint.h>
|
|
5
|
+
#include <stdlib.h>
|
|
6
|
+
#include <string>
|
|
7
|
+
#include <vector>
|
|
8
|
+
#include <ostream>
|
|
9
|
+
#include "limonp/LocalVector.hpp"
|
|
10
|
+
|
|
11
|
+
namespace cppjieba {
|
|
12
|
+
|
|
13
|
+
using std::string;
|
|
14
|
+
using std::vector;
|
|
15
|
+
|
|
16
|
+
typedef uint32_t Rune;
|
|
17
|
+
|
|
18
|
+
struct Word {
|
|
19
|
+
string word;
|
|
20
|
+
uint32_t offset;
|
|
21
|
+
uint32_t unicode_offset;
|
|
22
|
+
uint32_t unicode_length;
|
|
23
|
+
Word(const string& w, uint32_t o)
|
|
24
|
+
: word(w), offset(o) {
|
|
25
|
+
}
|
|
26
|
+
Word(const string& w, uint32_t o, uint32_t unicode_offset, uint32_t unicode_length)
|
|
27
|
+
: word(w), offset(o), unicode_offset(unicode_offset), unicode_length(unicode_length) {
|
|
28
|
+
}
|
|
29
|
+
}; // struct Word
|
|
30
|
+
|
|
31
|
+
inline std::ostream& operator << (std::ostream& os, const Word& w) {
|
|
32
|
+
return os << "{\"word\": \"" << w.word << "\", \"offset\": " << w.offset << "}";
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
struct RuneStr {
|
|
36
|
+
Rune rune;
|
|
37
|
+
uint32_t offset;
|
|
38
|
+
uint32_t len;
|
|
39
|
+
uint32_t unicode_offset;
|
|
40
|
+
uint32_t unicode_length;
|
|
41
|
+
RuneStr(): rune(0), offset(0), len(0), unicode_offset(0), unicode_length(0) {
|
|
42
|
+
}
|
|
43
|
+
RuneStr(Rune r, uint32_t o, uint32_t l)
|
|
44
|
+
: rune(r), offset(o), len(l), unicode_offset(0), unicode_length(0) {
|
|
45
|
+
}
|
|
46
|
+
RuneStr(Rune r, uint32_t o, uint32_t l, uint32_t unicode_offset, uint32_t unicode_length)
|
|
47
|
+
: rune(r), offset(o), len(l), unicode_offset(unicode_offset), unicode_length(unicode_length) {
|
|
48
|
+
}
|
|
49
|
+
}; // struct RuneStr
|
|
50
|
+
|
|
51
|
+
inline std::ostream& operator << (std::ostream& os, const RuneStr& r) {
|
|
52
|
+
return os << "{\"rune\": \"" << r.rune << "\", \"offset\": " << r.offset << ", \"len\": " << r.len << "}";
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
typedef limonp::LocalVector<Rune> Unicode;
|
|
56
|
+
typedef limonp::LocalVector<struct RuneStr> RuneStrArray;
|
|
57
|
+
|
|
58
|
+
// [left, right]
|
|
59
|
+
struct WordRange {
|
|
60
|
+
RuneStrArray::const_iterator left;
|
|
61
|
+
RuneStrArray::const_iterator right;
|
|
62
|
+
WordRange(RuneStrArray::const_iterator l, RuneStrArray::const_iterator r)
|
|
63
|
+
: left(l), right(r) {
|
|
64
|
+
}
|
|
65
|
+
size_t Length() const {
|
|
66
|
+
return right - left + 1;
|
|
67
|
+
}
|
|
68
|
+
bool IsAllAscii() const {
|
|
69
|
+
for (RuneStrArray::const_iterator iter = left; iter <= right; ++iter) {
|
|
70
|
+
if (iter->rune >= 0x80) {
|
|
71
|
+
return false;
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
return true;
|
|
75
|
+
}
|
|
76
|
+
}; // struct WordRange
|
|
77
|
+
|
|
78
|
+
struct RuneStrLite {
|
|
79
|
+
uint32_t rune;
|
|
80
|
+
uint32_t len;
|
|
81
|
+
RuneStrLite(): rune(0), len(0) {
|
|
82
|
+
}
|
|
83
|
+
RuneStrLite(uint32_t r, uint32_t l): rune(r), len(l) {
|
|
84
|
+
}
|
|
85
|
+
}; // struct RuneStrLite
|
|
86
|
+
|
|
87
|
+
inline RuneStrLite DecodeUTF8ToRune(const char* str, size_t len) {
|
|
88
|
+
RuneStrLite rp(0, 0);
|
|
89
|
+
if (str == NULL || len == 0) {
|
|
90
|
+
return rp;
|
|
91
|
+
}
|
|
92
|
+
if (!(str[0] & 0x80)) { // 0xxxxxxx
|
|
93
|
+
// 7bit, total 7bit
|
|
94
|
+
rp.rune = (uint8_t)(str[0]) & 0x7f;
|
|
95
|
+
rp.len = 1;
|
|
96
|
+
} else if ((uint8_t)str[0] <= 0xdf && 1 < len) {
|
|
97
|
+
// 110xxxxxx
|
|
98
|
+
// 5bit, total 5bit
|
|
99
|
+
rp.rune = (uint8_t)(str[0]) & 0x1f;
|
|
100
|
+
|
|
101
|
+
// 6bit, total 11bit
|
|
102
|
+
rp.rune <<= 6;
|
|
103
|
+
rp.rune |= (uint8_t)(str[1]) & 0x3f;
|
|
104
|
+
rp.len = 2;
|
|
105
|
+
} else if((uint8_t)str[0] <= 0xef && 2 < len) { // 1110xxxxxx
|
|
106
|
+
// 4bit, total 4bit
|
|
107
|
+
rp.rune = (uint8_t)(str[0]) & 0x0f;
|
|
108
|
+
|
|
109
|
+
// 6bit, total 10bit
|
|
110
|
+
rp.rune <<= 6;
|
|
111
|
+
rp.rune |= (uint8_t)(str[1]) & 0x3f;
|
|
112
|
+
|
|
113
|
+
// 6bit, total 16bit
|
|
114
|
+
rp.rune <<= 6;
|
|
115
|
+
rp.rune |= (uint8_t)(str[2]) & 0x3f;
|
|
116
|
+
|
|
117
|
+
rp.len = 3;
|
|
118
|
+
} else if((uint8_t)str[0] <= 0xf7 && 3 < len) { // 11110xxxx
|
|
119
|
+
// 3bit, total 3bit
|
|
120
|
+
rp.rune = (uint8_t)(str[0]) & 0x07;
|
|
121
|
+
|
|
122
|
+
// 6bit, total 9bit
|
|
123
|
+
rp.rune <<= 6;
|
|
124
|
+
rp.rune |= (uint8_t)(str[1]) & 0x3f;
|
|
125
|
+
|
|
126
|
+
// 6bit, total 15bit
|
|
127
|
+
rp.rune <<= 6;
|
|
128
|
+
rp.rune |= (uint8_t)(str[2]) & 0x3f;
|
|
129
|
+
|
|
130
|
+
// 6bit, total 21bit
|
|
131
|
+
rp.rune <<= 6;
|
|
132
|
+
rp.rune |= (uint8_t)(str[3]) & 0x3f;
|
|
133
|
+
|
|
134
|
+
rp.len = 4;
|
|
135
|
+
} else {
|
|
136
|
+
rp.rune = 0;
|
|
137
|
+
rp.len = 0;
|
|
138
|
+
}
|
|
139
|
+
return rp;
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
inline bool DecodeUTF8RunesInString(const char* s, size_t len, RuneStrArray& runes) {
|
|
143
|
+
runes.clear();
|
|
144
|
+
runes.reserve(len / 2);
|
|
145
|
+
for (uint32_t i = 0, j = 0; i < len;) {
|
|
146
|
+
RuneStrLite rp = DecodeUTF8ToRune(s + i, len - i);
|
|
147
|
+
if (rp.len == 0) {
|
|
148
|
+
runes.clear();
|
|
149
|
+
return false;
|
|
150
|
+
}
|
|
151
|
+
RuneStr x(rp.rune, i, rp.len, j, 1);
|
|
152
|
+
runes.push_back(x);
|
|
153
|
+
i += rp.len;
|
|
154
|
+
++j;
|
|
155
|
+
}
|
|
156
|
+
return true;
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
inline bool DecodeUTF8RunesInString(const string& s, RuneStrArray& runes) {
|
|
160
|
+
return DecodeUTF8RunesInString(s.c_str(), s.size(), runes);
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
inline bool DecodeUTF8RunesInString(const char* s, size_t len, Unicode& unicode) {
|
|
164
|
+
unicode.clear();
|
|
165
|
+
RuneStrArray runes;
|
|
166
|
+
if (!DecodeUTF8RunesInString(s, len, runes)) {
|
|
167
|
+
return false;
|
|
168
|
+
}
|
|
169
|
+
unicode.reserve(runes.size());
|
|
170
|
+
for (size_t i = 0; i < runes.size(); i++) {
|
|
171
|
+
unicode.push_back(runes[i].rune);
|
|
172
|
+
}
|
|
173
|
+
return true;
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
inline bool IsSingleWord(const string& str) {
|
|
177
|
+
RuneStrLite rp = DecodeUTF8ToRune(str.c_str(), str.size());
|
|
178
|
+
return rp.len == str.size();
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
inline bool DecodeUTF8RunesInString(const string& s, Unicode& unicode) {
|
|
182
|
+
return DecodeUTF8RunesInString(s.c_str(), s.size(), unicode);
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
inline Unicode DecodeUTF8RunesInString(const string& s) {
|
|
186
|
+
Unicode result;
|
|
187
|
+
DecodeUTF8RunesInString(s, result);
|
|
188
|
+
return result;
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
|
|
192
|
+
// [left, right]
|
|
193
|
+
inline Word GetWordFromRunes(const string& s, RuneStrArray::const_iterator left, RuneStrArray::const_iterator right) {
|
|
194
|
+
assert(right->offset >= left->offset);
|
|
195
|
+
uint32_t len = right->offset - left->offset + right->len;
|
|
196
|
+
uint32_t unicode_length = right->unicode_offset - left->unicode_offset + right->unicode_length;
|
|
197
|
+
return Word(s.substr(left->offset, len), left->offset, left->unicode_offset, unicode_length);
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
inline string GetStringFromRunes(const string& s, RuneStrArray::const_iterator left, RuneStrArray::const_iterator right) {
|
|
201
|
+
assert(right->offset >= left->offset);
|
|
202
|
+
uint32_t len = right->offset - left->offset + right->len;
|
|
203
|
+
return s.substr(left->offset, len);
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
inline void GetWordsFromWordRanges(const string& s, const vector<WordRange>& wrs, vector<Word>& words) {
|
|
207
|
+
for (size_t i = 0; i < wrs.size(); i++) {
|
|
208
|
+
words.push_back(GetWordFromRunes(s, wrs[i].left, wrs[i].right));
|
|
209
|
+
}
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
inline vector<Word> GetWordsFromWordRanges(const string& s, const vector<WordRange>& wrs) {
|
|
213
|
+
vector<Word> result;
|
|
214
|
+
GetWordsFromWordRanges(s, wrs, result);
|
|
215
|
+
return result;
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
inline void GetStringsFromWords(const vector<Word>& words, vector<string>& strs) {
|
|
219
|
+
strs.resize(words.size());
|
|
220
|
+
for (size_t i = 0; i < words.size(); ++i) {
|
|
221
|
+
strs[i] = words[i].word;
|
|
222
|
+
}
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
} // namespace cppjieba
|
|
226
|
+
|
|
227
|
+
#endif // CPPJIEBA_UNICODE_H
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
var should = require("should");
|
|
2
|
+
var nodejieba = require("../index.js");
|
|
3
|
+
|
|
4
|
+
describe("nodejieba", function() {
|
|
5
|
+
var userDict = __dirname + '/testdata/userdict.utf8';
|
|
6
|
+
nodejieba.load({
|
|
7
|
+
userDict: userDict,
|
|
8
|
+
});
|
|
9
|
+
it('nodejieba.cut("红掌拨清波")', function() {
|
|
10
|
+
nodejieba.cut("红掌拨清波").should.eql([
|
|
11
|
+
'红掌拨清波',
|
|
12
|
+
]);
|
|
13
|
+
});
|
|
14
|
+
});
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
var fs = require("fs");
|
|
2
|
+
var os = require("os");
|
|
3
|
+
var path = require("path");
|
|
4
|
+
var should = require("should");
|
|
5
|
+
|
|
6
|
+
describe("nodejieba missing binding", function() {
|
|
7
|
+
it("loads without the native binding until first use", function() {
|
|
8
|
+
var fixtureDir = fs.mkdtempSync(path.join(os.tmpdir(), "nodejieba-missing-binding-"));
|
|
9
|
+
try {
|
|
10
|
+
var fixtureIndex = path.join(fixtureDir, "index.js");
|
|
11
|
+
|
|
12
|
+
fs.copyFileSync(path.join(__dirname, "..", "index.js"), fixtureIndex);
|
|
13
|
+
|
|
14
|
+
var nodejieba = require(fixtureIndex);
|
|
15
|
+
|
|
16
|
+
should(nodejieba.DEFAULT_DICT).type("string");
|
|
17
|
+
should(nodejieba.cut).type("function");
|
|
18
|
+
|
|
19
|
+
var firstError;
|
|
20
|
+
var secondError;
|
|
21
|
+
|
|
22
|
+
try {
|
|
23
|
+
nodejieba.cut("南京市长江大桥");
|
|
24
|
+
} catch (err) {
|
|
25
|
+
firstError = err;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
try {
|
|
29
|
+
nodejieba.cut("南京市长江大桥");
|
|
30
|
+
} catch (err) {
|
|
31
|
+
secondError = err;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
should(firstError).be.an.Error();
|
|
35
|
+
firstError.message.should.match(/nodejieba native binding was not found/);
|
|
36
|
+
firstError.code.should.equal("BINDING_NOT_FOUND");
|
|
37
|
+
should(secondError).equal(firstError);
|
|
38
|
+
} finally {
|
|
39
|
+
fs.rmSync(fixtureDir, { recursive: true, force: true });
|
|
40
|
+
}
|
|
41
|
+
});
|
|
42
|
+
});
|
package/test/test.js
ADDED
|
@@ -0,0 +1,366 @@
|
|
|
1
|
+
var should = require("should");
|
|
2
|
+
var nodejieba = require("../index.js");
|
|
3
|
+
|
|
4
|
+
describe("nodejieba", function() {
|
|
5
|
+
|
|
6
|
+
var sentence = "我是拖拉机学院手扶拖拉机专业的。不用多久,我就会升职加薪,当上CEO,走上人生巅峰。";
|
|
7
|
+
|
|
8
|
+
it("nodejieba.cut(sentence, true)", function() {
|
|
9
|
+
nodejieba.cut(sentence, true).should.eql([ '我',
|
|
10
|
+
'是',
|
|
11
|
+
'拖拉机',
|
|
12
|
+
'学院',
|
|
13
|
+
'手扶拖拉机',
|
|
14
|
+
'专业',
|
|
15
|
+
'的',
|
|
16
|
+
'。',
|
|
17
|
+
'不用',
|
|
18
|
+
'多久',
|
|
19
|
+
',',
|
|
20
|
+
'我',
|
|
21
|
+
'就',
|
|
22
|
+
'会',
|
|
23
|
+
'升职',
|
|
24
|
+
'加薪',
|
|
25
|
+
',',
|
|
26
|
+
'当上',
|
|
27
|
+
'CEO',
|
|
28
|
+
',',
|
|
29
|
+
'走上',
|
|
30
|
+
'人生',
|
|
31
|
+
'巅峰',
|
|
32
|
+
'。' ]);
|
|
33
|
+
});
|
|
34
|
+
|
|
35
|
+
it("nodejieba.cut('南京市长江大桥')", function() {
|
|
36
|
+
nodejieba.cut('南京市长江大桥').should.eql([ '南京市', '长江大桥' ]);
|
|
37
|
+
});
|
|
38
|
+
|
|
39
|
+
it("nodejieba.cut('南京市长江大桥')", function() {
|
|
40
|
+
nodejieba.cut('南京市长江大桥').should.eql([ '南京市', '长江大桥' ]);
|
|
41
|
+
});
|
|
42
|
+
|
|
43
|
+
it("nodejieba.cutHMM('南京市长江大桥')", function() {
|
|
44
|
+
nodejieba.cutHMM('南京市长江大桥').should.eql([ '南京市', '长江大桥' ]);
|
|
45
|
+
});
|
|
46
|
+
|
|
47
|
+
it("nodejieba.cut('南京市长江大桥', true)", function() {
|
|
48
|
+
nodejieba.cut('南京市长江大桥', true).should.eql([ '南京市', '长江大桥' ]);
|
|
49
|
+
});
|
|
50
|
+
|
|
51
|
+
it("nodejieba.cut('南京长江大桥')", function() {
|
|
52
|
+
nodejieba.cut('南京长江大桥').should.eql([ '南京长江大桥' ]);
|
|
53
|
+
});
|
|
54
|
+
|
|
55
|
+
it("nodejieba.cut('南京长江大桥')", function() {
|
|
56
|
+
nodejieba.cut('南京长江大桥').should.eql([ '南京长江大桥' ]);
|
|
57
|
+
});
|
|
58
|
+
|
|
59
|
+
it("nodejieba.cutHMM('南京长江大桥')", function() {
|
|
60
|
+
nodejieba.cutHMM('南京长江大桥').should.eql([ '南京长', '江大桥' ]);
|
|
61
|
+
});
|
|
62
|
+
|
|
63
|
+
it("nodejieba.cut('南京长江大桥', true)", function() {
|
|
64
|
+
nodejieba.cut('南京长江大桥', true).should.eql([ '南京长江大桥' ]);
|
|
65
|
+
});
|
|
66
|
+
|
|
67
|
+
it('nodejieba.cut(sentence)', function() {
|
|
68
|
+
nodejieba.cut(sentence).should.eql([ '我',
|
|
69
|
+
'是',
|
|
70
|
+
'拖拉机',
|
|
71
|
+
'学院',
|
|
72
|
+
'手扶拖拉机',
|
|
73
|
+
'专业',
|
|
74
|
+
'的',
|
|
75
|
+
'。',
|
|
76
|
+
'不用',
|
|
77
|
+
'多久',
|
|
78
|
+
',',
|
|
79
|
+
'我',
|
|
80
|
+
'就',
|
|
81
|
+
'会',
|
|
82
|
+
'升职',
|
|
83
|
+
'加薪',
|
|
84
|
+
',',
|
|
85
|
+
'当',
|
|
86
|
+
'上',
|
|
87
|
+
'C',
|
|
88
|
+
'E',
|
|
89
|
+
'O',
|
|
90
|
+
',',
|
|
91
|
+
'走上',
|
|
92
|
+
'人生',
|
|
93
|
+
'巅峰',
|
|
94
|
+
'。' ]);
|
|
95
|
+
})
|
|
96
|
+
|
|
97
|
+
it('nodejieba.cutHMM(sentence)', function() {
|
|
98
|
+
nodejieba.cutHMM(sentence).should.eql([ '我',
|
|
99
|
+
'是',
|
|
100
|
+
'拖拉机',
|
|
101
|
+
'学院',
|
|
102
|
+
'手',
|
|
103
|
+
'扶',
|
|
104
|
+
'拖拉机',
|
|
105
|
+
'专业',
|
|
106
|
+
'的',
|
|
107
|
+
'。',
|
|
108
|
+
'不用',
|
|
109
|
+
'多久',
|
|
110
|
+
',',
|
|
111
|
+
'我',
|
|
112
|
+
'就',
|
|
113
|
+
'会升',
|
|
114
|
+
'职加薪',
|
|
115
|
+
',',
|
|
116
|
+
'当上',
|
|
117
|
+
'CEO',
|
|
118
|
+
',',
|
|
119
|
+
'走上',
|
|
120
|
+
'人生',
|
|
121
|
+
'巅峰',
|
|
122
|
+
'。' ]);
|
|
123
|
+
});
|
|
124
|
+
|
|
125
|
+
it('nodejieba.cut(sentence, true)', function() {
|
|
126
|
+
nodejieba.cut(sentence, true).should.eql([ '我',
|
|
127
|
+
'是',
|
|
128
|
+
'拖拉机',
|
|
129
|
+
'学院',
|
|
130
|
+
'手扶拖拉机',
|
|
131
|
+
'专业',
|
|
132
|
+
'的',
|
|
133
|
+
'。',
|
|
134
|
+
'不用',
|
|
135
|
+
'多久',
|
|
136
|
+
',',
|
|
137
|
+
'我',
|
|
138
|
+
'就',
|
|
139
|
+
'会',
|
|
140
|
+
'升职',
|
|
141
|
+
'加薪',
|
|
142
|
+
',',
|
|
143
|
+
'当上',
|
|
144
|
+
'CEO',
|
|
145
|
+
',',
|
|
146
|
+
'走上',
|
|
147
|
+
'人生',
|
|
148
|
+
'巅峰',
|
|
149
|
+
'。' ]);
|
|
150
|
+
});
|
|
151
|
+
|
|
152
|
+
it('nodejieba.cutAll(sentence)', function() {
|
|
153
|
+
nodejieba.cutAll(sentence).should.eql([ '我',
|
|
154
|
+
'是',
|
|
155
|
+
'拖拉',
|
|
156
|
+
'拖拉机',
|
|
157
|
+
'学院',
|
|
158
|
+
'手扶',
|
|
159
|
+
'手扶拖拉机',
|
|
160
|
+
'拖拉',
|
|
161
|
+
'拖拉机',
|
|
162
|
+
'专业',
|
|
163
|
+
'的',
|
|
164
|
+
'。',
|
|
165
|
+
'不用',
|
|
166
|
+
'多久',
|
|
167
|
+
',',
|
|
168
|
+
'我',
|
|
169
|
+
'就',
|
|
170
|
+
'会升',
|
|
171
|
+
'升职',
|
|
172
|
+
'加薪',
|
|
173
|
+
',',
|
|
174
|
+
'当上',
|
|
175
|
+
'C',
|
|
176
|
+
'E',
|
|
177
|
+
'O',
|
|
178
|
+
',',
|
|
179
|
+
'走上',
|
|
180
|
+
'人生',
|
|
181
|
+
'巅峰',
|
|
182
|
+
'。' ]);
|
|
183
|
+
});
|
|
184
|
+
|
|
185
|
+
it('nodejieba.cutForSearch(sentence)', function() {
|
|
186
|
+
nodejieba.cutForSearch(sentence).should.eql([ '我',
|
|
187
|
+
'是',
|
|
188
|
+
'拖拉',
|
|
189
|
+
'拖拉机',
|
|
190
|
+
'学院',
|
|
191
|
+
'手扶',
|
|
192
|
+
'拖拉',
|
|
193
|
+
'拖拉机',
|
|
194
|
+
'手扶拖拉机',
|
|
195
|
+
'专业',
|
|
196
|
+
'的',
|
|
197
|
+
'。',
|
|
198
|
+
'不用',
|
|
199
|
+
'多久',
|
|
200
|
+
',',
|
|
201
|
+
'我',
|
|
202
|
+
'就',
|
|
203
|
+
'会',
|
|
204
|
+
'升职',
|
|
205
|
+
'加薪',
|
|
206
|
+
',',
|
|
207
|
+
'当',
|
|
208
|
+
'上',
|
|
209
|
+
'C',
|
|
210
|
+
'E',
|
|
211
|
+
'O',
|
|
212
|
+
',',
|
|
213
|
+
'走上',
|
|
214
|
+
'人生',
|
|
215
|
+
'巅峰',
|
|
216
|
+
'。' ]);
|
|
217
|
+
});
|
|
218
|
+
|
|
219
|
+
it('nodejieba.cutForSearch(sentence, true)', function() {
|
|
220
|
+
nodejieba.cutForSearch(sentence, true).should.eql([ '我',
|
|
221
|
+
'是',
|
|
222
|
+
'拖拉',
|
|
223
|
+
'拖拉机',
|
|
224
|
+
'学院',
|
|
225
|
+
'手扶',
|
|
226
|
+
'拖拉',
|
|
227
|
+
'拖拉机',
|
|
228
|
+
'手扶拖拉机',
|
|
229
|
+
'专业',
|
|
230
|
+
'的',
|
|
231
|
+
'。',
|
|
232
|
+
'不用',
|
|
233
|
+
'多久',
|
|
234
|
+
',',
|
|
235
|
+
'我',
|
|
236
|
+
'就',
|
|
237
|
+
'会',
|
|
238
|
+
'升职',
|
|
239
|
+
'加薪',
|
|
240
|
+
',',
|
|
241
|
+
'当上',
|
|
242
|
+
'CEO',
|
|
243
|
+
',',
|
|
244
|
+
'走上',
|
|
245
|
+
'人生',
|
|
246
|
+
'巅峰',
|
|
247
|
+
'。' ]);
|
|
248
|
+
});
|
|
249
|
+
|
|
250
|
+
it('nodejieba.tag(sentence)', function() {
|
|
251
|
+
nodejieba.tag(sentence).should.eql([ { word: '我', tag: 'r' },
|
|
252
|
+
{ word: '是', tag: 'v' },
|
|
253
|
+
{ word: '拖拉机', tag: 'n' },
|
|
254
|
+
{ word: '学院', tag: 'n' },
|
|
255
|
+
{ word: '手扶拖拉机', tag: 'n' },
|
|
256
|
+
{ word: '专业', tag: 'n' },
|
|
257
|
+
{ word: '的', tag: 'uj' },
|
|
258
|
+
{ word: '。', tag: 'x' },
|
|
259
|
+
{ word: '不用', tag: 'v' },
|
|
260
|
+
{ word: '多久', tag: 'm' },
|
|
261
|
+
{ word: ',', tag: 'x' },
|
|
262
|
+
{ word: '我', tag: 'r' },
|
|
263
|
+
{ word: '就', tag: 'd' },
|
|
264
|
+
{ word: '会', tag: 'v' },
|
|
265
|
+
{ word: '升职', tag: 'v' },
|
|
266
|
+
{ word: '加薪', tag: 'nr' },
|
|
267
|
+
{ word: ',', tag: 'x' },
|
|
268
|
+
{ word: '当上', tag: 't' },
|
|
269
|
+
{ word: 'CEO', tag: 'eng' },
|
|
270
|
+
{ word: ',', tag: 'x' },
|
|
271
|
+
{ word: '走上', tag: 'v' },
|
|
272
|
+
{ word: '人生', tag: 'n' },
|
|
273
|
+
{ word: '巅峰', tag: 'n' },
|
|
274
|
+
{ word: '。', tag: 'x' } ]);
|
|
275
|
+
});
|
|
276
|
+
|
|
277
|
+
// float compare error in multi platform (macos, windows)
|
|
278
|
+
//it('nodejieba.extract(sentence, 5)', function() {
|
|
279
|
+
// nodejieba.extract(sentence, 5).should.eql([
|
|
280
|
+
// {
|
|
281
|
+
// "weight": 11.739204307083542,
|
|
282
|
+
// "word": "CEO"
|
|
283
|
+
// },
|
|
284
|
+
// {
|
|
285
|
+
// "weight": 10.8561552143,
|
|
286
|
+
// "word": "升职"
|
|
287
|
+
// },
|
|
288
|
+
// {
|
|
289
|
+
// "weight": 10.642581114,
|
|
290
|
+
// "word": "加薪"
|
|
291
|
+
// },
|
|
292
|
+
// {
|
|
293
|
+
// "weight": 10.0088573539,
|
|
294
|
+
// "word": "手扶拖拉机"
|
|
295
|
+
// },
|
|
296
|
+
// {
|
|
297
|
+
// "weight": 9.49395840471,
|
|
298
|
+
// "word": "巅峰"
|
|
299
|
+
// }]);
|
|
300
|
+
//});
|
|
301
|
+
|
|
302
|
+
//it('nodejieba.textRankExtract(sentence, 5)', function() {
|
|
303
|
+
// nodejieba.textRankExtract(sentence, 5).should.eql([
|
|
304
|
+
// {
|
|
305
|
+
// "weight": 1,
|
|
306
|
+
// "word": "当上"
|
|
307
|
+
// },
|
|
308
|
+
// {
|
|
309
|
+
// "weight": 0.9898479330698993,
|
|
310
|
+
// "word": "不用"
|
|
311
|
+
// },
|
|
312
|
+
// {
|
|
313
|
+
// "weight": 0.9851260595435759,
|
|
314
|
+
// "word": "多久"
|
|
315
|
+
// },
|
|
316
|
+
// {
|
|
317
|
+
// "weight": 0.9830464899847804,
|
|
318
|
+
// "word": "加薪"
|
|
319
|
+
// },
|
|
320
|
+
// {
|
|
321
|
+
// "weight": 0.9802777682279076,
|
|
322
|
+
// "word": "升职"
|
|
323
|
+
// }]);
|
|
324
|
+
//});
|
|
325
|
+
|
|
326
|
+
it('nodejieba.cut("红掌拨清波")', function() {
|
|
327
|
+
nodejieba.cut("红掌拨清波").should.eql([
|
|
328
|
+
'红掌',
|
|
329
|
+
'拨',
|
|
330
|
+
'清波',
|
|
331
|
+
]);
|
|
332
|
+
});
|
|
333
|
+
|
|
334
|
+
it('nodejieba.cut("男默女泪")', function() {
|
|
335
|
+
nodejieba.cut("男默女泪").should.eql([ '男', '默',
|
|
336
|
+
'女', '泪' ]);
|
|
337
|
+
});
|
|
338
|
+
it('nodejieba.insertWord("男默女泪")', function() {
|
|
339
|
+
nodejieba.insertWord("男默女泪").should.eql(true);
|
|
340
|
+
});
|
|
341
|
+
it('nodejieba.cut("男默女泪")', function() {
|
|
342
|
+
nodejieba.cut("男默女泪").should.eql([ '男默女泪' ]);
|
|
343
|
+
});
|
|
344
|
+
|
|
345
|
+
it('nodejieba.cut("今天天气很好,🙋 我们去郊游。")', function() {
|
|
346
|
+
nodejieba.cut("今天天气很好,🙋 我们去郊游。").should.eql([ '今天天气', '很', '好', ',', '🙋', ' ', '我们', '去', '郊游', '。' ]);
|
|
347
|
+
});
|
|
348
|
+
|
|
349
|
+
it('nodejieba.cutSmall("南京市长江大桥", 3)', function() {
|
|
350
|
+
nodejieba.cutSmall("南京市长江大桥", 3).should.eql([ '南京市',
|
|
351
|
+
'长江',
|
|
352
|
+
'大桥']);
|
|
353
|
+
});
|
|
354
|
+
|
|
355
|
+
it('nodejieba.cut("区块链")', function() {
|
|
356
|
+
nodejieba.cut("区块链").should.eql(['区块链']);
|
|
357
|
+
});
|
|
358
|
+
it('nodejieba.tag("区块链")', function() {
|
|
359
|
+
nodejieba.tag("区块链").should.eql([ { word: '区块链', tag: 'nz' }]);
|
|
360
|
+
});
|
|
361
|
+
it('nodejieba.cut("访问www.baidu.com进行搜索")',function(){
|
|
362
|
+
nodejieba.cut("访问www.baidu.com进行搜索", true).should.eql([
|
|
363
|
+
'访问', 'www', '.', 'baidu', '.', 'com', '进行', '搜索',
|
|
364
|
+
]);
|
|
365
|
+
});
|
|
366
|
+
});
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
红掌拨清波
|
package/types/index.d.ts
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
declare module "nodejieba" {
|
|
2
|
+
export interface LoadOptions {
|
|
3
|
+
dict?: string;
|
|
4
|
+
hmmDict?: string;
|
|
5
|
+
userDict?: string;
|
|
6
|
+
idfDict?: string;
|
|
7
|
+
stopWordDict?: string;
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
export interface TagResult {
|
|
11
|
+
word: string;
|
|
12
|
+
tag: string;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export interface ExtractResult {
|
|
16
|
+
word: string;
|
|
17
|
+
weight: number;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
export function load(dict?: LoadOptions): void;
|
|
21
|
+
export function cut(sentence: string, strict?: boolean): string[];
|
|
22
|
+
export function cutHMM(sentence: string): string[];
|
|
23
|
+
export function cutAll(sentence: string): string[];
|
|
24
|
+
export function cutForSearch(sentence: string, strict?: boolean): string[];
|
|
25
|
+
export function tag(sentence: string): TagResult[];
|
|
26
|
+
export function extract(sentence: string, threshold: number): ExtractResult[];
|
|
27
|
+
export function textRankExtract(sentence: string, threshold: number): ExtractResult[];
|
|
28
|
+
export function insertWord(word: string, tag?: string): boolean;
|
|
29
|
+
export function cutSmall(sentence: string, small: number): string[];
|
|
30
|
+
}
|