nodejieba-plus 3.5.8

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.
Files changed (130) hide show
  1. package/.github/FUNDING.yml +12 -0
  2. package/.github/workflows/github_release.yml +61 -0
  3. package/.github/workflows/npm_publish.yml +24 -0
  4. package/.github/workflows/stale-issues.yml +24 -0
  5. package/.github/workflows/test.yml +42 -0
  6. package/.gitmodules +3 -0
  7. package/.npmignore +15 -0
  8. package/CHANGELOG.md +360 -0
  9. package/CONTRIBUTING.md +78 -0
  10. package/LICENSE +21 -0
  11. package/README.md +349 -0
  12. package/binding.gyp +63 -0
  13. package/index.js +77 -0
  14. package/lib/index.cpp +3 -0
  15. package/lib/nodejieba.cpp +218 -0
  16. package/lib/nodejieba.h +28 -0
  17. package/lib/utils.h +47 -0
  18. package/package.json +48 -0
  19. package/submodules/cppjieba/.github/workflows/cmake.yml +51 -0
  20. package/submodules/cppjieba/.github/workflows/stale-issues.yml +24 -0
  21. package/submodules/cppjieba/.gitmodules +3 -0
  22. package/submodules/cppjieba/CHANGELOG.md +305 -0
  23. package/submodules/cppjieba/CMakeLists.txt +42 -0
  24. package/submodules/cppjieba/LICENSE +20 -0
  25. package/submodules/cppjieba/README.md +280 -0
  26. package/submodules/cppjieba/deps/limonp/.github/workflows/cmake.yml +43 -0
  27. package/submodules/cppjieba/deps/limonp/.gitmodules +0 -0
  28. package/submodules/cppjieba/deps/limonp/CHANGELOG.md +160 -0
  29. package/submodules/cppjieba/deps/limonp/CMakeLists.txt +61 -0
  30. package/submodules/cppjieba/deps/limonp/LICENSE +20 -0
  31. package/submodules/cppjieba/deps/limonp/README.md +38 -0
  32. package/submodules/cppjieba/deps/limonp/include/limonp/ArgvContext.hpp +70 -0
  33. package/submodules/cppjieba/deps/limonp/include/limonp/Closure.hpp +206 -0
  34. package/submodules/cppjieba/deps/limonp/include/limonp/Colors.hpp +31 -0
  35. package/submodules/cppjieba/deps/limonp/include/limonp/Condition.hpp +38 -0
  36. package/submodules/cppjieba/deps/limonp/include/limonp/Config.hpp +103 -0
  37. package/submodules/cppjieba/deps/limonp/include/limonp/ForcePublic.hpp +7 -0
  38. package/submodules/cppjieba/deps/limonp/include/limonp/LocalVector.hpp +139 -0
  39. package/submodules/cppjieba/deps/limonp/include/limonp/Logging.hpp +90 -0
  40. package/submodules/cppjieba/deps/limonp/include/limonp/NonCopyable.hpp +21 -0
  41. package/submodules/cppjieba/deps/limonp/include/limonp/StdExtension.hpp +157 -0
  42. package/submodules/cppjieba/deps/limonp/include/limonp/StringUtil.hpp +386 -0
  43. package/submodules/cppjieba/deps/limonp/test/CMakeLists.txt +8 -0
  44. package/submodules/cppjieba/deps/limonp/test/demo.cpp +40 -0
  45. package/submodules/cppjieba/deps/limonp/test/testdata/1.conf +5 -0
  46. package/submodules/cppjieba/deps/limonp/test/testdata/StdExtension.data +3 -0
  47. package/submodules/cppjieba/deps/limonp/test/testdata/dict.gbk +50 -0
  48. package/submodules/cppjieba/deps/limonp/test/testdata/dict.utf8 +50 -0
  49. package/submodules/cppjieba/deps/limonp/test/testdata/io_testfile +2 -0
  50. package/submodules/cppjieba/deps/limonp/test/testdata/jieba.dict.0.1.utf8 +93 -0
  51. package/submodules/cppjieba/deps/limonp/test/testdata/jieba.dict.0.utf8 +93 -0
  52. package/submodules/cppjieba/deps/limonp/test/testdata/jieba.dict.1.utf8 +67 -0
  53. package/submodules/cppjieba/deps/limonp/test/testdata/jieba.dict.2.utf8 +64 -0
  54. package/submodules/cppjieba/deps/limonp/test/unittest/CMakeLists.txt +30 -0
  55. package/submodules/cppjieba/deps/limonp/test/unittest/TArgvContext.cpp +16 -0
  56. package/submodules/cppjieba/deps/limonp/test/unittest/TCastFloat.cpp +19 -0
  57. package/submodules/cppjieba/deps/limonp/test/unittest/TClosure.cpp +85 -0
  58. package/submodules/cppjieba/deps/limonp/test/unittest/TColorPrint.cpp +20 -0
  59. package/submodules/cppjieba/deps/limonp/test/unittest/TConfig.cpp +17 -0
  60. package/submodules/cppjieba/deps/limonp/test/unittest/TLocalVector.cpp +41 -0
  61. package/submodules/cppjieba/deps/limonp/test/unittest/TLogging.cpp +12 -0
  62. package/submodules/cppjieba/deps/limonp/test/unittest/TStdExtension.cpp +95 -0
  63. package/submodules/cppjieba/deps/limonp/test/unittest/TStringUtil.cpp +183 -0
  64. package/submodules/cppjieba/deps/limonp/test/unittest/gtest_main.cpp +39 -0
  65. package/submodules/cppjieba/dict/README.md +31 -0
  66. package/submodules/cppjieba/dict/hmm_model.utf8 +34 -0
  67. package/submodules/cppjieba/dict/idf.utf8 +258826 -0
  68. package/submodules/cppjieba/dict/jieba.dict.utf8 +348982 -0
  69. package/submodules/cppjieba/dict/pos_dict/char_state_tab.utf8 +6653 -0
  70. package/submodules/cppjieba/dict/pos_dict/prob_emit.utf8 +166 -0
  71. package/submodules/cppjieba/dict/pos_dict/prob_start.utf8 +259 -0
  72. package/submodules/cppjieba/dict/pos_dict/prob_trans.utf8 +5222 -0
  73. package/submodules/cppjieba/dict/stop_words.utf8 +1534 -0
  74. package/submodules/cppjieba/dict/user.dict.utf8 +4 -0
  75. package/submodules/cppjieba/include/cppjieba/DictTrie.hpp +381 -0
  76. package/submodules/cppjieba/include/cppjieba/FullSegment.hpp +93 -0
  77. package/submodules/cppjieba/include/cppjieba/HMMModel.hpp +129 -0
  78. package/submodules/cppjieba/include/cppjieba/HMMSegment.hpp +190 -0
  79. package/submodules/cppjieba/include/cppjieba/Jieba.hpp +169 -0
  80. package/submodules/cppjieba/include/cppjieba/KeywordExtractor.hpp +153 -0
  81. package/submodules/cppjieba/include/cppjieba/MPSegment.hpp +137 -0
  82. package/submodules/cppjieba/include/cppjieba/MixSegment.hpp +109 -0
  83. package/submodules/cppjieba/include/cppjieba/PosTagger.hpp +77 -0
  84. package/submodules/cppjieba/include/cppjieba/PreFilter.hpp +54 -0
  85. package/submodules/cppjieba/include/cppjieba/QuerySegment.hpp +89 -0
  86. package/submodules/cppjieba/include/cppjieba/SegmentBase.hpp +48 -0
  87. package/submodules/cppjieba/include/cppjieba/SegmentTagged.hpp +23 -0
  88. package/submodules/cppjieba/include/cppjieba/TextRankExtractor.hpp +190 -0
  89. package/submodules/cppjieba/include/cppjieba/Trie.hpp +200 -0
  90. package/submodules/cppjieba/include/cppjieba/Unicode.hpp +231 -0
  91. package/submodules/cppjieba/test/CMakeLists.txt +4 -0
  92. package/submodules/cppjieba/test/load_test.cpp +54 -0
  93. package/submodules/cppjieba/test/testdata/curl.res +1 -0
  94. package/submodules/cppjieba/test/testdata/extra_dict/jieba.dict.small.utf8 +109750 -0
  95. package/submodules/cppjieba/test/testdata/gbk_dict/hmm_model.gbk +34 -0
  96. package/submodules/cppjieba/test/testdata/gbk_dict/jieba.dict.gbk +348982 -0
  97. package/submodules/cppjieba/test/testdata/jieba.dict.0.1.utf8 +93 -0
  98. package/submodules/cppjieba/test/testdata/jieba.dict.0.utf8 +93 -0
  99. package/submodules/cppjieba/test/testdata/jieba.dict.1.utf8 +67 -0
  100. package/submodules/cppjieba/test/testdata/jieba.dict.2.utf8 +64 -0
  101. package/submodules/cppjieba/test/testdata/load_test.urls +2 -0
  102. package/submodules/cppjieba/test/testdata/review.100 +100 -0
  103. package/submodules/cppjieba/test/testdata/review.100.res +200 -0
  104. package/submodules/cppjieba/test/testdata/server.conf +19 -0
  105. package/submodules/cppjieba/test/testdata/testlines.gbk +9 -0
  106. package/submodules/cppjieba/test/testdata/testlines.utf8 +8 -0
  107. package/submodules/cppjieba/test/testdata/userdict.2.utf8 +1 -0
  108. package/submodules/cppjieba/test/testdata/userdict.english +2 -0
  109. package/submodules/cppjieba/test/testdata/userdict.utf8 +8 -0
  110. package/submodules/cppjieba/test/testdata/weicheng.utf8 +247 -0
  111. package/submodules/cppjieba/test/unittest/CMakeLists.txt +33 -0
  112. package/submodules/cppjieba/test/unittest/gtest_main.cpp +39 -0
  113. package/submodules/cppjieba/test/unittest/jieba_test.cpp +166 -0
  114. package/submodules/cppjieba/test/unittest/keyword_extractor_test.cpp +79 -0
  115. package/submodules/cppjieba/test/unittest/pos_tagger_test.cpp +41 -0
  116. package/submodules/cppjieba/test/unittest/pre_filter_test.cpp +43 -0
  117. package/submodules/cppjieba/test/unittest/segments_test.cpp +256 -0
  118. package/submodules/cppjieba/test/unittest/textrank_test.cpp +86 -0
  119. package/submodules/cppjieba/test/unittest/trie_test.cpp +177 -0
  120. package/submodules/cppjieba/test/unittest/unicode_test.cpp +43 -0
  121. package/test/debug_split +0 -0
  122. package/test/debug_split2 +0 -0
  123. package/test/debug_split3 +0 -0
  124. package/test/load_dict_test.js +14 -0
  125. package/test/missing_binding_test.js +42 -0
  126. package/test/test.js +366 -0
  127. package/test/testdata/userdict.utf8 +1 -0
  128. package/tsconfig.json +59 -0
  129. package/types/index.d.ts +30 -0
  130. package/typescript_demo.ts +38 -0
