21 namespace SpellChecker.Net.Search.Spell
28 private float threshold = 0.7f;
30 private static int[] Matches(String s1, String s2)
34 if (s1.Length > s2.Length)
45 var range = Math.Max(Max.Length / 2 - 1, 0);
46 var matchIndexes =
new int[Min.Length];
48 for (var i = 0; i < matchIndexes.Length; i++)
51 var matchFlags =
new bool[Max.Length];
54 for (var mi = 0; mi < Min.Length; mi++)
57 for (
int xi = Math.Max(mi - range, 0),
58 xn = Math.Min(mi + range + 1, Max.Length); xi < xn; xi++)
60 if (matchFlags[xi] || c1 != Max[xi])
continue;
62 matchIndexes[mi] = xi;
63 matchFlags[xi] =
true;
69 var ms1 =
new char[matches];
70 var ms2 =
new char[matches];
72 for (
int i = 0, si = 0; i < Min.Length; i++)
74 if (matchIndexes[i] != -1)
81 for (
int i = 0, si = 0; i < Max.Length; i++)
90 var transpositions = ms1.Where((t, mi) => t != ms2[mi]).Count();
93 for (var mi = 0; mi < Min.Length; mi++)
105 return new int[] { matches, transpositions / 2, prefix, Max.Length };
108 public float GetDistance(String s1, String s2)
110 var mtp = Matches(s1, s2);
111 var m = (float)mtp[0];
116 float j = ((m / s1.Length + m / s2.Length + (m - mtp[1]) / m)) / 3;
117 float jw = j < Threshold ? j : j + Math.Min(0.1f, 1f / mtp[3]) * mtp[2] * (1 - j);
125 public float Threshold
127 get {
return threshold; }
128 set { this.threshold = value; }