ci for insertTrie 2 or 3 column

This commit is contained in:
gwdwyy 2013-08-16 20:21:12 +08:00
parent 2e85c1c833
commit da294a04f8
4 changed files with 17 additions and 24 deletions

2
.gitignore vendored
View File

@ -6,4 +6,4 @@ tags
*.ut *.ut
log log
main main
cmlib.a lib*.a

View File

@ -9,8 +9,8 @@ int main(int argc, char ** argv)
{ {
if(argc < 2) if(argc < 2)
{ {
cerr<<"usage: "<<argv[0]<<" filename"<<endl; cout<<"usage: "<<argv[0]<<" <filename>"<<endl;
return 1; return -1;
} }
Segment seg; Segment seg;
if(!seg.init()) if(!seg.init())

View File

@ -77,10 +77,10 @@ namespace CppJieba
return false; return false;
} }
bool res = false; bool res = false;
res = _buildTree(filePath); res = _trieInsert(filePath);
if(!res) if(!res)
{ {
LogError("_buildTree failed."); LogError("_trieInsert failed.");
return false; return false;
} }
res = _countWeight(); res = _countWeight();
@ -92,40 +92,35 @@ namespace CppJieba
return true; return true;
} }
bool Trie::_buildTree(const char * const filePath) bool Trie::_trieInsert(const char * const filePath)
{ {
ifstream ifile(filePath); ifstream ifile(filePath);
string line; string line;
vector<string> vecBuf; vector<string> vecBuf;
TrieNodeInfo nodeInfo;
while(getline(ifile, line)) while(getline(ifile, line))
{ {
vecBuf.clear(); vecBuf.clear();
splitStr(line, vecBuf, " "); splitStr(line, vecBuf, " ");
if(3 != vecBuf.size()) if(3 < vecBuf.size())
{ {
LogError(string_format("line[%s] illegal.", line.c_str())); LogError(string_format("line[%s] illegal.", line.c_str()));
return false; return false;
} }
string chWord = vecBuf[0]; nodeInfo.word = vecBuf[0];
uint count = atoi(vecBuf[1].c_str()); nodeInfo.count = atoi(vecBuf[1].c_str());
const string& tag = vecBuf[2]; if(3 == vecBuf.size())
{
nodeInfo.tag = vecBuf[2];
}
//insert node //insert node
TrieNodeInfo nodeInfo; if(!insert(nodeInfo))
nodeInfo.word = chWord;
nodeInfo.wLen = 0;
nodeInfo.count = count;
nodeInfo.tag = tag;
bool flag = insert(nodeInfo);
if(!flag)
{ {
LogError("insert node failed!"); LogError("insert node failed!");
return false;
} }
} }
return true; return true;
} }

View File

@ -35,10 +35,8 @@ namespace CppJieba
double weight; double weight;
TrieNodeInfo() TrieNodeInfo()
{ {
word = "";
wLen = 0; wLen = 0;
count = 0; count = 0;
tag = "";
weight = 0.0; weight = 0.0;
} }
}; };
@ -101,7 +99,7 @@ namespace CppJieba
bool insert(const TrieNodeInfo& nodeInfo); bool insert(const TrieNodeInfo& nodeInfo);
private: private:
bool _buildTree(const char * const filePath); bool _trieInsert(const char * const filePath);
bool _countWeight(); bool _countWeight();
bool _deleteNode(TrieNode* node); bool _deleteNode(TrieNode* node);