cvsimport
[beagle.git] / beagled / Lucene.Net / Search / Spans / SpanScorer.cs
blob46b389d6f0d2e17de5a66aede442a6214aeddaaa
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.
17 using System;
18 using Explanation = Lucene.Net.Search.Explanation;
19 using Scorer = Lucene.Net.Search.Scorer;
20 using Similarity = Lucene.Net.Search.Similarity;
21 using Weight = Lucene.Net.Search.Weight;
23 namespace Lucene.Net.Search.Spans
27 class SpanScorer : Scorer
29 private Spans spans;
30 private Weight weight;
31 private byte[] norms;
32 private float value_Renamed;
34 private bool firstTime = true;
35 private bool more = true;
37 private int doc;
38 private float freq;
40 internal SpanScorer(Spans spans, Weight weight, Similarity similarity, byte[] norms) : base(similarity)
42 this.spans = spans;
43 this.norms = norms;
44 this.weight = weight;
45 this.value_Renamed = weight.GetValue();
48 public override bool Next()
50 if (firstTime)
52 more = spans.Next();
53 firstTime = false;
56 if (!more)
57 return false;
59 freq = 0.0f;
60 doc = spans.Doc();
62 while (more && doc == spans.Doc())
64 int matchLength = spans.End() - spans.Start();
65 freq += GetSimilarity().SloppyFreq(matchLength);
66 more = spans.Next();
69 return more || freq != 0.0f;
72 public override int Doc()
74 return doc;
77 public override float Score()
79 float raw = GetSimilarity().Tf(freq) * value_Renamed; // raw score
80 return raw * Similarity.DecodeNorm(norms[doc]); // normalize
83 public override bool SkipTo(int target)
85 more = spans.SkipTo(target);
87 if (!more)
88 return false;
90 freq = 0.0f;
91 doc = spans.Doc();
93 while (more && spans.Doc() == target)
95 freq += GetSimilarity().SloppyFreq(spans.End() - spans.Start());
96 more = spans.Next();
99 return more || freq != 0.0f;
102 public override Explanation Explain(int doc)
104 Explanation tfExplanation = new Explanation();
106 SkipTo(doc);
108 float phraseFreq = (Doc() == doc)?freq:0.0f;
109 tfExplanation.SetValue(GetSimilarity().Tf(phraseFreq));
110 tfExplanation.SetDescription("tf(phraseFreq=" + phraseFreq + ")");
112 return tfExplanation;