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,109 @@
1
+ #ifndef CPPJIEBA_MIXSEGMENT_H
2
+ #define CPPJIEBA_MIXSEGMENT_H
3
+
4
+ #include <cassert>
5
+ #include "MPSegment.hpp"
6
+ #include "HMMSegment.hpp"
7
+ #include "limonp/StringUtil.hpp"
8
+ #include "PosTagger.hpp"
9
+
10
+ namespace cppjieba {
11
+ class MixSegment: public SegmentTagged {
12
+ public:
13
+ MixSegment(const string& mpSegDict, const string& hmmSegDict,
14
+ const string& userDict = "")
15
+ : mpSeg_(mpSegDict, userDict),
16
+ hmmSeg_(hmmSegDict) {
17
+ }
18
+ MixSegment(const DictTrie* dictTrie, const HMMModel* model)
19
+ : mpSeg_(dictTrie), hmmSeg_(model) {
20
+ }
21
+ ~MixSegment() {
22
+ }
23
+
24
+ void Cut(const string& sentence, vector<string>& words) const {
25
+ Cut(sentence, words, true);
26
+ }
27
+ void Cut(const string& sentence, vector<string>& words, bool hmm) const {
28
+ vector<Word> tmp;
29
+ Cut(sentence, tmp, hmm);
30
+ GetStringsFromWords(tmp, words);
31
+ }
32
+ void Cut(const string& sentence, vector<Word>& words, bool hmm = true) const {
33
+ PreFilter pre_filter(symbols_, sentence);
34
+ PreFilter::Range range;
35
+ vector<WordRange> wrs;
36
+ wrs.reserve(sentence.size() / 2);
37
+ while (pre_filter.HasNext()) {
38
+ range = pre_filter.Next();
39
+ Cut(range.begin, range.end, wrs, hmm);
40
+ }
41
+ words.clear();
42
+ words.reserve(wrs.size());
43
+ GetWordsFromWordRanges(sentence, wrs, words);
44
+ }
45
+
46
+ void Cut(RuneStrArray::const_iterator begin, RuneStrArray::const_iterator end, vector<WordRange>& res, bool hmm) const {
47
+ if (!hmm) {
48
+ mpSeg_.Cut(begin, end, res);
49
+ return;
50
+ }
51
+ vector<WordRange> words;
52
+ assert(end >= begin);
53
+ words.reserve(end - begin);
54
+ mpSeg_.Cut(begin, end, words);
55
+
56
+ vector<WordRange> hmmRes;
57
+ hmmRes.reserve(end - begin);
58
+ for (size_t i = 0; i < words.size(); i++) {
59
+ //if mp Get a word, it's ok, put it into result
60
+ if (words[i].left != words[i].right || (words[i].left == words[i].right && mpSeg_.IsUserDictSingleChineseWord(words[i].left->rune))) {
61
+ res.push_back(words[i]);
62
+ continue;
63
+ }
64
+
65
+ // if mp Get a single one and it is not in userdict, collect it in sequence
66
+ size_t j = i;
67
+ while (j < words.size() && words[j].left == words[j].right && !mpSeg_.IsUserDictSingleChineseWord(words[j].left->rune)) {
68
+ j++;
69
+ }
70
+
71
+ // Cut the sequence with hmm
72
+ assert(j - 1 >= i);
73
+ // TODO
74
+ hmmSeg_.Cut(words[i].left, words[j - 1].left + 1, hmmRes);
75
+ //put hmm result to result
76
+ for (size_t k = 0; k < hmmRes.size(); k++) {
77
+ res.push_back(hmmRes[k]);
78
+ }
79
+
80
+ //clear tmp vars
81
+ hmmRes.clear();
82
+
83
+ //let i jump over this piece
84
+ i = j - 1;
85
+ }
86
+ }
87
+
88
+ const DictTrie* GetDictTrie() const {
89
+ return mpSeg_.GetDictTrie();
90
+ }
91
+
92
+ bool Tag(const string& src, vector<pair<string, string> >& res) const {
93
+ return tagger_.Tag(src, res, *this);
94
+ }
95
+
96
+ string LookupTag(const string &str) const {
97
+ return tagger_.LookupTag(str, *this);
98
+ }
99
+
100
+ private:
101
+ MPSegment mpSeg_;
102
+ HMMSegment hmmSeg_;
103
+ PosTagger tagger_;
104
+
105
+ }; // class MixSegment
106
+
107
+ } // namespace cppjieba
108
+
109
+ #endif
@@ -0,0 +1,77 @@
1
+ #ifndef CPPJIEBA_POS_TAGGING_H
2
+ #define CPPJIEBA_POS_TAGGING_H
3
+
4
+ #include "limonp/StringUtil.hpp"
5
+ #include "SegmentTagged.hpp"
6
+ #include "DictTrie.hpp"
7
+
8
+ namespace cppjieba {
9
+ using namespace limonp;
10
+
11
+ static const char* const POS_M = "m";
12
+ static const char* const POS_ENG = "eng";
13
+ static const char* const POS_X = "x";
14
+
15
+ class PosTagger {
16
+ public:
17
+ PosTagger() {
18
+ }
19
+ ~PosTagger() {
20
+ }
21
+
22
+ bool Tag(const string& src, vector<pair<string, string> >& res, const SegmentTagged& segment) const {
23
+ vector<string> CutRes;
24
+ segment.Cut(src, CutRes);
25
+
26
+ for (vector<string>::iterator itr = CutRes.begin(); itr != CutRes.end(); ++itr) {
27
+ res.push_back(make_pair(*itr, LookupTag(*itr, segment)));
28
+ }
29
+ return !res.empty();
30
+ }
31
+
32
+ string LookupTag(const string &str, const SegmentTagged& segment) const {
33
+ const DictUnit *tmp = NULL;
34
+ RuneStrArray runes;
35
+ const DictTrie * dict = segment.GetDictTrie();
36
+ assert(dict != NULL);
37
+ if (!DecodeUTF8RunesInString(str, runes)) {
38
+ XLOG(ERROR) << "UTF-8 decode failed for word: " << str;
39
+ return POS_X;
40
+ }
41
+ tmp = dict->Find(runes.begin(), runes.end());
42
+ if (tmp == NULL || tmp->tag.empty()) {
43
+ return SpecialRule(runes);
44
+ } else {
45
+ return tmp->tag;
46
+ }
47
+ }
48
+
49
+ private:
50
+ const char* SpecialRule(const RuneStrArray& unicode) const {
51
+ size_t m = 0;
52
+ size_t eng = 0;
53
+ for (size_t i = 0; i < unicode.size() && eng < unicode.size() / 2; i++) {
54
+ if (unicode[i].rune < 0x80) {
55
+ eng ++;
56
+ if ('0' <= unicode[i].rune && unicode[i].rune <= '9') {
57
+ m++;
58
+ }
59
+ }
60
+ }
61
+ // ascii char is not found
62
+ if (eng == 0) {
63
+ return POS_X;
64
+ }
65
+ // all the ascii is number char
66
+ if (m == eng) {
67
+ return POS_M;
68
+ }
69
+ // the ascii chars contain english letter
70
+ return POS_ENG;
71
+ }
72
+
73
+ }; // class PosTagger
74
+
75
+ } // namespace cppjieba
76
+
77
+ #endif
@@ -0,0 +1,54 @@
1
+ #ifndef CPPJIEBA_PRE_FILTER_H
2
+ #define CPPJIEBA_PRE_FILTER_H
3
+
4
+ #include "Trie.hpp"
5
+ #include "limonp/Logging.hpp"
6
+
7
+ namespace cppjieba {
8
+
9
+ class PreFilter {
10
+ public:
11
+ //TODO use WordRange instead of Range
12
+ struct Range {
13
+ RuneStrArray::const_iterator begin;
14
+ RuneStrArray::const_iterator end;
15
+ }; // struct Range
16
+
17
+ PreFilter(const unordered_set<Rune>& symbols,
18
+ const string& sentence)
19
+ : symbols_(symbols) {
20
+ if (!DecodeUTF8RunesInString(sentence, sentence_)) {
21
+ XLOG(ERROR) << "UTF-8 decode failed for input sentence";
22
+ }
23
+ cursor_ = sentence_.begin();
24
+ }
25
+ ~PreFilter() {
26
+ }
27
+ bool HasNext() const {
28
+ return cursor_ != sentence_.end();
29
+ }
30
+ Range Next() {
31
+ Range range;
32
+ range.begin = cursor_;
33
+ while (cursor_ != sentence_.end()) {
34
+ if (IsIn(symbols_, cursor_->rune)) {
35
+ if (range.begin == cursor_) {
36
+ cursor_ ++;
37
+ }
38
+ range.end = cursor_;
39
+ return range;
40
+ }
41
+ cursor_ ++;
42
+ }
43
+ range.end = sentence_.end();
44
+ return range;
45
+ }
46
+ private:
47
+ RuneStrArray::const_iterator cursor_;
48
+ RuneStrArray sentence_;
49
+ const unordered_set<Rune>& symbols_;
50
+ }; // class PreFilter
51
+
52
+ } // namespace cppjieba
53
+
54
+ #endif // CPPJIEBA_PRE_FILTER_H
@@ -0,0 +1,89 @@
1
+ #ifndef CPPJIEBA_QUERYSEGMENT_H
2
+ #define CPPJIEBA_QUERYSEGMENT_H
3
+
4
+ #include <algorithm>
5
+ #include <set>
6
+ #include <cassert>
7
+ #include "limonp/Logging.hpp"
8
+ #include "DictTrie.hpp"
9
+ #include "SegmentBase.hpp"
10
+ #include "FullSegment.hpp"
11
+ #include "MixSegment.hpp"
12
+ #include "Unicode.hpp"
13
+
14
+ namespace cppjieba {
15
+ class QuerySegment: public SegmentBase {
16
+ public:
17
+ QuerySegment(const string& dict, const string& model, const string& userDict = "")
18
+ : mixSeg_(dict, model, userDict),
19
+ trie_(mixSeg_.GetDictTrie()) {
20
+ }
21
+ QuerySegment(const DictTrie* dictTrie, const HMMModel* model)
22
+ : mixSeg_(dictTrie, model), trie_(dictTrie) {
23
+ }
24
+ ~QuerySegment() {
25
+ }
26
+
27
+ void Cut(const string& sentence, vector<string>& words) const {
28
+ Cut(sentence, words, true);
29
+ }
30
+ void Cut(const string& sentence, vector<string>& words, bool hmm) const {
31
+ vector<Word> tmp;
32
+ Cut(sentence, tmp, hmm);
33
+ GetStringsFromWords(tmp, words);
34
+ }
35
+ void Cut(const string& sentence, vector<Word>& words, bool hmm = true) const {
36
+ PreFilter pre_filter(symbols_, sentence);
37
+ PreFilter::Range range;
38
+ vector<WordRange> wrs;
39
+ wrs.reserve(sentence.size()/2);
40
+ while (pre_filter.HasNext()) {
41
+ range = pre_filter.Next();
42
+ Cut(range.begin, range.end, wrs, hmm);
43
+ }
44
+ words.clear();
45
+ words.reserve(wrs.size());
46
+ GetWordsFromWordRanges(sentence, wrs, words);
47
+ }
48
+ void Cut(RuneStrArray::const_iterator begin, RuneStrArray::const_iterator end, vector<WordRange>& res, bool hmm) const {
49
+ //use mix Cut first
50
+ vector<WordRange> mixRes;
51
+ mixSeg_.Cut(begin, end, mixRes, hmm);
52
+
53
+ vector<WordRange> fullRes;
54
+ for (vector<WordRange>::const_iterator mixResItr = mixRes.begin(); mixResItr != mixRes.end(); mixResItr++) {
55
+ if (mixResItr->Length() > 2) {
56
+ for (size_t i = 0; i + 1 < mixResItr->Length(); i++) {
57
+ WordRange wr(mixResItr->left + i, mixResItr->left + i + 1);
58
+ if (trie_->Find(wr.left, wr.right + 1) != NULL) {
59
+ res.push_back(wr);
60
+ }
61
+ }
62
+ }
63
+ if (mixResItr->Length() > 3) {
64
+ for (size_t i = 0; i + 2 < mixResItr->Length(); i++) {
65
+ WordRange wr(mixResItr->left + i, mixResItr->left + i + 2);
66
+ if (trie_->Find(wr.left, wr.right + 1) != NULL) {
67
+ res.push_back(wr);
68
+ }
69
+ }
70
+ }
71
+ res.push_back(*mixResItr);
72
+ }
73
+ }
74
+ private:
75
+ bool IsAllAscii(const Unicode& s) const {
76
+ for(size_t i = 0; i < s.size(); i++) {
77
+ if (s[i] >= 0x80) {
78
+ return false;
79
+ }
80
+ }
81
+ return true;
82
+ }
83
+ MixSegment mixSeg_;
84
+ const DictTrie* trie_;
85
+ }; // QuerySegment
86
+
87
+ } // namespace cppjieba
88
+
89
+ #endif
@@ -0,0 +1,48 @@
1
+ #ifndef CPPJIEBA_SEGMENTBASE_H
2
+ #define CPPJIEBA_SEGMENTBASE_H
3
+
4
+ #include "limonp/Logging.hpp"
5
+ #include "PreFilter.hpp"
6
+ #include <cassert>
7
+
8
+
9
+ namespace cppjieba {
10
+
11
+ // 修改分隔符,移除空格,只保留其他分隔符
12
+ // 这样英文单词之间的空格不会被当作分隔符
13
+ const char* const SPECIAL_SEPARATORS = "\t\n\xEF\xBC\x8C\xE3\x80\x82";
14
+
15
+ using namespace limonp;
16
+
17
+ class SegmentBase {
18
+ public:
19
+ SegmentBase() {
20
+ XCHECK(ResetSeparators(SPECIAL_SEPARATORS));
21
+ }
22
+ virtual ~SegmentBase() {
23
+ }
24
+
25
+ virtual void Cut(const string& sentence, vector<string>& words) const = 0;
26
+
27
+ bool ResetSeparators(const string& s) {
28
+ symbols_.clear();
29
+ RuneStrArray runes;
30
+ if (!DecodeUTF8RunesInString(s, runes)) {
31
+ XLOG(ERROR) << "UTF-8 decode failed for separators: " << s;
32
+ return false;
33
+ }
34
+ for (size_t i = 0; i < runes.size(); i++) {
35
+ if (!symbols_.insert(runes[i].rune).second) {
36
+ XLOG(ERROR) << s.substr(runes[i].offset, runes[i].len) << " already exists";
37
+ return false;
38
+ }
39
+ }
40
+ return true;
41
+ }
42
+ protected:
43
+ unordered_set<Rune> symbols_;
44
+ }; // class SegmentBase
45
+
46
+ } // cppjieba
47
+
48
+ #endif
@@ -0,0 +1,23 @@
1
+ #ifndef CPPJIEBA_SEGMENTTAGGED_H
2
+ #define CPPJIEBA_SEGMENTTAGGED_H
3
+
4
+ #include "SegmentBase.hpp"
5
+
6
+ namespace cppjieba {
7
+
8
+ class SegmentTagged : public SegmentBase{
9
+ public:
10
+ SegmentTagged() {
11
+ }
12
+ virtual ~SegmentTagged() {
13
+ }
14
+
15
+ virtual bool Tag(const string& src, vector<pair<string, string> >& res) const = 0;
16
+
17
+ virtual const DictTrie* GetDictTrie() const = 0;
18
+
19
+ }; // class SegmentTagged
20
+
21
+ } // cppjieba
22
+
23
+ #endif
@@ -0,0 +1,190 @@
1
+ #ifndef CPPJIEBA_TEXTRANK_EXTRACTOR_H
2
+ #define CPPJIEBA_TEXTRANK_EXTRACTOR_H
3
+
4
+ #include <cmath>
5
+ #include "Jieba.hpp"
6
+
7
+ namespace cppjieba {
8
+ using namespace limonp;
9
+ using namespace std;
10
+
11
+ class TextRankExtractor {
12
+ public:
13
+ typedef struct _Word {string word;vector<size_t> offsets;double weight;} Word; // struct Word
14
+ private:
15
+ typedef std::map<string,Word> WordMap;
16
+
17
+ class WordGraph{
18
+ private:
19
+ typedef double Score;
20
+ typedef string Node;
21
+ typedef std::set<Node> NodeSet;
22
+
23
+ typedef std::map<Node,double> Edges;
24
+ typedef std::map<Node,Edges> Graph;
25
+ //typedef std::unordered_map<Node,double> Edges;
26
+ //typedef std::unordered_map<Node,Edges> Graph;
27
+
28
+ double d;
29
+ Graph graph;
30
+ NodeSet nodeSet;
31
+ public:
32
+ WordGraph(): d(0.85) {};
33
+ WordGraph(double in_d): d(in_d) {};
34
+
35
+ void addEdge(Node start,Node end,double weight){
36
+ Edges temp;
37
+ Edges::iterator gotEdges;
38
+ nodeSet.insert(start);
39
+ nodeSet.insert(end);
40
+ graph[start][end]+=weight;
41
+ graph[end][start]+=weight;
42
+ }
43
+
44
+ void rank(WordMap &ws,size_t rankTime=10){
45
+ WordMap outSum;
46
+ Score wsdef, min_rank, max_rank;
47
+
48
+ if( graph.size() == 0)
49
+ return;
50
+
51
+ wsdef = 1.0 / graph.size();
52
+
53
+ for(Graph::iterator edges=graph.begin();edges!=graph.end();++edges){
54
+ // edges->first start节点;edge->first end节点;edge->second 权重
55
+ ws[edges->first].word=edges->first;
56
+ ws[edges->first].weight=wsdef;
57
+ outSum[edges->first].weight=0;
58
+ for(Edges::iterator edge=edges->second.begin();edge!=edges->second.end();++edge){
59
+ outSum[edges->first].weight+=edge->second;
60
+ }
61
+ }
62
+ //sort(nodeSet.begin(),nodeSet.end()); 是否需要排序?
63
+ for( size_t i=0; i<rankTime; i++ ){
64
+ for(NodeSet::iterator node = nodeSet.begin(); node != nodeSet.end(); node++ ){
65
+ double s = 0;
66
+ for( Edges::iterator edge= graph[*node].begin(); edge != graph[*node].end(); edge++ )
67
+ // edge->first end节点;edge->second 权重
68
+ s += edge->second / outSum[edge->first].weight * ws[edge->first].weight;
69
+ ws[*node].weight = (1 - d) + d * s;
70
+ }
71
+ }
72
+
73
+ min_rank=max_rank=ws.begin()->second.weight;
74
+ for(WordMap::iterator i = ws.begin(); i != ws.end(); i ++){
75
+ if( i->second.weight < min_rank ){
76
+ min_rank = i->second.weight;
77
+ }
78
+ if( i->second.weight > max_rank ){
79
+ max_rank = i->second.weight;
80
+ }
81
+ }
82
+ for(WordMap::iterator i = ws.begin(); i != ws.end(); i ++){
83
+ ws[i->first].weight = (i->second.weight - min_rank / 10.0) / (max_rank - min_rank / 10.0);
84
+ }
85
+ }
86
+ };
87
+
88
+ public:
89
+ TextRankExtractor(const string& dictPath,
90
+ const string& hmmFilePath,
91
+ const string& stopWordPath,
92
+ const string& userDict = "")
93
+ : segment_(dictPath, hmmFilePath, userDict) {
94
+ LoadStopWordDict(stopWordPath);
95
+ }
96
+ TextRankExtractor(const DictTrie* dictTrie,
97
+ const HMMModel* model,
98
+ const string& stopWordPath)
99
+ : segment_(dictTrie, model) {
100
+ LoadStopWordDict(stopWordPath);
101
+ }
102
+ TextRankExtractor(const Jieba& jieba, const string& stopWordPath) : segment_(jieba.GetDictTrie(), jieba.GetHMMModel()) {
103
+ LoadStopWordDict(stopWordPath);
104
+ }
105
+ ~TextRankExtractor() {
106
+ }
107
+
108
+ void Extract(const string& sentence, vector<string>& keywords, size_t topN) const {
109
+ vector<Word> topWords;
110
+ Extract(sentence, topWords, topN);
111
+ for (size_t i = 0; i < topWords.size(); i++) {
112
+ keywords.push_back(topWords[i].word);
113
+ }
114
+ }
115
+
116
+ void Extract(const string& sentence, vector<pair<string, double> >& keywords, size_t topN) const {
117
+ vector<Word> topWords;
118
+ Extract(sentence, topWords, topN);
119
+ for (size_t i = 0; i < topWords.size(); i++) {
120
+ keywords.push_back(pair<string, double>(topWords[i].word, topWords[i].weight));
121
+ }
122
+ }
123
+
124
+ void Extract(const string& sentence, vector<Word>& keywords, size_t topN, size_t span=5,size_t rankTime=10) const {
125
+ vector<string> words;
126
+ segment_.Cut(sentence, words);
127
+
128
+ TextRankExtractor::WordGraph graph;
129
+ WordMap wordmap;
130
+ size_t offset = 0;
131
+
132
+ for(size_t i=0; i < words.size(); i++){
133
+ size_t t = offset;
134
+ offset += words[i].size();
135
+ if (IsSingleWord(words[i]) || stopWords_.find(words[i]) != stopWords_.end()) {
136
+ continue;
137
+ }
138
+ for(size_t j=i+1,skip=0;j<i+span+skip && j<words.size();j++){
139
+ if (IsSingleWord(words[j]) || stopWords_.find(words[j]) != stopWords_.end()) {
140
+ skip++;
141
+ continue;
142
+ }
143
+ graph.addEdge(words[i],words[j],1);
144
+ }
145
+ wordmap[words[i]].offsets.push_back(t);
146
+ }
147
+ if (offset != sentence.size()) {
148
+ XLOG(ERROR) << "words illegal";
149
+ return;
150
+ }
151
+
152
+ graph.rank(wordmap,rankTime);
153
+
154
+ keywords.clear();
155
+ keywords.reserve(wordmap.size());
156
+ for (WordMap::iterator itr = wordmap.begin(); itr != wordmap.end(); ++itr) {
157
+ keywords.push_back(itr->second);
158
+ }
159
+
160
+ topN = min(topN, keywords.size());
161
+ partial_sort(keywords.begin(), keywords.begin() + topN, keywords.end(), Compare);
162
+ keywords.resize(topN);
163
+ }
164
+ private:
165
+ void LoadStopWordDict(const string& filePath) {
166
+ ifstream ifs(filePath.c_str());
167
+ XCHECK(ifs.is_open()) << "open " << filePath << " failed";
168
+ string line ;
169
+ while (getline(ifs, line)) {
170
+ stopWords_.insert(line);
171
+ }
172
+ assert(stopWords_.size());
173
+ }
174
+
175
+ static bool Compare(const Word &x,const Word &y){
176
+ return x.weight > y.weight;
177
+ }
178
+
179
+ MixSegment segment_;
180
+ unordered_set<string> stopWords_;
181
+ }; // class TextRankExtractor
182
+
183
+ inline ostream& operator << (ostream& os, const TextRankExtractor::Word& word) {
184
+ return os << "{\"word\": \"" << word.word << "\", \"offset\": " << word.offsets << ", \"weight\": " << word.weight << "}";
185
+ }
186
+ } // namespace cppjieba
187
+
188
+ #endif
189
+
190
+