2003-08-04 08:43:34 +08:00
|
|
|
/*
|
2003-07-21 18:27:44 +08:00
|
|
|
* ISpell interface
|
|
|
|
* Teodor Sigaev <teodor@sigaev.ru>
|
|
|
|
*/
|
|
|
|
#include "postgres.h"
|
|
|
|
|
2005-05-07 01:24:55 +08:00
|
|
|
#include <ctype.h>
|
|
|
|
|
2003-07-21 18:27:44 +08:00
|
|
|
#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)
|
|
|
|
{
|
2003-11-18 01:34:35 +08:00
|
|
|
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)
|
2003-07-25 01:52:50 +08:00
|
|
|
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)
|
2003-07-25 01:52:50 +08:00
|
|
|
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)
|
|
|
|
{
|
2004-05-07 08:24:59 +08:00
|
|
|
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);
|
2003-07-25 01:52:50 +08:00
|
|
|
ereport(ERROR,
|
2005-10-15 10:49:52 +08:00
|
|
|
(errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
|
|
|
|
errmsg("dictionary already loaded")));
|
2003-07-21 18:27:44 +08:00
|
|
|
}
|
2003-11-18 01:34:35 +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);
|
2003-07-25 01:52:50 +08:00
|
|
|
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;
|
|
|
|
}
|
2004-05-07 08:24:59 +08:00
|
|
|
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);
|
2003-07-25 01:52:50 +08:00
|
|
|
ereport(ERROR,
|
2005-10-15 10:49:52 +08:00
|
|
|
(errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
|
|
|
|
errmsg("affixes already loaded")));
|
2003-07-21 18:27:44 +08:00
|
|
|
}
|
2003-11-18 01:34:35 +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);
|
2003-07-25 01:52:50 +08:00
|
|
|
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;
|
|
|
|
}
|
2004-05-07 08:24:59 +08:00
|
|
|
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);
|
2003-07-25 01:52:50 +08:00
|
|
|
ereport(ERROR,
|
2005-10-15 10:49:52 +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);
|
2003-07-25 01:52:50 +08:00
|
|
|
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)
|
|
|
|
{
|
2003-11-18 01:34:35 +08:00
|
|
|
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);
|
2003-07-25 01:52:50 +08:00
|
|
|
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);
|
2003-07-25 01:52:50 +08:00
|
|
|
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;
|
2005-10-15 10:49:52 +08:00
|
|
|
TSLexeme *res;
|
|
|
|
TSLexeme *ptr,
|
|
|
|
*cptr;
|
2003-08-04 08:43:34 +08:00
|
|
|
|
|
|
|
if (!PG_GETARG_INT32(2))
|
2003-07-21 18:27:44 +08:00
|
|
|
PG_RETURN_POINTER(NULL);
|
|
|
|
|
|
|
|
txt = pnstrdup(in, PG_GETARG_INT32(2));
|
2003-11-18 01:34:35 +08:00
|
|
|
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;
|
2005-01-25 23:24:38 +08:00
|
|
|
while (ptr->lexeme)
|
2003-08-04 08:43:34 +08:00
|
|
|
{
|
2005-01-25 23:24:38 +08:00
|
|
|
if (searchstoplist(&(d->stoplist), ptr->lexeme))
|
2003-08-04 08:43:34 +08:00
|
|
|
{
|
2005-01-25 23:24:38 +08:00
|
|
|
pfree(ptr->lexeme);
|
|
|
|
ptr->lexeme = NULL;
|
2003-08-04 08:43:34 +08:00
|
|
|
ptr++;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2005-01-25 23:24:38 +08:00
|
|
|
memcpy(cptr, ptr, sizeof(TSLexeme));
|
2003-08-04 08:43:34 +08:00
|
|
|
cptr++;
|
2003-07-21 18:27:44 +08:00
|
|
|
ptr++;
|
|
|
|
}
|
|
|
|
}
|
2005-01-25 23:24:38 +08:00
|
|
|
cptr->lexeme = NULL;
|
2003-07-21 18:27:44 +08:00
|
|
|
|
|
|
|
PG_RETURN_POINTER(res);
|
|
|
|
}
|