#include "CLucene/StdHeader.h" #ifndef NO_FUZZY_QUERY #include "SloppyPhraseScorer.h" #include "PhraseScorer.h" #include "CLucene/index/Terms.h" using namespace lucene::index; namespace lucene{ namespace search{ SloppyPhraseScorer::SloppyPhraseScorer(TermPositions** tps, int_t tpsLength, int_t s, l_byte_t* n, float_t w): PhraseScorer(tps,tpsLength,n,w), slop(s) { } float_t SloppyPhraseScorer::phraseFreq() { pq.clear(); int_t end = 0; for (PhrasePositions* pp = first; pp != NULL; pp = pp->next) { pp->firstPosition(); if (pp->position > end) end = pp->position; pq.put(pp); // build pq from list } float_t freq = 0.0f; bool done = false; do { PhrasePositions* pp = pq.pop(); int_t start = pp->position; int_t next = pq.top()->position; for (int_t pos = start; pos <= next; pos = pp->position) { start = pos; // advance pp to min window if (!pp->nextPosition()) { done = true; // ran out of a term -- done break; } } int_t matchLength = end - start; if (matchLength <= slop) freq += 1.0 / (matchLength + 1); // penalize longer matches if (pp->position > end) end = pp->position; pq.put(pp); // restore pq } while (!done); return freq; } }} #endif