From 12908b9a2d948533076ad2faa7e1d0043529b4a5 Mon Sep 17 00:00:00 2001 From: gwdwyy Date: Mon, 8 Jul 2013 19:25:03 +0800 Subject: [PATCH] finished _calcDP --- Segment.cpp | 41 ++++++++++++++++++++++++++++++++++++++++- Segment.h | 2 +- 2 files changed, 41 insertions(+), 2 deletions(-) diff --git a/Segment.cpp b/Segment.cpp index 9703601..3359ba2 100644 --- a/Segment.cpp +++ b/Segment.cpp @@ -48,9 +48,21 @@ namespace CppJieba } dag.push_back(vec); } + + cout<<__FILE__<<__LINE__< > dp; + _calcDP(uniStr, len, dag, dp); + + cout<<__FILE__<<__LINE__< >& res) + bool Segment::_calcDP(const ChUnicode* uniStr, size_t len, const vector >& dag, vector >& res) { + if(len != dag.size()) + { + LogFatal("dag is illegal!"); + return false; + } + res.clear(); + res.assign(len + 1, pair(-1, 0.0)); + res[len].first = -1; + res[len].second = 0.0; + for(int i = len - 1; i >= 0; i--) + { + // calc max + res[i].first = -1; + res[i].second = -(numeric_limits::max()); + for(int j = 0; j < dag[i].size(); j++) + { + int pos = dag[i][j]; + double val = _trie.getWeight(uniStr + i, pos - i + 1) + res[pos+1].second; + //cout<<__LINE__<<"," + // < res[i].second) + { + res[i].first = pos; + res[i].second = val; + } + } + } return true; } diff --git a/Segment.h b/Segment.h index d3fb150..4ea4226 100644 --- a/Segment.h +++ b/Segment.h @@ -22,7 +22,7 @@ namespace CppJieba private: size_t _utf8ToUni(const string& chStr, ChUnicode* uniStr, size_t size); - bool _calcDP(const ChUnicode* uniStr, size_t len, vector >& res); + bool _calcDP(const ChUnicode* uniStr, size_t len, const vector >& dag, vector >& res); private: enum {bufSize = 1024};