Skip to content

Instantly share code, notes, and snippets.

@DerekZiemba
Created April 14, 2017 03:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save DerekZiemba/cef3e18f576bb659f71118de6b930344 to your computer and use it in GitHub Desktop.
Save DerekZiemba/cef3e18f576bb659f71118de6b930344 to your computer and use it in GitHub Desktop.
function getLevenshteinDistance(a, b) {
var tmp;
if (a.length === 0) { return b.length; }
if (b.length === 0) { return a.length; }
if (a.length > b.length) { tmp = a; a = b; b = tmp; }
var i, j, res, alen = a.length, blen = b.length, row = Array(alen);
for (i = 0; i <= alen; i++) { row[i] = i; }
for (i = 1; i <= blen; i++) {
res = i;
for (j = 1; j <= alen; j++) {
tmp = row[j - 1];
row[j - 1] = res;
res = b[i - 1] === a[j - 1] ? tmp : Math.min(tmp + 1, Math.min(res + 1, row[j] + 1));
}
}
return res;
}
/*Returns a percentage indicating how close the strings matched. Is included because jsPerf doesn't let you remove cases...*/
function findMatchPercent(a, b) {
var tmp;
if (a.length === 0) { return b.length; }
if (b.length === 0) { return a.length; }
if (a.length > b.length) { tmp = a; a = b; b = tmp; }
var i, j, res, alen = a.length, blen = b.length, row = Array(alen);
for (i = 0; i <= alen; i++) { row[i] = i; }
for (i = 1; i <= blen; i++) {
res = i;
for (j = 1; j <= alen; j++) {
tmp = row[j - 1];
row[j - 1] = res;
res = b[i - 1] === a[j - 1] ? tmp : Math.min(tmp + 1, Math.min(res + 1, row[j] + 1));
}
}
return (blen - res) / blen;
}
@sramam
Copy link

sramam commented Oct 12, 2017

License?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment