Lucene.Net  3.0.3
Lucene.Net is a port of the Lucene search engine library, written in C# and targeted at .NET runtime users.
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Properties Pages
DefaultSimilarity.cs
Go to the documentation of this file.
1 /*
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
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
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.
16  */
17 
18 using System;
19 
20 using FieldInvertState = Lucene.Net.Index.FieldInvertState;
21 
22 namespace Lucene.Net.Search
23 {
24 
25  /// <summary>Expert: Default scoring implementation. </summary>
26  [Serializable]
28  {
29 
30  /// <summary>Implemented as
31  /// <c>state.getBoost()*lengthNorm(numTerms)</c>, where
32  /// <c>numTerms</c> is <see cref="FieldInvertState.Length" /> if <see cref="DiscountOverlaps" />
33  /// is false, else it's <see cref="FieldInvertState.Length" />
34  /// - <see cref="FieldInvertState.NumOverlap" />
35  ///.
36  ///
37  /// <p/><b>WARNING</b>: This API is new and experimental, and may suddenly
38  /// change.<p/>
39  /// </summary>
40  public override float ComputeNorm(System.String field, FieldInvertState state)
41  {
42  int numTerms;
43  if (internalDiscountOverlaps)
44  numTerms = state.Length - state.NumOverlap;
45  else
46  numTerms = state.Length;
47  return (state.Boost * LengthNorm(field, numTerms));
48  }
49 
50  /// <summary>Implemented as <c>1/sqrt(numTerms)</c>. </summary>
51  public override float LengthNorm(System.String fieldName, int numTerms)
52  {
53  return (float) (1.0 / System.Math.Sqrt(numTerms));
54  }
55 
56  /// <summary>Implemented as <c>1/sqrt(sumOfSquaredWeights)</c>. </summary>
57  public override float QueryNorm(float sumOfSquaredWeights)
58  {
59  return (float) (1.0 / System.Math.Sqrt(sumOfSquaredWeights));
60  }
61 
62  /// <summary>Implemented as <c>sqrt(freq)</c>. </summary>
63  public override float Tf(float freq)
64  {
65  return (float) System.Math.Sqrt(freq);
66  }
67 
68  /// <summary>Implemented as <c>1 / (distance + 1)</c>. </summary>
69  public override float SloppyFreq(int distance)
70  {
71  return 1.0f / (distance + 1);
72  }
73 
74  /// <summary>Implemented as <c>log(numDocs/(docFreq+1)) + 1</c>. </summary>
75  public override float Idf(int docFreq, int numDocs)
76  {
77  return (float) (System.Math.Log(numDocs / (double) (docFreq + 1)) + 1.0);
78  }
79 
80  /// <summary>Implemented as <c>overlap / maxOverlap</c>. </summary>
81  public override float Coord(int overlap, int maxOverlap)
82  {
83  return overlap / (float) maxOverlap;
84  }
85 
86  /// <seealso cref="DiscountOverlaps">
87  /// </seealso>
88  // Default false
89  protected internal bool internalDiscountOverlaps;
90 
91  /// <summary>Determines whether overlap tokens (Tokens with
92  /// 0 position increment) are ignored when computing
93  /// norm. By default this is false, meaning overlap
94  /// tokens are counted just like non-overlap tokens.
95  ///
96  /// <p/><b>WARNING</b>: This API is new and experimental, and may suddenly
97  /// change.<p/>
98  ///
99  /// </summary>
100  /// <seealso cref="ComputeNorm">
101  /// </seealso>
102  public virtual bool DiscountOverlaps
103  {
104  get { return internalDiscountOverlaps; }
105  set { internalDiscountOverlaps = value; }
106  }
107  }
108 }