2 * Copyright 2004 The Apache Software Foundation
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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 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
;
35 internal PhraseScorer(Weight weight
, TermPositions
[] tps
, int[] positions
, Similarity similarity
, byte[] norms
) : base(similarity
)
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
]);
47 // add next to end of list
55 pq
= new PhraseQueue(tps
.Length
); // construct empty pq
58 public override int Doc()
63 public override bool Next()
72 more
= last
.Next(); // trigger further scanning
77 // next without initial increment
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
91 // found a doc with all of the terms
92 freq
= PhraseFreq(); // check for phrase
96 // trigger further scanning
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
);
122 protected internal abstract float PhraseFreq();
126 for (PhrasePositions pp
= first
; more
&& pp
!= null; pp
= pp
.next
)
135 for (PhrasePositions pp
= first
; pp
!= null; pp
= pp
.next
)
140 protected internal void PqToList()
143 while (pq
.Top() != null)
145 PhrasePositions pp
= (PhrasePositions
) pq
.Pop();
148 // add next to end of list
158 protected internal void FirstToLast()
160 last
.next
= first
; // move first to end of list
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
+ ")";