Last active
July 17, 2024 22:36
-
-
Save luchaninov/a5730c453129ae159dfc to your computer and use it in GitHub Desktop.
Levenshtein distance is a string metric for measuring the difference between two sequences. Informally, the Levenshtein distance between two words is the minimum number of single-character edits (i.e. insertions, deletions or substitutions) required to change one word into the other.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// from https://github.com/thinkphp/String.levenshtein/blob/master/Source/String.levenshtein.js but no depencencies | |
function levenshtein(str1, str2) { | |
var cost = new Array(), | |
n = str1.length, | |
m = str2.length, | |
i, j; | |
var minimum = function(a, b, c) { | |
var min = a; | |
if (b < min) { | |
min = b; | |
} | |
if (c < min) { | |
min = c; | |
} | |
return min; | |
} | |
if (n == 0) { | |
return; | |
} | |
if (m == 0) { | |
return; | |
} | |
for (var i = 0; i <= n; i++) { | |
cost[i] = new Array(); | |
} | |
for (i = 0; i <= n; i++) { | |
cost[i][0] = i; | |
} | |
for (j = 0; j <= m; j++) { | |
cost[0][j] = j; | |
} | |
for (i = 1; i <= n; i++) { | |
var x = str1.charAt(i - 1); | |
for (j = 1; j <= m; j++) { | |
var y = str2.charAt(j - 1); | |
if (x == y) { | |
cost[i][j] = cost[i - 1][j - 1]; | |
} else { | |
cost[i][j] = 1 + minimum(cost[i - 1][j - 1], cost[i][j - 1], cost[i - 1][j]); | |
} | |
} //endfor | |
} //endfor | |
return cost[n][m]; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi !
This code can optimized / shortened :
new Array()
can be replaced by a[ ]
Math.min( a, b, c )
now;
) are useless if followed by a newline{ }
) are useless if followed by a single statementstr1.charAt( i - 1 ) == str1[i - 1]