21 using Term = Lucene.Net.Index.Term;
23 namespace Lucene.Net.Search
40 private float similarity;
41 private bool endEnum =
false;
43 private bool isDisposed;
45 private Term searchTerm = null;
46 private System.String field;
47 private System.String text;
48 private System.String prefix;
50 private float minimumSimilarity;
51 private float scale_factor;
109 if (minSimilarity >= 1.0f)
110 throw new System.ArgumentException(
"minimumSimilarity cannot be greater than or equal to 1");
111 else if (minSimilarity < 0.0f)
112 throw new System.ArgumentException(
"minimumSimilarity cannot be less than 0");
113 if (prefixLength < 0)
114 throw new System.ArgumentException(
"prefixLength cannot be less than 0");
116 this.minimumSimilarity = minSimilarity;
117 this.scale_factor = 1.0f / (1.0f - minimumSimilarity);
118 this.searchTerm = term;
119 this.field = searchTerm.Field;
123 int fullSearchTermLength = searchTerm.Text.Length;
124 int realPrefixLength = prefixLength > fullSearchTermLength?fullSearchTermLength:prefixLength;
126 this.text = searchTerm.Text.Substring(realPrefixLength);
127 this.prefix = searchTerm.Text.Substring(0, (realPrefixLength) - (0));
129 this.p =
new int[this.text.Length + 1];
130 this.d =
new int[this.text.Length + 1];
132 SetEnum(reader.Terms(
new Term(searchTerm.Field, prefix)));
138 protected internal override bool TermCompare(
Term term)
140 if ((System.Object) field == (System.Object) term.Field && term.Text.StartsWith(prefix))
142 System.String target = term.Text.Substring(prefix.Length);
144 return (similarity > minimumSimilarity);
150 public override float Difference()
152 return ((similarity - minimumSimilarity) * scale_factor);
155 public override bool EndEnum()
204 private float Similarity(System.String target)
207 int m = target.Length;
213 return prefix.Length == 0 ? 0.0f : 1.0f - ((float)m / prefix.Length);
217 return prefix.Length == 0 ? 0.0f : 1.0f - ((float)n / prefix.Length);
220 int maxDistance = CalculateMaxDistance(m);
222 if (maxDistance < System.Math.Abs(m - n))
235 for (
int i = 0; i < n; ++i)
241 for (
int j = 1; j <= m; ++j)
243 int bestPossibleEditDistance = m;
244 char t_j = target[j - 1];
246 for (
int i = 1; i <= n; ++i)
249 if (t_j != text[i - 1])
251 d[i] = Math.Min(Math.Min(d[i - 1], p[i]), p[i - 1]) + 1;
255 d[i] = Math.Min(Math.Min(d[i - 1] + 1, p[i] + 1), p[i - 1]);
257 bestPossibleEditDistance = System.Math.Min(bestPossibleEditDistance, d[i]);
264 if (j > maxDistance && bestPossibleEditDistance > maxDistance)
286 return 1.0f - (p[n] / (float)(prefix.Length + System.Math.Min(n, m)));
298 private int CalculateMaxDistance(
int m)
300 return (
int) ((1 - minimumSimilarity) * (System.Math.Min(text.Length, m) + prefix.Length));
303 protected override void Dispose(
bool disposing)
305 if (isDisposed)
return;
315 base.Dispose(disposing);