Add --enable-deletion option to buildindex. If used, buildindex will remove deleted...
[beagle.git] / beagled / Lucene.Net / Search / PhraseScorer.cs
blob9c7afc20c467768a2b404a2d20e5e125a68d9dcf
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 Lucene.Net.Index;
18 namespace Lucene.Net.Search
21 abstract class PhraseScorer:Scorer
23 private Weight weight;
24 protected internal byte[] norms;
25 protected internal float value_Renamed;
27 private bool firstTime = true;
28 private bool more = true;
29 protected internal PhraseQueue pq;
30 protected internal PhrasePositions first, last;
32 private float freq;
35 internal PhraseScorer(Weight weight, TermPositions[] tps, int[] positions, Similarity similarity, byte[] norms) : base(similarity)
37 this.norms = norms;
38 this.weight = weight;
39 this.value_Renamed = weight.GetValue();
41 // convert tps to a list
42 for (int i = 0; i < tps.Length; i++)
44 PhrasePositions pp = new PhrasePositions(tps[i], positions[i]);
45 if (last != null)
47 // add next to end of list
48 last.next = pp;
50 else
51 first = pp;
52 last = pp;
55 pq = new PhraseQueue(tps.Length); // construct empty pq
58 public override int Doc()
60 return first.doc;
63 public override bool Next()
65 if (firstTime)
67 Init();
68 firstTime = false;
70 else if (more)
72 more = last.Next(); // trigger further scanning
74 return DoNext();
77 // next without initial increment
78 private bool DoNext()
80 while (more)
82 while (more && first.doc < last.doc)
84 // find doc w/ all the terms
85 more = first.SkipTo(last.doc); // skip first upto last
86 FirstToLast(); // and move it to the end
89 if (more)
91 // found a doc with all of the terms
92 freq = PhraseFreq(); // check for phrase
93 if (freq == 0.0f)
94 // no match
95 more = last.Next();
96 // trigger further scanning
97 else
98 return true; // found a match
101 return false; // no more matches
104 public override float Score()
106 //System.out.println("scoring " + first.doc);
107 float raw = GetSimilarity().Tf(freq) * value_Renamed; // raw score
108 return raw * Similarity.DecodeNorm(norms[first.doc]); // normalize
111 public override bool SkipTo(int target)
113 for (PhrasePositions pp = first; more && pp != null; pp = pp.next)
115 more = pp.SkipTo(target);
117 if (more)
118 Sort(); // re-sort
119 return DoNext();
122 protected internal abstract float PhraseFreq();
124 private void Init()
126 for (PhrasePositions pp = first; more && pp != null; pp = pp.next)
127 more = pp.Next();
128 if (more)
129 Sort();
132 private void Sort()
134 pq.Clear();
135 for (PhrasePositions pp = first; pp != null; pp = pp.next)
136 pq.Put(pp);
137 PqToList();
140 protected internal void PqToList()
142 last = first = null;
143 while (pq.Top() != null)
145 PhrasePositions pp = (PhrasePositions) pq.Pop();
146 if (last != null)
148 // add next to end of list
149 last.next = pp;
151 else
152 first = pp;
153 last = pp;
154 pp.next = null;
158 protected internal void FirstToLast()
160 last.next = first; // move first to end of list
161 last = first;
162 first = first.next;
163 last.next = null;
166 public override Explanation Explain(int doc)
168 Explanation tfExplanation = new Explanation();
170 while (Next() && Doc() < doc)
174 float phraseFreq = (Doc() == doc)?freq:0.0f;
175 tfExplanation.SetValue(GetSimilarity().Tf(phraseFreq));
176 tfExplanation.SetDescription("tf(phraseFreq=" + phraseFreq + ")");
178 return tfExplanation;
181 public override System.String ToString()
183 return "scorer(" + weight + ")";