Thumbnail file hits. Based on a patch from D Bera
[beagle.git] / beagled / Lucene.Net / Search / FuzzyQuery.cs
blob1af09c326f185f7a0390d4abe874bc0c40955800
1 /*
2 * Copyright 2004 The Apache Software Foundation
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
16 using System;
17 using IndexReader = Lucene.Net.Index.IndexReader;
18 using Term = Lucene.Net.Index.Term;
19 namespace Lucene.Net.Search
22 /// <summary>Implements the fuzzy search query. The similiarity measurement
23 /// is based on the Levenshtein (edit distance) algorithm.
24 /// </summary>
25 [Serializable]
26 public sealed class FuzzyQuery:MultiTermQuery
29 public const float defaultMinSimilarity = 0.5f;
30 private float minimumSimilarity;
31 private int prefixLength;
33 /// <summary> Create a new FuzzyQuery that will match terms with a similarity
34 /// of at least <code>minimumSimilarity</code> to <code>term</code>.
35 /// If a <code>prefixLength</code> &gt; 0 is specified, a common prefix
36 /// of that length is also required.
37 ///
38 /// </summary>
39 /// <param name="term">the term to search for
40 /// </param>
41 /// <param name="minimumSimilarity">a value between 0 and 1 to set the required similarity
42 /// between the query term and the matching terms. For example, for a
43 /// <code>minimumSimilarity</code> of <code>0.5</code> a term of the same length
44 /// as the query term is considered similar to the query term if the edit distance
45 /// between both terms is less than <code>length(term)*0.5</code>
46 /// </param>
47 /// <param name="prefixLength">length of common (non-fuzzy) prefix
48 /// </param>
49 /// <throws> IllegalArgumentException if minimumSimilarity is &gt; 1 or &lt; 0 </throws>
50 /// <summary> or if prefixLength &lt; 0 or &gt; <code>term.text().length()</code>.
51 /// </summary>
52 public FuzzyQuery(Term term, float minimumSimilarity, int prefixLength):base(term)
55 if (minimumSimilarity > 1.0f)
56 throw new System.ArgumentException("minimumSimilarity > 1");
57 else if (minimumSimilarity < 0.0f)
58 throw new System.ArgumentException("minimumSimilarity < 0");
59 this.minimumSimilarity = minimumSimilarity;
61 if (prefixLength < 0)
62 throw new System.ArgumentException("prefixLength < 0");
63 else if (prefixLength >= term.Text().Length)
64 throw new System.ArgumentException("prefixLength >= term.text().length()");
65 this.prefixLength = prefixLength;
68 /// <summary> Calls {@link #FuzzyQuery(Term, float) FuzzyQuery(term, minimumSimilarity, 0)}.</summary>
69 public FuzzyQuery(Term term, float minimumSimilarity):this(term, minimumSimilarity, 0)
73 /// <summary> Calls {@link #FuzzyQuery(Term, float) FuzzyQuery(term, 0.5f, 0)}.</summary>
74 public FuzzyQuery(Term term):this(term, defaultMinSimilarity, 0)
78 /// <summary> Returns the minimum similarity that is required for this query to match.</summary>
79 /// <returns> float value between 0.0 and 1.0
80 /// </returns>
81 public float GetMinSimilarity()
83 return minimumSimilarity;
86 /// <summary> Returns the prefix length, i.e. the number of characters at the start
87 /// of a term that must be identical (not fuzzy) to the query term if the query
88 /// is to match that term.
89 /// </summary>
90 public int GetPrefixLength()
92 return prefixLength;
95 protected internal override FilteredTermEnum GetEnum(IndexReader reader)
97 return new FuzzyTermEnum(reader, GetTerm(), minimumSimilarity, prefixLength);
100 public override System.String ToString(System.String field)
102 return base.ToString(field) + '~' + minimumSimilarity.ToString();