mirror of
https://github.com/yanyiwu/cppjieba.git
synced 2025-07-18 00:00:12 +08:00
merge HMMSegment.h/cpp into hpp
This commit is contained in:
parent
6484342c8f
commit
55c64e9893
@ -1,7 +1,7 @@
|
|||||||
SET(EXECUTABLE_OUTPUT_PATH ${PROJECT_BINARY_DIR}/bin)
|
SET(EXECUTABLE_OUTPUT_PATH ${PROJECT_BINARY_DIR}/bin)
|
||||||
SET(LIBRARY_OUTPUT_PATH ${PROJECT_BINARY_DIR}/lib)
|
SET(LIBRARY_OUTPUT_PATH ${PROJECT_BINARY_DIR}/lib)
|
||||||
|
|
||||||
SET(LIBCPPJIEBA_SRC HMMSegment.cpp MixSegment.cpp)
|
SET(LIBCPPJIEBA_SRC MixSegment.cpp)
|
||||||
ADD_LIBRARY(cppjieba STATIC ${LIBCPPJIEBA_SRC})
|
ADD_LIBRARY(cppjieba STATIC ${LIBCPPJIEBA_SRC})
|
||||||
ADD_EXECUTABLE(cjsegment segment.cpp)
|
ADD_EXECUTABLE(cjsegment segment.cpp)
|
||||||
ADD_EXECUTABLE(cjserver server.cpp)
|
ADD_EXECUTABLE(cjserver server.cpp)
|
||||||
|
@ -1,59 +0,0 @@
|
|||||||
#ifndef CPPJIBEA_HMMSEGMENT_H
|
|
||||||
#define CPPJIBEA_HMMSEGMENT_H
|
|
||||||
|
|
||||||
#include <iostream>
|
|
||||||
#include <fstream>
|
|
||||||
#include <memory.h>
|
|
||||||
#include "Limonp/str_functs.hpp"
|
|
||||||
#include "Limonp/logger.hpp"
|
|
||||||
#include "globals.h"
|
|
||||||
#include "TransCode.hpp"
|
|
||||||
#include "ISegment.hpp"
|
|
||||||
#include "SegmentBase.hpp"
|
|
||||||
|
|
||||||
namespace CppJieba
|
|
||||||
{
|
|
||||||
using namespace Limonp;
|
|
||||||
class HMMSegment: public SegmentBase
|
|
||||||
{
|
|
||||||
public:
|
|
||||||
/*
|
|
||||||
* STATUS:
|
|
||||||
* 0:B, 1:E, 2:M, 3:S
|
|
||||||
* */
|
|
||||||
enum {B = 0, E = 1, M = 2, S = 3, STATUS_SUM = 4};
|
|
||||||
private:
|
|
||||||
char _statMap[STATUS_SUM];
|
|
||||||
double _startProb[STATUS_SUM];
|
|
||||||
double _transProb[STATUS_SUM][STATUS_SUM];
|
|
||||||
EmitProbMap _emitProbB;
|
|
||||||
EmitProbMap _emitProbE;
|
|
||||||
EmitProbMap _emitProbM;
|
|
||||||
EmitProbMap _emitProbS;
|
|
||||||
vector<EmitProbMap* > _emitProbVec;
|
|
||||||
|
|
||||||
public:
|
|
||||||
HMMSegment();
|
|
||||||
virtual ~HMMSegment();
|
|
||||||
public:
|
|
||||||
bool init(const char* const modelPath);
|
|
||||||
bool dispose();
|
|
||||||
public:
|
|
||||||
bool cut(Unicode::const_iterator begin, Unicode::const_iterator end, vector<Unicode>& res)const ;
|
|
||||||
bool cut(const string& str, vector<string>& res)const;
|
|
||||||
bool cut(Unicode::const_iterator begin, Unicode::const_iterator end, vector<string>& res)const;
|
|
||||||
//virtual bool cut(const string& str, vector<string>& res)const;
|
|
||||||
|
|
||||||
private:
|
|
||||||
bool _viterbi(Unicode::const_iterator begin, Unicode::const_iterator end, vector<uint>& status)const;
|
|
||||||
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);
|
|
||||||
double _getEmitProb(const EmitProbMap* ptMp, uint16_t key, double defVal)const ;
|
|
||||||
|
|
||||||
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
#endif
|
|
@ -1,8 +1,39 @@
|
|||||||
#include "HMMSegment.h"
|
#ifndef CPPJIBEA_HMMSEGMENT_H
|
||||||
|
#define CPPJIBEA_HMMSEGMENT_H
|
||||||
|
|
||||||
|
#include <iostream>
|
||||||
|
#include <fstream>
|
||||||
|
#include <memory.h>
|
||||||
|
#include "Limonp/str_functs.hpp"
|
||||||
|
#include "Limonp/logger.hpp"
|
||||||
|
#include "globals.h"
|
||||||
|
#include "TransCode.hpp"
|
||||||
|
#include "ISegment.hpp"
|
||||||
|
#include "SegmentBase.hpp"
|
||||||
|
|
||||||
namespace CppJieba
|
namespace CppJieba
|
||||||
{
|
{
|
||||||
HMMSegment::HMMSegment()
|
using namespace Limonp;
|
||||||
|
class HMMSegment: public SegmentBase
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
/*
|
||||||
|
* STATUS:
|
||||||
|
* 0:B, 1:E, 2:M, 3:S
|
||||||
|
* */
|
||||||
|
enum {B = 0, E = 1, M = 2, S = 3, STATUS_SUM = 4};
|
||||||
|
private:
|
||||||
|
char _statMap[STATUS_SUM];
|
||||||
|
double _startProb[STATUS_SUM];
|
||||||
|
double _transProb[STATUS_SUM][STATUS_SUM];
|
||||||
|
EmitProbMap _emitProbB;
|
||||||
|
EmitProbMap _emitProbE;
|
||||||
|
EmitProbMap _emitProbM;
|
||||||
|
EmitProbMap _emitProbS;
|
||||||
|
vector<EmitProbMap* > _emitProbVec;
|
||||||
|
|
||||||
|
public:
|
||||||
|
HMMSegment()
|
||||||
{
|
{
|
||||||
memset(_startProb, 0, sizeof(_startProb));
|
memset(_startProb, 0, sizeof(_startProb));
|
||||||
memset(_transProb, 0, sizeof(_transProb));
|
memset(_transProb, 0, sizeof(_transProb));
|
||||||
@ -15,97 +46,22 @@ namespace CppJieba
|
|||||||
_emitProbVec.push_back(&_emitProbM);
|
_emitProbVec.push_back(&_emitProbM);
|
||||||
_emitProbVec.push_back(&_emitProbS);
|
_emitProbVec.push_back(&_emitProbS);
|
||||||
}
|
}
|
||||||
|
virtual ~HMMSegment()
|
||||||
HMMSegment::~HMMSegment()
|
|
||||||
{
|
{
|
||||||
dispose();
|
dispose();
|
||||||
}
|
}
|
||||||
|
public:
|
||||||
bool HMMSegment::init(const char* const modelPath)
|
bool init(const char* const modelPath)
|
||||||
{
|
{
|
||||||
return _setInitFlag(_loadModel(modelPath));
|
return _setInitFlag(_loadModel(modelPath));
|
||||||
}
|
}
|
||||||
|
bool dispose()
|
||||||
bool HMMSegment::dispose()
|
|
||||||
{
|
{
|
||||||
_setInitFlag(false);
|
_setInitFlag(false);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
public:
|
||||||
bool HMMSegment::_loadModel(const char* const filePath)
|
bool cut(Unicode::const_iterator begin, Unicode::const_iterator end, vector<Unicode>& res)const
|
||||||
{
|
|
||||||
LogInfo("loadModel [%s] start ...", filePath);
|
|
||||||
ifstream ifile(filePath);
|
|
||||||
string line;
|
|
||||||
vector<string> tmp;
|
|
||||||
vector<string> tmp2;
|
|
||||||
//load _startProb
|
|
||||||
if(!_getLine(ifile, line))
|
|
||||||
{
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
splitStr(line, tmp, " ");
|
|
||||||
if(tmp.size() != STATUS_SUM)
|
|
||||||
{
|
|
||||||
LogError("start_p illegal");
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
for(uint j = 0; j< tmp.size(); j++)
|
|
||||||
{
|
|
||||||
_startProb[j] = atof(tmp[j].c_str());
|
|
||||||
//cout<<_startProb[j]<<endl;
|
|
||||||
}
|
|
||||||
|
|
||||||
//load _transProb
|
|
||||||
for(uint i = 0; i < STATUS_SUM; i++)
|
|
||||||
{
|
|
||||||
if(!_getLine(ifile, line))
|
|
||||||
{
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
splitStr(line, tmp, " ");
|
|
||||||
if(tmp.size() != STATUS_SUM)
|
|
||||||
{
|
|
||||||
LogError("trans_p illegal");
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
for(uint j =0; j < STATUS_SUM; j++)
|
|
||||||
{
|
|
||||||
_transProb[i][j] = atof(tmp[j].c_str());
|
|
||||||
//cout<<_transProb[i][j]<<endl;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
//load _emitProbB
|
|
||||||
if(!_getLine(ifile, line) || !_loadEmitProb(line, _emitProbB))
|
|
||||||
{
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
//load _emitProbE
|
|
||||||
if(!_getLine(ifile, line) || !_loadEmitProb(line, _emitProbE))
|
|
||||||
{
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
//load _emitProbM
|
|
||||||
if(!_getLine(ifile, line) || !_loadEmitProb(line, _emitProbM))
|
|
||||||
{
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
//load _emitProbS
|
|
||||||
if(!_getLine(ifile, line) || !_loadEmitProb(line, _emitProbS))
|
|
||||||
{
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
LogInfo("loadModel [%s] end.", filePath);
|
|
||||||
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
bool HMMSegment::cut(Unicode::const_iterator begin, Unicode::const_iterator end, vector<Unicode>& res)const
|
|
||||||
{
|
{
|
||||||
if(!_getInitFlag())
|
if(!_getInitFlag())
|
||||||
{
|
{
|
||||||
@ -132,13 +88,11 @@ namespace CppJieba
|
|||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
bool cut(const string& str, vector<string>& res)const
|
||||||
bool HMMSegment::cut(const string& str, vector<string>& res)const
|
|
||||||
{
|
{
|
||||||
return SegmentBase::cut(str, res);
|
return SegmentBase::cut(str, res);
|
||||||
}
|
}
|
||||||
|
bool cut(Unicode::const_iterator begin, Unicode::const_iterator end, vector<string>& res)const
|
||||||
bool HMMSegment::cut(Unicode::const_iterator begin, Unicode::const_iterator end, vector<string>& res) const
|
|
||||||
{
|
{
|
||||||
if(!_getInitFlag())
|
if(!_getInitFlag())
|
||||||
{
|
{
|
||||||
@ -164,8 +118,10 @@ namespace CppJieba
|
|||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
//virtual bool cut(const string& str, vector<string>& res)const;
|
||||||
|
|
||||||
bool HMMSegment::_viterbi(Unicode::const_iterator begin, Unicode::const_iterator end, vector<uint>& status)const
|
private:
|
||||||
|
bool _viterbi(Unicode::const_iterator begin, Unicode::const_iterator end, vector<uint>& status)const
|
||||||
{
|
{
|
||||||
if(begin == end)
|
if(begin == end)
|
||||||
{
|
{
|
||||||
@ -247,8 +203,79 @@ namespace CppJieba
|
|||||||
delete [] weight;
|
delete [] weight;
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
bool _loadModel(const char* const filePath)
|
||||||
|
{
|
||||||
|
LogInfo("loadModel [%s] start ...", filePath);
|
||||||
|
ifstream ifile(filePath);
|
||||||
|
string line;
|
||||||
|
vector<string> tmp;
|
||||||
|
vector<string> tmp2;
|
||||||
|
//load _startProb
|
||||||
|
if(!_getLine(ifile, line))
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
splitStr(line, tmp, " ");
|
||||||
|
if(tmp.size() != STATUS_SUM)
|
||||||
|
{
|
||||||
|
LogError("start_p illegal");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
for(uint j = 0; j< tmp.size(); j++)
|
||||||
|
{
|
||||||
|
_startProb[j] = atof(tmp[j].c_str());
|
||||||
|
//cout<<_startProb[j]<<endl;
|
||||||
|
}
|
||||||
|
|
||||||
bool HMMSegment::_getLine(ifstream& ifile, string& line)
|
//load _transProb
|
||||||
|
for(uint i = 0; i < STATUS_SUM; i++)
|
||||||
|
{
|
||||||
|
if(!_getLine(ifile, line))
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
splitStr(line, tmp, " ");
|
||||||
|
if(tmp.size() != STATUS_SUM)
|
||||||
|
{
|
||||||
|
LogError("trans_p illegal");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
for(uint j =0; j < STATUS_SUM; j++)
|
||||||
|
{
|
||||||
|
_transProb[i][j] = atof(tmp[j].c_str());
|
||||||
|
//cout<<_transProb[i][j]<<endl;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
//load _emitProbB
|
||||||
|
if(!_getLine(ifile, line) || !_loadEmitProb(line, _emitProbB))
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
//load _emitProbE
|
||||||
|
if(!_getLine(ifile, line) || !_loadEmitProb(line, _emitProbE))
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
//load _emitProbM
|
||||||
|
if(!_getLine(ifile, line) || !_loadEmitProb(line, _emitProbM))
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
//load _emitProbS
|
||||||
|
if(!_getLine(ifile, line) || !_loadEmitProb(line, _emitProbS))
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
LogInfo("loadModel [%s] end.", filePath);
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
bool _getLine(ifstream& ifile, string& line)
|
||||||
{
|
{
|
||||||
while(getline(ifile, line))
|
while(getline(ifile, line))
|
||||||
{
|
{
|
||||||
@ -265,8 +292,7 @@ namespace CppJieba
|
|||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
bool _loadEmitProb(const string& line, EmitProbMap& mp)
|
||||||
bool HMMSegment::_loadEmitProb(const string& line, EmitProbMap& mp)
|
|
||||||
{
|
{
|
||||||
if(line.empty())
|
if(line.empty())
|
||||||
{
|
{
|
||||||
@ -292,8 +318,7 @@ namespace CppJieba
|
|||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
bool _decodeOne(const string& str, uint16_t& res)
|
||||||
bool HMMSegment::_decodeOne(const string& str, uint16_t& res)
|
|
||||||
{
|
{
|
||||||
Unicode ui16;
|
Unicode ui16;
|
||||||
if(!TransCode::decode(str, ui16) || ui16.size() != 1)
|
if(!TransCode::decode(str, ui16) || ui16.size() != 1)
|
||||||
@ -303,8 +328,7 @@ namespace CppJieba
|
|||||||
res = ui16[0];
|
res = ui16[0];
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
double _getEmitProb(const EmitProbMap* ptMp, uint16_t key, double defVal)const
|
||||||
double HMMSegment::_getEmitProb(const EmitProbMap* ptMp, uint16_t key, double defVal)const
|
|
||||||
{
|
{
|
||||||
EmitProbMap::const_iterator cit = ptMp->find(key);
|
EmitProbMap::const_iterator cit = ptMp->find(key);
|
||||||
if(cit == ptMp->end())
|
if(cit == ptMp->end())
|
||||||
@ -314,28 +338,9 @@ namespace CppJieba
|
|||||||
return cit->second;
|
return cit->second;
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
#ifdef HMMSEGMENT_UT
|
};
|
||||||
using namespace CppJieba;
|
|
||||||
|
|
||||||
|
|
||||||
size_t add(size_t a, size_t b)
|
|
||||||
{
|
|
||||||
return a*b;
|
|
||||||
}
|
|
||||||
int main()
|
|
||||||
{
|
|
||||||
TransCode::setUtf8Enc();
|
|
||||||
HMMSegment hmm;
|
|
||||||
hmm.loadModel("../dicts/hmm_model.utf8");
|
|
||||||
vector<string> res;
|
|
||||||
hmm.cut("小明硕士毕业于北邮网络研究院。。.", res);
|
|
||||||
cout<<joinStr(res, "/")<<endl;
|
|
||||||
|
|
||||||
|
|
||||||
return 0;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#endif
|
#endif
|
@ -2,7 +2,7 @@
|
|||||||
#define CPPJIEBA_MIXSEGMENT_H
|
#define CPPJIEBA_MIXSEGMENT_H
|
||||||
|
|
||||||
#include "MPSegment.hpp"
|
#include "MPSegment.hpp"
|
||||||
#include "HMMSegment.h"
|
#include "HMMSegment.hpp"
|
||||||
#include "Limonp/str_functs.hpp"
|
#include "Limonp/str_functs.hpp"
|
||||||
|
|
||||||
namespace CppJieba
|
namespace CppJieba
|
||||||
|
@ -2,7 +2,7 @@
|
|||||||
#include <fstream>
|
#include <fstream>
|
||||||
#include "Limonp/ArgvContext.hpp"
|
#include "Limonp/ArgvContext.hpp"
|
||||||
#include "MPSegment.hpp"
|
#include "MPSegment.hpp"
|
||||||
#include "HMMSegment.h"
|
#include "HMMSegment.hpp"
|
||||||
#include "MixSegment.h"
|
#include "MixSegment.h"
|
||||||
|
|
||||||
using namespace CppJieba;
|
using namespace CppJieba;
|
||||||
|
@ -8,7 +8,7 @@
|
|||||||
#include "Husky/Daemon.h"
|
#include "Husky/Daemon.h"
|
||||||
#include "Husky/ServerFrame.h"
|
#include "Husky/ServerFrame.h"
|
||||||
#include "MPSegment.hpp"
|
#include "MPSegment.hpp"
|
||||||
#include "HMMSegment.h"
|
#include "HMMSegment.hpp"
|
||||||
#include "MixSegment.h"
|
#include "MixSegment.h"
|
||||||
|
|
||||||
using namespace Husky;
|
using namespace Husky;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user