Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions include/cppjieba/MPSegment.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ class MPSegment: public SegmentTagged {
return dictTrie_->IsUserDictSingleChineseWord(value);
}
private:
/*
void CalcDP(vector<DatDag>& dags) const {
for (auto rit = dags.rbegin(); rit != dags.rend(); rit++) {
rit->max_next = -1;
Expand All @@ -64,6 +65,33 @@ class MPSegment: public SegmentTagged {
}
}
}
*/
void CalcDP(vector<DatDag>& dags) const {
size_t size = dags.size();

for (size_t i = 0; i < size; i++) {
dags[size - i - 1].max_next = -1;
dags[size - i - 1].max_weight = MIN_DOUBLE;

for (const auto & it : dags[size - i - 1].nexts) {
const auto nextPos = it.first;
double val = dictTrie_->GetMinWeight();

if (nullptr != it.second) {
val = it.second->weight;
}

if (nextPos < dags.size()) {
val += dags[nextPos].max_weight;
}

if ((nextPos <= dags.size()) && (val > dags[size - i - 1].max_weight)) {
dags[size - i - 1].max_weight = val;
dags[size - i - 1].max_next = nextPos;
}
}
}
}
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

原来的实现是 for (auto rit = dags.rbegin(); rit != dags.rend(); rit++) {,也是倒序遍历,跟新加的这个是完全一样的逻辑。


void CutByDag(RuneStrArray::const_iterator begin,
RuneStrArray::const_iterator,
Expand Down