mirror of
https://github.com/yanyiwu/cppjieba.git
synced 2025-07-18 00:00:12 +08:00
improve viterbi
This commit is contained in:
parent
278b93e851
commit
687ebfc19b
@ -22,4 +22,3 @@ ADD_SUBDIRECTORY(test)
|
|||||||
|
|
||||||
ENABLE_TESTING()
|
ENABLE_TESTING()
|
||||||
ADD_TEST(NAME test.run COMMAND test.run)
|
ADD_TEST(NAME test.run COMMAND test.run)
|
||||||
ADD_TEST(NAME load_test COMMAND load_test)
|
|
||||||
|
@ -131,15 +131,11 @@ namespace CppJieba
|
|||||||
size_t Y = STATUS_SUM;
|
size_t Y = STATUS_SUM;
|
||||||
size_t X = end - begin;
|
size_t X = end - begin;
|
||||||
size_t XYSize = X * Y;
|
size_t XYSize = X * Y;
|
||||||
int * path;
|
|
||||||
double * weight;
|
|
||||||
size_t now, old, stat;
|
size_t now, old, stat;
|
||||||
double tmp, endE, endS;
|
double tmp, endE, endS;
|
||||||
|
|
||||||
path = new int [XYSize];
|
vector<int> path(XYSize);
|
||||||
assert(path);
|
vector<double> weight(XYSize);
|
||||||
weight = new double [XYSize];
|
|
||||||
assert(weight);
|
|
||||||
|
|
||||||
//start
|
//start
|
||||||
for(size_t y = 0; y < Y; y++)
|
for(size_t y = 0; y < Y; y++)
|
||||||
@ -147,8 +143,10 @@ namespace CppJieba
|
|||||||
weight[0 + y * X] = _startProb[y] + _getEmitProb(_emitProbVec[y], *begin, MIN_DOUBLE);
|
weight[0 + y * X] = _startProb[y] + _getEmitProb(_emitProbVec[y], *begin, MIN_DOUBLE);
|
||||||
path[0 + y * X] = -1;
|
path[0 + y * X] = -1;
|
||||||
}
|
}
|
||||||
//process
|
|
||||||
//for(; begin != end; begin++)
|
|
||||||
|
double emitProb;
|
||||||
|
|
||||||
for(size_t x = 1; x < X; x++)
|
for(size_t x = 1; x < X; x++)
|
||||||
{
|
{
|
||||||
for(size_t y = 0; y < Y; y++)
|
for(size_t y = 0; y < Y; y++)
|
||||||
@ -156,10 +154,11 @@ namespace CppJieba
|
|||||||
now = x + y*X;
|
now = x + y*X;
|
||||||
weight[now] = MIN_DOUBLE;
|
weight[now] = MIN_DOUBLE;
|
||||||
path[now] = E; // warning
|
path[now] = E; // warning
|
||||||
|
emitProb = _getEmitProb(_emitProbVec[y], *(begin+x), MIN_DOUBLE);
|
||||||
for(size_t preY = 0; preY < Y; preY++)
|
for(size_t preY = 0; preY < Y; preY++)
|
||||||
{
|
{
|
||||||
old = x - 1 + preY * X;
|
old = x - 1 + preY * X;
|
||||||
tmp = weight[old] + _transProb[preY][y] + _getEmitProb(_emitProbVec[y], *(begin+x), MIN_DOUBLE);
|
tmp = weight[old] + _transProb[preY][y] + emitProb;
|
||||||
if(tmp > weight[now])
|
if(tmp > weight[now])
|
||||||
{
|
{
|
||||||
weight[now] = tmp;
|
weight[now] = tmp;
|
||||||
@ -188,8 +187,6 @@ namespace CppJieba
|
|||||||
stat = path[x + stat*X];
|
stat = path[x + stat*X];
|
||||||
}
|
}
|
||||||
|
|
||||||
delete [] path;
|
|
||||||
delete [] weight;
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
bool _loadModel(const char* const filePath)
|
bool _loadModel(const char* const filePath)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user