Skip to content

Instantly share code, notes, and snippets.

@shibukawa
Created April 11, 2012 00:10
Show Gist options
  • Save shibukawa/2355852 to your computer and use it in GitHub Desktop.
Save shibukawa/2355852 to your computer and use it in GitHub Desktop.
Arranged Damerau–Levenshtein distance
function calcWeight(index, length)
{
var distance = (length - 1 - index) / (length - 1);
return distance * distance * 0.5 + 1;
}
function stringsDistance(source, target, distanceLimit)
{
if (source === target)
{
return 0;
}
var m = source.length;
var n = target.length;
if (Math.abs(m - n) > distanceLimit)
{
return 100;
}
var H = [];
var i, j;
for (i = 0; i < m + 2; i++)
{
var HH = [];
H[i] = HH;
for (j = 0; j < n + 2; j++)
{
HH[j] = 0;
}
}
var INF = m + n;
H[0][0] = INF;
for (i = 0; i <= m; i++)
{
H[i + 1].splice(0, 2, INF, i);
}
var H1 = H[1];
var H0 = H[0];
for (j = 0; j <= n; j++)
{
H1[j + 1] = j;
H0[j + 1] = INF;
}
var sd = {};
for (i = 1; i <= m; i++)
{
var DB = 0;
var Hi = H[i];
var Hi1 = H[i + 1];
var weight = calcWeight(i - 1, m);
var c1 = source[i - 1].toLowerCase();
for (j = 1; j <= n; j++)
{
var i1 = sd[target[j - 1]] || 0;
var j1 = DB;
var c2 = target[j - 1];
if (c1 === c2.toLowerCase())
{
Hi1[j + 1] = Hi[j];
DB = j;
}
else
{
Hi1[j + 1] = Math.min(Hi[j], Hi1[j], Hi[j + 1]) + 1 * weight;
}
Hi1[j + 1] = Math.min(Hi1[j + 1], H[i1][j1] + ((i - i1 - 1) + 1 + (j - j1 - 1)) * weight);
}
sd[source[i - 1]] = i;
//console.log(H);
}
return H[m + 1][n + 1];
}
function test(source, target)
{
console.log(source + " <--> " + target + " = " + stringsDistance(source, target, 3));
}
test("hello", "hellow");
test("hello", "helol");
test("hello", "helo");
test("hello", "helo");
test("hello", "Hello");
test("log", "cos");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment