This commit is contained in:
wyy 2013-06-24 16:20:10 +08:00
parent 5be8d48a18
commit 651800d07e
2 changed files with 12 additions and 16 deletions

View File

@ -22,7 +22,6 @@ namespace CppJieba
return false; return false;
} }
_root = new TrieNode; _root = new TrieNode;
_root->isLeaf = false;
ifstream ifile(filepath); ifstream ifile(filepath);
string line; string line;
vector<string> vecBuf; vector<string> vecBuf;
@ -36,20 +35,11 @@ namespace CppJieba
LogError(msgBuf); LogError(msgBuf);
return false; return false;
} }
//PRINT_VECTOR(vecBuf);
//getchar();
string chWord = vecBuf[0]; string chWord = vecBuf[0];
unsigned int count = atoi(vecBuf[1].c_str());
const string& tag = vecBuf[2];
size_t uniLen = utf8ToUnicode(chWord.c_str(), chWord.size(), chUniBuf); size_t uniLen = utf8ToUnicode(chWord.c_str(), chWord.size(), chUniBuf);
_insert(chUniBuf, uniLen); _insert(chUniBuf, uniLen, count, tag);
//for(int i = 0; i < unilen; i++)
//{
// // printf("%x\n", strbuf[i]);
//}
//char utf8str[512]={0};
//unicodeToUtf8(strbuf, unilen, utf8str);
//cout<<strlen(utf8str);
//cout<<utf8str<<endl;
} }
return true; return true;
} }
@ -145,7 +135,7 @@ namespace CppJieba
} }
} }
bool Trie::_insert(const ChUnicode* chUniStr, size_t len) bool Trie::_insert(const ChUnicode* chUniStr, size_t len, unsigned int cnt, const string& tag)
{ {
if(0 == len) if(0 == len)
{ {
@ -159,7 +149,6 @@ namespace CppJieba
if(p->hmap.end() == p->hmap.find(cu)) if(p->hmap.end() == p->hmap.find(cu))
{ {
TrieNode * next = new TrieNode; TrieNode * next = new TrieNode;
next->isLeaf = false;
p->hmap[cu] = next; p->hmap[cu] = next;
p = next; p = next;
} }

9
Trie.h
View File

@ -22,6 +22,13 @@ namespace CppJieba
{ {
TrieNodeHashMap hmap; TrieNodeHashMap hmap;
bool isLeaf; bool isLeaf;
unsigned int count;
string tag;
TrieNode()
{
isLeaf = false;
count = 0;
}
}; };
class Trie class Trie
@ -39,7 +46,7 @@ namespace CppJieba
private: private:
bool _destroyNode(TrieNode* node); bool _destroyNode(TrieNode* node);
void _display(TrieNode* node, int level); void _display(TrieNode* node, int level);
bool _insert(const ChUnicode* chUniBuf, size_t len); bool _insert(const ChUnicode* chUniBuf, size_t len, unsigned int cnt, const string& tag);
}; };
} }