Skip to content

Instantly share code, notes, and snippets.

@nadeesha
Created September 10, 2013 08:52
Show Gist options
  • Save nadeesha/6506733 to your computer and use it in GitHub Desktop.
Save nadeesha/6506733 to your computer and use it in GitHub Desktop.
private double FuzzyStringComparison(string source, string target)
{
// if both of the strings are empty, return 0
// else if the source is empty but not the target, the distance is the target length
// else if the target is empty and not the source, the the distance is the source length.
if (string.IsNullOrEmpty(source))
{
if (string.IsNullOrEmpty(target))
{
return 0;
}
else
{
return target.Length;
}
}
else if (string.IsNullOrEmpty(target))
{
return source.Length;
}
// enter the matrix!
// the +2: for the first row and column + since the index starts at 0
var score = new int[source.Length + 2, target.Length + 2];
var INF = source.Length + target.Length;
score[0, 0] = INF;
for (var i = 0; i <= source.Length; i++)
{
score[i + 1, 1] = i;
score[i + 1, 0] = INF;
}
for (var j = 0; j <= target.Length; j++)
{
score[1, j + 1] = j;
score[0, j + 1] = INF;
}
var sd = new SortedDictionary<char, int>();
foreach (var letter in (source + target))
{
if (!sd.ContainsKey(letter))
sd.Add(letter, 0);
}
for (var i = 1; i <= source.Length; i++)
{
var DB = 0;
for (var j = 1; j <= target.Length; j++)
{
var i1 = sd[target[j - 1]];
var j1 = DB;
if (source[i - 1] == target[j - 1])
{
score[i + 1, j + 1] = score[i, j];
DB = j;
}
else
{
score[i + 1, j + 1] = Math.Min(score[i, j], Math.Min(score[i + 1, j], score[i, j + 1])) + 1;
}
score[i + 1, j + 1] = Math.Min(score[i + 1, j + 1], score[i1, j1] + (i - i1 - 1) + 1 + (j - j1 - 1));
}
sd[source[i - 1]] = i;
}
return 1.0 - (score[source.Length + 1, target.Length + 1] / (Math.Max(source.Length, target.Length) * 1.0));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment