Skip to content

Instantly share code, notes, and snippets.

@isaaclyman
Created May 28, 2015 15:09
Show Gist options
  • Save isaaclyman/a38c43ea7af694235f4f to your computer and use it in GitHub Desktop.
Save isaaclyman/a38c43ea7af694235f4f to your computer and use it in GitHub Desktop.
Damerau-Levenshtein: Compare a string to an array of strings, each of which may be one or more words which are compared separately; return X of the closest words/phrases
var levenshteinArray = function (item, itemArray, howMany) {
return itemArray.map(function (arrayItem) {
return {
item: arrayItem,
distance: splitLevDist(item, arrayItem)
};
}).sort(function (a, b) {
return a.distance - b.distance;
}).slice(0, howMany).map(function (item) {
return item.item;
});
};
var splitLevDist = function (item, arrayItem) {
if (arrayItem.indexOf(' ') === -1) {
return levDist(item, arrayItem);
}
return Math.min.apply(null, arrayItem.split(' ').map(function (splitArrayItem) {
return levDist(item, splitArrayItem);
}));
};
// Thanks to James Westgate for this highly optimized Levenshtein distance finder
var levDist=function(r,a){var t=[],f=r.length,n=a.length;if(0==f)return n;if(0==n)return f;for(var v=f;v>=0;v--)t[v]=[];for(var v=f;v>=0;v--)t[v][0]=v;for(var e=n;e>=0;e--)t[0][e]=e;for(var v=1;f>=v;v++)for(var h=r.charAt(v-1),e=1;n>=e;e++){if(v==e&&t[v][e]>4)return f;var i=a.charAt(e-1),o=h==i?0:1,c=t[v-1][e]+1,u=t[v][e-1]+1,A=t[v-1][e-1]+o;c>u&&(c=u),c>A&&(c=A),t[v][e]=c,v>1&&e>1&&h==a.charAt(e-2)&&r.charAt(v-2)==i&&(t[v][e]=Math.min(t[v][e],t[v-2][e-2]+o))}return t[f][n]};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment