mirror of
https://github.com/yanyiwu/cppjieba.git
synced 2025-07-18 00:00:12 +08:00
commit
e6fdd1c98b
@ -50,6 +50,17 @@ class DictTrie {
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool InsertUserWord(const string& word,int freq, const string& tag = UNKNOWN_TAG) {
|
||||||
|
DictUnit node_info;
|
||||||
|
double weight = freq ? log(1.0 * freq / freq_sum_) : user_word_default_weight_ ;
|
||||||
|
if (!MakeNodeInfo(node_info, word, weight , tag)) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
active_node_infos_.push_back(node_info);
|
||||||
|
trie_->InsertNode(node_info.word, &active_node_infos_.back());
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
const DictUnit* Find(RuneStrArray::const_iterator begin, RuneStrArray::const_iterator end) const {
|
const DictUnit* Find(RuneStrArray::const_iterator begin, RuneStrArray::const_iterator end) const {
|
||||||
return trie_->Find(begin, end);
|
return trie_->Find(begin, end);
|
||||||
}
|
}
|
||||||
@ -69,6 +80,63 @@ class DictTrie {
|
|||||||
return min_weight_;
|
return min_weight_;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void InserUserDictNode(const string& line) {
|
||||||
|
vector<string> buf;
|
||||||
|
DictUnit node_info;
|
||||||
|
Split(line, buf, " ");
|
||||||
|
if(buf.size() == 1){
|
||||||
|
MakeNodeInfo(node_info,
|
||||||
|
buf[0],
|
||||||
|
user_word_default_weight_,
|
||||||
|
UNKNOWN_TAG);
|
||||||
|
} else if (buf.size() == 2) {
|
||||||
|
MakeNodeInfo(node_info,
|
||||||
|
buf[0],
|
||||||
|
user_word_default_weight_,
|
||||||
|
buf[1]);
|
||||||
|
} else if (buf.size() == 3) {
|
||||||
|
int freq = atoi(buf[1].c_str());
|
||||||
|
assert(freq_sum_ > 0.0);
|
||||||
|
double weight = log(1.0 * freq / freq_sum_);
|
||||||
|
MakeNodeInfo(node_info, buf[0], weight, buf[2]);
|
||||||
|
}
|
||||||
|
static_node_infos_.push_back(node_info);
|
||||||
|
if (node_info.word.size() == 1) {
|
||||||
|
user_dict_single_chinese_word_.insert(node_info.word[0]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void LoadUserDict(const vector<string>& buf) {
|
||||||
|
for (size_t i = 0; i < buf.size(); i++) {
|
||||||
|
InserUserDictNode(buf[i]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void LoadUserDict(const set<string>& buf) {
|
||||||
|
std::set<string>::const_iterator iter;
|
||||||
|
for (iter = buf.begin(); iter != buf.end(); iter++){
|
||||||
|
InserUserDictNode(*iter);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void LoadUserDict(const string& filePaths) {
|
||||||
|
vector<string> files = limonp::Split(filePaths, "|;");
|
||||||
|
size_t lineno = 0;
|
||||||
|
for (size_t i = 0; i < files.size(); i++) {
|
||||||
|
ifstream ifs(files[i].c_str());
|
||||||
|
XCHECK(ifs.is_open()) << "open " << files[i] << " failed";
|
||||||
|
string line;
|
||||||
|
|
||||||
|
for (; getline(ifs, line); lineno++) {
|
||||||
|
if (line.size() == 0) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
InserUserDictNode(line);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
private:
|
private:
|
||||||
void Init(const string& dict_path, const string& user_dict_paths, UserWordWeightOption user_word_weight_opt) {
|
void Init(const string& dict_path, const string& user_dict_paths, UserWordWeightOption user_word_weight_opt) {
|
||||||
LoadDict(dict_path);
|
LoadDict(dict_path);
|
||||||
@ -95,45 +163,8 @@ class DictTrie {
|
|||||||
trie_ = new Trie(words, valuePointers);
|
trie_ = new Trie(words, valuePointers);
|
||||||
}
|
}
|
||||||
|
|
||||||
void LoadUserDict(const string& filePaths) {
|
|
||||||
vector<string> files = limonp::Split(filePaths, "|;");
|
|
||||||
size_t lineno = 0;
|
|
||||||
for (size_t i = 0; i < files.size(); i++) {
|
|
||||||
ifstream ifs(files[i].c_str());
|
|
||||||
XCHECK(ifs.is_open()) << "open " << files[i] << " failed";
|
|
||||||
string line;
|
|
||||||
DictUnit node_info;
|
|
||||||
vector<string> buf;
|
|
||||||
for (; getline(ifs, line); lineno++) {
|
|
||||||
if (line.size() == 0) {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
buf.clear();
|
|
||||||
Split(line, buf, " ");
|
|
||||||
DictUnit node_info;
|
|
||||||
if(buf.size() == 1){
|
|
||||||
MakeNodeInfo(node_info,
|
|
||||||
buf[0],
|
|
||||||
user_word_default_weight_,
|
|
||||||
UNKNOWN_TAG);
|
|
||||||
} else if (buf.size() == 2) {
|
|
||||||
MakeNodeInfo(node_info,
|
|
||||||
buf[0],
|
|
||||||
user_word_default_weight_,
|
|
||||||
buf[1]);
|
|
||||||
} else if (buf.size() == 3) {
|
|
||||||
int freq = atoi(buf[1].c_str());
|
|
||||||
assert(freq_sum_ > 0.0);
|
|
||||||
double weight = log(1.0 * freq / freq_sum_);
|
|
||||||
MakeNodeInfo(node_info, buf[0], weight, buf[2]);
|
|
||||||
}
|
|
||||||
static_node_infos_.push_back(node_info);
|
|
||||||
if (node_info.word.size() == 1) {
|
|
||||||
user_dict_single_chinese_word_.insert(node_info.word[0]);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
bool MakeNodeInfo(DictUnit& node_info,
|
bool MakeNodeInfo(DictUnit& node_info,
|
||||||
const string& word,
|
const string& word,
|
||||||
|
@ -84,10 +84,23 @@ class Jieba {
|
|||||||
const DictTrie* GetDictTrie() const {
|
const DictTrie* GetDictTrie() const {
|
||||||
return &dict_trie_;
|
return &dict_trie_;
|
||||||
}
|
}
|
||||||
|
|
||||||
const HMMModel* GetHMMModel() const {
|
const HMMModel* GetHMMModel() const {
|
||||||
return &model_;
|
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:
|
private:
|
||||||
DictTrie dict_trie_;
|
DictTrie dict_trie_;
|
||||||
HMMModel model_;
|
HMMModel model_;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user