Skip to content

Instantly share code, notes, and snippets.

@mrswadge
Forked from IgorInger/levenshtein.js
Created November 4, 2014 14:33
Show Gist options
  • Save mrswadge/34fcac77314e3d19695f to your computer and use it in GitHub Desktop.
Save mrswadge/34fcac77314e3d19695f to your computer and use it in GitHub Desktop.
(function($) {
$.fn.levenshteinDistance = function(u, v) {
var m = u.length;
var n = v.length;
var D = [];
for(var i = 0; i <= m; i++) {
D.push([]);
for(var j = 0; j <= n; j++) {
D[i][j] = 0;
}
}
for(var i = 1; i <= m; i++) {
for(var j = 1; j <= n; j++) {
if (j == 0) {
D[i][j] = i;
} else if (i == 0) {
D[i][j] = j;
} else {
D[i][j] = [D[i-1][j-1] + (u.charAt(i-1) != v.charAt(j-1)), D[i][j-1] + 1, D[i-1][j] + 1].sort()[0];
}
}
}
return D[m][n];
};
})(jQuery);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment