View Javadoc

1   /* Copyright (c) 2008 Sascha Kohlmann
2    *
3    * This program is free software: you can redistribute it and/or modify
4    * it under the terms of the GNU Affero General Public License as published by
5    * the Free Software Foundation, either version 3 of the License, or
6    * (at your option) any later version.
7    *
8    * This program is distributed in the hope that it will be useful,
9    * but WITHOUT ANY WARRANTY; without even the implied warranty of
10   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11   * GNU Affero General Public License for more details.
12   *
13   * You should have received a copy of the GNU Affero General Public License
14   * along with this program.  If not, see <http://www.gnu.org/licenses/>.
15   */
16  package net.sf.eos.lucene;
17  
18  import org.apache.commons.logging.Log;
19  import org.apache.commons.logging.LogFactory;
20  import org.apache.lucene.search.DefaultSimilarity;
21  
22  /**
23   * Normalize the length for {@link #lengthNorm(String, int)} to 1.0.
24   * @author Sascha Kohlmann
25   */
26  public class NormedLengthSimilarity extends DefaultSimilarity {
27  
28      private static final long serialVersionUID = 1070479651291502989L;
29  
30      /** For logging. */
31      private static final Log LOG =
32          LogFactory.getLog(NormedLengthSimilarity.class.getName());
33  
34      @Override
35      public float lengthNorm(final String fieldName, final int numTerms) {
36          if (! "TODO".equals(fieldName)) {
37              final float retval = super.lengthNorm(fieldName, numTerms);
38              if (LOG.isTraceEnabled()) {
39                  LOG.trace("fieldname: "+ fieldName
40                            + " - numTerms: "+ numTerms + " - retval: "
41                            + retval);
42              }
43              return retval;
44          }
45          if (LOG.isTraceEnabled()) {
46              LOG.trace("fieldname: "+ fieldName
47                        + " - numTerms: "+ numTerms + " - retval: 1.0");
48          }
49          return 1.0f;
50      }
51      @Override
52      public float queryNorm(final float sumOfSquaredWeights) {
53          final float retval = super.queryNorm(sumOfSquaredWeights);
54          if (LOG.isTraceEnabled()) {
55              LOG.trace("sumOfSquaredWeights: "
56                        + sumOfSquaredWeights + " - retval: " + retval);
57          }
58          return retval;
59      }
60      @Override
61      public float tf(final float freq) {
62          final float retval =  super.tf(freq);
63          if (LOG.isTraceEnabled()) {
64              LOG.trace("freq: "+ freq + " - retval: " + retval);
65          }
66          return retval;
67      }
68      @Override
69      public float sloppyFreq(final int distance) {
70          final float retval = super.sloppyFreq(distance);
71          if (LOG.isTraceEnabled()) {
72              LOG.trace("distance: "+ distance + " - retval: " + retval);
73          }
74          return retval;
75      }
76      @Override
77      public float idf(final int docFreq, final int numDocs) {
78          final float retval = super.idf(docFreq, numDocs);
79          if (LOG.isTraceEnabled()) {
80              LOG.trace("docFreq: "+ docFreq
81                      + " - docFreq: " + docFreq + " - retval: " + retval);
82          }
83          return retval;
84      }
85      @Override
86      public float coord(final int overlap, final int maxOverlap) {
87          final float retval = super.idf(overlap, maxOverlap);
88          if (LOG.isTraceEnabled()) {
89              LOG.trace("overlap: "+ overlap
90                        + " - maxOverlap: " + maxOverlap + " - retval: "
91                        + retval);
92          }
93         return retval;
94      }
95  }