/* * Copyright 2004 The Apache Software Foundation * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ using System; using IndexReader = Lucene.Net.Index.IndexReader; using Term = Lucene.Net.Index.Term; namespace Lucene.Net.Search { /// Implements the fuzzy search query. The similiarity measurement /// is based on the Levenshtein (edit distance) algorithm. /// [Serializable] public sealed class FuzzyQuery:MultiTermQuery { public const float defaultMinSimilarity = 0.5f; private float minimumSimilarity; private int prefixLength; /// Create a new FuzzyQuery that will match terms with a similarity /// of at least minimumSimilarity to term. /// If a prefixLength > 0 is specified, a common prefix /// of that length is also required. /// /// /// the term to search for /// /// a value between 0 and 1 to set the required similarity /// between the query term and the matching terms. For example, for a /// minimumSimilarity of 0.5 a term of the same length /// as the query term is considered similar to the query term if the edit distance /// between both terms is less than length(term)*0.5 /// /// length of common (non-fuzzy) prefix /// /// IllegalArgumentException if minimumSimilarity is > 1 or < 0 /// or if prefixLength < 0 or > term.text().length(). /// public FuzzyQuery(Term term, float minimumSimilarity, int prefixLength):base(term) { if (minimumSimilarity > 1.0f) throw new System.ArgumentException("minimumSimilarity > 1"); else if (minimumSimilarity < 0.0f) throw new System.ArgumentException("minimumSimilarity < 0"); this.minimumSimilarity = minimumSimilarity; if (prefixLength < 0) throw new System.ArgumentException("prefixLength < 0"); else if (prefixLength >= term.Text().Length) throw new System.ArgumentException("prefixLength >= term.text().length()"); this.prefixLength = prefixLength; } /// Calls {@link #FuzzyQuery(Term, float) FuzzyQuery(term, minimumSimilarity, 0)}. public FuzzyQuery(Term term, float minimumSimilarity):this(term, minimumSimilarity, 0) { } /// Calls {@link #FuzzyQuery(Term, float) FuzzyQuery(term, 0.5f, 0)}. public FuzzyQuery(Term term):this(term, defaultMinSimilarity, 0) { } /// Returns the minimum similarity that is required for this query to match. /// float value between 0.0 and 1.0 /// public float GetMinSimilarity() { return minimumSimilarity; } /// Returns the prefix length, i.e. the number of characters at the start /// of a term that must be identical (not fuzzy) to the query term if the query /// is to match that term. /// public int GetPrefixLength() { return prefixLength; } protected internal override FilteredTermEnum GetEnum(IndexReader reader) { return new FuzzyTermEnum(reader, GetTerm(), minimumSimilarity, prefixLength); } public override System.String ToString(System.String field) { return base.ToString(field) + '~' + minimumSimilarity.ToString(); } } }