Lucene.Net  3.0.3
Lucene.Net is a port of the Lucene search engine library, written in C# and targeted at .NET runtime users.
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Properties Pages
SpanScorer.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 
20 using Explanation = Lucene.Net.Search.Explanation;
21 using Scorer = Lucene.Net.Search.Scorer;
22 using Similarity = Lucene.Net.Search.Similarity;
23 using Weight = Lucene.Net.Search.Weight;
24 
25 namespace Lucene.Net.Search.Spans
26 {
27  /// <summary> Public for extension only.</summary>
28  public class SpanScorer:Scorer
29  {
30  protected internal Spans spans;
31  protected internal Weight weight;
32  protected internal byte[] norms;
33  protected internal float value_Renamed;
34 
35  protected internal bool more = true;
36 
37  protected internal int doc;
38  protected internal float freq;
39 
40  protected internal SpanScorer(Spans spans, Weight weight, Similarity similarity, byte[] norms):base(similarity)
41  {
42  this.spans = spans;
43  this.norms = norms;
44  this.weight = weight;
45  this.value_Renamed = weight.Value;
46  if (this.spans.Next())
47  {
48  doc = - 1;
49  }
50  else
51  {
52  doc = NO_MORE_DOCS;
53  more = false;
54  }
55  }
56 
57  public override int NextDoc()
58  {
59  if (!SetFreqCurrentDoc())
60  {
61  doc = NO_MORE_DOCS;
62  }
63  return doc;
64  }
65 
66  public override int Advance(int target)
67  {
68  if (!more)
69  {
70  return doc = NO_MORE_DOCS;
71  }
72  if (spans.Doc() < target)
73  {
74  // setFreqCurrentDoc() leaves spans.doc() ahead
75  more = spans.SkipTo(target);
76  }
77  if (!SetFreqCurrentDoc())
78  {
79  doc = NO_MORE_DOCS;
80  }
81  return doc;
82  }
83 
84  public /*protected internal*/ virtual bool SetFreqCurrentDoc()
85  {
86  if (!more)
87  {
88  return false;
89  }
90  doc = spans.Doc();
91  freq = 0.0f;
92  do
93  {
94  int matchLength = spans.End() - spans.Start();
95  freq += Similarity.SloppyFreq(matchLength);
96  more = spans.Next();
97  }
98  while (more && (doc == spans.Doc()));
99  return true;
100  }
101 
102  public override int DocID()
103  {
104  return doc;
105  }
106 
107  public override float Score()
108  {
109  float raw = Similarity.Tf(freq) * value_Renamed; // raw score
110  return norms == null?raw:raw * Similarity.DecodeNorm(norms[doc]); // normalize
111  }
112 
113  /// <summary>
114  /// This method is no longer an official member of <see cref="Scorer"/>
115  /// but it is needed by SpanWeight to build an explanation.
116  /// </summary>
117  protected internal virtual Explanation Explain(int doc)
118  {
119  Explanation tfExplanation = new Explanation();
120 
121  int expDoc = Advance(doc);
122 
123  float phraseFreq = (expDoc == doc)?freq:0.0f;
124  tfExplanation.Value = Similarity.Tf(phraseFreq);
125  tfExplanation.Description = "tf(phraseFreq=" + phraseFreq + ")";
126 
127  return tfExplanation;
128  }
129  }
130 }