2 * Licensed to the Apache Software Foundation (ASF) under one or more
3 * contributor license agreements. See the NOTICE file distributed with
4 * this work for additional information regarding copyright ownership.
5 * The ASF licenses this file to You under the Apache License, Version 2.0
6 * (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
20 using FieldInvertState = Mono.Lucene.Net.Index.FieldInvertState;
22 namespace Mono.Lucene.Net.Search
25 /// <summary>Expert: Default scoring implementation. </summary>
27 public class DefaultSimilarity:Similarity
30 /// <summary>Implemented as
31 /// <code>state.getBoost()*lengthNorm(numTerms)</code>, where
32 /// <code>numTerms</code> is {@link FieldInvertState#GetLength()} if {@link
33 /// #setDiscountOverlaps} is false, else it's {@link
34 /// FieldInvertState#GetLength()} - {@link
35 /// FieldInvertState#GetNumOverlap()}.
37 /// <p/><b>WARNING</b>: This API is new and experimental, and may suddenly
40 public override float ComputeNorm(System.String field, FieldInvertState state)
44 numTerms = state.GetLength() - state.GetNumOverlap();
46 numTerms = state.GetLength();
47 return (float) (state.GetBoost() * LengthNorm(field, numTerms));
50 /// <summary>Implemented as <code>1/sqrt(numTerms)</code>. </summary>
51 public override float LengthNorm(System.String fieldName, int numTerms)
53 return (float) (1.0 / System.Math.Sqrt(numTerms));
56 /// <summary>Implemented as <code>1/sqrt(sumOfSquaredWeights)</code>. </summary>
57 public override float QueryNorm(float sumOfSquaredWeights)
59 return (float) (1.0 / System.Math.Sqrt(sumOfSquaredWeights));
62 /// <summary>Implemented as <code>sqrt(freq)</code>. </summary>
63 public override float Tf(float freq)
65 return (float) System.Math.Sqrt(freq);
68 /// <summary>Implemented as <code>1 / (distance + 1)</code>. </summary>
69 public override float SloppyFreq(int distance)
71 return 1.0f / (distance + 1);
74 /// <summary>Implemented as <code>log(numDocs/(docFreq+1)) + 1</code>. </summary>
75 public override float Idf(int docFreq, int numDocs)
77 return (float) (System.Math.Log(numDocs / (double) (docFreq + 1)) + 1.0);
80 /// <summary>Implemented as <code>overlap / maxOverlap</code>. </summary>
81 public override float Coord(int overlap, int maxOverlap)
83 return overlap / (float) maxOverlap;
87 protected internal bool discountOverlaps;
89 /// <summary>Determines whether overlap tokens (Tokens with
90 /// 0 position increment) are ignored when computing
91 /// norm. By default this is false, meaning overlap
92 /// tokens are counted just like non-overlap tokens.
94 /// <p/><b>WARNING</b>: This API is new and experimental, and may suddenly
98 /// <seealso cref="computeNorm">
100 public virtual void SetDiscountOverlaps(bool v)
102 discountOverlaps = v;
105 /// <seealso cref="setDiscountOverlaps">
107 public virtual bool GetDiscountOverlaps()
109 return discountOverlaps;