Skip to content

Instantly share code, notes, and snippets.

@robrobbins
Created December 26, 2011 07:06
Show Gist options
  • Save robrobbins/1520662 to your computer and use it in GitHub Desktop.
Save robrobbins/1520662 to your computer and use it in GitHub Desktop.
A (node|common).js module implementation of the Levenshtein Distance Algorithm
// module for calculating the Levenshtein distance between 2 strings
// a matrix to hold the distances
var d = [],
// reusable lengths of the passed in words
lenOne, lenTwo,
// counters
i,j,
// temp equality check
eq;
exports.distance = function(one, two) {
lenOne = one.length;
lenTwo = two.length;
// filling along the y axis,
// NOTE: <= operator to retain the initial 0
for (i = 0; i <= lenOne; i++) {
// start an array at each char of word one
d[i] = [];
// numeric count of each char beyond 0
d[i][0] = i;
}
// filling along the x axis, adjusted for pre-existing d[0][0]
for (j = 1; j <= lenTwo; j++) {
d[0][j] = j;
}
// flood fill the matrix with the edit distances,
// note: we'll reuse the len vars
for (i = 1; i <= lenOne; i++) {
for (j = 1; j <= lenTwo; j++) {
// if eq no edit is needed
eq = one[i-1] === two[j-1] ? 0 : 1;
d[i][j] = Math.min(
// insertions, deletions, or nothing
d[i-1][j]+1,
d[i][j-1]+1,
d[i-1][j-1] + eq);
}
}
// return the last item in the matrix (return d to see the matrix)
return d[lenOne][lenTwo];
};
// as an example, used with a node.js 'entrypoint':
// var lev = require('./levenshtein.js');
// console.log(lev.distance(process.argv[2], process.argv[3]));
//
// ~$ node entrypoint.js foshizzle fubizzle
// => 3
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment