Merge pull request #409 from Alkarex/patch-1
[mono.git] / mcs / tools / monkeydoc / Lucene.Net / Lucene.Net / Search / DefaultSimilarity.cs
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 = Mono.Lucene.Net.Index.FieldInvertState;
21
22 namespace Mono.Lucene.Net.Search
23 {
24         
25         /// <summary>Expert: Default scoring implementation. </summary>
26         [Serializable]
27         public class DefaultSimilarity:Similarity
28         {
29                 
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()}.
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 (discountOverlaps)
44                                 numTerms = state.GetLength() - state.GetNumOverlap();
45                         else
46                                 numTerms = state.GetLength();
47                         return (float) (state.GetBoost() * LengthNorm(field, numTerms));
48                 }
49                 
50                 /// <summary>Implemented as <code>1/sqrt(numTerms)</code>. </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 <code>1/sqrt(sumOfSquaredWeights)</code>. </summary>
57                 public override float QueryNorm(float sumOfSquaredWeights)
58                 {
59                         return (float) (1.0 / System.Math.Sqrt(sumOfSquaredWeights));
60                 }
61                 
62                 /// <summary>Implemented as <code>sqrt(freq)</code>. </summary>
63                 public override float Tf(float freq)
64                 {
65                         return (float) System.Math.Sqrt(freq);
66                 }
67                 
68                 /// <summary>Implemented as <code>1 / (distance + 1)</code>. </summary>
69                 public override float SloppyFreq(int distance)
70                 {
71                         return 1.0f / (distance + 1);
72                 }
73                 
74                 /// <summary>Implemented as <code>log(numDocs/(docFreq+1)) + 1</code>. </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 <code>overlap / maxOverlap</code>. </summary>
81                 public override float Coord(int overlap, int maxOverlap)
82                 {
83                         return overlap / (float) maxOverlap;
84                 }
85                 
86                 // Default false
87                 protected internal bool discountOverlaps;
88                 
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.
93                 /// 
94                 /// <p/><b>WARNING</b>: This API is new and experimental, and may suddenly
95                 /// change.<p/>
96                 /// 
97                 /// </summary>
98                 /// <seealso cref="computeNorm">
99                 /// </seealso>
100                 public virtual void  SetDiscountOverlaps(bool v)
101                 {
102                         discountOverlaps = v;
103                 }
104                 
105                 /// <seealso cref="setDiscountOverlaps">
106                 /// </seealso>
107                 public virtual bool GetDiscountOverlaps()
108                 {
109                         return discountOverlaps;
110                 }
111         }
112 }