Created
April 14, 2017 03:00
-
-
Save DerekZiemba/cef3e18f576bb659f71118de6b930344 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
License?