2014-07-16 23:15:57 +04:00
|
|
|
/*
|
|
|
|
** 2014 May 31
|
|
|
|
**
|
|
|
|
** The author disclaims copyright to this source code. In place of
|
|
|
|
** a legal notice, here is a blessing:
|
|
|
|
**
|
|
|
|
** May you do good and not evil.
|
|
|
|
** May you find forgiveness for yourself and forgive others.
|
|
|
|
** May you share freely, never taking more than you give.
|
|
|
|
**
|
|
|
|
******************************************************************************
|
|
|
|
*/
|
|
|
|
|
2015-03-04 11:29:24 +03:00
|
|
|
|
2014-07-16 23:15:57 +04:00
|
|
|
#include "fts5Int.h"
|
2015-10-08 23:28:33 +03:00
|
|
|
#include <math.h> /* amalgamator: keep */
|
2014-07-16 23:15:57 +04:00
|
|
|
|
2014-12-23 00:01:52 +03:00
|
|
|
/*
|
|
|
|
** Object used to iterate through all "coalesced phrase instances" in
|
|
|
|
** a single column of the current row. If the phrase instances in the
|
|
|
|
** column being considered do not overlap, this object simply iterates
|
|
|
|
** through them. Or, if they do overlap (share one or more tokens in
|
|
|
|
** common), each set of overlapping instances is treated as a single
|
|
|
|
** match. See documentation for the highlight() auxiliary function for
|
|
|
|
** details.
|
|
|
|
**
|
|
|
|
** Usage is:
|
|
|
|
**
|
|
|
|
** for(rc = fts5CInstIterNext(pApi, pFts, iCol, &iter);
|
|
|
|
** (rc==SQLITE_OK && 0==fts5CInstIterEof(&iter);
|
|
|
|
** rc = fts5CInstIterNext(&iter)
|
|
|
|
** ){
|
|
|
|
** printf("instance starts at %d, ends at %d\n", iter.iStart, iter.iEnd);
|
|
|
|
** }
|
|
|
|
**
|
2014-11-24 19:24:33 +03:00
|
|
|
*/
|
2014-12-23 00:01:52 +03:00
|
|
|
typedef struct CInstIter CInstIter;
|
|
|
|
struct CInstIter {
|
2014-11-24 19:24:33 +03:00
|
|
|
const Fts5ExtensionApi *pApi; /* API offered by current FTS version */
|
|
|
|
Fts5Context *pFts; /* First arg to pass to pApi functions */
|
2014-12-23 00:01:52 +03:00
|
|
|
int iCol; /* Column to search */
|
|
|
|
int iInst; /* Next phrase instance index */
|
2014-11-27 23:03:45 +03:00
|
|
|
int nInst; /* Total number of phrase instances */
|
2014-11-24 19:24:33 +03:00
|
|
|
|
2014-12-23 00:01:52 +03:00
|
|
|
/* Output variables */
|
|
|
|
int iStart; /* First token in coalesced phrase instance */
|
|
|
|
int iEnd; /* Last token in coalesced phrase instance */
|
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
** Advance the iterator to the next coalesced phrase instance. Return
|
|
|
|
** an SQLite error code if an error occurs, or SQLITE_OK otherwise.
|
|
|
|
*/
|
|
|
|
static int fts5CInstIterNext(CInstIter *pIter){
|
|
|
|
int rc = SQLITE_OK;
|
|
|
|
pIter->iStart = -1;
|
|
|
|
pIter->iEnd = -1;
|
|
|
|
|
|
|
|
while( rc==SQLITE_OK && pIter->iInst<pIter->nInst ){
|
|
|
|
int ip; int ic; int io;
|
|
|
|
rc = pIter->pApi->xInst(pIter->pFts, pIter->iInst, &ip, &ic, &io);
|
|
|
|
if( rc==SQLITE_OK ){
|
|
|
|
if( ic==pIter->iCol ){
|
|
|
|
int iEnd = io - 1 + pIter->pApi->xPhraseSize(pIter->pFts, ip);
|
|
|
|
if( pIter->iStart<0 ){
|
|
|
|
pIter->iStart = io;
|
|
|
|
pIter->iEnd = iEnd;
|
|
|
|
}else if( io<=pIter->iEnd ){
|
|
|
|
if( iEnd>pIter->iEnd ) pIter->iEnd = iEnd;
|
|
|
|
}else{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
pIter->iInst++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
** Initialize the iterator object indicated by the final parameter to
|
|
|
|
** iterate through coalesced phrase instances in column iCol.
|
|
|
|
*/
|
|
|
|
static int fts5CInstIterInit(
|
|
|
|
const Fts5ExtensionApi *pApi,
|
|
|
|
Fts5Context *pFts,
|
|
|
|
int iCol,
|
|
|
|
CInstIter *pIter
|
|
|
|
){
|
|
|
|
int rc;
|
|
|
|
|
|
|
|
memset(pIter, 0, sizeof(CInstIter));
|
|
|
|
pIter->pApi = pApi;
|
|
|
|
pIter->pFts = pFts;
|
|
|
|
pIter->iCol = iCol;
|
|
|
|
rc = pApi->xInstCount(pFts, &pIter->nInst);
|
|
|
|
|
|
|
|
if( rc==SQLITE_OK ){
|
|
|
|
rc = fts5CInstIterNext(pIter);
|
|
|
|
}
|
|
|
|
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*************************************************************************
|
|
|
|
** Start of highlight() implementation.
|
|
|
|
*/
|
|
|
|
typedef struct HighlightContext HighlightContext;
|
|
|
|
struct HighlightContext {
|
|
|
|
CInstIter iter; /* Coalesced Instance Iterator */
|
2015-01-06 22:08:26 +03:00
|
|
|
int iPos; /* Current token offset in zIn[] */
|
2014-12-23 22:18:34 +03:00
|
|
|
int iRangeStart; /* First token to include */
|
|
|
|
int iRangeEnd; /* If non-zero, last token to include */
|
2014-11-24 19:24:33 +03:00
|
|
|
const char *zOpen; /* Opening highlight */
|
|
|
|
const char *zClose; /* Closing highlight */
|
|
|
|
const char *zIn; /* Input text */
|
|
|
|
int nIn; /* Size of input text in bytes */
|
|
|
|
int iOff; /* Current offset within zIn[] */
|
|
|
|
char *zOut; /* Output value */
|
|
|
|
};
|
|
|
|
|
2014-11-27 23:03:45 +03:00
|
|
|
/*
|
|
|
|
** Append text to the HighlightContext output string - p->zOut. Argument
|
|
|
|
** z points to a buffer containing n bytes of text to append. If n is
|
|
|
|
** negative, everything up until the first '\0' is appended to the output.
|
2014-12-23 00:01:52 +03:00
|
|
|
**
|
|
|
|
** If *pRc is set to any value other than SQLITE_OK when this function is
|
|
|
|
** called, it is a no-op. If an error (i.e. an OOM condition) is encountered,
|
|
|
|
** *pRc is set to an error code before returning.
|
2014-11-27 23:03:45 +03:00
|
|
|
*/
|
|
|
|
static void fts5HighlightAppend(
|
|
|
|
int *pRc,
|
|
|
|
HighlightContext *p,
|
|
|
|
const char *z, int n
|
|
|
|
){
|
2019-01-15 21:14:27 +03:00
|
|
|
if( *pRc==SQLITE_OK && z ){
|
2015-10-14 23:34:57 +03:00
|
|
|
if( n<0 ) n = (int)strlen(z);
|
2014-11-27 23:03:45 +03:00
|
|
|
p->zOut = sqlite3_mprintf("%z%.*s", p->zOut, n, z);
|
|
|
|
if( p->zOut==0 ) *pRc = SQLITE_NOMEM;
|
|
|
|
}
|
2014-11-24 19:24:33 +03:00
|
|
|
}
|
|
|
|
|
2014-12-23 00:01:52 +03:00
|
|
|
/*
|
|
|
|
** Tokenizer callback used by implementation of highlight() function.
|
|
|
|
*/
|
2014-11-24 19:24:33 +03:00
|
|
|
static int fts5HighlightCb(
|
|
|
|
void *pContext, /* Pointer to HighlightContext object */
|
2015-08-29 18:44:27 +03:00
|
|
|
int tflags, /* Mask of FTS5_TOKEN_* flags */
|
2014-11-24 19:24:33 +03:00
|
|
|
const char *pToken, /* Buffer containing token */
|
|
|
|
int nToken, /* Size of token in bytes */
|
2014-11-27 23:03:45 +03:00
|
|
|
int iStartOff, /* Start offset of token */
|
2015-08-29 18:44:27 +03:00
|
|
|
int iEndOff /* End offset of token */
|
2014-11-24 19:24:33 +03:00
|
|
|
){
|
|
|
|
HighlightContext *p = (HighlightContext*)pContext;
|
|
|
|
int rc = SQLITE_OK;
|
2015-08-29 18:44:27 +03:00
|
|
|
int iPos;
|
2015-08-28 22:56:47 +03:00
|
|
|
|
2016-02-11 20:01:32 +03:00
|
|
|
UNUSED_PARAM2(pToken, nToken);
|
|
|
|
|
2015-08-29 18:44:27 +03:00
|
|
|
if( tflags & FTS5_TOKEN_COLOCATED ) return SQLITE_OK;
|
|
|
|
iPos = p->iPos++;
|
2014-11-24 19:24:33 +03:00
|
|
|
|
2014-12-23 00:01:52 +03:00
|
|
|
if( p->iRangeEnd>0 ){
|
|
|
|
if( iPos<p->iRangeStart || iPos>p->iRangeEnd ) return SQLITE_OK;
|
2014-12-23 22:18:34 +03:00
|
|
|
if( p->iRangeStart && iPos==p->iRangeStart ) p->iOff = iStartOff;
|
2014-12-23 00:01:52 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if( iPos==p->iter.iStart ){
|
2014-11-27 23:03:45 +03:00
|
|
|
fts5HighlightAppend(&rc, p, &p->zIn[p->iOff], iStartOff - p->iOff);
|
|
|
|
fts5HighlightAppend(&rc, p, p->zOpen, -1);
|
|
|
|
p->iOff = iStartOff;
|
2014-11-24 19:24:33 +03:00
|
|
|
}
|
|
|
|
|
2014-12-23 00:01:52 +03:00
|
|
|
if( iPos==p->iter.iEnd ){
|
|
|
|
if( p->iRangeEnd && p->iter.iStart<p->iRangeStart ){
|
|
|
|
fts5HighlightAppend(&rc, p, p->zOpen, -1);
|
|
|
|
}
|
|
|
|
fts5HighlightAppend(&rc, p, &p->zIn[p->iOff], iEndOff - p->iOff);
|
|
|
|
fts5HighlightAppend(&rc, p, p->zClose, -1);
|
|
|
|
p->iOff = iEndOff;
|
|
|
|
if( rc==SQLITE_OK ){
|
|
|
|
rc = fts5CInstIterNext(&p->iter);
|
2014-11-27 23:03:45 +03:00
|
|
|
}
|
2014-12-23 00:01:52 +03:00
|
|
|
}
|
2014-11-24 19:24:33 +03:00
|
|
|
|
2014-12-23 00:01:52 +03:00
|
|
|
if( p->iRangeEnd>0 && iPos==p->iRangeEnd ){
|
|
|
|
fts5HighlightAppend(&rc, p, &p->zIn[p->iOff], iEndOff - p->iOff);
|
|
|
|
p->iOff = iEndOff;
|
2016-08-17 14:14:39 +03:00
|
|
|
if( iPos>=p->iter.iStart && iPos<p->iter.iEnd ){
|
2014-11-27 23:03:45 +03:00
|
|
|
fts5HighlightAppend(&rc, p, p->zClose, -1);
|
2014-11-24 19:24:33 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
2014-12-23 00:01:52 +03:00
|
|
|
/*
|
|
|
|
** Implementation of highlight() function.
|
|
|
|
*/
|
2014-11-24 19:24:33 +03:00
|
|
|
static void fts5HighlightFunction(
|
|
|
|
const Fts5ExtensionApi *pApi, /* API offered by current FTS version */
|
|
|
|
Fts5Context *pFts, /* First arg to pass to pApi functions */
|
|
|
|
sqlite3_context *pCtx, /* Context for returning result/error */
|
|
|
|
int nVal, /* Number of values in apVal[] array */
|
|
|
|
sqlite3_value **apVal /* Array of trailing arguments */
|
|
|
|
){
|
|
|
|
HighlightContext ctx;
|
|
|
|
int rc;
|
2014-12-23 00:01:52 +03:00
|
|
|
int iCol;
|
2014-11-24 19:24:33 +03:00
|
|
|
|
|
|
|
if( nVal!=3 ){
|
|
|
|
const char *zErr = "wrong number of arguments to function highlight()";
|
|
|
|
sqlite3_result_error(pCtx, zErr, -1);
|
|
|
|
return;
|
|
|
|
}
|
2014-11-27 23:03:45 +03:00
|
|
|
|
2014-12-23 00:01:52 +03:00
|
|
|
iCol = sqlite3_value_int(apVal[0]);
|
2014-11-24 19:24:33 +03:00
|
|
|
memset(&ctx, 0, sizeof(HighlightContext));
|
|
|
|
ctx.zOpen = (const char*)sqlite3_value_text(apVal[1]);
|
|
|
|
ctx.zClose = (const char*)sqlite3_value_text(apVal[2]);
|
2014-12-23 00:01:52 +03:00
|
|
|
rc = pApi->xColumnText(pFts, iCol, &ctx.zIn, &ctx.nIn);
|
|
|
|
|
2015-01-05 23:41:39 +03:00
|
|
|
if( ctx.zIn ){
|
|
|
|
if( rc==SQLITE_OK ){
|
|
|
|
rc = fts5CInstIterInit(pApi, pFts, iCol, &ctx.iter);
|
|
|
|
}
|
2014-11-24 19:24:33 +03:00
|
|
|
|
2015-01-05 23:41:39 +03:00
|
|
|
if( rc==SQLITE_OK ){
|
|
|
|
rc = pApi->xTokenize(pFts, ctx.zIn, ctx.nIn, (void*)&ctx,fts5HighlightCb);
|
|
|
|
}
|
|
|
|
fts5HighlightAppend(&rc, &ctx, &ctx.zIn[ctx.iOff], ctx.nIn - ctx.iOff);
|
2014-11-24 19:24:33 +03:00
|
|
|
|
2015-01-05 23:41:39 +03:00
|
|
|
if( rc==SQLITE_OK ){
|
|
|
|
sqlite3_result_text(pCtx, (const char*)ctx.zOut, -1, SQLITE_TRANSIENT);
|
|
|
|
}
|
|
|
|
sqlite3_free(ctx.zOut);
|
2014-11-24 19:24:33 +03:00
|
|
|
}
|
2015-04-29 23:54:08 +03:00
|
|
|
if( rc!=SQLITE_OK ){
|
|
|
|
sqlite3_result_error_code(pCtx, rc);
|
|
|
|
}
|
2014-11-24 19:24:33 +03:00
|
|
|
}
|
|
|
|
/*
|
2014-12-23 22:18:34 +03:00
|
|
|
** End of highlight() implementation.
|
2014-11-24 19:24:33 +03:00
|
|
|
**************************************************************************/
|
|
|
|
|
2016-08-17 22:05:12 +03:00
|
|
|
/*
|
|
|
|
** Context object passed to the fts5SentenceFinderCb() function.
|
|
|
|
*/
|
|
|
|
typedef struct Fts5SFinder Fts5SFinder;
|
|
|
|
struct Fts5SFinder {
|
|
|
|
int iPos; /* Current token position */
|
|
|
|
int nFirstAlloc; /* Allocated size of aFirst[] */
|
|
|
|
int nFirst; /* Number of entries in aFirst[] */
|
|
|
|
int *aFirst; /* Array of first token in each sentence */
|
|
|
|
const char *zDoc; /* Document being tokenized */
|
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
** Add an entry to the Fts5SFinder.aFirst[] array. Grow the array if
|
|
|
|
** necessary. Return SQLITE_OK if successful, or SQLITE_NOMEM if an
|
|
|
|
** error occurs.
|
|
|
|
*/
|
|
|
|
static int fts5SentenceFinderAdd(Fts5SFinder *p, int iAdd){
|
|
|
|
if( p->nFirstAlloc==p->nFirst ){
|
|
|
|
int nNew = p->nFirstAlloc ? p->nFirstAlloc*2 : 64;
|
|
|
|
int *aNew;
|
|
|
|
|
2019-01-08 23:02:48 +03:00
|
|
|
aNew = (int*)sqlite3_realloc64(p->aFirst, nNew*sizeof(int));
|
2016-08-17 22:05:12 +03:00
|
|
|
if( aNew==0 ) return SQLITE_NOMEM;
|
|
|
|
p->aFirst = aNew;
|
|
|
|
p->nFirstAlloc = nNew;
|
|
|
|
}
|
|
|
|
p->aFirst[p->nFirst++] = iAdd;
|
|
|
|
return SQLITE_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
** This function is an xTokenize() callback used by the auxiliary snippet()
|
|
|
|
** function. Its job is to identify tokens that are the first in a sentence.
|
|
|
|
** For each such token, an entry is added to the SFinder.aFirst[] array.
|
|
|
|
*/
|
|
|
|
static int fts5SentenceFinderCb(
|
|
|
|
void *pContext, /* Pointer to HighlightContext object */
|
|
|
|
int tflags, /* Mask of FTS5_TOKEN_* flags */
|
|
|
|
const char *pToken, /* Buffer containing token */
|
|
|
|
int nToken, /* Size of token in bytes */
|
|
|
|
int iStartOff, /* Start offset of token */
|
|
|
|
int iEndOff /* End offset of token */
|
|
|
|
){
|
|
|
|
int rc = SQLITE_OK;
|
|
|
|
|
2016-10-05 23:14:29 +03:00
|
|
|
UNUSED_PARAM2(pToken, nToken);
|
|
|
|
UNUSED_PARAM(iEndOff);
|
|
|
|
|
2016-08-17 22:05:12 +03:00
|
|
|
if( (tflags & FTS5_TOKEN_COLOCATED)==0 ){
|
|
|
|
Fts5SFinder *p = (Fts5SFinder*)pContext;
|
|
|
|
if( p->iPos>0 ){
|
|
|
|
int i;
|
|
|
|
char c = 0;
|
|
|
|
for(i=iStartOff-1; i>=0; i--){
|
|
|
|
c = p->zDoc[i];
|
|
|
|
if( c!=' ' && c!='\t' && c!='\n' && c!='\r' ) break;
|
|
|
|
}
|
|
|
|
if( i!=iStartOff-1 && (c=='.' || c==':') ){
|
|
|
|
rc = fts5SentenceFinderAdd(p, p->iPos);
|
|
|
|
}
|
|
|
|
}else{
|
|
|
|
rc = fts5SentenceFinderAdd(p, 0);
|
|
|
|
}
|
|
|
|
p->iPos++;
|
|
|
|
}
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int fts5SnippetScore(
|
|
|
|
const Fts5ExtensionApi *pApi, /* API offered by current FTS version */
|
|
|
|
Fts5Context *pFts, /* First arg to pass to pApi functions */
|
|
|
|
int nDocsize, /* Size of column in tokens */
|
|
|
|
unsigned char *aSeen, /* Array with one element per query phrase */
|
|
|
|
int iCol, /* Column to score */
|
|
|
|
int iPos, /* Starting offset to score */
|
|
|
|
int nToken, /* Max tokens per snippet */
|
|
|
|
int *pnScore, /* OUT: Score */
|
|
|
|
int *piPos /* OUT: Adjusted offset */
|
|
|
|
){
|
|
|
|
int rc;
|
|
|
|
int i;
|
|
|
|
int ip = 0;
|
|
|
|
int ic = 0;
|
|
|
|
int iOff = 0;
|
|
|
|
int iFirst = -1;
|
|
|
|
int nInst;
|
|
|
|
int nScore = 0;
|
2016-08-24 21:50:47 +03:00
|
|
|
int iLast = 0;
|
2019-01-25 19:54:06 +03:00
|
|
|
sqlite3_int64 iEnd = (sqlite3_int64)iPos + nToken;
|
2016-08-17 22:05:12 +03:00
|
|
|
|
|
|
|
rc = pApi->xInstCount(pFts, &nInst);
|
|
|
|
for(i=0; i<nInst && rc==SQLITE_OK; i++){
|
|
|
|
rc = pApi->xInst(pFts, i, &ip, &ic, &iOff);
|
2019-01-25 19:54:06 +03:00
|
|
|
if( rc==SQLITE_OK && ic==iCol && iOff>=iPos && iOff<iEnd ){
|
2016-08-17 22:05:12 +03:00
|
|
|
nScore += (aSeen[ip] ? 1 : 1000);
|
|
|
|
aSeen[ip] = 1;
|
|
|
|
if( iFirst<0 ) iFirst = iOff;
|
2016-08-24 21:50:47 +03:00
|
|
|
iLast = iOff + pApi->xPhraseSize(pFts, ip);
|
2016-08-17 22:05:12 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
*pnScore = nScore;
|
|
|
|
if( piPos ){
|
2019-01-25 19:54:06 +03:00
|
|
|
sqlite3_int64 iAdj = iFirst - (nToken - (iLast-iFirst)) / 2;
|
2016-08-17 22:05:12 +03:00
|
|
|
if( (iAdj+nToken)>nDocsize ) iAdj = nDocsize - nToken;
|
|
|
|
if( iAdj<0 ) iAdj = 0;
|
2019-03-20 08:45:03 +03:00
|
|
|
*piPos = (int)iAdj;
|
2016-08-17 22:05:12 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
2017-12-26 17:32:25 +03:00
|
|
|
/*
|
|
|
|
** Return the value in pVal interpreted as utf-8 text. Except, if pVal
|
|
|
|
** contains a NULL value, return a pointer to a static string zero
|
|
|
|
** bytes in length instead of a NULL pointer.
|
|
|
|
*/
|
|
|
|
static const char *fts5ValueToText(sqlite3_value *pVal){
|
|
|
|
const char *zRet = (const char*)sqlite3_value_text(pVal);
|
|
|
|
return zRet ? zRet : "";
|
|
|
|
}
|
|
|
|
|
2014-12-23 22:18:34 +03:00
|
|
|
/*
|
|
|
|
** Implementation of snippet() function.
|
|
|
|
*/
|
2014-12-23 00:01:52 +03:00
|
|
|
static void fts5SnippetFunction(
|
2014-07-23 23:31:56 +04:00
|
|
|
const Fts5ExtensionApi *pApi, /* API offered by current FTS version */
|
|
|
|
Fts5Context *pFts, /* First arg to pass to pApi functions */
|
2014-12-23 00:01:52 +03:00
|
|
|
sqlite3_context *pCtx, /* Context for returning result/error */
|
|
|
|
int nVal, /* Number of values in apVal[] array */
|
|
|
|
sqlite3_value **apVal /* Array of trailing arguments */
|
2014-07-23 23:31:56 +04:00
|
|
|
){
|
2014-12-23 00:01:52 +03:00
|
|
|
HighlightContext ctx;
|
|
|
|
int rc = SQLITE_OK; /* Return code */
|
|
|
|
int iCol; /* 1st argument to snippet() */
|
|
|
|
const char *zEllips; /* 4th argument to snippet() */
|
|
|
|
int nToken; /* 5th argument to snippet() */
|
2015-07-14 20:18:05 +03:00
|
|
|
int nInst = 0; /* Number of instance matches this row */
|
2014-12-23 00:01:52 +03:00
|
|
|
int i; /* Used to iterate through instances */
|
2014-07-23 23:31:56 +04:00
|
|
|
int nPhrase; /* Number of phrases in query */
|
2014-12-23 00:01:52 +03:00
|
|
|
unsigned char *aSeen; /* Array of "seen instance" flags */
|
|
|
|
int iBestCol; /* Column containing best snippet */
|
|
|
|
int iBestStart = 0; /* First token of best snippet */
|
|
|
|
int nBestScore = 0; /* Score of best snippet */
|
2015-07-14 20:18:05 +03:00
|
|
|
int nColSize = 0; /* Total size of iBestCol in tokens */
|
2016-08-17 22:05:12 +03:00
|
|
|
Fts5SFinder sFinder; /* Used to find the beginnings of sentences */
|
|
|
|
int nCol;
|
2014-12-23 00:01:52 +03:00
|
|
|
|
|
|
|
if( nVal!=5 ){
|
|
|
|
const char *zErr = "wrong number of arguments to function snippet()";
|
|
|
|
sqlite3_result_error(pCtx, zErr, -1);
|
|
|
|
return;
|
2014-07-23 23:31:56 +04:00
|
|
|
}
|
|
|
|
|
2016-08-17 22:05:12 +03:00
|
|
|
nCol = pApi->xColumnCount(pFts);
|
2014-12-23 00:01:52 +03:00
|
|
|
memset(&ctx, 0, sizeof(HighlightContext));
|
|
|
|
iCol = sqlite3_value_int(apVal[0]);
|
2017-12-26 17:32:25 +03:00
|
|
|
ctx.zOpen = fts5ValueToText(apVal[1]);
|
|
|
|
ctx.zClose = fts5ValueToText(apVal[2]);
|
|
|
|
zEllips = fts5ValueToText(apVal[3]);
|
2014-12-23 00:01:52 +03:00
|
|
|
nToken = sqlite3_value_int(apVal[4]);
|
2014-07-23 23:31:56 +04:00
|
|
|
|
2014-12-23 00:01:52 +03:00
|
|
|
iBestCol = (iCol>=0 ? iCol : 0);
|
|
|
|
nPhrase = pApi->xPhraseCount(pFts);
|
|
|
|
aSeen = sqlite3_malloc(nPhrase);
|
|
|
|
if( aSeen==0 ){
|
|
|
|
rc = SQLITE_NOMEM;
|
|
|
|
}
|
|
|
|
if( rc==SQLITE_OK ){
|
|
|
|
rc = pApi->xInstCount(pFts, &nInst);
|
2014-07-23 23:31:56 +04:00
|
|
|
}
|
2014-12-23 00:01:52 +03:00
|
|
|
|
2016-08-17 22:05:12 +03:00
|
|
|
memset(&sFinder, 0, sizeof(Fts5SFinder));
|
|
|
|
for(i=0; i<nCol; i++){
|
|
|
|
if( iCol<0 || iCol==i ){
|
|
|
|
int nDoc;
|
|
|
|
int nDocsize;
|
|
|
|
int ii;
|
|
|
|
sFinder.iPos = 0;
|
|
|
|
sFinder.nFirst = 0;
|
|
|
|
rc = pApi->xColumnText(pFts, i, &sFinder.zDoc, &nDoc);
|
|
|
|
if( rc!=SQLITE_OK ) break;
|
|
|
|
rc = pApi->xTokenize(pFts,
|
|
|
|
sFinder.zDoc, nDoc, (void*)&sFinder,fts5SentenceFinderCb
|
|
|
|
);
|
|
|
|
if( rc!=SQLITE_OK ) break;
|
|
|
|
rc = pApi->xColumnSize(pFts, i, &nDocsize);
|
|
|
|
if( rc!=SQLITE_OK ) break;
|
|
|
|
|
|
|
|
for(ii=0; rc==SQLITE_OK && ii<nInst; ii++){
|
|
|
|
int ip, ic, io;
|
2016-08-19 17:25:38 +03:00
|
|
|
int iAdj;
|
2016-08-17 22:05:12 +03:00
|
|
|
int nScore;
|
2016-08-19 17:25:38 +03:00
|
|
|
int jj;
|
|
|
|
|
2016-08-17 22:05:12 +03:00
|
|
|
rc = pApi->xInst(pFts, ii, &ip, &ic, &io);
|
2019-01-29 19:34:45 +03:00
|
|
|
if( ic!=i ) continue;
|
|
|
|
if( io>nDocsize ) rc = FTS5_CORRUPT;
|
|
|
|
if( rc!=SQLITE_OK ) continue;
|
2016-08-17 22:05:12 +03:00
|
|
|
memset(aSeen, 0, nPhrase);
|
|
|
|
rc = fts5SnippetScore(pApi, pFts, nDocsize, aSeen, i,
|
2016-08-19 17:25:38 +03:00
|
|
|
io, nToken, &nScore, &iAdj
|
2016-08-17 22:05:12 +03:00
|
|
|
);
|
|
|
|
if( rc==SQLITE_OK && nScore>nBestScore ){
|
|
|
|
nBestScore = nScore;
|
|
|
|
iBestCol = i;
|
2016-08-19 17:25:38 +03:00
|
|
|
iBestStart = iAdj;
|
2016-08-17 22:05:12 +03:00
|
|
|
nColSize = nDocsize;
|
|
|
|
}
|
2016-08-19 17:25:38 +03:00
|
|
|
|
2016-08-20 20:23:45 +03:00
|
|
|
if( rc==SQLITE_OK && sFinder.nFirst && nDocsize>nToken ){
|
2016-08-19 17:25:38 +03:00
|
|
|
for(jj=0; jj<(sFinder.nFirst-1); jj++){
|
|
|
|
if( sFinder.aFirst[jj+1]>io ) break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if( sFinder.aFirst[jj]<io ){
|
|
|
|
memset(aSeen, 0, nPhrase);
|
|
|
|
rc = fts5SnippetScore(pApi, pFts, nDocsize, aSeen, i,
|
|
|
|
sFinder.aFirst[jj], nToken, &nScore, 0
|
|
|
|
);
|
|
|
|
|
|
|
|
nScore += (sFinder.aFirst[jj]==0 ? 120 : 100);
|
|
|
|
if( rc==SQLITE_OK && nScore>nBestScore ){
|
|
|
|
nBestScore = nScore;
|
|
|
|
iBestCol = i;
|
|
|
|
iBestStart = sFinder.aFirst[jj];
|
|
|
|
nColSize = nDocsize;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-07-23 23:31:56 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-12-23 00:01:52 +03:00
|
|
|
if( rc==SQLITE_OK ){
|
|
|
|
rc = pApi->xColumnText(pFts, iBestCol, &ctx.zIn, &ctx.nIn);
|
2014-07-23 23:31:56 +04:00
|
|
|
}
|
2016-08-20 20:23:45 +03:00
|
|
|
if( rc==SQLITE_OK && nColSize==0 ){
|
|
|
|
rc = pApi->xColumnSize(pFts, iBestCol, &nColSize);
|
|
|
|
}
|
2015-01-05 23:41:39 +03:00
|
|
|
if( ctx.zIn ){
|
|
|
|
if( rc==SQLITE_OK ){
|
|
|
|
rc = fts5CInstIterInit(pApi, pFts, iBestCol, &ctx.iter);
|
|
|
|
}
|
2014-07-23 23:31:56 +04:00
|
|
|
|
2015-01-05 23:41:39 +03:00
|
|
|
ctx.iRangeStart = iBestStart;
|
|
|
|
ctx.iRangeEnd = iBestStart + nToken - 1;
|
2014-07-23 23:31:56 +04:00
|
|
|
|
2015-01-05 23:41:39 +03:00
|
|
|
if( iBestStart>0 ){
|
|
|
|
fts5HighlightAppend(&rc, &ctx, zEllips, -1);
|
|
|
|
}
|
2016-08-17 14:14:39 +03:00
|
|
|
|
|
|
|
/* Advance iterator ctx.iter so that it points to the first coalesced
|
|
|
|
** phrase instance at or following position iBestStart. */
|
|
|
|
while( ctx.iter.iStart>=0 && ctx.iter.iStart<iBestStart && rc==SQLITE_OK ){
|
|
|
|
rc = fts5CInstIterNext(&ctx.iter);
|
|
|
|
}
|
|
|
|
|
2015-01-05 23:41:39 +03:00
|
|
|
if( rc==SQLITE_OK ){
|
|
|
|
rc = pApi->xTokenize(pFts, ctx.zIn, ctx.nIn, (void*)&ctx,fts5HighlightCb);
|
|
|
|
}
|
|
|
|
if( ctx.iRangeEnd>=(nColSize-1) ){
|
|
|
|
fts5HighlightAppend(&rc, &ctx, &ctx.zIn[ctx.iOff], ctx.nIn - ctx.iOff);
|
|
|
|
}else{
|
|
|
|
fts5HighlightAppend(&rc, &ctx, zEllips, -1);
|
|
|
|
}
|
2014-07-23 23:31:56 +04:00
|
|
|
}
|
2016-08-17 22:05:12 +03:00
|
|
|
if( rc==SQLITE_OK ){
|
|
|
|
sqlite3_result_text(pCtx, (const char*)ctx.zOut, -1, SQLITE_TRANSIENT);
|
|
|
|
}else{
|
|
|
|
sqlite3_result_error_code(pCtx, rc);
|
|
|
|
}
|
|
|
|
sqlite3_free(ctx.zOut);
|
2014-12-23 00:01:52 +03:00
|
|
|
sqlite3_free(aSeen);
|
2016-08-17 22:05:12 +03:00
|
|
|
sqlite3_free(sFinder.aFirst);
|
2014-07-23 23:31:56 +04:00
|
|
|
}
|
|
|
|
|
2014-12-23 00:01:52 +03:00
|
|
|
/************************************************************************/
|
|
|
|
|
2014-07-26 22:38:51 +04:00
|
|
|
/*
|
2014-12-23 22:18:34 +03:00
|
|
|
** The first time the bm25() function is called for a query, an instance
|
|
|
|
** of the following structure is allocated and populated.
|
2014-07-26 22:38:51 +04:00
|
|
|
*/
|
2014-12-23 22:18:34 +03:00
|
|
|
typedef struct Fts5Bm25Data Fts5Bm25Data;
|
|
|
|
struct Fts5Bm25Data {
|
|
|
|
int nPhrase; /* Number of phrases in query */
|
|
|
|
double avgdl; /* Average number of tokens in each row */
|
|
|
|
double *aIDF; /* IDF for each phrase */
|
|
|
|
double *aFreq; /* Array used to calculate phrase freq. */
|
2014-07-26 00:30:47 +04:00
|
|
|
};
|
|
|
|
|
2014-07-26 22:38:51 +04:00
|
|
|
/*
|
2014-12-23 22:18:34 +03:00
|
|
|
** Callback used by fts5Bm25GetData() to count the number of rows in the
|
|
|
|
** table matched by each individual phrase within the query.
|
2014-07-26 22:38:51 +04:00
|
|
|
*/
|
2014-12-23 22:18:34 +03:00
|
|
|
static int fts5CountCb(
|
2014-07-26 00:30:47 +04:00
|
|
|
const Fts5ExtensionApi *pApi,
|
|
|
|
Fts5Context *pFts,
|
2014-12-23 22:18:34 +03:00
|
|
|
void *pUserData /* Pointer to sqlite3_int64 variable */
|
2014-07-26 00:30:47 +04:00
|
|
|
){
|
2014-12-23 22:18:34 +03:00
|
|
|
sqlite3_int64 *pn = (sqlite3_int64*)pUserData;
|
2016-02-11 20:01:32 +03:00
|
|
|
UNUSED_PARAM2(pApi, pFts);
|
2014-12-23 22:18:34 +03:00
|
|
|
(*pn)++;
|
2014-07-26 00:30:47 +04:00
|
|
|
return SQLITE_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2014-12-23 22:18:34 +03:00
|
|
|
** Set *ppData to point to the Fts5Bm25Data object for the current query.
|
|
|
|
** If the object has not already been allocated, allocate and populate it
|
|
|
|
** now.
|
2014-07-26 00:30:47 +04:00
|
|
|
*/
|
2014-12-23 22:18:34 +03:00
|
|
|
static int fts5Bm25GetData(
|
|
|
|
const Fts5ExtensionApi *pApi,
|
|
|
|
Fts5Context *pFts,
|
|
|
|
Fts5Bm25Data **ppData /* OUT: bm25-data object for this query */
|
2014-07-16 23:15:57 +04:00
|
|
|
){
|
2014-12-23 22:18:34 +03:00
|
|
|
int rc = SQLITE_OK; /* Return code */
|
|
|
|
Fts5Bm25Data *p; /* Object to return */
|
2014-07-26 00:30:47 +04:00
|
|
|
|
2020-11-27 19:15:55 +03:00
|
|
|
p = (Fts5Bm25Data*)pApi->xGetAuxdata(pFts, 0);
|
2014-07-26 00:30:47 +04:00
|
|
|
if( p==0 ){
|
2014-12-23 22:18:34 +03:00
|
|
|
int nPhrase; /* Number of phrases in query */
|
2015-07-14 20:18:05 +03:00
|
|
|
sqlite3_int64 nRow = 0; /* Number of rows in table */
|
|
|
|
sqlite3_int64 nToken = 0; /* Number of tokens in table */
|
2019-01-08 23:02:48 +03:00
|
|
|
sqlite3_int64 nByte; /* Bytes of space to allocate */
|
2014-12-23 22:18:34 +03:00
|
|
|
int i;
|
|
|
|
|
|
|
|
/* Allocate the Fts5Bm25Data object */
|
|
|
|
nPhrase = pApi->xPhraseCount(pFts);
|
|
|
|
nByte = sizeof(Fts5Bm25Data) + nPhrase*2*sizeof(double);
|
2019-01-08 23:02:48 +03:00
|
|
|
p = (Fts5Bm25Data*)sqlite3_malloc64(nByte);
|
2014-07-26 00:30:47 +04:00
|
|
|
if( p==0 ){
|
|
|
|
rc = SQLITE_NOMEM;
|
|
|
|
}else{
|
2019-03-20 08:45:03 +03:00
|
|
|
memset(p, 0, (size_t)nByte);
|
2014-07-26 22:38:51 +04:00
|
|
|
p->nPhrase = nPhrase;
|
2014-12-23 22:18:34 +03:00
|
|
|
p->aIDF = (double*)&p[1];
|
|
|
|
p->aFreq = &p->aIDF[nPhrase];
|
2014-07-26 00:30:47 +04:00
|
|
|
}
|
|
|
|
|
2014-12-23 22:18:34 +03:00
|
|
|
/* Calculate the average document length for this FTS5 table */
|
|
|
|
if( rc==SQLITE_OK ) rc = pApi->xRowCount(pFts, &nRow);
|
2019-01-19 18:55:27 +03:00
|
|
|
assert( rc!=SQLITE_OK || nRow>0 );
|
2014-12-23 22:18:34 +03:00
|
|
|
if( rc==SQLITE_OK ) rc = pApi->xColumnTotalSize(pFts, -1, &nToken);
|
|
|
|
if( rc==SQLITE_OK ) p->avgdl = (double)nToken / (double)nRow;
|
|
|
|
|
|
|
|
/* Calculate an IDF for each phrase in the query */
|
|
|
|
for(i=0; rc==SQLITE_OK && i<nPhrase; i++){
|
|
|
|
sqlite3_int64 nHit = 0;
|
|
|
|
rc = pApi->xQueryPhrase(pFts, i, (void*)&nHit, fts5CountCb);
|
|
|
|
if( rc==SQLITE_OK ){
|
|
|
|
/* Calculate the IDF (Inverse Document Frequency) for phrase i.
|
|
|
|
** This is done using the standard BM25 formula as found on wikipedia:
|
2014-07-26 22:38:51 +04:00
|
|
|
**
|
|
|
|
** IDF = log( (N - nHit + 0.5) / (nHit + 0.5) )
|
|
|
|
**
|
|
|
|
** where "N" is the total number of documents in the set and nHit
|
|
|
|
** is the number that contain at least one instance of the phrase
|
|
|
|
** under consideration.
|
|
|
|
**
|
|
|
|
** The problem with this is that if (N < 2*nHit), the IDF is
|
|
|
|
** negative. Which is undesirable. So the mimimum allowable IDF is
|
|
|
|
** (1e-6) - roughly the same as a term that appears in just over
|
|
|
|
** half of set of 5,000,000 documents. */
|
2014-12-23 22:18:34 +03:00
|
|
|
double idf = log( (nRow - nHit + 0.5) / (nHit + 0.5) );
|
|
|
|
if( idf<=0.0 ) idf = 1e-6;
|
|
|
|
p->aIDF[i] = idf;
|
2014-07-26 00:30:47 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if( rc!=SQLITE_OK ){
|
|
|
|
sqlite3_free(p);
|
2014-12-23 22:18:34 +03:00
|
|
|
}else{
|
|
|
|
rc = pApi->xSetAuxdata(pFts, p, sqlite3_free);
|
2014-07-26 00:30:47 +04:00
|
|
|
}
|
2014-12-23 22:18:34 +03:00
|
|
|
if( rc!=SQLITE_OK ) p = 0;
|
2014-07-26 00:30:47 +04:00
|
|
|
}
|
2014-12-23 22:18:34 +03:00
|
|
|
*ppData = p;
|
2014-07-26 22:38:51 +04:00
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
2014-12-23 22:18:34 +03:00
|
|
|
/*
|
|
|
|
** Implementation of bm25() function.
|
|
|
|
*/
|
2014-07-26 22:38:51 +04:00
|
|
|
static void fts5Bm25Function(
|
|
|
|
const Fts5ExtensionApi *pApi, /* API offered by current FTS version */
|
|
|
|
Fts5Context *pFts, /* First arg to pass to pApi functions */
|
|
|
|
sqlite3_context *pCtx, /* Context for returning result/error */
|
|
|
|
int nVal, /* Number of values in apVal[] array */
|
|
|
|
sqlite3_value **apVal /* Array of trailing arguments */
|
|
|
|
){
|
2014-12-23 22:18:34 +03:00
|
|
|
const double k1 = 1.2; /* Constant "k1" from BM25 formula */
|
|
|
|
const double b = 0.75; /* Constant "b" from BM25 formula */
|
2020-11-27 22:40:13 +03:00
|
|
|
int rc; /* Error code */
|
2014-12-23 22:18:34 +03:00
|
|
|
double score = 0.0; /* SQL function return value */
|
|
|
|
Fts5Bm25Data *pData; /* Values allocated/calculated once only */
|
|
|
|
int i; /* Iterator variable */
|
2015-07-14 20:18:05 +03:00
|
|
|
int nInst = 0; /* Value returned by xInstCount() */
|
|
|
|
double D = 0.0; /* Total number of tokens in row */
|
|
|
|
double *aFreq = 0; /* Array of phrase freq. for current row */
|
2014-12-23 22:18:34 +03:00
|
|
|
|
|
|
|
/* Calculate the phrase frequency (symbol "f(qi,D)" in the documentation)
|
|
|
|
** for each phrase in the query for the current row. */
|
|
|
|
rc = fts5Bm25GetData(pApi, pFts, &pData);
|
2014-07-26 00:30:47 +04:00
|
|
|
if( rc==SQLITE_OK ){
|
2014-12-23 22:18:34 +03:00
|
|
|
aFreq = pData->aFreq;
|
|
|
|
memset(aFreq, 0, sizeof(double) * pData->nPhrase);
|
|
|
|
rc = pApi->xInstCount(pFts, &nInst);
|
|
|
|
}
|
|
|
|
for(i=0; rc==SQLITE_OK && i<nInst; i++){
|
|
|
|
int ip; int ic; int io;
|
|
|
|
rc = pApi->xInst(pFts, i, &ip, &ic, &io);
|
2014-07-26 00:30:47 +04:00
|
|
|
if( rc==SQLITE_OK ){
|
2014-12-23 22:18:34 +03:00
|
|
|
double w = (nVal > ic) ? sqlite3_value_double(apVal[ic]) : 1.0;
|
|
|
|
aFreq[ip] += w;
|
2014-07-26 00:30:47 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-12-23 22:18:34 +03:00
|
|
|
/* Figure out the total size of the current row in tokens. */
|
|
|
|
if( rc==SQLITE_OK ){
|
|
|
|
int nTok;
|
|
|
|
rc = pApi->xColumnSize(pFts, -1, &nTok);
|
|
|
|
D = (double)nTok;
|
|
|
|
}
|
|
|
|
|
2020-11-27 19:08:15 +03:00
|
|
|
/* Determine and return the BM25 score for the current row. Or, if an
|
|
|
|
** error has occurred, throw an exception. */
|
2020-11-27 19:05:31 +03:00
|
|
|
if( rc==SQLITE_OK ){
|
|
|
|
for(i=0; i<pData->nPhrase; i++){
|
|
|
|
score += pData->aIDF[i] * (
|
|
|
|
( aFreq[i] * (k1 + 1.0) ) /
|
|
|
|
( aFreq[i] + k1 * (1 - b + b * D / pData->avgdl) )
|
|
|
|
);
|
|
|
|
}
|
2015-03-07 14:50:31 +03:00
|
|
|
sqlite3_result_double(pCtx, -1.0 * score);
|
2014-12-23 22:18:34 +03:00
|
|
|
}else{
|
2014-07-26 00:30:47 +04:00
|
|
|
sqlite3_result_error_code(pCtx, rc);
|
|
|
|
}
|
2014-07-16 23:15:57 +04:00
|
|
|
}
|
|
|
|
|
2014-11-15 23:07:31 +03:00
|
|
|
int sqlite3Fts5AuxInit(fts5_api *pApi){
|
2014-07-16 23:15:57 +04:00
|
|
|
struct Builtin {
|
|
|
|
const char *zFunc; /* Function name (nul-terminated) */
|
|
|
|
void *pUserData; /* User-data pointer */
|
|
|
|
fts5_extension_function xFunc;/* Callback function */
|
|
|
|
void (*xDestroy)(void*); /* Destructor function */
|
|
|
|
} aBuiltin [] = {
|
2014-07-26 00:30:47 +04:00
|
|
|
{ "snippet", 0, fts5SnippetFunction, 0 },
|
2014-11-24 19:24:33 +03:00
|
|
|
{ "highlight", 0, fts5HighlightFunction, 0 },
|
2014-07-30 23:41:58 +04:00
|
|
|
{ "bm25", 0, fts5Bm25Function, 0 },
|
2014-07-16 23:15:57 +04:00
|
|
|
};
|
|
|
|
int rc = SQLITE_OK; /* Return code */
|
|
|
|
int i; /* To iterate through builtin functions */
|
|
|
|
|
2016-01-23 21:51:59 +03:00
|
|
|
for(i=0; rc==SQLITE_OK && i<ArraySize(aBuiltin); i++){
|
2014-11-15 23:07:31 +03:00
|
|
|
rc = pApi->xCreateFunction(pApi,
|
2014-07-16 23:15:57 +04:00
|
|
|
aBuiltin[i].zFunc,
|
|
|
|
aBuiltin[i].pUserData,
|
|
|
|
aBuiltin[i].xFunc,
|
|
|
|
aBuiltin[i].xDestroy
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
return rc;
|
|
|
|
}
|