You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
postgres/contrib/tsearch2/dict_ispell.c

197 lines
3.8 KiB

/* $PostgreSQL: pgsql/contrib/tsearch2/dict_ispell.c,v 1.10 2006/03/11 04:38:30 momjian Exp $ */
23 years ago
/*
23 years ago
* ISpell interface
* Teodor Sigaev <teodor@sigaev.ru>
*/
#include "postgres.h"
#include <ctype.h>
23 years ago
#include "dict.h"
#include "common.h"
#include "ispell/spell.h"
#include "ts_locale.h"
23 years ago
23 years ago
typedef struct
{
23 years ago
StopList stoplist;
IspellDict obj;
23 years ago
} DictISpell;
23 years ago
PG_FUNCTION_INFO_V1(spell_init);
23 years ago
Datum spell_init(PG_FUNCTION_ARGS);
23 years ago
PG_FUNCTION_INFO_V1(spell_lexize);
23 years ago
Datum spell_lexize(PG_FUNCTION_ARGS);
23 years ago
static void
23 years ago
freeDictISpell(DictISpell * d)
{
NIFree(&(d->obj));
23 years ago
freestoplist(&(d->stoplist));
free(d);
}
23 years ago
Datum
spell_init(PG_FUNCTION_ARGS)
{
DictISpell *d;
Map *cfg,
*pcfg;
text *in;
bool affloaded = false,
dictloaded = false,
stoploaded = false;
if (PG_ARGISNULL(0) || PG_GETARG_POINTER(0) == NULL)
ereport(ERROR,
(errcode(ERRCODE_CONFIG_FILE_ERROR),
errmsg("ISpell confguration error")));
23 years ago
d = (DictISpell *) malloc(sizeof(DictISpell));
if (!d)
ereport(ERROR,
(errcode(ERRCODE_OUT_OF_MEMORY),
errmsg("out of memory")));
23 years ago
memset(d, 0, sizeof(DictISpell));
d->stoplist.wordop = lowerstr;
23 years ago
in = PG_GETARG_TEXT_P(0);
23 years ago
parse_cfgdict(in, &cfg);
23 years ago
PG_FREE_IF_COPY(in, 0);
23 years ago
pcfg = cfg;
while (pcfg->key)
{
if (pg_strcasecmp("DictFile", pcfg->key) == 0)
23 years ago
{
if (dictloaded)
{
23 years ago
freeDictISpell(d);
ereport(ERROR,
(errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
errmsg("dictionary already loaded")));
23 years ago
}
if (NIImportDictionary(&(d->obj), pcfg->value))
23 years ago
{
23 years ago
freeDictISpell(d);
ereport(ERROR,
(errcode(ERRCODE_CONFIG_FILE_ERROR),
errmsg("could not load dictionary file \"%s\"",
pcfg->value)));
23 years ago
}
23 years ago
dictloaded = true;
}
else if (pg_strcasecmp("AffFile", pcfg->key) == 0)
23 years ago
{
if (affloaded)
{
23 years ago
freeDictISpell(d);
ereport(ERROR,
(errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
errmsg("affixes already loaded")));
23 years ago
}
if (NIImportAffixes(&(d->obj), pcfg->value))
23 years ago
{
23 years ago
freeDictISpell(d);
ereport(ERROR,
(errcode(ERRCODE_CONFIG_FILE_ERROR),
errmsg("could not load affix file \"%s\"",
pcfg->value)));
23 years ago
}
23 years ago
affloaded = true;
}
else if (pg_strcasecmp("StopFile", pcfg->key) == 0)
23 years ago
{
text *tmp = char2text(pcfg->value);
if (stoploaded)
{
23 years ago
freeDictISpell(d);
ereport(ERROR,
(errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
errmsg("stop words already loaded")));
23 years ago
}
readstoplist(tmp, &(d->stoplist));
sortstoplist(&(d->stoplist));
pfree(tmp);
23 years ago
stoploaded = true;
}
else
{
23 years ago
freeDictISpell(d);
ereport(ERROR,
(errcode(ERRCODE_SYNTAX_ERROR),
errmsg("unrecognized option: %s => %s",
23 years ago
pcfg->key, pcfg->value)));
23 years ago
}
pfree(pcfg->key);
pfree(pcfg->value);
pcfg++;
}
pfree(cfg);
23 years ago
if (affloaded && dictloaded)
{
NISortDictionary(&(d->obj));
NISortAffixes(&(d->obj));
23 years ago
}
else if (!affloaded)
{
23 years ago
freeDictISpell(d);
ereport(ERROR,
(errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
errmsg("no affixes")));
23 years ago
}
else
{
23 years ago
freeDictISpell(d);
ereport(ERROR,
(errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
errmsg("no dictionary")));
23 years ago
}
PG_RETURN_POINTER(d);
}
Datum
23 years ago
spell_lexize(PG_FUNCTION_ARGS)
{
DictISpell *d = (DictISpell *) PG_GETARG_POINTER(0);
char *in = (char *) PG_GETARG_POINTER(1);
char *txt;
TSLexeme *res;
TSLexeme *ptr,
*cptr;
23 years ago
if (!PG_GETARG_INT32(2))
23 years ago
PG_RETURN_POINTER(NULL);
txt = pnstrdup(in, PG_GETARG_INT32(2));
res = NINormalizeWord(&(d->obj), txt);
23 years ago
pfree(txt);
23 years ago
if (res == NULL)
23 years ago
PG_RETURN_POINTER(NULL);
23 years ago
ptr = cptr = res;
while (ptr->lexeme)
23 years ago
{
if (searchstoplist(&(d->stoplist), ptr->lexeme))
23 years ago
{
pfree(ptr->lexeme);
ptr->lexeme = NULL;
23 years ago
ptr++;
}
else
{
memcpy(cptr, ptr, sizeof(TSLexeme));
23 years ago
cptr++;
23 years ago
ptr++;
}
}
cptr->lexeme = NULL;
23 years ago
PG_RETURN_POINTER(res);
}