Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cppjieba动态规划部分代码有bug #156

Open
rookie-J opened this issue Jul 9, 2021 · 2 comments
Open

cppjieba动态规划部分代码有bug #156

rookie-J opened this issue Jul 9, 2021 · 2 comments

Comments

@rookie-J
Copy link

rookie-J commented Jul 9, 2021

参考python版本计算动态规划的逻辑,将CalcDP函数的计算顺序改为从后往前就可以了。
可直接替换代码如下:

void CalcDP(vector& dags) const {
size_t dagSize = dags.size();
size_t nextPos;
const DictUnit* p;
double val;

  for(size_t i = 0; i < dagSize; i++){
      dags[dagSize - i - 1].pInfo = NULL;
      dags[dagSize - i - 1].weight = MIN_DOUBLE;
      assert(!dags[dagSize - i - 1].nexts.empty());
      for(LocalVector<pair<size_t, const DictUnit*> >::const_iterator it = dags[dagSize - i - 1].nexts.begin();
          it != dags[dagSize - i - 1].nexts.end(); it++) {
          nextPos = it->first;
          p = it->second;
          val = 0.0;
          if(nextPos + 1 < dags.size()) {
              val += dags[nextPos + 1].weight;
          }

          if(p) {
              val += p->weight;
          } else {
              val += dictTrie_->GetMinWeight();
          }
          if(val > dags[dagSize - i - 1].weight) {
              dags[dagSize - i - 1].pInfo = p;
              dags[dagSize - i - 1].weight = val;
          }
      }
  }

}

@moyu505
Copy link

moyu505 commented Aug 3, 2021

解决的bug,能举例几个吗

@dawnranger
Copy link

作者本来用的就是从后往前遍历的逻辑(rbegin/rend)
见代码:
https://github.com/yanyiwu/cppjieba/blob/master/include/cppjieba/MPSegment.hpp#L85

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants