diff --git a/src/HMMSegment.hpp b/src/HMMSegment.hpp index fc8935d..c9839a4 100644 --- a/src/HMMSegment.hpp +++ b/src/HMMSegment.hpp @@ -31,9 +31,11 @@ namespace CppJieba EmitProbMap _emitProbM; EmitProbMap _emitProbS; vector _emitProbVec; + private: + const string _hmmModelPath; public: - HMMSegment() + HMMSegment(const char * const filePath): _hmmModelPath(filePath) { memset(_startProb, 0, sizeof(_startProb)); memset(_transProb, 0, sizeof(_transProb)); @@ -51,11 +53,11 @@ namespace CppJieba dispose(); } public: - bool init(const char* const modelPath) + virtual bool init() { - return _setInitFlag(_loadModel(modelPath)); + return _setInitFlag(_loadModel(_hmmModelPath.c_str())); } - bool dispose() + virtual bool dispose() { _setInitFlag(false); return true; @@ -88,11 +90,8 @@ namespace CppJieba } return true; } - //bool cut(const string& str, vector& res)const - //{ - // return SegmentBase::cut(str, res); - //} - bool cut(Unicode::const_iterator begin, Unicode::const_iterator end, vector& res)const + public: + virtual bool cut(Unicode::const_iterator begin, Unicode::const_iterator end, vector& res)const { if(!_getInitFlag()) { diff --git a/src/ISegment.hpp b/src/ISegment.hpp index 5099fa0..8821289 100644 --- a/src/ISegment.hpp +++ b/src/ISegment.hpp @@ -8,6 +8,9 @@ namespace CppJieba { public: virtual ~ISegment(){}; + public: + virtual bool init() = 0; + virtual bool dispose() = 0; public: virtual bool cut(Unicode::const_iterator begin , Unicode::const_iterator end, vector& res) const = 0; virtual bool cut(const string& str, vector& res) const = 0; diff --git a/src/MPSegment.hpp b/src/MPSegment.hpp index 0601980..d5f39f7 100644 --- a/src/MPSegment.hpp +++ b/src/MPSegment.hpp @@ -32,12 +32,14 @@ namespace CppJieba { private: Trie _trie; + private: + const string _dictPath; public: - MPSegment(){}; + MPSegment(const char * const dictPath): _dictPath(dictPath){}; virtual ~MPSegment(){dispose();}; public: - bool init(const char* const filePath) + virtual bool init() { if(_getInitFlag()) { @@ -49,8 +51,8 @@ namespace CppJieba LogError("_trie.init failed."); return false; } - LogInfo("_trie.loadDict(%s) start...", filePath); - if(!_trie.loadDict(filePath)) + LogInfo("_trie.loadDict(%s) start...", _dictPath.c_str()); + if(!_trie.loadDict(_dictPath.c_str())) { LogError("_trie.loadDict faield."); return false; @@ -58,7 +60,7 @@ namespace CppJieba LogInfo("_trie.loadDict end."); return _setInitFlag(true); } - bool dispose() + virtual bool dispose() { if(!_getInitFlag()) { @@ -69,12 +71,7 @@ namespace CppJieba return true; } public: - //bool cut(const string& str, vector& segWordInfos)const; - //bool cut(const string& str, vector& res)const - //{ - // return SegmentBase::cut(str, res); - //} - bool cut(Unicode::const_iterator begin, Unicode::const_iterator end, vector& res)const + virtual bool cut(Unicode::const_iterator begin, Unicode::const_iterator end, vector& res)const { if(!_getInitFlag()) { diff --git a/src/MixSegment.hpp b/src/MixSegment.hpp index 6749f7f..8914256 100644 --- a/src/MixSegment.hpp +++ b/src/MixSegment.hpp @@ -13,7 +13,7 @@ namespace CppJieba MPSegment _mpSeg; HMMSegment _hmmSeg; public: - MixSegment() + MixSegment(const char * const mpSegDict, const char * const hmmSegDict): _mpSeg(mpSegDict), _hmmSeg(hmmSegDict) { } virtual ~MixSegment() @@ -21,26 +21,26 @@ namespace CppJieba dispose(); } public: - bool init(const char* const mpSegDict, const char* const hmmSegDict) + virtual bool init() { if(_getInitFlag()) { LogError("inited."); return false; } - if(!_mpSeg.init(mpSegDict)) + if(!_mpSeg.init()) { LogError("_mpSeg init"); return false; } - if(!_hmmSeg.init(hmmSegDict)) + if(!_hmmSeg.init()) { LogError("_hmmSeg init"); return false; } return _setInitFlag(true); } - bool dispose() + virtual bool dispose() { if(!_getInitFlag()) { @@ -52,13 +52,9 @@ namespace CppJieba return true; } public: - //virtual bool cut(const string& str, vector& res) const; - //bool cut(const string& str, vector& res)const - //{ - // return SegmentBase::cut(str, res); - //} using SegmentBase::cut; - bool cut(Unicode::const_iterator begin, Unicode::const_iterator end, vector& res)const + public: + virtual bool cut(Unicode::const_iterator begin, Unicode::const_iterator end, vector& res)const { if(!_getInitFlag()) { diff --git a/src/SegmentBase.hpp b/src/SegmentBase.hpp index e56688d..44e4df3 100644 --- a/src/SegmentBase.hpp +++ b/src/SegmentBase.hpp @@ -18,6 +18,10 @@ namespace CppJieba bool _isInited; bool _getInitFlag()const{return _isInited;}; bool _setInitFlag(bool flag){return _isInited = flag;}; + public: + virtual bool init() = 0; + virtual bool dispose() = 0; + public: virtual bool cut(Unicode::const_iterator begin, Unicode::const_iterator end, vector& res)const = 0; virtual bool cut(const string& str, vector& res)const diff --git a/src/segment.cpp b/src/segment.cpp index 50fffba..28f3e80 100644 --- a/src/segment.cpp +++ b/src/segment.cpp @@ -53,8 +53,8 @@ int main(int argc, char ** argv) if("cutHMM" == algorithm) { - HMMSegment seg; - if(!seg.init(modelPath.c_str())) + HMMSegment seg(modelPath.c_str()); + if(!seg.init()) { cout<<"seg init failed."<