cvsimport
[beagle.git] / beagled / Lucene.Net / Search / PhraseScorer.cs
blobd790480cac258b839df013453f9a6ded48f2eb68
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 Lucene.Net.Index;
20 namespace Lucene.Net.Search
23 abstract class PhraseScorer : Scorer
25 private Weight weight;
26 protected internal byte[] norms;
27 protected internal float value_Renamed;
29 private bool firstTime = true;
30 private bool more = true;
31 protected internal PhraseQueue pq;
32 protected internal PhrasePositions first, last;
34 private float freq;
37 internal PhraseScorer(Weight weight, TermPositions[] tps, int[] positions, Similarity similarity, byte[] norms) : base(similarity)
39 this.norms = norms;
40 this.weight = weight;
41 this.value_Renamed = weight.GetValue();
43 // convert tps to a list
44 for (int i = 0; i < tps.Length; i++)
46 PhrasePositions pp = new PhrasePositions(tps[i], positions[i]);
47 if (last != null)
49 // add next to end of list
50 last.next = pp;
52 else
53 first = pp;
54 last = pp;
57 pq = new PhraseQueue(tps.Length); // construct empty pq
60 public override int Doc()
62 return first.doc;
65 public override bool Next()
67 if (firstTime)
69 Init();
70 firstTime = false;
72 else if (more)
74 more = last.Next(); // trigger further scanning
76 return DoNext();
79 // next without initial increment
80 private bool DoNext()
82 while (more)
84 while (more && first.doc < last.doc)
86 // find doc w/ all the terms
87 more = first.SkipTo(last.doc); // skip first upto last
88 FirstToLast(); // and move it to the end
91 if (more)
93 // found a doc with all of the terms
94 freq = PhraseFreq(); // check for phrase
95 if (freq == 0.0f)
96 // no match
97 more = last.Next();
98 // trigger further scanning
99 else
100 return true; // found a match
103 return false; // no more matches
106 public override float Score()
108 //System.out.println("scoring " + first.doc);
109 float raw = GetSimilarity().Tf(freq) * value_Renamed; // raw score
110 return raw * Similarity.DecodeNorm(norms[first.doc]); // normalize
113 public override bool SkipTo(int target)
115 for (PhrasePositions pp = first; more && pp != null; pp = pp.next)
117 more = pp.SkipTo(target);
119 if (more)
120 Sort(); // re-sort
121 return DoNext();
124 protected internal abstract float PhraseFreq();
126 private void Init()
128 for (PhrasePositions pp = first; more && pp != null; pp = pp.next)
129 more = pp.Next();
130 if (more)
131 Sort();
134 private void Sort()
136 pq.Clear();
137 for (PhrasePositions pp = first; pp != null; pp = pp.next)
138 pq.Put(pp);
139 PqToList();
142 protected internal void PqToList()
144 last = first = null;
145 while (pq.Top() != null)
147 PhrasePositions pp = (PhrasePositions) pq.Pop();
148 if (last != null)
150 // add next to end of list
151 last.next = pp;
153 else
154 first = pp;
155 last = pp;
156 pp.next = null;
160 protected internal void FirstToLast()
162 last.next = first; // move first to end of list
163 last = first;
164 first = first.next;
165 last.next = null;
168 public override Explanation Explain(int doc)
170 Explanation tfExplanation = new Explanation();
172 while (Next() && Doc() < doc)
176 float phraseFreq = (Doc() == doc)?freq:0.0f;
177 tfExplanation.SetValue(GetSimilarity().Tf(phraseFreq));
178 tfExplanation.SetDescription("tf(phraseFreq=" + phraseFreq + ")");
180 return tfExplanation;
183 public override System.String ToString()
185 return "scorer(" + weight + ")";