1
/*------------------------------------------------------------------------------
2
* Copyright (C) 2003-2006 Ben van Klinken and the CLucene Team
4
* Distributable under the terms of either the Apache License (Version 2.0) or
5
* the GNU Lesser General Public License, as specified in the COPYING file.
6
------------------------------------------------------------------------------*/
7
#include "CLucene/_ApiHeader.h"
8
#include "_TermScorer.h"
9
#include "SearchHeader.h"
10
#include "Explanation.h"
11
#include "CLucene/index/Term.h"
12
#include "CLucene/index/Terms.h"
13
#include "TermQuery.h"
14
#include "Similarity.h"
15
#include "Explanation.h"
20
TermScorer::TermScorer(Weight* w, CL_NS(index)::TermDocs* td,
21
Similarity* similarity,uint8_t* _norms):
26
weightValue(w->getValue()),
31
memset(docs,0,32*sizeof(int32_t));
32
memset(freqs,0,32*sizeof(int32_t));
34
for (int32_t i = 0; i < LUCENE_SCORE_CACHE_SIZE; i++)
35
scoreCache[i] = getSimilarity()->tf(i) * weightValue;
38
TermScorer::~TermScorer(){
41
bool TermScorer::next(){
43
if (pointer >= pointerMax) {
44
pointerMax = termDocs->read(docs, freqs, 32); // refill buffer
45
if (pointerMax != 0) {
48
termDocs->close(); // close stream
49
_doc = LUCENE_INT32_MAX_SHOULDBE; // set to sentinel value
57
bool TermScorer::skipTo(int32_t target) {
58
// first scan in cache
59
for (pointer++; pointer < pointerMax; pointer++) {
60
if (docs[pointer] >= target) {
66
// not found in cache, seek underlying stream
67
bool result = termDocs->skipTo(target);
71
docs[pointer] = _doc = termDocs->doc();
72
freqs[pointer] = termDocs->freq();
74
_doc = LUCENE_INT32_MAX_SHOULDBE;
79
Explanation* TermScorer::explain(int32_t doc) {
80
TermQuery* query = (TermQuery*)weight->getQuery();
81
Explanation* tfExplanation = _CLNEW Explanation();
83
while (pointer < pointerMax) {
84
if (docs[pointer] == doc)
89
if (termDocs->skipTo(doc)) {
90
if (termDocs->doc() == doc) {
91
tf = termDocs->freq();
96
tfExplanation->setValue(getSimilarity()->tf(tf));
98
TCHAR buf[LUCENE_SEARCH_EXPLANATION_DESC_LEN+1];
99
TCHAR* termToString = query->getTerm(false)->toString();
100
_sntprintf(buf,LUCENE_SEARCH_EXPLANATION_DESC_LEN,_T("tf(termFreq(%s)=%d)"), termToString, tf);
101
_CLDELETE_LCARRAY(termToString);
102
tfExplanation->setDescription(buf);
103
return tfExplanation;
106
TCHAR* TermScorer::toString() {
107
TCHAR* wb = weight->toString();
108
int32_t rl = _tcslen(wb) + 9; //9=_tcslen("scorer(" ")") + 1
109
TCHAR* ret = _CL_NEWARRAY(TCHAR,rl);
110
_sntprintf(ret,rl,_T("scorer(%s)"), wb);
111
_CLDELETE_LCARRAY(wb);
115
float_t TermScorer::score(){
116
int32_t f = freqs[pointer];
117
float_t raw = // compute tf(f)*weight
118
f < LUCENE_SCORE_CACHE_SIZE // check cache
119
? scoreCache[f] // cache hit
120
: getSimilarity()->tf(f) * weightValue; // cache miss
122
return raw * Similarity::decodeNorm(norms[_doc]); // normalize for field
125
int32_t TermScorer::doc() const { return _doc; }