Initial revision
[beagle.git] / Lucene.Net / Search / PhraseScorer.cs
blob91f9c88b7491f6de68d7a84cc49b03d8963c37ca
1 using Lucene.Net.Util;
2 using Lucene.Net.Index;
4 namespace Lucene.Net.Search
6 /* ====================================================================
7 * The Apache Software License, Version 1.1
9 * Copyright (c) 2001 The Apache Software Foundation. All rights
10 * reserved.
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
14 * are met:
16 * 1. Redistributions of source code must retain the above copyright
17 * notice, this list of conditions and the following disclaimer.
19 * 2. Redistributions in binary form must reproduce the above copyright
20 * notice, this list of conditions and the following disclaimer in
21 * the documentation and/or other materials provided with the
22 * distribution.
24 * 3. The end-user documentation included with the redistribution,
25 * if any, must include the following acknowledgment:
26 * "This product includes software developed by the
27 * Apache Software Foundation (http://www.apache.org/)."
28 * Alternately, this acknowledgment may appear in the software itself,
29 * if and wherever such third-party acknowledgments normally appear.
31 * 4. The names "Apache" and "Apache Software Foundation" and
32 * "Apache Lucene" must not be used to endorse or promote products
33 * derived from this software without prior written permission. For
34 * written permission, please contact apache@apache.org.
36 * 5. Products derived from this software may not be called "Apache",
37 * "Apache Lucene", nor may "Apache" appear in their name, without
38 * prior written permission of the Apache Software Foundation.
40 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
41 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
42 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
43 * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
44 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
45 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
46 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
47 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
48 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
49 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
50 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51 * SUCH DAMAGE.
52 * ====================================================================
54 * This software consists of voluntary contributions made by many
55 * individuals on behalf of the Apache Software Foundation. For more
56 * information on the Apache Software Foundation, please see
57 * <http://www.apache.org/>.
60 abstract class PhraseScorer : Scorer
62 private Weight weight;
63 protected byte[] norms;
64 protected float value;
66 protected PhraseQueue pq;
67 protected PhrasePositions first, last;
69 private float freq;
71 internal PhraseScorer(Weight weight, TermPositions[] tps, Similarity similarity,
72 byte[] norms) : base(similarity)
74 this.norms = norms;
75 this.weight = weight;
76 this.value = weight.GetValue();
78 // use PQ to build a sorted list of PhrasePositions
79 pq = new PhraseQueue(tps.Length);
80 for (int i = 0; i < tps.Length; i++)
81 pq.Put(new PhrasePositions(tps[i], i));
82 PqToList();
85 public override void Score(HitCollector results, int end)
87 Similarity similarity = GetSimilarity();
88 while (last.doc < end)
89 { // find doc w/ all the terms
90 while (first.doc < last.doc)
91 { // scan forward in first
92 do
94 first.Next();
95 } while (first.doc < last.doc);
96 FirstToLast();
97 if (last.doc >= end)
98 return;
101 // found doc with all terms
102 freq = PhraseFreq(); // check for phrase
104 if (freq > 0.0)
106 float score = similarity.Tf(freq)*value; // compute score
107 score *= Similarity.DecodeNorm(norms[first.doc]); // normalize
108 results.Collect(first.doc, score); // add to results
110 last.Next(); // resume scanning
114 protected abstract float PhraseFreq() ;
116 protected void PqToList()
118 last = first = null;
119 while (pq.Top() != null)
121 PhrasePositions pp = (PhrasePositions)pq.Pop();
122 if (last != null)
123 { // add next to end of list
124 last.next = pp;
126 else
127 first = pp;
128 last = pp;
129 pp.next = null;
133 protected void FirstToLast()
135 last.next = first; // move first to end of list
136 last = first;
137 first = first.next;
138 last.next = null;
141 class PhraseScorerHitCollector : HitCollector
143 public override void Collect(int d, float score) {}
146 public override Explanation Explain(int doc)
148 Explanation tfExplanation = new Explanation();
150 Score(new PhraseScorerHitCollector(), doc+1);
152 float phraseFreq = (first.doc == doc) ? freq : 0.0f;
153 tfExplanation.SetValue(GetSimilarity().Tf(phraseFreq));
154 tfExplanation.SetDescription("tf(phraseFreq=" + phraseFreq + ")");
156 return tfExplanation;