Lucene.Net  3.0.3
Lucene.Net is a .NET port of the Java Lucene Indexing Library
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Properties
Similarity.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 using System.Collections.Generic;
20 using Lucene.Net.Documents;
21 using FieldInvertState = Lucene.Net.Index.FieldInvertState;
22 using Term = Lucene.Net.Index.Term;
23 using SmallFloat = Lucene.Net.Util.SmallFloat;
24 using IDFExplanation = Lucene.Net.Search.Explanation.IDFExplanation;
25 
26 namespace Lucene.Net.Search
27 {
28 
292  [Serializable]
293  public abstract class Similarity
294  {
295  protected Similarity()
296  {
297  InitBlock();
298  }
299  [Serializable]
300  private class AnonymousClassIDFExplanation1:IDFExplanation
301  {
302  public AnonymousClassIDFExplanation1(int df, int max, float idf, Similarity enclosingInstance)
303  {
304  InitBlock(df, max, idf, enclosingInstance);
305  }
306  private void InitBlock(int df, int max, float idf, Similarity enclosingInstance)
307  {
308  this.df = df;
309  this.max = max;
310  this.idf = idf;
311  this.enclosingInstance = enclosingInstance;
312  }
313  private int df;
314  private int max;
315  private float idf;
316  private Similarity enclosingInstance;
317  public Similarity Enclosing_Instance
318  {
319  get
320  {
321  return enclosingInstance;
322  }
323 
324  }
325  //@Override
326  public override System.String Explain()
327  {
328  return "idf(docFreq=" + df + ", maxDocs=" + max + ")";
329  }
330  //@Override
331 
332  public override float Idf
333  {
334  get { return idf; }
335  }
336  }
337  [Serializable]
338  private class AnonymousClassIDFExplanation3:IDFExplanation
339  {
340  public AnonymousClassIDFExplanation3(float fIdf, System.Text.StringBuilder exp, Similarity enclosingInstance)
341  {
342  InitBlock(fIdf, exp, enclosingInstance);
343  }
344  private void InitBlock(float fIdf, System.Text.StringBuilder exp, Similarity enclosingInstance)
345  {
346  this.fIdf = fIdf;
347  this.exp = exp;
348  this.enclosingInstance = enclosingInstance;
349  }
350  private float fIdf;
351  private System.Text.StringBuilder exp;
352  private Similarity enclosingInstance;
353  public Similarity Enclosing_Instance
354  {
355  get
356  {
357  return enclosingInstance;
358  }
359 
360  }
361  //@Override
362 
363  public override float Idf
364  {
365  get { return fIdf; }
366  }
367 
368  //@Override
369  public override System.String Explain()
370  {
371  return exp.ToString();
372  }
373  }
374  private void InitBlock()
375  {
376 
377  }
378 
380  private static Similarity defaultImpl = new DefaultSimilarity();
381  public const int NO_DOC_ID_PROVIDED = - 1;
382 
391  public static Similarity Default
392  {
393  get { return defaultImpl; }
394  set { defaultImpl = value; }
395  }
396 
398  private static readonly float[] NORM_TABLE = new float[256];
399 
403  public static float DecodeNorm(byte b)
404  {
405  return NORM_TABLE[b & 0xFF]; // & 0xFF maps negative bytes to positive above 127
406  }
407 
411  public static float[] GetNormDecoder()
412  {
413  return NORM_TABLE;
414  }
415 
437  public virtual float ComputeNorm(System.String field, FieldInvertState state)
438  {
439  return (float) (state.Boost * LengthNorm(field, state.Length));
440  }
441 
468  public abstract float LengthNorm(System.String fieldName, int numTokens);
469 
482  public abstract float QueryNorm(float sumOfSquaredWeights);
483 
498  public static byte EncodeNorm(float f)
499  {
500  return (byte) SmallFloat.FloatToByte315(f);
501  }
502 
503 
521  public virtual float Tf(int freq)
522  {
523  return Tf((float) freq);
524  }
525 
539  public abstract float SloppyFreq(int distance);
540 
556  public abstract float Tf(float freq);
557 
582  public virtual IDFExplanation IdfExplain(Term term, Searcher searcher)
583  {
584  int df = searcher.DocFreq(term);
585  int max = searcher.MaxDoc;
586  float idf2 = Idf(df, max);
587  return new AnonymousClassIDFExplanation1(df, max, idf2, this);
588  }
589 
606  public virtual IDFExplanation IdfExplain(ICollection<Term> terms, Searcher searcher)
607  {
608  int max = searcher.MaxDoc;
609  float idf2 = 0.0f;
610  System.Text.StringBuilder exp = new System.Text.StringBuilder();
611  foreach (Term term in terms)
612  {
613  int df = searcher.DocFreq(term);
614  idf2 += Idf(df, max);
615  exp.Append(" ");
616  exp.Append(term.Text);
617  exp.Append("=");
618  exp.Append(df);
619  }
620  float fIdf = idf2;
621  return new AnonymousClassIDFExplanation3(fIdf, exp, this);
622  }
623 
640  public abstract float Idf(int docFreq, int numDocs);
641 
657  public abstract float Coord(int overlap, int maxOverlap);
658 
659 
684  public virtual float ScorePayload(int docId, System.String fieldName, int start, int end, byte[] payload, int offset, int length)
685  {
686  return 1;
687  }
688 
689  static Similarity()
690  {
691  {
692  for (int i = 0; i < 256; i++)
693  NORM_TABLE[i] = SmallFloat.Byte315ToFloat((byte) i);
694  }
695  }
696  }
697 }