From b167777a313e941acb8246154b28cb72aeb4146e Mon Sep 17 00:00:00 2001 From: gwdwyy Date: Fri, 6 Sep 2013 00:53:36 +0800 Subject: [PATCH] change the public load* function to private && mv them into init(...) --- demo/keywordext_demo.cpp | 52 ++++------------------------------------ demo/segment_demo.cpp | 17 ++++++------- demo/testlines.utf8 | 1 + src/HMMSegment.cpp | 6 ++--- src/HMMSegment.h | 4 ++-- src/KeyWordExt.cpp | 35 ++++++++++++++++----------- src/KeyWordExt.h | 16 +++++-------- src/Segment.cpp | 20 +++++++--------- src/Segment.h | 5 ++-- 9 files changed, 58 insertions(+), 98 deletions(-) diff --git a/demo/keywordext_demo.cpp b/demo/keywordext_demo.cpp index 11edd76..6298be3 100644 --- a/demo/keywordext_demo.cpp +++ b/demo/keywordext_demo.cpp @@ -8,18 +8,11 @@ using namespace CppJieba; void testKeyWordExt(const char * dictPath, const char * filePath) { KeyWordExt ext; - ext.init(); + if(!ext.init(dictPath, "../dicts/stopwords.gbk.v1.0")) + { + return; + } - if(!ext.loadSegDict(dictPath)) - { - cerr<<__FILE__<<__LINE__< res; string line; @@ -36,43 +29,6 @@ void testKeyWordExt(const char * dictPath, const char * filePath) ext.dispose(); } -void testKeyWordExt2(const char * dictPath, const char * filePath) -{ - Segment seg; - seg.init(); - KeyWordExt ext; - ext.init(); - - if(!seg.loadSegDict(dictPath)) - { - cerr<<__FILE__<<__LINE__< words; - vector res; - string line; - while(getline(ifile, line)) - { - if(!line.empty()) - { - seg.cutDAG(line, words); - ext.extract(words, res, 20); - cout<& res); bool viterbi(const vector& unico, vector& status); private: + bool _loadModel(const char* const filePath); bool _getLine(ifstream& ifile, string& line); bool _loadEmitProb(const string& line, EmitProbMap& mp); bool _decodeOne(const string& str, uint16_t& res); diff --git a/src/KeyWordExt.cpp b/src/KeyWordExt.cpp index 2bc98d9..496bb43 100644 --- a/src/KeyWordExt.cpp +++ b/src/KeyWordExt.cpp @@ -16,19 +16,26 @@ namespace CppJieba { } - bool KeyWordExt::init() + bool KeyWordExt::init(const char* const segDictFile, const char* const stopWordDictFile) { - return _segment.init(); + LogInfo("KeyWordExt init start ..."); + if(!_segment.init(segDictFile)) + { + LogError("_segment.init failed."); + return false; + } + if(!_loadStopWords(stopWordDictFile)) + { + LogError("_loadStopWords failed."); + return false; + } + LogInfo("KeyWordExt init OK."); + return true; } - bool KeyWordExt::loadSegDict(const char * const filePath) + bool KeyWordExt::_loadPriorSubWords(const char * const filePath) { - return _segment.loadSegDict(filePath); - } - - bool KeyWordExt::loadPriorSubWords(const char * const filePath) - { - LogInfo(string_format("loadPriorSubWords(%s) start", filePath)); + LogInfo(string_format("_loadPriorSubWords(%s) start", filePath)); if(!checkFileExist(filePath)) { LogError(string_format("cann't find file[%s].",filePath)); @@ -45,15 +52,15 @@ namespace CppJieba { _priorSubWords.push_back(subword); } - LogInfo(string_format("loadPriorSubWords(%s) end", filePath)); + LogInfo(string_format("_loadPriorSubWords(%s) end", filePath)); infile.close(); return true; } - bool KeyWordExt::loadStopWords(const char * const filePath) + bool KeyWordExt::_loadStopWords(const char * const filePath) { - LogInfo(string_format("loadStopWords(%s) start", filePath)); + LogInfo(string_format("_loadStopWords(%s) start", filePath)); if(!_stopWords.empty()) { LogError("_stopWords has been loaded before! "); @@ -366,9 +373,9 @@ int main() { return 1; } - ext.loadStopWords("../dicts/stopwords.gbk.v1.0"); + ext._loadStopWords("../dicts/stopwords.gbk.v1.0"); - if(!ext.loadPriorSubWords("../dicts/prior.gbk")) + if(!ext._loadPriorSubWords("../dicts/prior.gbk")) { cerr<<"err"<& keyWordInfos, uint topN); bool extract(const vector& words, vector& keyWordInfos, uint topN); diff --git a/src/Segment.cpp b/src/Segment.cpp index f9e4d51..9919a75 100644 --- a/src/Segment.cpp +++ b/src/Segment.cpp @@ -14,25 +14,23 @@ namespace CppJieba { } - bool Segment::init() + bool Segment::init(const char* const filePath) { if(!_trie.init()) { LogError("_trie.init failed."); return false; } + LogInfo(string_format("_trie.loadDict(%s) start...", filePath)); + if(!_trie.loadDict(filePath)) + { + LogError("_trie.loadDict faield."); + return false; + } + LogInfo("_trie.loadDict end."); return true; } - bool Segment::loadSegDict(const char * const filePath) - { - LogInfo(string_format("_trie.loadDict(%s) start...", filePath)); - bool retFlag = _trie.loadDict(filePath); - LogInfo("_trie.loadDict end."); - return retFlag; - } - - bool Segment::dispose() { return _trie.dispose(); @@ -212,7 +210,7 @@ int main() { Segment segment; segment.init(); - if(!segment.loadSegDict("../dicts/segdict.gbk.v3.0")) + if(!segment._loadSegDict("../dicts/segdict.gbk.v3.0")) { cerr<<"1"< #include "Trie.h" #include "globals.h" +#include "HMMSegment.h" namespace CppJieba { @@ -16,12 +17,12 @@ namespace CppJieba { private: Trie _trie; + public: Segment(); ~Segment(); public: - bool init(); - bool loadSegDict(const char * const filePath); + bool init(const char* const filePath); bool dispose(); public: bool cutDAG(const string& str, vector& segWordInfos);