dict_simple.c 2.34 KB
Newer Older
1 2 3 4 5 6 7 8 9
/*-------------------------------------------------------------------------
 *
 * dict_simple.c
 *		Simple dictionary: just lowercase and check for stopword
 *
 * Portions Copyright (c) 1996-2007, PostgreSQL Global Development Group
 *
 *
 * IDENTIFICATION
10
 *	  $PostgreSQL: pgsql/src/backend/tsearch/dict_simple.c,v 1.4 2007/11/14 18:36:37 tgl Exp $
11 12 13 14 15
 *
 *-------------------------------------------------------------------------
 */
#include "postgres.h"

16
#include "commands/defrem.h"
17 18 19 20 21 22 23 24 25
#include "tsearch/ts_locale.h"
#include "tsearch/ts_public.h"
#include "tsearch/ts_utils.h"
#include "utils/builtins.h"


typedef struct
{
	StopList	stoplist;
26
	bool		accept;
27
} DictSimple;
28 29 30 31 32


Datum
dsimple_init(PG_FUNCTION_ARGS)
{
33
	List	   *dictoptions = (List *) PG_GETARG_POINTER(0);
34
	DictSimple *d = (DictSimple *) palloc0(sizeof(DictSimple));
35 36
	bool		stoploaded = false,
				acceptloaded = false;
37
	ListCell   *l;
38

39 40
	d->accept = true;			/* default */

41
	foreach(l, dictoptions)
42
	{
43
		DefElem    *defel = (DefElem *) lfirst(l);
44

45 46 47 48 49 50
		if (pg_strcasecmp("StopWords", defel->defname) == 0)
		{
			if (stoploaded)
				ereport(ERROR,
						(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
						 errmsg("multiple StopWords parameters")));
51
			readstoplist(defGetString(defel), &d->stoplist, lowerstr);
52 53
			stoploaded = true;
		}
54 55 56 57 58 59 60 61 62
		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;
		}
63 64 65 66 67 68 69
		else
		{
			ereport(ERROR,
					(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
					 errmsg("unrecognized simple dictionary parameter: \"%s\"",
							defel->defname)));
		}
70 71 72 73 74 75 76 77
	}

	PG_RETURN_POINTER(d);
}

Datum
dsimple_lexize(PG_FUNCTION_ARGS)
{
78
	DictSimple *d = (DictSimple *) PG_GETARG_POINTER(0);
79 80
	char	   *in = (char *) PG_GETARG_POINTER(1);
	int32	   len = PG_GETARG_INT32(2);
81
	char	   *txt;
82
	TSLexeme   *res;
83

84 85
	txt = lowerstr_with_len(in, len);

86
	if (*txt == '\0' || searchstoplist(&(d->stoplist), txt))
87 88
	{
		/* reject as stopword */
89
		pfree(txt);
90 91 92 93 94 95 96
		res = palloc0(sizeof(TSLexeme) * 2);
		PG_RETURN_POINTER(res);
	}
	else if (d->accept)
	{
		/* accept */
		res = palloc0(sizeof(TSLexeme) * 2);
97
		res[0].lexeme = txt;
98 99 100 101 102 103 104 105
		PG_RETURN_POINTER(res);
	}
	else
	{
		/* report as unrecognized */
		pfree(txt);
		PG_RETURN_POINTER(NULL);
	}
106
}