diff --git a/include/cppjieba/DictTrie.hpp b/include/cppjieba/DictTrie.hpp index 33fa5c6..7aae3f0 100644 --- a/include/cppjieba/DictTrie.hpp +++ b/include/cppjieba/DictTrie.hpp @@ -61,6 +61,15 @@ class DictTrie { return true; } + bool DeleteUserWord(const string& word, const string& tag = UNKNOWN_TAG) { + DictUnit node_info; + if (!MakeNodeInfo(node_info, word, user_word_default_weight_, tag)) { + return false; + } + trie_->DeleteNode(node_info.word, &node_info); + return true; + } + const DictUnit* Find(RuneStrArray::const_iterator begin, RuneStrArray::const_iterator end) const { return trie_->Find(begin, end); } diff --git a/include/cppjieba/Jieba.hpp b/include/cppjieba/Jieba.hpp index abd321d..8475404 100644 --- a/include/cppjieba/Jieba.hpp +++ b/include/cppjieba/Jieba.hpp @@ -76,6 +76,10 @@ class Jieba { 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); diff --git a/include/cppjieba/Trie.hpp b/include/cppjieba/Trie.hpp index fcd5e32..e6f71b1 100644 --- a/include/cppjieba/Trie.hpp +++ b/include/cppjieba/Trie.hpp @@ -141,7 +141,33 @@ class Trie { assert(ptNode != NULL); ptNode->ptValue = ptValue; } - + void DeleteNode(const Unicode& key, const DictUnit* ptValue) { + if (key.begin() == key.end()) { + return; + } + //定义一个NextMap迭代器 + TrieNode::NextMap::const_iterator kmIter; + //定义一个指向root的TrieNode指针 + TrieNode *ptNode = root_; + for (Unicode::const_iterator citer = key.begin(); citer != key.end(); ++citer) { + //链表不存在元素 + if (NULL == ptNode->next) { + return; + } + kmIter = ptNode->next->find(*citer); + //如果map中不存在,跳出循环 + if (ptNode->next->end() == kmIter) { + break; + } + //从unordered_map中擦除该项 + ptNode->next->erase(*citer); + //删除该node + ptNode = kmIter->second; + delete ptNode; + break; + } + return; + } private: void CreateTrie(const vector& keys, const vector& valuePointers) { if (valuePointers.empty() || keys.empty()) {