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,190 @@
1
+ #ifndef CPPJIBEA_HMMSEGMENT_H
2
+ #define CPPJIBEA_HMMSEGMENT_H
3
+
4
+ #include <iostream>
5
+ #include <fstream>
6
+ #include <memory.h>
7
+ #include <cassert>
8
+ #include "HMMModel.hpp"
9
+ #include "SegmentBase.hpp"
10
+
11
+ namespace cppjieba {
12
+ class HMMSegment: public SegmentBase {
13
+ public:
14
+ HMMSegment(const string& filePath)
15
+ : model_(new HMMModel(filePath)), isNeedDestroy_(true) {
16
+ }
17
+ HMMSegment(const HMMModel* model)
18
+ : model_(model), isNeedDestroy_(false) {
19
+ }
20
+ ~HMMSegment() {
21
+ if (isNeedDestroy_) {
22
+ delete model_;
23
+ }
24
+ }
25
+
26
+ void Cut(const string& sentence,
27
+ vector<string>& words) const {
28
+ vector<Word> tmp;
29
+ Cut(sentence, tmp);
30
+ GetStringsFromWords(tmp, words);
31
+ }
32
+ void Cut(const string& sentence,
33
+ vector<Word>& words) const {
34
+ PreFilter pre_filter(symbols_, sentence);
35
+ PreFilter::Range range;
36
+ vector<WordRange> wrs;
37
+ wrs.reserve(sentence.size()/2);
38
+ while (pre_filter.HasNext()) {
39
+ range = pre_filter.Next();
40
+ Cut(range.begin, range.end, wrs);
41
+ }
42
+ words.clear();
43
+ words.reserve(wrs.size());
44
+ GetWordsFromWordRanges(sentence, wrs, words);
45
+ }
46
+ void Cut(RuneStrArray::const_iterator begin, RuneStrArray::const_iterator end, vector<WordRange>& res) const {
47
+ RuneStrArray::const_iterator left = begin;
48
+ RuneStrArray::const_iterator right = begin;
49
+ while (right != end) {
50
+ if (right->rune < 0x80) {
51
+ if (left != right) {
52
+ InternalCut(left, right, res);
53
+ }
54
+ left = right;
55
+ do {
56
+ right = SequentialLetterRule(left, end);
57
+ if (right != left) {
58
+ break;
59
+ }
60
+ right = NumbersRule(left, end);
61
+ if (right != left) {
62
+ break;
63
+ }
64
+ right ++;
65
+ } while (false);
66
+ WordRange wr(left, right - 1);
67
+ res.push_back(wr);
68
+ left = right;
69
+ } else {
70
+ right++;
71
+ }
72
+ }
73
+ if (left != right) {
74
+ InternalCut(left, right, res);
75
+ }
76
+ }
77
+ private:
78
+ // sequential letters rule
79
+ RuneStrArray::const_iterator SequentialLetterRule(RuneStrArray::const_iterator begin, RuneStrArray::const_iterator end) const {
80
+ Rune x = begin->rune;
81
+ if (('a' <= x && x <= 'z') || ('A' <= x && x <= 'Z')) {
82
+ begin ++;
83
+ } else {
84
+ return begin;
85
+ }
86
+ while (begin != end) {
87
+ x = begin->rune;
88
+ if (('a' <= x && x <= 'z') || ('A' <= x && x <= 'Z') || ('0' <= x && x <= '9')) {
89
+ begin ++;
90
+ } else {
91
+ break;
92
+ }
93
+ }
94
+ return begin;
95
+ }
96
+ //
97
+ RuneStrArray::const_iterator NumbersRule(RuneStrArray::const_iterator begin, RuneStrArray::const_iterator end) const {
98
+ Rune x = begin->rune;
99
+ if ('0' <= x && x <= '9') {
100
+ begin ++;
101
+ } else {
102
+ return begin;
103
+ }
104
+ while (begin != end) {
105
+ x = begin->rune;
106
+ if ( ('0' <= x && x <= '9') || x == '.') {
107
+ begin++;
108
+ } else {
109
+ break;
110
+ }
111
+ }
112
+ return begin;
113
+ }
114
+ void InternalCut(RuneStrArray::const_iterator begin, RuneStrArray::const_iterator end, vector<WordRange>& res) const {
115
+ vector<size_t> status;
116
+ Viterbi(begin, end, status);
117
+
118
+ RuneStrArray::const_iterator left = begin;
119
+ RuneStrArray::const_iterator right;
120
+ for (size_t i = 0; i < status.size(); i++) {
121
+ if (status[i] % 2) { //if (HMMModel::E == status[i] || HMMModel::S == status[i])
122
+ right = begin + i + 1;
123
+ WordRange wr(left, right - 1);
124
+ res.push_back(wr);
125
+ left = right;
126
+ }
127
+ }
128
+ }
129
+
130
+ void Viterbi(RuneStrArray::const_iterator begin,
131
+ RuneStrArray::const_iterator end,
132
+ vector<size_t>& status) const {
133
+ size_t Y = HMMModel::STATUS_SUM;
134
+ size_t X = end - begin;
135
+
136
+ size_t XYSize = X * Y;
137
+ size_t now, old, stat;
138
+ double tmp, endE, endS;
139
+
140
+ vector<int> path(XYSize);
141
+ vector<double> weight(XYSize);
142
+
143
+ //start
144
+ for (size_t y = 0; y < Y; y++) {
145
+ weight[0 + y * X] = model_->startProb[y] + model_->GetEmitProb(model_->emitProbVec[y], begin->rune, MIN_DOUBLE);
146
+ path[0 + y * X] = -1;
147
+ }
148
+
149
+ double emitProb;
150
+
151
+ for (size_t x = 1; x < X; x++) {
152
+ for (size_t y = 0; y < Y; y++) {
153
+ now = x + y*X;
154
+ weight[now] = MIN_DOUBLE;
155
+ path[now] = HMMModel::E; // warning
156
+ emitProb = model_->GetEmitProb(model_->emitProbVec[y], (begin+x)->rune, MIN_DOUBLE);
157
+ for (size_t preY = 0; preY < Y; preY++) {
158
+ old = x - 1 + preY * X;
159
+ tmp = weight[old] + model_->transProb[preY][y] + emitProb;
160
+ if (tmp > weight[now]) {
161
+ weight[now] = tmp;
162
+ path[now] = preY;
163
+ }
164
+ }
165
+ }
166
+ }
167
+
168
+ endE = weight[X-1+HMMModel::E*X];
169
+ endS = weight[X-1+HMMModel::S*X];
170
+ stat = 0;
171
+ if (endE >= endS) {
172
+ stat = HMMModel::E;
173
+ } else {
174
+ stat = HMMModel::S;
175
+ }
176
+
177
+ status.resize(X);
178
+ for (int x = X -1 ; x >= 0; x--) {
179
+ status[x] = stat;
180
+ stat = path[x + stat*X];
181
+ }
182
+ }
183
+
184
+ const HMMModel* model_;
185
+ bool isNeedDestroy_;
186
+ }; // class HMMSegment
187
+
188
+ } // namespace cppjieba
189
+
190
+ #endif
@@ -0,0 +1,169 @@
1
+ #ifndef CPPJIEAB_JIEBA_H
2
+ #define CPPJIEAB_JIEBA_H
3
+
4
+ #include "QuerySegment.hpp"
5
+ #include "KeywordExtractor.hpp"
6
+
7
+ namespace cppjieba {
8
+
9
+ class Jieba {
10
+ public:
11
+ Jieba(const string& dict_path = "",
12
+ const string& model_path = "",
13
+ const string& user_dict_path = "",
14
+ const string& idf_path = "",
15
+ const string& stop_word_path = "")
16
+ : dict_trie_(getPath(dict_path, "jieba.dict.utf8"), getPath(user_dict_path, "user.dict.utf8")),
17
+ model_(getPath(model_path, "hmm_model.utf8")),
18
+ mp_seg_(&dict_trie_),
19
+ hmm_seg_(&model_),
20
+ mix_seg_(&dict_trie_, &model_),
21
+ full_seg_(&dict_trie_),
22
+ query_seg_(&dict_trie_, &model_),
23
+ extractor(&dict_trie_, &model_,
24
+ getPath(idf_path, "idf.utf8"),
25
+ getPath(stop_word_path, "stop_words.utf8")) {
26
+ }
27
+ ~Jieba() {
28
+ }
29
+
30
+ struct LocWord {
31
+ string word;
32
+ size_t begin;
33
+ size_t end;
34
+ }; // struct LocWord
35
+
36
+ void Cut(const string& sentence, vector<string>& words, bool hmm = true) const {
37
+ mix_seg_.Cut(sentence, words, hmm);
38
+ }
39
+ void Cut(const string& sentence, vector<Word>& words, bool hmm = true) const {
40
+ mix_seg_.Cut(sentence, words, hmm);
41
+ }
42
+ void CutAll(const string& sentence, vector<string>& words) const {
43
+ full_seg_.Cut(sentence, words);
44
+ }
45
+ void CutAll(const string& sentence, vector<Word>& words) const {
46
+ full_seg_.Cut(sentence, words);
47
+ }
48
+ void CutForSearch(const string& sentence, vector<string>& words, bool hmm = true) const {
49
+ query_seg_.Cut(sentence, words, hmm);
50
+ }
51
+ void CutForSearch(const string& sentence, vector<Word>& words, bool hmm = true) const {
52
+ query_seg_.Cut(sentence, words, hmm);
53
+ }
54
+ void CutHMM(const string& sentence, vector<string>& words) const {
55
+ hmm_seg_.Cut(sentence, words);
56
+ }
57
+ void CutHMM(const string& sentence, vector<Word>& words) const {
58
+ hmm_seg_.Cut(sentence, words);
59
+ }
60
+ void CutSmall(const string& sentence, vector<string>& words, size_t max_word_len) const {
61
+ mp_seg_.Cut(sentence, words, max_word_len);
62
+ }
63
+ void CutSmall(const string& sentence, vector<Word>& words, size_t max_word_len) const {
64
+ mp_seg_.Cut(sentence, words, max_word_len);
65
+ }
66
+
67
+ void Tag(const string& sentence, vector<pair<string, string> >& words) const {
68
+ mix_seg_.Tag(sentence, words);
69
+ }
70
+ string LookupTag(const string &str) const {
71
+ return mix_seg_.LookupTag(str);
72
+ }
73
+ bool InsertUserWord(const string& word, const string& tag = UNKNOWN_TAG) {
74
+ return dict_trie_.InsertUserWord(word, tag);
75
+ }
76
+
77
+ bool InsertUserWord(const string& word,int freq, const string& tag = UNKNOWN_TAG) {
78
+ return dict_trie_.InsertUserWord(word,freq, tag);
79
+ }
80
+
81
+ bool DeleteUserWord(const string& word, const string& tag = UNKNOWN_TAG) {
82
+ return dict_trie_.DeleteUserWord(word, tag);
83
+ }
84
+
85
+ bool Find(const string& word)
86
+ {
87
+ return dict_trie_.Find(word);
88
+ }
89
+
90
+ void ResetSeparators(const string& s) {
91
+ //TODO
92
+ mp_seg_.ResetSeparators(s);
93
+ hmm_seg_.ResetSeparators(s);
94
+ mix_seg_.ResetSeparators(s);
95
+ full_seg_.ResetSeparators(s);
96
+ query_seg_.ResetSeparators(s);
97
+ }
98
+
99
+ const DictTrie* GetDictTrie() const {
100
+ return &dict_trie_;
101
+ }
102
+
103
+ const HMMModel* GetHMMModel() const {
104
+ return &model_;
105
+ }
106
+
107
+ void LoadUserDict(const vector<string>& buf) {
108
+ dict_trie_.LoadUserDict(buf);
109
+ }
110
+
111
+ void LoadUserDict(const set<string>& buf) {
112
+ dict_trie_.LoadUserDict(buf);
113
+ }
114
+
115
+ void LoadUserDict(const string& path) {
116
+ dict_trie_.LoadUserDict(path);
117
+ }
118
+
119
+ private:
120
+ static string pathJoin(const string& dir, const string& filename) {
121
+ if (dir.empty()) {
122
+ return filename;
123
+ }
124
+
125
+ char last_char = dir[dir.length() - 1];
126
+ if (last_char == '/' || last_char == '\\') {
127
+ return dir + filename;
128
+ } else {
129
+ #ifdef _WIN32
130
+ return dir + '\\' + filename;
131
+ #else
132
+ return dir + '/' + filename;
133
+ #endif
134
+ }
135
+ }
136
+
137
+ static string getCurrentDirectory() {
138
+ string path(__FILE__);
139
+ size_t pos = path.find_last_of("/\\");
140
+ return (pos == string::npos) ? "" : path.substr(0, pos);
141
+ }
142
+
143
+ static string getPath(const string& path, const string& default_file) {
144
+ if (path.empty()) {
145
+ string current_dir = getCurrentDirectory();
146
+ string parent_dir = current_dir.substr(0, current_dir.find_last_of("/\\"));
147
+ string grandparent_dir = parent_dir.substr(0, parent_dir.find_last_of("/\\"));
148
+ return pathJoin(pathJoin(grandparent_dir, "dict"), default_file);
149
+ }
150
+ return path;
151
+ }
152
+
153
+ DictTrie dict_trie_;
154
+ HMMModel model_;
155
+
156
+ // They share the same dict trie and model
157
+ MPSegment mp_seg_;
158
+ HMMSegment hmm_seg_;
159
+ MixSegment mix_seg_;
160
+ FullSegment full_seg_;
161
+ QuerySegment query_seg_;
162
+
163
+ public:
164
+ KeywordExtractor extractor;
165
+ }; // class Jieba
166
+
167
+ } // namespace cppjieba
168
+
169
+ #endif // CPPJIEAB_JIEBA_H
@@ -0,0 +1,153 @@
1
+ #ifndef CPPJIEBA_KEYWORD_EXTRACTOR_H
2
+ #define CPPJIEBA_KEYWORD_EXTRACTOR_H
3
+
4
+ #include <cmath>
5
+ #include <set>
6
+ #include "MixSegment.hpp"
7
+
8
+ namespace cppjieba {
9
+
10
+ using namespace limonp;
11
+ using namespace std;
12
+
13
+ /*utf8*/
14
+ class KeywordExtractor {
15
+ public:
16
+ struct Word {
17
+ string word;
18
+ vector<size_t> offsets;
19
+ double weight;
20
+ }; // struct Word
21
+
22
+ KeywordExtractor(const string& dictPath,
23
+ const string& hmmFilePath,
24
+ const string& idfPath,
25
+ const string& stopWordPath,
26
+ const string& userDict = "")
27
+ : segment_(dictPath, hmmFilePath, userDict) {
28
+ LoadIdfDict(idfPath);
29
+ LoadStopWordDict(stopWordPath);
30
+ }
31
+ KeywordExtractor(const DictTrie* dictTrie,
32
+ const HMMModel* model,
33
+ const string& idfPath,
34
+ const string& stopWordPath)
35
+ : segment_(dictTrie, model) {
36
+ LoadIdfDict(idfPath);
37
+ LoadStopWordDict(stopWordPath);
38
+ }
39
+ ~KeywordExtractor() {
40
+ }
41
+
42
+ void Extract(const string& sentence, vector<string>& keywords, size_t topN) const {
43
+ vector<Word> topWords;
44
+ Extract(sentence, topWords, topN);
45
+ for (size_t i = 0; i < topWords.size(); i++) {
46
+ keywords.push_back(topWords[i].word);
47
+ }
48
+ }
49
+
50
+ void Extract(const string& sentence, vector<pair<string, double> >& keywords, size_t topN) const {
51
+ vector<Word> topWords;
52
+ Extract(sentence, topWords, topN);
53
+ for (size_t i = 0; i < topWords.size(); i++) {
54
+ keywords.push_back(pair<string, double>(topWords[i].word, topWords[i].weight));
55
+ }
56
+ }
57
+
58
+ void Extract(const string& sentence, vector<Word>& keywords, size_t topN) const {
59
+ vector<string> words;
60
+ segment_.Cut(sentence, words);
61
+
62
+ map<string, Word> wordmap;
63
+ size_t offset = 0;
64
+ for (size_t i = 0; i < words.size(); ++i) {
65
+ size_t t = offset;
66
+ offset += words[i].size();
67
+ if (IsSingleWord(words[i]) || stopWords_.find(words[i]) != stopWords_.end()) {
68
+ continue;
69
+ }
70
+ wordmap[words[i]].offsets.push_back(t);
71
+ wordmap[words[i]].weight += 1.0;
72
+ }
73
+ if (offset != sentence.size()) {
74
+ XLOG(ERROR) << "words illegal";
75
+ return;
76
+ }
77
+
78
+ keywords.clear();
79
+ keywords.reserve(wordmap.size());
80
+ for (map<string, Word>::iterator itr = wordmap.begin(); itr != wordmap.end(); ++itr) {
81
+ unordered_map<string, double>::const_iterator cit = idfMap_.find(itr->first);
82
+ if (cit != idfMap_.end()) {
83
+ itr->second.weight *= cit->second;
84
+ } else {
85
+ itr->second.weight *= idfAverage_;
86
+ }
87
+ itr->second.word = itr->first;
88
+ keywords.push_back(itr->second);
89
+ }
90
+ topN = min(topN, keywords.size());
91
+ partial_sort(keywords.begin(), keywords.begin() + topN, keywords.end(), Compare);
92
+ keywords.resize(topN);
93
+ }
94
+ private:
95
+ void LoadIdfDict(const string& idfPath) {
96
+ ifstream ifs(idfPath.c_str());
97
+ XCHECK(ifs.is_open()) << "open " << idfPath << " failed";
98
+ string line ;
99
+ vector<string> buf;
100
+ double idf = 0.0;
101
+ double idfSum = 0.0;
102
+ size_t lineno = 0;
103
+ for (; getline(ifs, line); lineno++) {
104
+ buf.clear();
105
+ if (line.empty()) {
106
+ XLOG(ERROR) << "lineno: " << lineno << " empty. skipped.";
107
+ continue;
108
+ }
109
+ Split(line, buf, " ");
110
+ if (buf.size() != 2) {
111
+ XLOG(ERROR) << "line: " << line << ", lineno: " << lineno << " empty. skipped.";
112
+ continue;
113
+ }
114
+ idf = atof(buf[1].c_str());
115
+ idfMap_[buf[0]] = idf;
116
+ idfSum += idf;
117
+
118
+ }
119
+
120
+ assert(lineno);
121
+ idfAverage_ = idfSum / lineno;
122
+ assert(idfAverage_ > 0.0);
123
+ }
124
+ void LoadStopWordDict(const string& filePath) {
125
+ ifstream ifs(filePath.c_str());
126
+ XCHECK(ifs.is_open()) << "open " << filePath << " failed";
127
+ string line ;
128
+ while (getline(ifs, line)) {
129
+ stopWords_.insert(line);
130
+ }
131
+ assert(stopWords_.size());
132
+ }
133
+
134
+ static bool Compare(const Word& lhs, const Word& rhs) {
135
+ return lhs.weight > rhs.weight;
136
+ }
137
+
138
+ MixSegment segment_;
139
+ unordered_map<string, double> idfMap_;
140
+ double idfAverage_;
141
+
142
+ unordered_set<string> stopWords_;
143
+ }; // class KeywordExtractor
144
+
145
+ inline ostream& operator << (ostream& os, const KeywordExtractor::Word& word) {
146
+ return os << "{\"word\": \"" << word.word << "\", \"offset\": " << word.offsets << ", \"weight\": " << word.weight << "}";
147
+ }
148
+
149
+ } // namespace cppjieba
150
+
151
+ #endif
152
+
153
+
@@ -0,0 +1,137 @@
1
+ #ifndef CPPJIEBA_MPSEGMENT_H
2
+ #define CPPJIEBA_MPSEGMENT_H
3
+
4
+ #include <algorithm>
5
+ #include <set>
6
+ #include <cassert>
7
+ #include "limonp/Logging.hpp"
8
+ #include "DictTrie.hpp"
9
+ #include "SegmentTagged.hpp"
10
+ #include "PosTagger.hpp"
11
+
12
+ namespace cppjieba {
13
+
14
+ class MPSegment: public SegmentTagged {
15
+ public:
16
+ MPSegment(const string& dictPath, const string& userDictPath = "")
17
+ : dictTrie_(new DictTrie(dictPath, userDictPath)), isNeedDestroy_(true) {
18
+ }
19
+ MPSegment(const DictTrie* dictTrie)
20
+ : dictTrie_(dictTrie), isNeedDestroy_(false) {
21
+ assert(dictTrie_);
22
+ }
23
+ ~MPSegment() {
24
+ if (isNeedDestroy_) {
25
+ delete dictTrie_;
26
+ }
27
+ }
28
+
29
+ void Cut(const string& sentence, vector<string>& words) const {
30
+ Cut(sentence, words, MAX_WORD_LENGTH);
31
+ }
32
+
33
+ void Cut(const string& sentence,
34
+ vector<string>& words,
35
+ size_t max_word_len) const {
36
+ vector<Word> tmp;
37
+ Cut(sentence, tmp, max_word_len);
38
+ GetStringsFromWords(tmp, words);
39
+ }
40
+ void Cut(const string& sentence,
41
+ vector<Word>& words,
42
+ size_t max_word_len = MAX_WORD_LENGTH) const {
43
+ PreFilter pre_filter(symbols_, sentence);
44
+ PreFilter::Range range;
45
+ vector<WordRange> wrs;
46
+ wrs.reserve(sentence.size()/2);
47
+ while (pre_filter.HasNext()) {
48
+ range = pre_filter.Next();
49
+ Cut(range.begin, range.end, wrs, max_word_len);
50
+ }
51
+ words.clear();
52
+ words.reserve(wrs.size());
53
+ GetWordsFromWordRanges(sentence, wrs, words);
54
+ }
55
+ void Cut(RuneStrArray::const_iterator begin,
56
+ RuneStrArray::const_iterator end,
57
+ vector<WordRange>& words,
58
+ size_t max_word_len = MAX_WORD_LENGTH) const {
59
+ vector<Dag> dags;
60
+ dictTrie_->Find(begin,
61
+ end,
62
+ dags,
63
+ max_word_len);
64
+ CalcDP(dags);
65
+ CutByDag(begin, end, dags, words);
66
+ }
67
+
68
+ const DictTrie* GetDictTrie() const {
69
+ return dictTrie_;
70
+ }
71
+
72
+ bool Tag(const string& src, vector<pair<string, string> >& res) const {
73
+ return tagger_.Tag(src, res, *this);
74
+ }
75
+
76
+ bool IsUserDictSingleChineseWord(const Rune& value) const {
77
+ return dictTrie_->IsUserDictSingleChineseWord(value);
78
+ }
79
+ private:
80
+ void CalcDP(vector<Dag>& dags) const {
81
+ size_t nextPos;
82
+ const DictUnit* p;
83
+ double val;
84
+
85
+ for (vector<Dag>::reverse_iterator rit = dags.rbegin(); rit != dags.rend(); rit++) {
86
+ rit->pInfo = NULL;
87
+ rit->weight = MIN_DOUBLE;
88
+ assert(!rit->nexts.empty());
89
+ for (LocalVector<pair<size_t, const DictUnit*> >::const_iterator it = rit->nexts.begin(); it != rit->nexts.end(); it++) {
90
+ nextPos = it->first;
91
+ p = it->second;
92
+ val = 0.0;
93
+ if (nextPos + 1 < dags.size()) {
94
+ val += dags[nextPos + 1].weight;
95
+ }
96
+
97
+ if (p) {
98
+ val += p->weight;
99
+ } else {
100
+ val += dictTrie_->GetMinWeight();
101
+ }
102
+ if (val > rit->weight) {
103
+ rit->pInfo = p;
104
+ rit->weight = val;
105
+ }
106
+ }
107
+ }
108
+ }
109
+ void CutByDag(RuneStrArray::const_iterator begin,
110
+ RuneStrArray::const_iterator end,
111
+ const vector<Dag>& dags,
112
+ vector<WordRange>& words) const {
113
+ size_t i = 0;
114
+ while (i < dags.size()) {
115
+ const DictUnit* p = dags[i].pInfo;
116
+ if (p) {
117
+ assert(p->word.size() >= 1);
118
+ WordRange wr(begin + i, begin + i + p->word.size() - 1);
119
+ words.push_back(wr);
120
+ i += p->word.size();
121
+ } else { //single chinese word
122
+ WordRange wr(begin + i, begin + i);
123
+ words.push_back(wr);
124
+ i++;
125
+ }
126
+ }
127
+ }
128
+
129
+ const DictTrie* dictTrie_;
130
+ bool isNeedDestroy_;
131
+ PosTagger tagger_;
132
+
133
+ }; // class MPSegment
134
+
135
+ } // namespace cppjieba
136
+
137
+ #endif