Home >Backend Development >PHP Tutorial >Customize levenshtein Purpose: Understand the principle of levenshtein algorithm

Customize levenshtein Purpose: Understand the principle of levenshtein algorithm

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOriginal
2016-07-25 09:01:251358browse
参考 : 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 <= strlen($src); $i++) {
  10. $temp[$i][0] = $i;
  11. }
  12. for($j = 0; $j <= strlen($dst); $j++) {
  13. $temp[0][$j] = $j;
  14. }
  15. for ($i = 1;$i <= strlen($src); $i++) {
  16. $src_i = $src{$i - 1};
  17. for ($j = 1; $j <= strlen($dst); $j++) {
  18. $dst_j = $dst{$j - 1};
  19. if ($src_i == $dst_j) {
  20. $cost = 0;
  21. } else {
  22. $cost = 1;
  23. }
  24. $temp[$i][$j] = min($temp[$i-1][$j]+1, $temp[$i][$j-1]+1, $temp[$i-1][$j-1] + $cost);
  25. }
  26. }
  27. return $temp[$i-1][$j-1];
  28. }
  29. echo _levenshtein("hello", "HElloo");
复制代码


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn