2007-08-21 03:11:32 +02:00
|
|
|
/*-------------------------------------------------------------------------
|
|
|
|
*
|
|
|
|
* dict_simple.c
|
|
|
|
* Simple dictionary: just lowercase and check for stopword
|
|
|
|
*
|
2010-01-02 17:58:17 +01:00
|
|
|
* Portions Copyright (c) 1996-2010, PostgreSQL Global Development Group
|
2007-08-21 03:11:32 +02:00
|
|
|
*
|
|
|
|
*
|
|
|
|
* IDENTIFICATION
|
2010-01-02 17:58:17 +01:00
|
|
|
* $PostgreSQL: pgsql/src/backend/tsearch/dict_simple.c,v 1.9 2010/01/02 16:57:53 momjian Exp $
|
2007-08-21 03:11:32 +02:00
|
|
|
*
|
|
|
|
*-------------------------------------------------------------------------
|
|
|
|
*/
|
|
|
|
#include "postgres.h"
|
|
|
|
|
2007-08-22 03:39:46 +02:00
|
|
|
#include "commands/defrem.h"
|
2007-08-21 03:11:32 +02:00
|
|
|
#include "tsearch/ts_locale.h"
|
|
|
|
#include "tsearch/ts_public.h"
|
|
|
|
#include "tsearch/ts_utils.h"
|
|
|
|
#include "utils/builtins.h"
|
|
|
|
|
|
|
|
|
|
|
|
typedef struct
|
|
|
|
{
|
|
|
|
StopList stoplist;
|
2007-11-14 19:36:37 +01:00
|
|
|
bool accept;
|
2007-11-15 23:25:18 +01:00
|
|
|
} DictSimple;
|
2007-08-21 03:11:32 +02:00
|
|
|
|
|
|
|
|
|
|
|
Datum
|
|
|
|
dsimple_init(PG_FUNCTION_ARGS)
|
|
|
|
{
|
2007-08-22 03:39:46 +02:00
|
|
|
List *dictoptions = (List *) PG_GETARG_POINTER(0);
|
2007-08-25 02:03:59 +02:00
|
|
|
DictSimple *d = (DictSimple *) palloc0(sizeof(DictSimple));
|
2007-11-14 19:36:37 +01:00
|
|
|
bool stoploaded = false,
|
|
|
|
acceptloaded = false;
|
2007-08-22 03:39:46 +02:00
|
|
|
ListCell *l;
|
2007-08-21 03:11:32 +02:00
|
|
|
|
2007-11-14 19:36:37 +01:00
|
|
|
d->accept = true; /* default */
|
|
|
|
|
2007-08-22 03:39:46 +02:00
|
|
|
foreach(l, dictoptions)
|
2007-08-21 03:11:32 +02:00
|
|
|
{
|
2007-08-22 03:39:46 +02:00
|
|
|
DefElem *defel = (DefElem *) lfirst(l);
|
2007-08-21 03:11:32 +02:00
|
|
|
|
2007-08-22 03:39:46 +02:00
|
|
|
if (pg_strcasecmp("StopWords", defel->defname) == 0)
|
|
|
|
{
|
|
|
|
if (stoploaded)
|
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
|
|
|
|
errmsg("multiple StopWords parameters")));
|
2007-08-25 02:03:59 +02:00
|
|
|
readstoplist(defGetString(defel), &d->stoplist, lowerstr);
|
2007-08-22 03:39:46 +02:00
|
|
|
stoploaded = true;
|
|
|
|
}
|
2007-11-14 19:36:37 +01:00
|
|
|
else if (pg_strcasecmp("Accept", defel->defname) == 0)
|
|
|
|
{
|
|
|
|
if (acceptloaded)
|
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
|
|
|
|
errmsg("multiple Accept parameters")));
|
|
|
|
d->accept = defGetBoolean(defel);
|
|
|
|
acceptloaded = true;
|
|
|
|
}
|
2007-08-22 03:39:46 +02:00
|
|
|
else
|
|
|
|
{
|
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
|
2007-11-15 22:14:46 +01:00
|
|
|
errmsg("unrecognized simple dictionary parameter: \"%s\"",
|
|
|
|
defel->defname)));
|
2007-08-22 03:39:46 +02:00
|
|
|
}
|
2007-08-21 03:11:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
PG_RETURN_POINTER(d);
|
|
|
|
}
|
|
|
|
|
|
|
|
Datum
|
|
|
|
dsimple_lexize(PG_FUNCTION_ARGS)
|
|
|
|
{
|
2007-08-25 02:03:59 +02:00
|
|
|
DictSimple *d = (DictSimple *) PG_GETARG_POINTER(0);
|
2007-08-21 03:11:32 +02:00
|
|
|
char *in = (char *) PG_GETARG_POINTER(1);
|
2007-11-15 22:14:46 +01:00
|
|
|
int32 len = PG_GETARG_INT32(2);
|
2007-08-25 02:03:59 +02:00
|
|
|
char *txt;
|
2007-11-14 19:36:37 +01:00
|
|
|
TSLexeme *res;
|
2007-08-21 03:11:32 +02:00
|
|
|
|
2007-08-25 02:03:59 +02:00
|
|
|
txt = lowerstr_with_len(in, len);
|
|
|
|
|
2007-08-21 03:11:32 +02:00
|
|
|
if (*txt == '\0' || searchstoplist(&(d->stoplist), txt))
|
2007-11-14 19:36:37 +01:00
|
|
|
{
|
|
|
|
/* reject as stopword */
|
2007-08-21 03:11:32 +02:00
|
|
|
pfree(txt);
|
2007-11-14 19:36:37 +01:00
|
|
|
res = palloc0(sizeof(TSLexeme) * 2);
|
|
|
|
PG_RETURN_POINTER(res);
|
|
|
|
}
|
|
|
|
else if (d->accept)
|
|
|
|
{
|
|
|
|
/* accept */
|
|
|
|
res = palloc0(sizeof(TSLexeme) * 2);
|
2007-08-21 03:11:32 +02:00
|
|
|
res[0].lexeme = txt;
|
2007-11-14 19:36:37 +01:00
|
|
|
PG_RETURN_POINTER(res);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* report as unrecognized */
|
|
|
|
pfree(txt);
|
|
|
|
PG_RETURN_POINTER(NULL);
|
|
|
|
}
|
2007-08-21 03:11:32 +02:00
|
|
|
}
|