Skip to content

Instantly share code, notes, and snippets.

@luchaninov
Last active August 28, 2020 20:36
Show Gist options
  • Star 6 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save luchaninov/a5730c453129ae159dfc to your computer and use it in GitHub Desktop.
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.
// 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