游雁
2024-02-19 94de39dde2e616a01683c518023d0fab72b4e103
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
#ifndef CPPJIEAB_JIEBA_H
#define CPPJIEAB_JIEBA_H
 
#include "QuerySegment.hpp"
#include "KeywordExtractor.hpp"
 
namespace cppjieba {
 
class Jieba {
 public:
  Jieba(DictTrie *jieba_dict_trie, 
        HMMModel *jieba_model)
    : dict_trie_(jieba_dict_trie),
      model_(jieba_model),
      mp_seg_(dict_trie_),
      hmm_seg_(model_),
      mix_seg_(dict_trie_, model_),
      full_seg_(dict_trie_),
      query_seg_(dict_trie_, model_) {
  }
  Jieba() {
    dict_trie_ = NULL;
    model_ = NULL;
  }
  ~Jieba() {
  }
 
  struct LocWord {
    string word;
    size_t begin;
    size_t end;
  }; // struct LocWord
  void SetJiebaRes(cppjieba::DictTrie *&dict, cppjieba::HMMModel *&hmm) {
    dict_trie_ = dict;
    model_ = hmm;
    mp_seg_.setRes(dict);
    hmm_seg_.setRes(hmm);
    mix_seg_.setRes(dict, hmm);
    full_seg_.setRes(dict);
    query_seg_.setRes(dict, hmm);
  }
  void Cut(const string& sentence, vector<string>& words, bool hmm = true) const {
    mix_seg_.Cut(sentence, words, hmm);
  }
  void Cut(const string& sentence, vector<Word>& words, bool hmm = true) const {
    mix_seg_.Cut(sentence, words, hmm);
  }
  void CutAll(const string& sentence, vector<string>& words) const {
    full_seg_.Cut(sentence, words);
  }
  void CutAll(const string& sentence, vector<Word>& words) const {
    full_seg_.Cut(sentence, words);
  }
  void CutForSearch(const string& sentence, vector<string>& words, bool hmm = true) const {
    query_seg_.Cut(sentence, words, hmm);
  }
  void CutForSearch(const string& sentence, vector<Word>& words, bool hmm = true) const {
    query_seg_.Cut(sentence, words, hmm);
  }
  void CutHMM(const string& sentence, vector<string>& words) const {
    hmm_seg_.Cut(sentence, words);
  }
  void CutHMM(const string& sentence, vector<Word>& words) const {
    hmm_seg_.Cut(sentence, words);
  }
  void CutSmall(const string& sentence, vector<string>& words, size_t max_word_len) const {
    mp_seg_.Cut(sentence, words, max_word_len);
  }
  void CutSmall(const string& sentence, vector<Word>& words, size_t max_word_len) const {
    mp_seg_.Cut(sentence, words, max_word_len);
  }
  
  void Tag(const string& sentence, vector<pair<string, string> >& words) const {
    mix_seg_.Tag(sentence, words);
  }
  string LookupTag(const string &str) const {
    return mix_seg_.LookupTag(str);
  }
  bool InsertUserWord(const string& word, const string& tag = UNKNOWN_TAG) {
    return dict_trie_->InsertUserWord(word, tag);
  }
 
  bool InsertUserWord(const string& word,int freq, const string& tag = UNKNOWN_TAG) {
    return dict_trie_->InsertUserWord(word,freq, tag);
  }
 
  bool DeleteUserWord(const string& word, const string& tag = UNKNOWN_TAG) {
    return dict_trie_->DeleteUserWord(word, tag);
  }
  
  bool Find(const string& word)
  {
    return dict_trie_->Find(word);
  }
 
  void ResetSeparators(const string& s) {
    //TODO
    mp_seg_.ResetSeparators(s);
    hmm_seg_.ResetSeparators(s);
    mix_seg_.ResetSeparators(s);
    full_seg_.ResetSeparators(s);
    query_seg_.ResetSeparators(s);
  }
 
  const DictTrie* GetDictTrie() const {
    return dict_trie_;
  } 
  
  const HMMModel* GetHMMModel() const {
    return model_;
  }
 
  void LoadUserDict(const vector<string>& buf)  {
    dict_trie_->LoadUserDict(buf);
  }
 
  void LoadUserDict(const set<string>& buf)  {
    dict_trie_->LoadUserDict(buf);
  }
 
  void LoadUserDict(const string& path)  {
    dict_trie_->LoadUserDict(path);
  }
 
 private:
  DictTrie *dict_trie_;
  HMMModel *model_;
  
  // They share the same dict trie and model
  MPSegment mp_seg_;
  HMMSegment hmm_seg_;
  MixSegment mix_seg_;
  FullSegment full_seg_;
  QuerySegment query_seg_;
 
 public:
}; // class Jieba
 
} // namespace cppjieba
 
#endif // CPPJIEAB_JIEBA_H