1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
|
/*------------------------------------------------------------------------------
* Copyright (C) 2003-2006 Ben van Klinken and the CLucene Team
*
* Distributable under the terms of either the Apache License (Version 2.0) or
* the GNU Lesser General Public License, as specified in the COPYING file.
------------------------------------------------------------------------------*/
#include "CLucene/StdHeader.h"
#include "PhraseScorer.h"
#include "PhraseQueue.h"
#include "PhrasePositions.h"
#include "Scorer.h"
#include "Similarity.h"
CL_NS_USE(index)
CL_NS_USE(util)
CL_NS_DEF(search)
PhraseScorer::PhraseScorer(Weight* weight, TermPositions** tps,
int32_t* positions, Similarity* similarity, uint8_t* norms):
Scorer(similarity)
{
//Func - Constructor
//Pre - tps != NULL and is an array of TermPositions
// tpsLength >= 0
// n != NULL
//Post - The instance has been created
CND_PRECONDITION(tps != NULL,"tps is NULL");
//norms are only used if phraseFreq returns more than 0.0
//phraseFreq should only return more than 0.0 if norms != NULL
//CND_PRECONDITION(n != NULL,"n is NULL");
firstTime = true;
more = true;
this->norms = norms;
this->weight = weight;
this->value = weight->getValue();
//reset internal pointers
first = NULL;
last = NULL;
//use pq to build a sorted list of PhrasePositions
int32_t i = 0;
while(tps[i] != NULL){
PhrasePositions *pp = _CLNEW PhrasePositions(tps[i], positions[i]);
CND_CONDITION(pp != NULL,"Could not allocate memory for pp");
//Store PhrasePos into the PhrasePos pq
if (last != NULL) { // add next to end of list
last->_next = pp;
} else
first = pp;
last = pp;
i++;
}
pq = _CLNEW PhraseQueue(i); //i==tps.length
CND_CONDITION(pq != NULL,"Could not allocate memory for pq");
}
PhraseScorer::~PhraseScorer() {
//Func - Destructor
//Pre - true
//Post - The instance has been destroyed
//The PhraseQueue pq (which is a PriorityQueue) pq is actually empty at present, the elements
//having been transferred by pqToList() to the linked list starting with
//first. The nodes of that linked list are deleted by the destructor of
//first, rather than the destructor of pq.
_CLDELETE(first);
_CLDELETE(pq);
}
bool PhraseScorer::next(){
if (firstTime) {
init();
firstTime = false;
} else if (more) {
more = last->next(); // trigger further scanning
}
return doNext();
}
// next without initial increment
bool PhraseScorer::doNext() {
while (more) {
while (more && first->doc < last->doc) { // find doc w/ all the terms
more = first->skipTo(last->doc); // skip first upto last
firstToLast(); // and move it to the end
}
if (more) {
// found a doc with all of the terms
freq = phraseFreq(); // check for phrase
if (freq == 0.0f) // no match
more = last->next(); // trigger further scanning
else
return true; // found a match
}
}
return false; // no more matches
}
qreal PhraseScorer::score(){
//System.out.println("scoring " + first.doc);
qreal raw = getSimilarity()->tf(freq) * value; // raw score
return raw * Similarity::decodeNorm(norms[first->doc]); // normalize
}
bool PhraseScorer::skipTo(int32_t target) {
for (PhrasePositions* pp = first; more && pp != NULL; pp = pp->_next) {
more = pp->skipTo(target);
}
if (more)
sort(); // re-sort
return doNext();
}
void PhraseScorer::init() {
for (PhrasePositions* pp = first; more && pp != NULL; pp = pp->_next)
more = pp->next();
if(more)
sort();
}
void PhraseScorer::sort() {
pq->clear();
for (PhrasePositions* pp = first; pp != NULL; pp = pp->_next)
pq->put(pp);
pqToList();
}
void PhraseScorer::pqToList(){
//Func - Transfers the PhrasePositions from the PhraseQueue pq to
// the PhrasePositions list with first as its first element
//Pre - pq != NULL
// first = NULL
// last = NULL
//Post - All PhrasePositions have been transfered to the list
// of PhrasePositions of which the first element is pointed to by first
// and the last element is pointed to by last
CND_PRECONDITION(pq != NULL,"pq is NULL");
last = first = NULL;
PhrasePositions* PhrasePos = NULL;
//As long pq is not empty
while (pq->top() != NULL){
//Pop a PhrasePositions instance
PhrasePos = pq->pop();
// add next to end of list
if (last != NULL) {
last->_next = PhrasePos;
} else {
first = PhrasePos;
}
//Let last point to the new last PhrasePositions instance just added
last = PhrasePos;
//Reset the next of last to NULL
last->_next = NULL;
}
//Check to see that pq is empty now
CND_CONDITION(pq->size()==0, "pq is not empty while it should be");
}
void PhraseScorer::firstToLast(){
//Func - Moves first to the end of the list
//Pre - first is NULL or points to an PhrasePositions Instance
// last is NULL or points to an PhrasePositions Instance
// first and last both are NULL or both are not NULL
//Post - The first element has become the last element in the list
CND_PRECONDITION(((first==NULL && last==NULL) ||(first !=NULL && last != NULL)),
"Either first or last is NULL but not both");
//Check if first and last are valid pointers
if(first && last){
last->_next = first;
last = first;
first = first->_next;
last->_next = NULL;
}
}
void PhraseScorer::explain(int32_t _doc, Explanation* tfExplanation) {
while (next() && doc() < _doc){
}
qreal phraseFreq = (doc() == _doc) ? freq : 0.0f;
tfExplanation->setValue(getSimilarity()->tf(phraseFreq));
StringBuffer buf;
buf.append(_T("tf(phraseFreq="));
buf.appendFloat(phraseFreq,2);
buf.append(_T(")"));
tfExplanation->setDescription(buf.getBuffer());
}
TCHAR* PhraseScorer::toString() {
StringBuffer buf;
buf.append(_T("scorer("));
TCHAR* tmp = weight->toString();
buf.append(tmp);
_CLDELETE_CARRAY(tmp);
buf.append(_T(")"));
return buf.toString();
}
CL_NS_END
|