When calculating the similarity of a string or graph, the two commonly used algorithms, one is the editing distance, the other is the Hamming distance.
Simply put:
Editing distance is the ability to turn a string into another string with several edits
Hamming distance is the corresponding position to compare, find out the different number of characters
To find out more, please search by yourself.
The following are their PHP code implementations.
<?php/** * Calculate editing distance * * @param string $s 1 * @param string $s 2 */function levdist ($s 1, $s 2) { $len 1 = strlen ($s 1); $len 2 = strlen ($s 2); if ($len 1 == 0) { return $len 2; } if ($len 2 == 0) { return $len 1; } for ($i = 0; $i <= $len 1; $i + +) { $ matrix[$i][0] = 0; } for ($j = 0; $j <= $len 2; $j + +) { $ matrix[0][$j] = 0; } for ($i = 1; $i <= $len 1; $i + +) { $ch 1 = $s 1[$i - 1]; for ($j = 1; $j <= $len 2; $J + +) { $ch 2 = $s 2[$j - 1]; $temp = $ch 1 == $ch 2 ? 0 : 1; $arr = array ( $matrix [$i - 1][ $j] + 1, $matrix [$i] [$j - 1] + 1, $matrix [$i - 1][$j - 1] + $temp ); $ matrix[$i] [$j] = min ($arr); } } return $matrix [$len 1][$len 2];} /** * calculating Hamming distance * * @param string $s 1 * @param string $s 2 * @return boolean number */function hamdist ($s 1, $s 2) { $len 1 = strlen ($s 1); $len 2 = strlen ($s 2); if ($len 1 != $len 2) { return false; } $dist &nbsP;= 0; for ($i = 0; $i < $len 1; $i + +) { if ($s 1[$i] != $s 2[$i]) { $ dist++; } } return $dist;} $s 1 = "abcde", $s 2 = "acdeb", echo levdist ($s 1, $s 2);// output 2echo hamdist ( $s 1, $s 2);// output 4
This article is from the "Van Star Technology Blog" blog, please be sure to keep this source http://ustb80.blog.51cto.com/6139482/1569542
The PHP implementation of editing distance and Hamming distance