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.
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
;
37 internal PhraseScorer(Weight weight
, TermPositions
[] tps
, int[] positions
, Similarity similarity
, byte[] norms
) : base(similarity
)
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
]);
49 // add next to end of list
57 pq
= new PhraseQueue(tps
.Length
); // construct empty pq
60 public override int Doc()
65 public override bool Next()
74 more
= last
.Next(); // trigger further scanning
79 // next without initial increment
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
93 // found a doc with all of the terms
94 freq
= PhraseFreq(); // check for phrase
98 // trigger further scanning
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
);
124 protected internal abstract float PhraseFreq();
128 for (PhrasePositions pp
= first
; more
&& pp
!= null; pp
= pp
.next
)
137 for (PhrasePositions pp
= first
; pp
!= null; pp
= pp
.next
)
142 protected internal void PqToList()
145 while (pq
.Top() != null)
147 PhrasePositions pp
= (PhrasePositions
) pq
.Pop();
150 // add next to end of list
160 protected internal void FirstToLast()
162 last
.next
= first
; // move first to end of list
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
+ ")";