postgresql/contrib/tsearch2/dict_ispell.c

197 lines
3.7 KiB
C
Raw Normal View History

2003-08-04 08:43:34 +08:00
/*
2003-07-21 18:27:44 +08:00
* ISpell interface
* Teodor Sigaev <teodor@sigaev.ru>
*/
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include "postgres.h"
#include "dict.h"
#include "common.h"
#include "ispell/spell.h"
2003-08-04 08:43:34 +08:00
typedef struct
{
2003-07-21 18:27:44 +08:00
StopList stoplist;
IspellDict obj;
2003-08-04 08:43:34 +08:00
} DictISpell;
2003-07-21 18:27:44 +08:00
PG_FUNCTION_INFO_V1(spell_init);
2003-08-04 08:43:34 +08:00
Datum spell_init(PG_FUNCTION_ARGS);
2003-07-21 18:27:44 +08:00
PG_FUNCTION_INFO_V1(spell_lexize);
2003-08-04 08:43:34 +08:00
Datum spell_lexize(PG_FUNCTION_ARGS);
2003-07-21 18:27:44 +08:00
static void
2003-08-04 08:43:34 +08:00
freeDictISpell(DictISpell * d)
{
NIFree(&(d->obj));
2003-07-21 18:27:44 +08:00
freestoplist(&(d->stoplist));
free(d);
}
2003-08-04 08:43:34 +08:00
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")));
2003-08-04 08:43:34 +08:00
d = (DictISpell *) malloc(sizeof(DictISpell));
if (!d)
ereport(ERROR,
(errcode(ERRCODE_OUT_OF_MEMORY),
errmsg("out of memory")));
2003-08-04 08:43:34 +08:00
memset(d, 0, sizeof(DictISpell));
d->stoplist.wordop = lowerstr;
2003-07-21 18:27:44 +08:00
in = PG_GETARG_TEXT_P(0);
2003-08-04 08:43:34 +08:00
parse_cfgdict(in, &cfg);
2003-07-21 18:27:44 +08:00
PG_FREE_IF_COPY(in, 0);
2003-08-04 08:43:34 +08:00
pcfg = cfg;
while (pcfg->key)
{
if (pg_strcasecmp("DictFile", pcfg->key) == 0)
2003-08-04 08:43:34 +08:00
{
if (dictloaded)
{
2003-07-21 18:27:44 +08:00
freeDictISpell(d);
ereport(ERROR,
2003-08-04 08:43:34 +08:00
(errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
errmsg("dictionary already loaded")));
2003-07-21 18:27:44 +08:00
}
if (NIImportDictionary(&(d->obj), pcfg->value))
2003-08-04 08:43:34 +08:00
{
2003-07-21 18:27:44 +08:00
freeDictISpell(d);
ereport(ERROR,
(errcode(ERRCODE_CONFIG_FILE_ERROR),
errmsg("could not load dictionary file \"%s\"",
pcfg->value)));
2003-07-21 18:27:44 +08:00
}
2003-08-04 08:43:34 +08:00
dictloaded = true;
}
else if (pg_strcasecmp("AffFile", pcfg->key) == 0)
2003-08-04 08:43:34 +08:00
{
if (affloaded)
{
2003-07-21 18:27:44 +08:00
freeDictISpell(d);
ereport(ERROR,
2003-08-04 08:43:34 +08:00
(errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
errmsg("affixes already loaded")));
2003-07-21 18:27:44 +08:00
}
if (NIImportAffixes(&(d->obj), pcfg->value))
2003-08-04 08:43:34 +08:00
{
2003-07-21 18:27:44 +08:00
freeDictISpell(d);
ereport(ERROR,
(errcode(ERRCODE_CONFIG_FILE_ERROR),
errmsg("could not load affix file \"%s\"",
pcfg->value)));
2003-07-21 18:27:44 +08:00
}
2003-08-04 08:43:34 +08:00
affloaded = true;
}
else if (pg_strcasecmp("StopFile", pcfg->key) == 0)
2003-08-04 08:43:34 +08:00
{
text *tmp = char2text(pcfg->value);
if (stoploaded)
{
2003-07-21 18:27:44 +08:00
freeDictISpell(d);
ereport(ERROR,
2003-08-04 08:43:34 +08:00
(errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
errmsg("stop words already loaded")));
2003-07-21 18:27:44 +08:00
}
readstoplist(tmp, &(d->stoplist));
sortstoplist(&(d->stoplist));
pfree(tmp);
2003-08-04 08:43:34 +08:00
stoploaded = true;
}
else
{
2003-07-21 18:27:44 +08:00
freeDictISpell(d);
ereport(ERROR,
(errcode(ERRCODE_SYNTAX_ERROR),
errmsg("unrecognized option: %s => %s",
2003-08-04 08:43:34 +08:00
pcfg->key, pcfg->value)));
2003-07-21 18:27:44 +08:00
}
pfree(pcfg->key);
pfree(pcfg->value);
pcfg++;
}
pfree(cfg);
2003-08-04 08:43:34 +08:00
if (affloaded && dictloaded)
{
NISortDictionary(&(d->obj));
NISortAffixes(&(d->obj));
2003-08-04 08:43:34 +08:00
}
else if (!affloaded)
{
2003-07-21 18:27:44 +08:00
freeDictISpell(d);
ereport(ERROR,
(errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
errmsg("no affixes")));
2003-08-04 08:43:34 +08:00
}
else
{
2003-07-21 18:27:44 +08:00
freeDictISpell(d);
ereport(ERROR,
(errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
errmsg("no dictionary")));
2003-07-21 18:27:44 +08:00
}
PG_RETURN_POINTER(d);
}
Datum
2003-08-04 08:43:34 +08:00
spell_lexize(PG_FUNCTION_ARGS)
{
DictISpell *d = (DictISpell *) PG_GETARG_POINTER(0);
char *in = (char *) PG_GETARG_POINTER(1);
char *txt;
char **res;
char **ptr,
**cptr;
if (!PG_GETARG_INT32(2))
2003-07-21 18:27:44 +08:00
PG_RETURN_POINTER(NULL);
2003-08-04 08:43:34 +08:00
res = palloc(sizeof(char *) * 2);
2003-07-21 18:27:44 +08:00
txt = pnstrdup(in, PG_GETARG_INT32(2));
res = NINormalizeWord(&(d->obj), txt);
2003-07-21 18:27:44 +08:00
pfree(txt);
2003-08-04 08:43:34 +08:00
if (res == NULL)
2003-07-21 18:27:44 +08:00
PG_RETURN_POINTER(NULL);
2003-08-04 08:43:34 +08:00
ptr = cptr = res;
while (*ptr)
{
if (searchstoplist(&(d->stoplist), *ptr))
{
2003-07-21 18:27:44 +08:00
pfree(*ptr);
2003-08-04 08:43:34 +08:00
*ptr = NULL;
ptr++;
}
else
{
*cptr = *ptr;
cptr++;
2003-07-21 18:27:44 +08:00
ptr++;
}
}
2003-08-04 08:43:34 +08:00
*cptr = NULL;
2003-07-21 18:27:44 +08:00
PG_RETURN_POINTER(res);
}