From 687ebfc19baabba1ed760adf8373b3f68110a3e3 Mon Sep 17 00:00:00 2001 From: wyy Date: Mon, 7 Apr 2014 22:54:01 -0700 Subject: [PATCH] improve viterbi --- CMakeLists.txt | 1 - src/HMMSegment.hpp | 19 ++++++++----------- 2 files changed, 8 insertions(+), 12 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 79247e2..1f4176b 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -22,4 +22,3 @@ ADD_SUBDIRECTORY(test) ENABLE_TESTING() ADD_TEST(NAME test.run COMMAND test.run) -ADD_TEST(NAME load_test COMMAND load_test) diff --git a/src/HMMSegment.hpp b/src/HMMSegment.hpp index bd3a1df..41c5a77 100644 --- a/src/HMMSegment.hpp +++ b/src/HMMSegment.hpp @@ -131,15 +131,11 @@ namespace CppJieba size_t Y = STATUS_SUM; size_t X = end - begin; size_t XYSize = X * Y; - int * path; - double * weight; size_t now, old, stat; double tmp, endE, endS; - path = new int [XYSize]; - assert(path); - weight = new double [XYSize]; - assert(weight); + vector path(XYSize); + vector weight(XYSize); //start 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); path[0 + y * X] = -1; } - //process - //for(; begin != end; begin++) + + + double emitProb; + for(size_t x = 1; x < X; x++) { for(size_t y = 0; y < Y; y++) @@ -156,10 +154,11 @@ namespace CppJieba now = x + y*X; weight[now] = MIN_DOUBLE; path[now] = E; // warning + emitProb = _getEmitProb(_emitProbVec[y], *(begin+x), MIN_DOUBLE); for(size_t preY = 0; preY < Y; preY++) { 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]) { weight[now] = tmp; @@ -188,8 +187,6 @@ namespace CppJieba stat = path[x + stat*X]; } - delete [] path; - delete [] weight; return true; } bool _loadModel(const char* const filePath)