2006-10-04 08:30:14 +08:00
|
|
|
/* $PostgreSQL: pgsql/contrib/tsearch2/dict_thesaurus.c,v 1.6 2006/10/04 00:29:46 momjian Exp $ */
|
2006-05-31 22:05:31 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* thesaurus
|
|
|
|
* Teodor Sigaev <teodor@sigaev.ru>
|
|
|
|
*/
|
|
|
|
#include "postgres.h"
|
|
|
|
#include "executor/spi.h"
|
|
|
|
|
|
|
|
#include <ctype.h>
|
|
|
|
|
|
|
|
#include "dict.h"
|
|
|
|
#include "common.h"
|
|
|
|
#include "ts_locale.h"
|
|
|
|
|
2006-06-07 00:25:55 +08:00
|
|
|
/*
|
|
|
|
* Temporay we use TSLexeme.flags for inner use...
|
|
|
|
*/
|
2006-10-04 08:30:14 +08:00
|
|
|
#define DT_USEASIS 0x1000
|
2006-06-07 00:25:55 +08:00
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
typedef struct LexemeInfo
|
|
|
|
{
|
|
|
|
uint16 idsubst; /* entry's number in DictThesaurus->subst */
|
|
|
|
uint16 posinsubst; /* pos info in entry */
|
|
|
|
uint16 tnvariant; /* total num lexemes in one variant */
|
2006-05-31 22:05:31 +08:00
|
|
|
struct LexemeInfo *nextentry;
|
|
|
|
struct LexemeInfo *nextvariant;
|
2006-10-04 08:30:14 +08:00
|
|
|
} LexemeInfo;
|
2006-05-31 22:05:31 +08:00
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
typedef struct
|
|
|
|
{
|
|
|
|
char *lexeme;
|
|
|
|
LexemeInfo *entries;
|
|
|
|
} TheLexeme;
|
2006-05-31 22:05:31 +08:00
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
typedef struct
|
|
|
|
{
|
|
|
|
uint16 lastlexeme; /* number lexemes to substitute */
|
|
|
|
uint16 reslen;
|
|
|
|
TSLexeme *res; /* prepared substituted result */
|
|
|
|
} TheSubstitute;
|
2006-05-31 22:05:31 +08:00
|
|
|
|
|
|
|
typedef struct
|
|
|
|
{
|
2006-10-04 08:30:14 +08:00
|
|
|
/* subdictionary to normalize lexemes */
|
2006-05-31 22:05:31 +08:00
|
|
|
DictInfo subdict;
|
|
|
|
|
|
|
|
/* Array to search lexeme by exact match */
|
2006-10-04 08:30:14 +08:00
|
|
|
TheLexeme *wrds;
|
2006-05-31 22:05:31 +08:00
|
|
|
int nwrds;
|
|
|
|
int ntwrds;
|
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
/*
|
|
|
|
* Storage of substituted result, n-th element is for n-th expression
|
|
|
|
*/
|
|
|
|
TheSubstitute *subst;
|
|
|
|
int nsubst;
|
2006-05-31 22:05:31 +08:00
|
|
|
} DictThesaurus;
|
|
|
|
|
|
|
|
PG_FUNCTION_INFO_V1(thesaurus_init);
|
|
|
|
Datum thesaurus_init(PG_FUNCTION_ARGS);
|
|
|
|
|
|
|
|
PG_FUNCTION_INFO_V1(thesaurus_lexize);
|
|
|
|
Datum thesaurus_lexize(PG_FUNCTION_ARGS);
|
|
|
|
|
|
|
|
static void
|
|
|
|
freeDictThesaurus(DictThesaurus * d)
|
|
|
|
{
|
|
|
|
free(d);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2006-10-04 08:30:14 +08:00
|
|
|
newLexeme(DictThesaurus * d, char *b, char *e, uint16 idsubst, uint16 posinsubst)
|
|
|
|
{
|
|
|
|
TheLexeme *ptr;
|
2006-05-31 22:05:31 +08:00
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
if (d->nwrds >= d->ntwrds)
|
|
|
|
{
|
|
|
|
if (d->ntwrds == 0)
|
|
|
|
{
|
2006-05-31 22:05:31 +08:00
|
|
|
d->ntwrds = 16;
|
2006-10-04 08:30:14 +08:00
|
|
|
d->wrds = (TheLexeme *) malloc(sizeof(TheLexeme) * d->ntwrds);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2006-05-31 22:05:31 +08:00
|
|
|
d->ntwrds *= 2;
|
2006-10-04 08:30:14 +08:00
|
|
|
d->wrds = (TheLexeme *) realloc(d->wrds, sizeof(TheLexeme) * d->ntwrds);
|
2006-05-31 22:05:31 +08:00
|
|
|
}
|
|
|
|
if (!d->wrds)
|
2006-10-04 08:30:14 +08:00
|
|
|
elog(ERROR, "Out of memory");
|
2006-05-31 22:05:31 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
ptr = d->wrds + d->nwrds;
|
|
|
|
d->nwrds++;
|
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
if ((ptr->lexeme = malloc(e - b + 1)) == NULL)
|
|
|
|
elog(ERROR, "Out of memory");
|
2006-05-31 22:05:31 +08:00
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
memcpy(ptr->lexeme, b, e - b);
|
|
|
|
ptr->lexeme[e - b] = '\0';
|
2006-05-31 22:05:31 +08:00
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
if ((ptr->entries = (LexemeInfo *) malloc(sizeof(LexemeInfo))) == NULL)
|
|
|
|
elog(ERROR, "Out of memory");
|
2006-05-31 22:05:31 +08:00
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
ptr->entries->nextentry = NULL;
|
2006-05-31 22:05:31 +08:00
|
|
|
ptr->entries->idsubst = idsubst;
|
|
|
|
ptr->entries->posinsubst = posinsubst;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2006-10-04 08:30:14 +08:00
|
|
|
addWrd(DictThesaurus * d, char *b, char *e, uint16 idsubst, uint16 nwrd, uint16 posinsubst, bool useasis)
|
|
|
|
{
|
|
|
|
static int nres = 0;
|
|
|
|
static int ntres = 0;
|
|
|
|
TheSubstitute *ptr;
|
2006-05-31 22:05:31 +08:00
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
if (nwrd == 0)
|
|
|
|
{
|
2006-05-31 22:05:31 +08:00
|
|
|
nres = ntres = 0;
|
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
if (idsubst <= d->nsubst)
|
|
|
|
{
|
|
|
|
if (d->nsubst == 0)
|
|
|
|
{
|
2006-05-31 22:05:31 +08:00
|
|
|
d->nsubst = 16;
|
2006-10-04 08:30:14 +08:00
|
|
|
d->subst = (TheSubstitute *) malloc(sizeof(TheSubstitute) * d->nsubst);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2006-05-31 22:05:31 +08:00
|
|
|
d->nsubst *= 2;
|
2006-10-04 08:30:14 +08:00
|
|
|
d->subst = (TheSubstitute *) realloc(d->subst, sizeof(TheSubstitute) * d->nsubst);
|
2006-05-31 22:05:31 +08:00
|
|
|
}
|
|
|
|
if (!d->subst)
|
2006-10-04 08:30:14 +08:00
|
|
|
elog(ERROR, "Out of memory");
|
2006-05-31 22:05:31 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ptr = d->subst + idsubst;
|
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
ptr->lastlexeme = posinsubst - 1;
|
2006-05-31 22:05:31 +08:00
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
if (nres + 1 >= ntres)
|
|
|
|
{
|
|
|
|
if (ntres == 0)
|
|
|
|
{
|
2006-05-31 22:05:31 +08:00
|
|
|
ntres = 2;
|
2006-10-04 08:30:14 +08:00
|
|
|
ptr->res = (TSLexeme *) malloc(sizeof(TSLexeme) * ntres);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2006-05-31 22:05:31 +08:00
|
|
|
ntres *= 2;
|
2006-10-04 08:30:14 +08:00
|
|
|
ptr->res = (TSLexeme *) realloc(ptr->res, sizeof(TSLexeme) * ntres);
|
2006-05-31 22:05:31 +08:00
|
|
|
}
|
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
if (!ptr->res)
|
|
|
|
elog(ERROR, "Out of memory");
|
2006-05-31 22:05:31 +08:00
|
|
|
}
|
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
if ((ptr->res[nres].lexeme = malloc(e - b + 1)) == 0)
|
|
|
|
elog(ERROR, "Out of memory");
|
|
|
|
memcpy(ptr->res[nres].lexeme, b, e - b);
|
|
|
|
ptr->res[nres].lexeme[e - b] = '\0';
|
2006-05-31 22:05:31 +08:00
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
ptr->res[nres].nvariant = nwrd;
|
|
|
|
if (useasis)
|
|
|
|
ptr->res[nres].flags = DT_USEASIS;
|
2006-06-07 00:25:55 +08:00
|
|
|
else
|
2006-10-04 08:30:14 +08:00
|
|
|
ptr->res[nres].flags = 0;
|
2006-05-31 22:05:31 +08:00
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
ptr->res[++nres].lexeme = NULL;
|
2006-05-31 22:05:31 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
#define TR_WAITLEX 1
|
|
|
|
#define TR_INLEX 2
|
2006-10-04 08:30:14 +08:00
|
|
|
#define TR_WAITSUBS 3
|
2006-05-31 22:05:31 +08:00
|
|
|
#define TR_INSUBS 4
|
|
|
|
|
|
|
|
static void
|
2006-10-04 08:30:14 +08:00
|
|
|
thesaurusRead(char *filename, DictThesaurus * d)
|
|
|
|
{
|
|
|
|
FILE *fh;
|
|
|
|
char str[BUFSIZ];
|
|
|
|
int lineno = 0;
|
|
|
|
uint16 idsubst = 0;
|
|
|
|
bool useasis = false;
|
2006-05-31 22:05:31 +08:00
|
|
|
|
|
|
|
fh = fopen(to_absfilename(filename), "r");
|
|
|
|
if (!fh)
|
2006-10-04 08:30:14 +08:00
|
|
|
elog(ERROR, "Thesaurus: can't open '%s' file", filename);
|
2006-05-31 22:05:31 +08:00
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
while (fgets(str, sizeof(str), fh))
|
|
|
|
{
|
|
|
|
char *ptr = str;
|
|
|
|
int state = TR_WAITLEX;
|
|
|
|
char *beginwrd = NULL;
|
|
|
|
uint16 posinsubst = 0;
|
|
|
|
uint16 nwrd = 0;
|
2006-05-31 22:05:31 +08:00
|
|
|
|
|
|
|
lineno++;
|
|
|
|
|
|
|
|
/* is it comment ? */
|
2006-10-04 08:30:14 +08:00
|
|
|
while (t_isspace(ptr))
|
2006-05-31 22:05:31 +08:00
|
|
|
ptr += pg_mblen(ptr);
|
2006-10-04 08:30:14 +08:00
|
|
|
if (t_iseq(str, '#') || *str == '\0' || t_iseq(str, '\n') || t_iseq(str, '\r'))
|
2006-05-31 22:05:31 +08:00
|
|
|
continue;
|
|
|
|
|
|
|
|
pg_verifymbstr(ptr, strlen(ptr), false);
|
2006-10-04 08:30:14 +08:00
|
|
|
while (*ptr)
|
|
|
|
{
|
|
|
|
if (state == TR_WAITLEX)
|
|
|
|
{
|
|
|
|
if (t_iseq(ptr, ':'))
|
|
|
|
{
|
|
|
|
if (posinsubst == 0)
|
|
|
|
{
|
2006-05-31 22:05:31 +08:00
|
|
|
fclose(fh);
|
|
|
|
elog(ERROR, "Thesaurus: Unexpected delimiter at %d line", lineno);
|
|
|
|
}
|
|
|
|
state = TR_WAITSUBS;
|
2006-10-04 08:30:14 +08:00
|
|
|
}
|
|
|
|
else if (!t_isspace(ptr))
|
|
|
|
{
|
2006-05-31 22:05:31 +08:00
|
|
|
beginwrd = ptr;
|
|
|
|
state = TR_INLEX;
|
|
|
|
}
|
2006-10-04 08:30:14 +08:00
|
|
|
}
|
|
|
|
else if (state == TR_INLEX)
|
|
|
|
{
|
|
|
|
if (t_iseq(ptr, ':'))
|
|
|
|
{
|
|
|
|
newLexeme(d, beginwrd, ptr, idsubst, posinsubst++);
|
2006-05-31 22:05:31 +08:00
|
|
|
state = TR_WAITSUBS;
|
2006-10-04 08:30:14 +08:00
|
|
|
}
|
|
|
|
else if (t_isspace(ptr))
|
|
|
|
{
|
|
|
|
newLexeme(d, beginwrd, ptr, idsubst, posinsubst++);
|
2006-05-31 22:05:31 +08:00
|
|
|
state = TR_WAITLEX;
|
|
|
|
}
|
2006-10-04 08:30:14 +08:00
|
|
|
}
|
|
|
|
else if (state == TR_WAITSUBS)
|
|
|
|
{
|
|
|
|
if (t_iseq(ptr, '*'))
|
|
|
|
{
|
2006-06-07 00:25:55 +08:00
|
|
|
useasis = true;
|
|
|
|
state = TR_INSUBS;
|
|
|
|
beginwrd = ptr + pg_mblen(ptr);
|
2006-10-04 08:30:14 +08:00
|
|
|
}
|
|
|
|
else if (t_iseq(ptr, '\\'))
|
|
|
|
{
|
2006-06-07 00:25:55 +08:00
|
|
|
useasis = false;
|
|
|
|
state = TR_INSUBS;
|
|
|
|
beginwrd = ptr + pg_mblen(ptr);
|
2006-10-04 08:30:14 +08:00
|
|
|
}
|
|
|
|
else if (!t_isspace(ptr))
|
|
|
|
{
|
2006-06-07 00:25:55 +08:00
|
|
|
useasis = false;
|
2006-05-31 22:05:31 +08:00
|
|
|
beginwrd = ptr;
|
|
|
|
state = TR_INSUBS;
|
|
|
|
}
|
2006-10-04 08:30:14 +08:00
|
|
|
}
|
|
|
|
else if (state == TR_INSUBS)
|
|
|
|
{
|
|
|
|
if (t_isspace(ptr))
|
|
|
|
{
|
|
|
|
if (ptr == beginwrd)
|
2006-06-07 00:25:55 +08:00
|
|
|
elog(ERROR, "Thesaurus: Unexpected end of line or lexeme at %d line", lineno);
|
2006-10-04 08:30:14 +08:00
|
|
|
addWrd(d, beginwrd, ptr, idsubst, nwrd++, posinsubst, useasis);
|
2006-05-31 22:05:31 +08:00
|
|
|
state = TR_WAITSUBS;
|
|
|
|
}
|
2006-10-04 08:30:14 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
elog(ERROR, "Thesaurus: Unknown state: %d", state);
|
|
|
|
|
2006-05-31 22:05:31 +08:00
|
|
|
ptr += pg_mblen(ptr);
|
|
|
|
}
|
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
if (state == TR_INSUBS)
|
|
|
|
{
|
|
|
|
if (ptr == beginwrd)
|
2006-06-07 00:25:55 +08:00
|
|
|
elog(ERROR, "Thesaurus: Unexpected end of line or lexeme at %d line", lineno);
|
2006-10-04 08:30:14 +08:00
|
|
|
addWrd(d, beginwrd, ptr, idsubst, nwrd++, posinsubst, useasis);
|
2006-06-07 00:25:55 +08:00
|
|
|
}
|
2006-05-31 22:05:31 +08:00
|
|
|
|
|
|
|
idsubst++;
|
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
if (!(nwrd && posinsubst))
|
|
|
|
{
|
2006-05-31 22:05:31 +08:00
|
|
|
fclose(fh);
|
|
|
|
elog(ERROR, "Thesaurus: Unexpected end of line at %d line", lineno);
|
|
|
|
}
|
2006-10-04 08:30:14 +08:00
|
|
|
|
2006-05-31 22:05:31 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
d->nsubst = idsubst;
|
|
|
|
|
|
|
|
fclose(fh);
|
|
|
|
}
|
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
static TheLexeme *
|
|
|
|
addCompiledLexeme(TheLexeme * newwrds, int *nnw, int *tnm, TSLexeme * lexeme, LexemeInfo * src, uint16 tnvariant)
|
|
|
|
{
|
2006-05-31 22:05:31 +08:00
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
if (*nnw >= *tnm)
|
|
|
|
{
|
2006-05-31 22:05:31 +08:00
|
|
|
*tnm *= 2;
|
2006-10-04 08:30:14 +08:00
|
|
|
newwrds = (TheLexeme *) realloc(newwrds, sizeof(TheLexeme) * *tnm);
|
2006-05-31 22:05:31 +08:00
|
|
|
if (!newwrds)
|
2006-10-04 08:30:14 +08:00
|
|
|
elog(ERROR, "Out of memory");
|
2006-05-31 22:05:31 +08:00
|
|
|
}
|
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
newwrds[*nnw].entries = (LexemeInfo *) malloc(sizeof(LexemeInfo));
|
|
|
|
if (!newwrds[*nnw].entries)
|
|
|
|
elog(ERROR, "Out of memory");
|
2006-05-31 22:05:31 +08:00
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
if (lexeme && lexeme->lexeme)
|
|
|
|
{
|
|
|
|
newwrds[*nnw].lexeme = strdup(lexeme->lexeme);
|
|
|
|
if (!newwrds[*nnw].lexeme)
|
|
|
|
elog(ERROR, "Out of memory");
|
2006-05-31 22:05:31 +08:00
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
newwrds[*nnw].entries->tnvariant = tnvariant;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
newwrds[*nnw].lexeme = NULL;
|
|
|
|
newwrds[*nnw].entries->tnvariant = 1;
|
2006-05-31 22:05:31 +08:00
|
|
|
}
|
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
newwrds[*nnw].entries->idsubst = src->idsubst;
|
|
|
|
newwrds[*nnw].entries->posinsubst = src->posinsubst;
|
2006-05-31 22:05:31 +08:00
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
newwrds[*nnw].entries->nextentry = NULL;
|
2006-05-31 22:05:31 +08:00
|
|
|
|
|
|
|
(*nnw)++;
|
|
|
|
return newwrds;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2006-10-04 08:30:14 +08:00
|
|
|
cmpLexemeInfo(LexemeInfo * a, LexemeInfo * b)
|
|
|
|
{
|
|
|
|
if (a == NULL || b == NULL)
|
2006-05-31 22:05:31 +08:00
|
|
|
return 0;
|
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
if (a->idsubst == b->idsubst)
|
|
|
|
{
|
|
|
|
if (a->posinsubst == b->posinsubst)
|
|
|
|
{
|
|
|
|
if (a->tnvariant == b->tnvariant)
|
|
|
|
return 0;
|
2006-05-31 22:05:31 +08:00
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
return (a->tnvariant > b->tnvariant) ? 1 : -1;
|
2006-05-31 22:05:31 +08:00
|
|
|
}
|
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
return (a->posinsubst > b->posinsubst) ? 1 : -1;
|
2006-05-31 22:05:31 +08:00
|
|
|
}
|
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
return (a->idsubst > b->idsubst) ? 1 : -1;
|
2006-05-31 22:05:31 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2006-10-04 08:30:14 +08:00
|
|
|
cmpLexeme(TheLexeme * a, TheLexeme * b)
|
|
|
|
{
|
|
|
|
if (a->lexeme == NULL)
|
|
|
|
{
|
|
|
|
if (b->lexeme == NULL)
|
2006-05-31 22:05:31 +08:00
|
|
|
return 0;
|
|
|
|
else
|
|
|
|
return 1;
|
2006-10-04 08:30:14 +08:00
|
|
|
}
|
|
|
|
else if (b->lexeme == NULL)
|
2006-05-31 22:05:31 +08:00
|
|
|
return -1;
|
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
return strcmp(a->lexeme, b->lexeme);
|
2006-05-31 22:05:31 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2006-10-04 08:30:14 +08:00
|
|
|
cmpLexemeQ(const void *a, const void *b)
|
|
|
|
{
|
|
|
|
return cmpLexeme((TheLexeme *) a, (TheLexeme *) b);
|
2006-05-31 22:05:31 +08:00
|
|
|
}
|
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
static int
|
|
|
|
cmpTheLexeme(const void *a, const void *b)
|
|
|
|
{
|
|
|
|
TheLexeme *la = (TheLexeme *) a;
|
|
|
|
TheLexeme *lb = (TheLexeme *) b;
|
|
|
|
int res;
|
2006-05-31 22:05:31 +08:00
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
if ((res = cmpLexeme(la, lb)) != 0)
|
2006-05-31 22:05:31 +08:00
|
|
|
return res;
|
|
|
|
|
|
|
|
return -cmpLexemeInfo(la->entries, lb->entries);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2006-10-04 08:30:14 +08:00
|
|
|
compileTheLexeme(DictThesaurus * d)
|
|
|
|
{
|
|
|
|
int i,
|
|
|
|
nnw = 0,
|
|
|
|
tnm = 16;
|
|
|
|
TheLexeme *newwrds = (TheLexeme *) malloc(sizeof(TheLexeme) * tnm),
|
|
|
|
*ptrwrds;
|
|
|
|
|
|
|
|
if (!newwrds)
|
|
|
|
elog(ERROR, "Out of memory");
|
|
|
|
|
|
|
|
for (i = 0; i < d->nwrds; i++)
|
|
|
|
{
|
|
|
|
TSLexeme *ptr;
|
|
|
|
|
|
|
|
ptr = (TSLexeme *) DatumGetPointer(
|
|
|
|
FunctionCall4(
|
|
|
|
&(d->subdict.lexize_info),
|
|
|
|
PointerGetDatum(d->subdict.dictionary),
|
|
|
|
PointerGetDatum(d->wrds[i].lexeme),
|
|
|
|
Int32GetDatum(strlen(d->wrds[i].lexeme)),
|
|
|
|
PointerGetDatum(NULL)
|
|
|
|
)
|
2006-05-31 22:05:31 +08:00
|
|
|
);
|
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
if (!(ptr && ptr->lexeme))
|
|
|
|
{
|
|
|
|
if (!ptr)
|
|
|
|
elog(ERROR, "Thesaurus: word-sample '%s' isn't recognized by subdictionary (rule %d)",
|
|
|
|
d->wrds[i].lexeme, d->wrds[i].entries->idsubst + 1);
|
2006-06-02 23:35:42 +08:00
|
|
|
else
|
2006-10-04 08:30:14 +08:00
|
|
|
elog(NOTICE, "Thesaurus: word-sample '%s' is recognized as stop-word, assign any stop-word (rule %d)",
|
|
|
|
d->wrds[i].lexeme, d->wrds[i].entries->idsubst + 1);
|
2006-06-02 23:35:42 +08:00
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
newwrds = addCompiledLexeme(newwrds, &nnw, &tnm, NULL, d->wrds[i].entries, 0);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
while (ptr->lexeme)
|
|
|
|
{
|
|
|
|
TSLexeme *remptr = ptr + 1;
|
|
|
|
int tnvar = 1;
|
|
|
|
int curvar = ptr->nvariant;
|
2006-05-31 22:05:31 +08:00
|
|
|
|
|
|
|
/* compute n words in one variant */
|
2006-10-04 08:30:14 +08:00
|
|
|
while (remptr->lexeme)
|
|
|
|
{
|
|
|
|
if (remptr->nvariant != (remptr - 1)->nvariant)
|
2006-05-31 22:05:31 +08:00
|
|
|
break;
|
|
|
|
tnvar++;
|
|
|
|
remptr++;
|
|
|
|
}
|
|
|
|
|
|
|
|
remptr = ptr;
|
2006-10-04 08:30:14 +08:00
|
|
|
while (remptr->lexeme && remptr->nvariant == curvar)
|
|
|
|
{
|
|
|
|
newwrds = addCompiledLexeme(newwrds, &nnw, &tnm, remptr, d->wrds[i].entries, tnvar);
|
2006-05-31 22:05:31 +08:00
|
|
|
remptr++;
|
|
|
|
}
|
|
|
|
|
|
|
|
ptr = remptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
free(d->wrds[i].lexeme);
|
|
|
|
free(d->wrds[i].entries);
|
2006-05-31 22:05:31 +08:00
|
|
|
}
|
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
free(d->wrds);
|
2006-05-31 22:05:31 +08:00
|
|
|
d->wrds = newwrds;
|
|
|
|
d->nwrds = nnw;
|
|
|
|
d->ntwrds = tnm;
|
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
if (d->nwrds > 1)
|
|
|
|
{
|
|
|
|
qsort(d->wrds, d->nwrds, sizeof(TheLexeme), cmpTheLexeme);
|
2006-05-31 22:05:31 +08:00
|
|
|
|
|
|
|
/* uniq */
|
|
|
|
newwrds = d->wrds;
|
|
|
|
ptrwrds = d->wrds + 1;
|
2006-10-04 08:30:14 +08:00
|
|
|
while (ptrwrds - d->wrds < d->nwrds)
|
|
|
|
{
|
|
|
|
if (cmpLexeme(ptrwrds, newwrds) == 0)
|
|
|
|
{
|
|
|
|
if (cmpLexemeInfo(ptrwrds->entries, newwrds->entries))
|
|
|
|
{
|
2006-05-31 22:05:31 +08:00
|
|
|
ptrwrds->entries->nextentry = newwrds->entries;
|
|
|
|
newwrds->entries = ptrwrds->entries;
|
2006-10-04 08:30:14 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
free(ptrwrds->entries);
|
2006-05-31 22:05:31 +08:00
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
if (ptrwrds->lexeme)
|
|
|
|
free(ptrwrds->lexeme);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2006-05-31 22:05:31 +08:00
|
|
|
newwrds++;
|
|
|
|
*newwrds = *ptrwrds;
|
|
|
|
}
|
|
|
|
|
|
|
|
ptrwrds++;
|
|
|
|
}
|
|
|
|
|
|
|
|
d->nwrds = newwrds - d->wrds + 1;
|
2006-10-04 08:30:14 +08:00
|
|
|
d->wrds = (TheLexeme *) realloc(d->wrds, sizeof(TheLexeme) * d->nwrds);
|
2006-05-31 22:05:31 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2006-10-04 08:30:14 +08:00
|
|
|
compileTheSubstitute(DictThesaurus * d)
|
|
|
|
{
|
|
|
|
int i;
|
2006-05-31 22:05:31 +08:00
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
for (i = 0; i < d->nsubst; i++)
|
|
|
|
{
|
|
|
|
TSLexeme *rem = d->subst[i].res,
|
|
|
|
*outptr,
|
|
|
|
*inptr;
|
|
|
|
int n = 2;
|
|
|
|
|
|
|
|
outptr = d->subst[i].res = (TSLexeme *) malloc(sizeof(TSLexeme) * n);
|
|
|
|
if (d->subst[i].res == NULL)
|
|
|
|
elog(ERROR, "Out of Memory");
|
2006-05-31 22:05:31 +08:00
|
|
|
outptr->lexeme = NULL;
|
|
|
|
inptr = rem;
|
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
while (inptr && inptr->lexeme)
|
|
|
|
{
|
|
|
|
TSLexeme *lexized,
|
|
|
|
tmplex[2];
|
2006-06-07 00:25:55 +08:00
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
if (inptr->flags & DT_USEASIS)
|
|
|
|
{ /* do not lexize */
|
2006-06-07 00:25:55 +08:00
|
|
|
tmplex[0] = *inptr;
|
2006-10-04 08:30:14 +08:00
|
|
|
tmplex[0].flags = 0;
|
2006-06-07 00:25:55 +08:00
|
|
|
tmplex[1].lexeme = NULL;
|
2006-10-04 08:30:14 +08:00
|
|
|
lexized = tmplex;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
lexized = (TSLexeme *) DatumGetPointer(
|
|
|
|
FunctionCall4(
|
|
|
|
&(d->subdict.lexize_info),
|
|
|
|
PointerGetDatum(d->subdict.dictionary),
|
|
|
|
PointerGetDatum(inptr->lexeme),
|
|
|
|
Int32GetDatum(strlen(inptr->lexeme)),
|
|
|
|
PointerGetDatum(NULL)
|
|
|
|
)
|
|
|
|
);
|
2006-06-07 00:25:55 +08:00
|
|
|
}
|
2006-05-31 22:05:31 +08:00
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
if (lexized && lexized->lexeme)
|
|
|
|
{
|
|
|
|
int toset = (lexized->lexeme && outptr != d->subst[i].res) ? (outptr - d->subst[i].res) : -1;
|
|
|
|
|
|
|
|
while (lexized->lexeme)
|
|
|
|
{
|
|
|
|
if (outptr - d->subst[i].res + 1 >= n)
|
|
|
|
{
|
|
|
|
int diff = outptr - d->subst[i].res;
|
2006-05-31 22:05:31 +08:00
|
|
|
|
|
|
|
n *= 2;
|
2006-10-04 08:30:14 +08:00
|
|
|
d->subst[i].res = (TSLexeme *) realloc(d->subst[i].res, sizeof(TSLexeme) * n);
|
|
|
|
if (d->subst[i].res == NULL)
|
|
|
|
elog(ERROR, "Out of Memory");
|
2006-05-31 22:05:31 +08:00
|
|
|
outptr = d->subst[i].res + diff;
|
|
|
|
}
|
|
|
|
|
|
|
|
*outptr = *lexized;
|
2006-10-04 08:30:14 +08:00
|
|
|
if ((outptr->lexeme = strdup(lexized->lexeme)) == NULL)
|
|
|
|
elog(ERROR, "Out of Memory");
|
2006-05-31 22:05:31 +08:00
|
|
|
|
|
|
|
outptr++;
|
|
|
|
lexized++;
|
|
|
|
}
|
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
if (toset > 0)
|
2006-05-31 22:05:31 +08:00
|
|
|
d->subst[i].res[toset].flags |= TSL_ADDPOS;
|
2006-10-04 08:30:14 +08:00
|
|
|
}
|
|
|
|
else if (lexized)
|
|
|
|
{
|
|
|
|
elog(NOTICE, "Thesaurus: word '%s' in substition is a stop-word, ignored (rule %d)", inptr->lexeme, i + 1);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
elog(ERROR, "Thesaurus: word '%s' in substition isn't recognized (rule %d)", inptr->lexeme, i + 1);
|
2006-05-31 22:05:31 +08:00
|
|
|
}
|
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
if (inptr->lexeme)
|
|
|
|
free(inptr->lexeme);
|
2006-05-31 22:05:31 +08:00
|
|
|
inptr++;
|
|
|
|
}
|
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
if (outptr == d->subst[i].res)
|
|
|
|
elog(ERROR, "Thesaurus: all words in subsitution are stop word (rule %d)", i + 1);
|
2006-06-02 23:35:42 +08:00
|
|
|
|
2006-05-31 22:05:31 +08:00
|
|
|
d->subst[i].reslen = outptr - d->subst[i].res;
|
|
|
|
|
|
|
|
free(rem);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Datum
|
|
|
|
thesaurus_init(PG_FUNCTION_ARGS)
|
|
|
|
{
|
|
|
|
DictThesaurus *d;
|
|
|
|
Map *cfg,
|
|
|
|
*pcfg;
|
2006-10-04 08:30:14 +08:00
|
|
|
text *in,
|
|
|
|
*subdictname = NULL;
|
|
|
|
bool fileloaded = false;
|
2006-05-31 22:05:31 +08:00
|
|
|
|
|
|
|
if (PG_ARGISNULL(0) || PG_GETARG_POINTER(0) == NULL)
|
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_CONFIG_FILE_ERROR),
|
|
|
|
errmsg("Thesaurus confguration error")));
|
|
|
|
|
|
|
|
d = (DictThesaurus *) malloc(sizeof(DictThesaurus));
|
|
|
|
if (!d)
|
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_OUT_OF_MEMORY),
|
|
|
|
errmsg("out of memory")));
|
|
|
|
memset(d, 0, sizeof(DictThesaurus));
|
|
|
|
|
|
|
|
in = PG_GETARG_TEXT_P(0);
|
|
|
|
parse_cfgdict(in, &cfg);
|
|
|
|
PG_FREE_IF_COPY(in, 0);
|
|
|
|
pcfg = cfg;
|
|
|
|
while (pcfg->key)
|
|
|
|
{
|
|
|
|
if (pg_strcasecmp("DictFile", pcfg->key) == 0)
|
|
|
|
{
|
|
|
|
if (fileloaded)
|
|
|
|
{
|
|
|
|
freeDictThesaurus(d);
|
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
|
|
|
|
errmsg("Thesaurus file is already loaded")));
|
|
|
|
}
|
|
|
|
fileloaded = true;
|
2006-10-04 08:30:14 +08:00
|
|
|
thesaurusRead(pcfg->value, d);
|
2006-05-31 22:05:31 +08:00
|
|
|
}
|
|
|
|
else if (pg_strcasecmp("Dictionary", pcfg->key) == 0)
|
|
|
|
{
|
|
|
|
if (subdictname)
|
|
|
|
{
|
|
|
|
freeDictThesaurus(d);
|
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
|
2006-10-04 08:30:14 +08:00
|
|
|
errmsg("Thesaurus: SubDictionary is already defined")));
|
2006-05-31 22:05:31 +08:00
|
|
|
}
|
2006-10-04 08:30:14 +08:00
|
|
|
subdictname = char2text(pcfg->value);
|
2006-05-31 22:05:31 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
freeDictThesaurus(d);
|
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_SYNTAX_ERROR),
|
|
|
|
errmsg("unrecognized option: %s => %s",
|
|
|
|
pcfg->key, pcfg->value)));
|
|
|
|
}
|
|
|
|
pfree(pcfg->key);
|
|
|
|
pfree(pcfg->value);
|
|
|
|
pcfg++;
|
|
|
|
}
|
|
|
|
pfree(cfg);
|
|
|
|
|
|
|
|
if (!fileloaded)
|
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
|
|
|
|
errmsg("Thesaurus file isn't defined")));
|
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
if (subdictname)
|
|
|
|
{
|
|
|
|
DictInfo *subdictptr;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* we already in SPI, but name2id_dict()/finddict() invoke
|
|
|
|
* SPI_connect()
|
2006-05-31 22:05:31 +08:00
|
|
|
*/
|
2006-10-04 08:30:14 +08:00
|
|
|
SPI_push();
|
2006-05-31 22:05:31 +08:00
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
subdictptr = finddict(name2id_dict(subdictname));
|
2006-05-31 22:05:31 +08:00
|
|
|
|
|
|
|
SPI_pop();
|
|
|
|
|
|
|
|
d->subdict = *subdictptr;
|
2006-10-04 08:30:14 +08:00
|
|
|
}
|
|
|
|
else
|
2006-05-31 22:05:31 +08:00
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
|
|
|
|
errmsg("Thesaurus: SubDictionary isn't defined")));
|
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
compileTheLexeme(d);
|
2006-05-31 22:05:31 +08:00
|
|
|
compileTheSubstitute(d);
|
|
|
|
|
|
|
|
PG_RETURN_POINTER(d);
|
|
|
|
}
|
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
static LexemeInfo *
|
|
|
|
findTheLexeme(DictThesaurus * d, char *lexeme)
|
|
|
|
{
|
|
|
|
TheLexeme key = {lexeme, NULL}, *res;
|
2006-05-31 22:05:31 +08:00
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
if (d->nwrds == 0)
|
2006-05-31 22:05:31 +08:00
|
|
|
return NULL;
|
|
|
|
|
|
|
|
res = bsearch(&key, d->wrds, d->nwrds, sizeof(TheLexeme), cmpLexemeQ);
|
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
if (res == NULL)
|
2006-05-31 22:05:31 +08:00
|
|
|
return NULL;
|
|
|
|
return res->entries;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool
|
2006-10-04 08:30:14 +08:00
|
|
|
matchIdSubst(LexemeInfo * stored, uint16 idsubst)
|
|
|
|
{
|
|
|
|
bool res = true;
|
2006-05-31 22:05:31 +08:00
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
if (stored)
|
|
|
|
{
|
2006-05-31 22:05:31 +08:00
|
|
|
res = false;
|
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
for (; stored; stored = stored->nextvariant)
|
|
|
|
if (stored->idsubst == idsubst)
|
|
|
|
{
|
2006-05-31 22:05:31 +08:00
|
|
|
res = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
static LexemeInfo *
|
|
|
|
findVariant(LexemeInfo * in, LexemeInfo * stored, uint16 curpos, LexemeInfo ** newin, int newn)
|
|
|
|
{
|
|
|
|
for (;;)
|
|
|
|
{
|
|
|
|
int i;
|
2006-05-31 22:05:31 +08:00
|
|
|
LexemeInfo *ptr = newin[0];
|
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
for (i = 0; i < newn; i++)
|
|
|
|
{
|
|
|
|
while (newin[i] && newin[i]->idsubst < ptr->idsubst)
|
2006-05-31 22:05:31 +08:00
|
|
|
newin[i] = newin[i]->nextentry;
|
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
if (newin[i] == NULL)
|
2006-05-31 22:05:31 +08:00
|
|
|
return in;
|
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
if (newin[i]->idsubst > ptr->idsubst)
|
|
|
|
{
|
2006-05-31 22:05:31 +08:00
|
|
|
ptr = newin[i];
|
2006-10-04 08:30:14 +08:00
|
|
|
i = -1;
|
2006-05-31 22:05:31 +08:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
while (newin[i]->idsubst == ptr->idsubst)
|
|
|
|
{
|
|
|
|
if (newin[i]->posinsubst == curpos && newin[i]->tnvariant == newn)
|
|
|
|
{
|
2006-05-31 22:05:31 +08:00
|
|
|
ptr = newin[i];
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
newin[i] = newin[i]->nextentry;
|
2006-10-04 08:30:14 +08:00
|
|
|
if (newin[i] == NULL)
|
2006-05-31 22:05:31 +08:00
|
|
|
return in;
|
|
|
|
}
|
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
if (newin[i]->idsubst != ptr->idsubst)
|
|
|
|
{
|
2006-05-31 22:05:31 +08:00
|
|
|
ptr = newin[i];
|
2006-10-04 08:30:14 +08:00
|
|
|
i = -1;
|
2006-05-31 22:05:31 +08:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
if (i == newn && matchIdSubst(stored, ptr->idsubst) && (in == NULL || !matchIdSubst(in, ptr->idsubst)))
|
|
|
|
{ /* found */
|
2006-05-31 22:05:31 +08:00
|
|
|
|
|
|
|
ptr->nextvariant = in;
|
|
|
|
in = ptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* step forward */
|
2006-10-04 08:30:14 +08:00
|
|
|
for (i = 0; i < newn; i++)
|
2006-05-31 22:05:31 +08:00
|
|
|
newin[i] = newin[i]->nextentry;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
static TSLexeme *
|
|
|
|
copyTSLexeme(TheSubstitute * ts)
|
|
|
|
{
|
|
|
|
TSLexeme *res;
|
|
|
|
uint16 i;
|
2006-05-31 22:05:31 +08:00
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
res = (TSLexeme *) palloc(sizeof(TSLexeme) * (ts->reslen + 1));
|
|
|
|
for (i = 0; i < ts->reslen; i++)
|
|
|
|
{
|
2006-05-31 22:05:31 +08:00
|
|
|
res[i] = ts->res[i];
|
2006-10-04 08:30:14 +08:00
|
|
|
res[i].lexeme = pstrdup(ts->res[i].lexeme);
|
2006-05-31 22:05:31 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
res[ts->reslen].lexeme = NULL;
|
|
|
|
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
static TSLexeme *
|
|
|
|
checkMatch(DictThesaurus * d, LexemeInfo * info, uint16 curpos, bool *moreres)
|
|
|
|
{
|
2006-05-31 22:05:31 +08:00
|
|
|
*moreres = false;
|
2006-10-04 08:30:14 +08:00
|
|
|
while (info)
|
|
|
|
{
|
|
|
|
Assert(info->idsubst < d->nsubst);
|
|
|
|
if (info->nextvariant)
|
2006-05-31 22:05:31 +08:00
|
|
|
*moreres = true;
|
2006-10-04 08:30:14 +08:00
|
|
|
if (d->subst[info->idsubst].lastlexeme == curpos)
|
|
|
|
return copyTSLexeme(d->subst + info->idsubst);
|
2006-05-31 22:05:31 +08:00
|
|
|
info = info->nextvariant;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
Datum
|
|
|
|
thesaurus_lexize(PG_FUNCTION_ARGS)
|
|
|
|
{
|
|
|
|
DictThesaurus *d = (DictThesaurus *) PG_GETARG_POINTER(0);
|
2006-10-04 08:30:14 +08:00
|
|
|
DictSubState *dstate = (DictSubState *) PG_GETARG_POINTER(3);
|
|
|
|
TSLexeme *res = NULL;
|
|
|
|
LexemeInfo *stored,
|
|
|
|
*info = NULL;
|
|
|
|
uint16 curpos = 0;
|
|
|
|
bool moreres = false;
|
2006-05-31 22:05:31 +08:00
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
if (dstate == NULL || PG_NARGS() < 4)
|
|
|
|
elog(ERROR, "Forbidden call of thesaurus or nested call");
|
2006-05-31 22:05:31 +08:00
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
if (dstate->isend)
|
2006-05-31 22:05:31 +08:00
|
|
|
PG_RETURN_POINTER(NULL);
|
2006-10-04 08:30:14 +08:00
|
|
|
stored = (LexemeInfo *) dstate->private;
|
|
|
|
|
|
|
|
if (stored)
|
|
|
|
curpos = stored->posinsubst + 1;
|
|
|
|
|
|
|
|
res = (TSLexeme *) DatumGetPointer(
|
|
|
|
FunctionCall4(
|
|
|
|
&(d->subdict.lexize_info),
|
|
|
|
PointerGetDatum(d->subdict.dictionary),
|
|
|
|
PG_GETARG_DATUM(1),
|
|
|
|
PG_GETARG_INT32(2),
|
|
|
|
PointerGetDatum(NULL)
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
|
|
|
if (res && res->lexeme)
|
|
|
|
{
|
|
|
|
TSLexeme *ptr = res,
|
|
|
|
*basevar;
|
2006-05-31 22:05:31 +08:00
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
while (ptr->lexeme)
|
|
|
|
{
|
2006-05-31 22:05:31 +08:00
|
|
|
uint16 nv = ptr->nvariant;
|
2006-10-04 08:30:14 +08:00
|
|
|
uint16 i,
|
|
|
|
nlex = 0;
|
|
|
|
LexemeInfo **infos;
|
2006-05-31 22:05:31 +08:00
|
|
|
|
|
|
|
basevar = ptr;
|
2006-10-04 08:30:14 +08:00
|
|
|
while (ptr->lexeme && nv == ptr->nvariant)
|
|
|
|
{
|
2006-05-31 22:05:31 +08:00
|
|
|
nlex++;
|
|
|
|
ptr++;
|
|
|
|
}
|
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
infos = (LexemeInfo **) palloc(sizeof(LexemeInfo *) * nlex);
|
|
|
|
for (i = 0; i < nlex; i++)
|
|
|
|
if ((infos[i] = findTheLexeme(d, basevar[i].lexeme)) == NULL)
|
2006-05-31 22:05:31 +08:00
|
|
|
break;
|
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
if (i < nlex)
|
|
|
|
{
|
2006-05-31 22:05:31 +08:00
|
|
|
/* no chance to find */
|
2006-10-04 08:30:14 +08:00
|
|
|
pfree(infos);
|
2006-05-31 22:05:31 +08:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
info = findVariant(info, stored, curpos, infos, nlex);
|
2006-05-31 22:05:31 +08:00
|
|
|
}
|
|
|
|
}
|
2006-10-04 08:30:14 +08:00
|
|
|
else if (res)
|
|
|
|
{ /* stop-word */
|
|
|
|
LexemeInfo *infos = findTheLexeme(d, NULL);
|
2006-05-31 22:05:31 +08:00
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
info = findVariant(NULL, stored, curpos, &infos, 1);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
info = NULL; /* word isn't recognized */
|
|
|
|
}
|
|
|
|
|
|
|
|
dstate->private = (void *) info;
|
2006-05-31 22:05:31 +08:00
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
if (!info)
|
|
|
|
{
|
2006-05-31 22:05:31 +08:00
|
|
|
dstate->getnext = false;
|
|
|
|
PG_RETURN_POINTER(NULL);
|
|
|
|
}
|
2006-10-04 08:30:14 +08:00
|
|
|
|
|
|
|
if ((res = checkMatch(d, info, curpos, &moreres)) != NULL)
|
|
|
|
{
|
2006-05-31 22:05:31 +08:00
|
|
|
dstate->getnext = moreres;
|
|
|
|
PG_RETURN_POINTER(res);
|
|
|
|
}
|
|
|
|
|
|
|
|
dstate->getnext = true;
|
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
PG_RETURN_POINTER(NULL);
|
2006-05-31 22:05:31 +08:00
|
|
|
}
|