@@ -0,0 +1,200 @@
1
+ #ifndef CPPJIEBA_TRIE_HPP
2
+ #define CPPJIEBA_TRIE_HPP
3
+
4
+ #include <vector>
5
+ #include <queue>
6
+ #include "limonp/StdExtension.hpp"
7
+ #include "Unicode.hpp"
8
+
9
+ namespace cppjieba {
10
+
11
+ using namespace std;
12
+
13
+ const size_t MAX_WORD_LENGTH = 512;
14
+
15
+ struct DictUnit {
16
+ Unicode word;
17
+ double weight;
18
+ string tag;
19
+ }; // struct DictUnit
20
+
21
+ // for debugging
22
+ // inline ostream & operator << (ostream& os, const DictUnit& unit) {
23
+ // string s;
24
+ // s << unit.word;
25
+ // return os << StringFormat("%s %s %.3lf", s.c_str(), unit.tag.c_str(), unit.weight);
26
+ // }
27
+
28
+ struct Dag {
29
+ RuneStr runestr;
30
+ // [offset, nexts.first]
31
+ limonp::LocalVector<pair<size_t, const DictUnit*> > nexts;
32
+ const DictUnit * pInfo;
33
+ double weight;
34
+ size_t nextPos; // TODO
35
+ Dag():runestr(), pInfo(NULL), weight(0.0), nextPos(0) {
36
+ }
37
+ }; // struct Dag
38
+
39
+ typedef Rune TrieKey;
40
+
41
+ class TrieNode {
42
+ public :
43
+ TrieNode(): next(NULL), ptValue(NULL) {
44
+ }
45
+ public:
46
+ typedef unordered_map<TrieKey, TrieNode*> NextMap;
47
+ NextMap *next;
48
+ const DictUnit *ptValue;
49
+ };
50
+
51
+ class Trie {
52
+ public:
53
+ Trie(const vector<Unicode>& keys, const vector<const DictUnit*>& valuePointers)
54
+ : root_(new TrieNode) {
55
+ CreateTrie(keys, valuePointers);
56
+ }
57
+ ~Trie() {
58
+ DeleteNode(root_);
59
+ }
60
+
61
+ const DictUnit* Find(RuneStrArray::const_iterator begin, RuneStrArray::const_iterator end) const {
62
+ if (begin == end) {
63
+ return NULL;
64
+ }
65
+
66
+ const TrieNode* ptNode = root_;
67
+ TrieNode::NextMap::const_iterator citer;
68
+ for (RuneStrArray::const_iterator it = begin; it != end; it++) {
69
+ if (NULL == ptNode->next) {
70
+ return NULL;
71
+ }
72
+ citer = ptNode->next->find(it->rune);
73
+ if (ptNode->next->end() == citer) {
74
+ return NULL;
75
+ }
76
+ ptNode = citer->second;
77
+ }
78
+ return ptNode->ptValue;
79
+ }
80
+
81
+ void Find(RuneStrArray::const_iterator begin,
82
+ RuneStrArray::const_iterator end,
83
+ vector<struct Dag>&res,
84
+ size_t max_word_len = MAX_WORD_LENGTH) const {
85
+ assert(root_ != NULL);
86
+ res.resize(end - begin);
87
+
88
+ const TrieNode *ptNode = NULL;
89
+ TrieNode::NextMap::const_iterator citer;
90
+ for (size_t i = 0; i < size_t(end - begin); i++) {
91
+ res[i].runestr = *(begin + i);
92
+
93
+ if (root_->next != NULL && root_->next->end() != (citer = root_->next->find(res[i].runestr.rune))) {
94
+ ptNode = citer->second;
95
+ } else {
96
+ ptNode = NULL;
97
+ }
98
+ if (ptNode != NULL) {
99
+ res[i].nexts.push_back(pair<size_t, const DictUnit*>(i, ptNode->ptValue));
100
+ } else {
101
+ res[i].nexts.push_back(pair<size_t, const DictUnit*>(i, static_cast<const DictUnit*>(NULL)));
102
+ }
103
+
104
+ for (size_t j = i + 1; j < size_t(end - begin) && (j - i + 1) <= max_word_len; j++) {
105
+ if (ptNode == NULL || ptNode->next == NULL) {
106
+ break;
107
+ }
108
+ citer = ptNode->next->find((begin + j)->rune);
109
+ if (ptNode->next->end() == citer) {
110
+ break;
111
+ }
112
+ ptNode = citer->second;
113
+ if (NULL != ptNode->ptValue) {
114
+ res[i].nexts.push_back(pair<size_t, const DictUnit*>(j, ptNode->ptValue));
115
+ }
116
+ }
117
+ }
118
+ }
119
+
120
+ void InsertNode(const Unicode& key, const DictUnit* ptValue) {
121
+ if (key.begin() == key.end()) {
122
+ return;
123
+ }
124
+
125
+ TrieNode::NextMap::const_iterator kmIter;
126
+ TrieNode *ptNode = root_;
127
+ for (Unicode::const_iterator citer = key.begin(); citer != key.end(); ++citer) {
128
+ if (NULL == ptNode->next) {
129
+ ptNode->next = new TrieNode::NextMap;
130
+ }
131
+ kmIter = ptNode->next->find(*citer);
132
+ if (ptNode->next->end() == kmIter) {
133
+ TrieNode *nextNode = new TrieNode;
134
+
135
+ ptNode->next->insert(make_pair(*citer, nextNode));
136
+ ptNode = nextNode;
137
+ } else {
138
+ ptNode = kmIter->second;
139
+ }
140
+ }
141
+ assert(ptNode != NULL);
142
+ ptNode->ptValue = ptValue;
143
+ }
144
+ void DeleteNode(const Unicode& key, const DictUnit* ptValue) {
145
+ if (key.begin() == key.end()) {
146
+ return;
147
+ }
148
+ //定义一个NextMap迭代器
149
+ TrieNode::NextMap::const_iterator kmIter;
150
+ //定义一个指向root的TrieNode指针
151
+ TrieNode *ptNode = root_;
152
+ for (Unicode::const_iterator citer = key.begin(); citer != key.end(); ++citer) {
153
+ //链表不存在元素
154
+ if (NULL == ptNode->next) {
155
+ return;
156
+ }
157
+ kmIter = ptNode->next->find(*citer);
158
+ //如果map中不存在,跳出循环
159
+ if (ptNode->next->end() == kmIter) {
160
+ break;
161
+ }
162
+ //从unordered_map中擦除该项
163
+ ptNode->next->erase(*citer);
164
+ //删除该node
165
+ ptNode = kmIter->second;
166
+ delete ptNode;
167
+ break;
168
+ }
169
+ return;
170
+ }
171
+ private:
172
+ void CreateTrie(const vector<Unicode>& keys, const vector<const DictUnit*>& valuePointers) {
173
+ if (valuePointers.empty() || keys.empty()) {
174
+ return;
175
+ }
176
+ assert(keys.size() == valuePointers.size());
177
+
178
+ for (size_t i = 0; i < keys.size(); i++) {
179
+ InsertNode(keys[i], valuePointers[i]);
180
+ }
181
+ }
182
+
183
+ void DeleteNode(TrieNode* node) {
184
+ if (NULL == node) {
185
+ return;
186
+ }
187
+ if (NULL != node->next) {
188
+ for (TrieNode::NextMap::iterator it = node->next->begin(); it != node->next->end(); ++it) {
189
+ DeleteNode(it->second);
190
+ }
191
+ delete node->next;
192
+ }
193
+ delete node;
194
+ }
195
+
196
+ TrieNode* root_;
197
+ }; // class Trie
198
+ } // namespace cppjieba
199
+
200
+ #endif // CPPJIEBA_TRIE_HPP
@@ -0,0 +1,231 @@
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
+ // 将大写英文字母转换为小写,实现大小写不敏感匹配
96
+ if (rp.rune >= 'A' && rp.rune <= 'Z') {
97
+ rp.rune = rp.rune - 'A' + 'a';
98
+ }
99
+ rp.len = 1;
100
+ } else if ((uint8_t)str[0] <= 0xdf && 1 < len) {
101
+ // 110xxxxxx
102
+ // 5bit, total 5bit
103
+ rp.rune = (uint8_t)(str[0]) & 0x1f;
104
+
105
+ // 6bit, total 11bit
106
+ rp.rune <<= 6;
107
+ rp.rune |= (uint8_t)(str[1]) & 0x3f;
108
+ rp.len = 2;
109
+ } else if((uint8_t)str[0] <= 0xef && 2 < len) { // 1110xxxxxx
110
+ // 4bit, total 4bit
111
+ rp.rune = (uint8_t)(str[0]) & 0x0f;
112
+
113
+ // 6bit, total 10bit
114
+ rp.rune <<= 6;
115
+ rp.rune |= (uint8_t)(str[1]) & 0x3f;
116
+
117
+ // 6bit, total 16bit
118
+ rp.rune <<= 6;
119
+ rp.rune |= (uint8_t)(str[2]) & 0x3f;
120
+
121
+ rp.len = 3;
122
+ } else if((uint8_t)str[0] <= 0xf7 && 3 < len) { // 11110xxxx
123
+ // 3bit, total 3bit
124
+ rp.rune = (uint8_t)(str[0]) & 0x07;
125
+
126
+ // 6bit, total 9bit
127
+ rp.rune <<= 6;
128
+ rp.rune |= (uint8_t)(str[1]) & 0x3f;
129
+
130
+ // 6bit, total 15bit
131
+ rp.rune <<= 6;
132
+ rp.rune |= (uint8_t)(str[2]) & 0x3f;
133
+
134
+ // 6bit, total 21bit
135
+ rp.rune <<= 6;
136
+ rp.rune |= (uint8_t)(str[3]) & 0x3f;
137
+
138
+ rp.len = 4;
139
+ } else {
140
+ rp.rune = 0;
141
+ rp.len = 0;
142
+ }
143
+ return rp;
144
+ }
145
+
146
+ inline bool DecodeUTF8RunesInString(const char* s, size_t len, RuneStrArray& runes) {
147
+ runes.clear();
148
+ runes.reserve(len / 2);
149
+ for (uint32_t i = 0, j = 0; i < len;) {
150
+ RuneStrLite rp = DecodeUTF8ToRune(s + i, len - i);
151
+ if (rp.len == 0) {
152
+ runes.clear();
153
+ return false;
154
+ }
155
+ RuneStr x(rp.rune, i, rp.len, j, 1);
156
+ runes.push_back(x);
157
+ i += rp.len;
158
+ ++j;
159
+ }
160
+ return true;
161
+ }
162
+
163
+ inline bool DecodeUTF8RunesInString(const string& s, RuneStrArray& runes) {
164
+ return DecodeUTF8RunesInString(s.c_str(), s.size(), runes);
165
+ }
166
+
167
+ inline bool DecodeUTF8RunesInString(const char* s, size_t len, Unicode& unicode) {
168
+ unicode.clear();
169
+ RuneStrArray runes;
170
+ if (!DecodeUTF8RunesInString(s, len, runes)) {
171
+ return false;
172
+ }
173
+ unicode.reserve(runes.size());
174
+ for (size_t i = 0; i < runes.size(); i++) {
175
+ unicode.push_back(runes[i].rune);
176
+ }
177
+ return true;
178
+ }
179
+
180
+ inline bool IsSingleWord(const string& str) {
181
+ RuneStrLite rp = DecodeUTF8ToRune(str.c_str(), str.size());
182
+ return rp.len == str.size();
183
+ }
184
+
185
+ inline bool DecodeUTF8RunesInString(const string& s, Unicode& unicode) {
186
+ return DecodeUTF8RunesInString(s.c_str(), s.size(), unicode);
187
+ }
188
+
189
+ inline Unicode DecodeUTF8RunesInString(const string& s) {
190
+ Unicode result;
191
+ DecodeUTF8RunesInString(s, result);
192
+ return result;
193
+ }
194
+
195
+
196
+ // [left, right]
197
+ inline Word GetWordFromRunes(const string& s, RuneStrArray::const_iterator left, RuneStrArray::const_iterator right) {
198
+ assert(right->offset >= left->offset);
199
+ uint32_t len = right->offset - left->offset + right->len;
200
+ uint32_t unicode_length = right->unicode_offset - left->unicode_offset + right->unicode_length;
201
+ return Word(s.substr(left->offset, len), left->offset, left->unicode_offset, unicode_length);
202
+ }
203
+
204
+ inline string GetStringFromRunes(const string& s, RuneStrArray::const_iterator left, RuneStrArray::const_iterator right) {
205
+ assert(right->offset >= left->offset);
206
+ uint32_t len = right->offset - left->offset + right->len;
207
+ return s.substr(left->offset, len);
208
+ }
209
+
210
+ inline void GetWordsFromWordRanges(const string& s, const vector<WordRange>& wrs, vector<Word>& words) {
211
+ for (size_t i = 0; i < wrs.size(); i++) {
212
+ words.push_back(GetWordFromRunes(s, wrs[i].left, wrs[i].right));
213
+ }
214
+ }
215
+
216
+ inline vector<Word> GetWordsFromWordRanges(const string& s, const vector<WordRange>& wrs) {
217
+ vector<Word> result;
218
+ GetWordsFromWordRanges(s, wrs, result);
219
+ return result;
220
+ }
221
+
222
+ inline void GetStringsFromWords(const vector<Word>& words, vector<string>& strs) {
223
+ strs.resize(words.size());
224
+ for (size_t i = 0; i < words.size(); ++i) {
225
+ strs[i] = words[i].word;
226
+ }
227
+ }
228
+
229
+ } // namespace cppjieba
230
+
231
+ #endif // CPPJIEBA_UNICODE_H
@@ -0,0 +1,4 @@
1
+ SET(EXECUTABLE_OUTPUT_PATH ${PROJECT_BINARY_DIR})
2
+
3
+ ADD_EXECUTABLE(load_test load_test.cpp)
4
+ ADD_SUBDIRECTORY(unittest)
@@ -0,0 +1,54 @@
1
+ #include <iostream>
2
+ #include <ctime>
3
+ #include <fstream>
4
+ #include "cppjieba/MPSegment.hpp"
5
+ #include "cppjieba/HMMSegment.hpp"
6
+ #include "cppjieba/MixSegment.hpp"
7
+ #include "cppjieba/KeywordExtractor.hpp"
8
+ #include "limonp/Colors.hpp"
9
+
10
+ using namespace cppjieba;
11
+
12
+ void Cut(size_t times = 50) {
13
+ MixSegment seg("../dict/jieba.dict.utf8", "../dict/hmm_model.utf8");
14
+ vector<string> res;
15
+ string doc;
16
+ ifstream ifs("../test/testdata/weicheng.utf8");
17
+ assert(ifs);
18
+ doc << ifs;
19
+ long beginTime = clock();
20
+ for (size_t i = 0; i < times; i ++) {
21
+ printf("process [%3.0lf %%]\r", 100.0*(i+1)/times);
22
+ fflush(stdout);
23
+ res.clear();
24
+ seg.Cut(doc, res);
25
+ }
26
+ printf("\n");
27
+ long endTime = clock();
28
+ ColorPrintln(GREEN, "Cut: [%.3lf seconds]time consumed.", double(endTime - beginTime)/CLOCKS_PER_SEC);
29
+ }
30
+
31
+ void Extract(size_t times = 400) {
32
+ KeywordExtractor Extractor("../dict/jieba.dict.utf8", "../dict/hmm_model.utf8", "../dict/idf.utf8", "../dict/stop_words.utf8");
33
+ vector<string> words;
34
+ string doc;
35
+ ifstream ifs("../test/testdata/review.100");
36
+ assert(ifs);
37
+ doc << ifs;
38
+ long beginTime = clock();
39
+ for (size_t i = 0; i < times; i ++) {
40
+ printf("process [%3.0lf %%]\r", 100.0*(i+1)/times);
41
+ fflush(stdout);
42
+ words.clear();
43
+ Extractor.Extract(doc, words, 5);
44
+ }
45
+ printf("\n");
46
+ long endTime = clock();
47
+ ColorPrintln(GREEN, "Extract: [%.3lf seconds]time consumed.", double(endTime - beginTime)/CLOCKS_PER_SEC);
48
+ }
49
+
50
+ int main(int argc, char ** argv) {
51
+ Cut();
52
+ Extract();
53
+ return EXIT_SUCCESS;
54
+ }
@@ -0,0 +1 @@
1
+ ["南京市", "长江大桥"]