20 using System.Collections.Generic;
23 namespace SpellChecker.Net.Search.Spell
46 public float GetDistance(String source, String target)
48 int sl = source.Length;
49 int tl = target.Length;
51 if (sl == 0 || tl == 0)
66 for (
int ii = 0, ni = Math.Min(sl, tl); ii < ni; ii++)
68 if (source[ii] == target[ii])
73 return (
float)cost / Math.Max(sl, tl);
76 char[] sa =
new char[sl + n - 1];
82 for (
int ii = 0; ii < sa.Length; ii++)
90 sa[ii] = source[ii - n + 1];
93 p =
new float[sl + 1];
94 d =
new float[sl + 1];
100 char[] t_j =
new char[n];
102 for (i = 0; i <= sl; i++)
107 for (j = 1; j <= tl; j++)
112 for (
int ti = 0; ti < n - j; ti++)
116 for (
int ti = n - j; ti < n; ti++)
118 t_j[ti] = target[ti - (n - j)];
123 t_j = target.Substring(j - n, n).ToCharArray();
126 for (i = 1; i <= sl; i++)
131 for (
int ni = 0; ni < n; ni++)
133 if (sa[i - 1 + ni] != t_j[ni])
137 else if (sa[i - 1 + ni] == 0)
142 float ec = (float)cost / tn;
144 d[i] = Math.Min(Math.Min(d[i - 1] + 1, p[i] + 1), p[i - 1] + ec);
154 return 1.0f - ((float)p[sl] / Math.Max(tl, sl));