pylucene 3.5.0-3
[pylucene.git] / lucene-java-3.5.0 / lucene / src / java / org / apache / lucene / search / DefaultSimilarity.java
1 package org.apache.lucene.search;
2
3 import org.apache.lucene.index.FieldInvertState;
4
5 /**
6  * Licensed to the Apache Software Foundation (ASF) under one or more
7  * contributor license agreements.  See the NOTICE file distributed with
8  * this work for additional information regarding copyright ownership.
9  * The ASF licenses this file to You under the Apache License, Version 2.0
10  * (the "License"); you may not use this file except in compliance with
11  * the License.  You may obtain a copy of the License at
12  *
13  *     http://www.apache.org/licenses/LICENSE-2.0
14  *
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  */
21
22 /** Expert: Default scoring implementation. */
23 public class DefaultSimilarity extends Similarity {
24
25   /** Implemented as
26    *  <code>state.getBoost()*lengthNorm(numTerms)</code>, where
27    *  <code>numTerms</code> is {@link FieldInvertState#getLength()} if {@link
28    *  #setDiscountOverlaps} is false, else it's {@link
29    *  FieldInvertState#getLength()} - {@link
30    *  FieldInvertState#getNumOverlap()}.
31    *
32    *  @lucene.experimental */
33   @Override
34   public float computeNorm(String field, FieldInvertState state) {
35     final int numTerms;
36     if (discountOverlaps)
37       numTerms = state.getLength() - state.getNumOverlap();
38     else
39       numTerms = state.getLength();
40     return state.getBoost() * ((float) (1.0 / Math.sqrt(numTerms)));
41   }
42   
43   /** Implemented as <code>1/sqrt(sumOfSquaredWeights)</code>. */
44   @Override
45   public float queryNorm(float sumOfSquaredWeights) {
46     return (float)(1.0 / Math.sqrt(sumOfSquaredWeights));
47   }
48
49   /** Implemented as <code>sqrt(freq)</code>. */
50   @Override
51   public float tf(float freq) {
52     return (float)Math.sqrt(freq);
53   }
54     
55   /** Implemented as <code>1 / (distance + 1)</code>. */
56   @Override
57   public float sloppyFreq(int distance) {
58     return 1.0f / (distance + 1);
59   }
60     
61   /** Implemented as <code>log(numDocs/(docFreq+1)) + 1</code>. */
62   @Override
63   public float idf(int docFreq, int numDocs) {
64     return (float)(Math.log(numDocs/(double)(docFreq+1)) + 1.0);
65   }
66     
67   /** Implemented as <code>overlap / maxOverlap</code>. */
68   @Override
69   public float coord(int overlap, int maxOverlap) {
70     return overlap / (float)maxOverlap;
71   }
72
73   // Default true
74   protected boolean discountOverlaps = true;
75
76   /** Determines whether overlap tokens (Tokens with
77    *  0 position increment) are ignored when computing
78    *  norm.  By default this is true, meaning overlap
79    *  tokens do not count when computing norms.
80    *
81    *  @lucene.experimental
82    *
83    *  @see #computeNorm
84    */
85   public void setDiscountOverlaps(boolean v) {
86     discountOverlaps = v;
87   }
88
89   /** @see #setDiscountOverlaps */
90   public boolean getDiscountOverlaps() {
91     return discountOverlaps;
92   }
93 }