Thumbnail file hits. Based on a patch from D Bera
[beagle.git] / beagled / Lucene.Net / Search / Spans / SpanScorer.cs
blob6a7412b9ed3ec8180c8e78b2008806166ae995a4
1 /*
2 * Copyright 2004 The Apache Software Foundation
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
16 using System;
17 using Explanation = Lucene.Net.Search.Explanation;
18 using Scorer = Lucene.Net.Search.Scorer;
19 using Similarity = Lucene.Net.Search.Similarity;
20 using Weight = Lucene.Net.Search.Weight;
21 namespace Lucene.Net.Search.Spans
25 class SpanScorer:Scorer
27 private Spans spans;
28 private Weight weight;
29 private byte[] norms;
30 private float value_Renamed;
32 private bool firstTime = true;
33 private bool more = true;
35 private int doc;
36 private float freq;
38 internal SpanScorer(Spans spans, Weight weight, Similarity similarity, byte[] norms) : base(similarity)
40 this.spans = spans;
41 this.norms = norms;
42 this.weight = weight;
43 this.value_Renamed = weight.Value;
46 public override bool Next()
48 if (firstTime)
50 more = spans.Next();
51 firstTime = false;
54 if (!more)
55 return false;
57 freq = 0.0f;
58 doc = spans.Doc();
60 while (more && doc == spans.Doc())
62 int matchLength = spans.End() - spans.Start();
63 freq += GetSimilarity().SloppyFreq(matchLength);
64 more = spans.Next();
67 return more || freq != 0.0f;
70 public override int Doc()
72 return doc;
75 public override float Score()
77 float raw = GetSimilarity().Tf(freq) * value_Renamed; // raw score
78 return raw * Similarity.DecodeNorm(norms[doc]); // normalize
81 public override bool SkipTo(int target)
83 more = spans.SkipTo(target);
85 if (!more)
86 return false;
88 freq = 0.0f;
89 doc = spans.Doc();
91 while (more && spans.Doc() == target)
93 freq += GetSimilarity().SloppyFreq(spans.End() - spans.Start());
94 more = spans.Next();
97 return more || freq != 0.0f;
100 public override Explanation Explain(int doc)
102 Explanation tfExplanation = new Explanation();
104 SkipTo(doc);
106 float phraseFreq = (Doc() == doc)?freq:0.0f;
107 tfExplanation.SetValue(GetSimilarity().Tf(phraseFreq));
108 tfExplanation.SetDescription("tf(phraseFreq=" + phraseFreq + ")");
110 return tfExplanation;