首页  >  文章  >  后端开发  >  自定义levenshtein 目的:了解levenshtein 算法原理

自定义levenshtein 目的:了解levenshtein 算法原理

WBOY
WBOY原创
2016-07-25 09:01:251253浏览
参考 : http://www.cnblogs.com/ymind/archive/2012/03/27/fast-memory-efficient-Levenshtein-algorithm.html
  1. function _levenshtein($src, $dst){
  2. if (empty($src)) {
  3. return $dst;
  4. }
  5. if (empty($dst)) {
  6. return $src;
  7. }
  8. $temp = array();
  9. for($i = 0; $i $temp[$i][0] = $i;
  10. }
  11. for($j = 0; $j $temp[0][$j] = $j;
  12. }
  13. for ($i = 1;$i
  14. $src_i = $src{$i - 1};
  15. for ($j = 1; $j
  16. $dst_j = $dst{$j - 1};
  17. if ($src_i == $dst_j) {
  18. $cost = 0;
  19. } else {
  20. $cost = 1;
  21. }
  22. $temp[$i][$j] = min($temp[$i-1][$j]+1, $temp[$i][$j-1]+1, $temp[$i-1][$j-1] + $cost);
  23. }
  24. }
  25. return $temp[$i-1][$j-1];
  26. }
  27. echo _levenshtein("hello", "HElloo");
复制代码


声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn