| 1 |
/*------------------------------------------------------------------------- |
| 2 |
* |
| 3 |
* dict_ispell.c |
| 4 |
* Ispell dictionary interface |
| 5 |
* |
| 6 |
* Portions Copyright (c) 1996-2025, PostgreSQL Global Development Group |
| 7 |
* |
| 8 |
* |
| 9 |
* IDENTIFICATION |
| 10 |
* src/backend/tsearch/dict_ispell.c |
| 11 |
* |
| 12 |
*------------------------------------------------------------------------- |
| 13 |
*/ |
| 14 |
#include "postgres.h" |
| 15 |
|
| 16 |
#include "catalog/pg_collation_d.h" |
| 17 |
#include "commands/defrem.h" |
| 18 |
#include "tsearch/dicts/spell.h" |
| 19 |
#include "tsearch/ts_public.h" |
| 20 |
#include "utils/fmgrprotos.h" |
| 21 |
#include "utils/formatting.h" |
| 22 |
|
| 23 |
|
| 24 |
typedef struct |
| 25 |
{ |
| 26 |
StopList stoplist; |
| 27 |
IspellDict obj; |
| 28 |
} DictISpell; |
| 29 |
|
| 30 |
Datum |
| 31 |
dispell_init(PG_FUNCTION_ARGS) |
| 32 |
{ |
| 33 |
List *dictoptions = (List *) PG_GETARG_POINTER(0); |
| 34 |
DictISpell *d; |
| 35 |
bool affloaded = false, |
| 36 |
dictloaded = false, |
| 37 |
stoploaded = false; |
| 38 |
ListCell *l; |
| 39 |
|
| 40 |
d = (DictISpell *) palloc0(sizeof(DictISpell)); |
| 41 |
|
| 42 |
NIStartBuild(&(d->obj)); |
| 43 |
|
| 44 |
foreach(l, dictoptions) |
| 45 |
{ |
| 46 |
DefElem *defel = (DefElem *) lfirst(l); |
| 47 |
|
| 48 |
if (strcmp(defel->defname, "dictfile") == 0) |
| 49 |
{ |
| 50 |
if (dictloaded) |
| 51 |
ereport(ERROR, |
| 52 |
(errcode(ERRCODE_INVALID_PARAMETER_VALUE), |
| 53 |
errmsg("multiple DictFile parameters"))); |
| 54 |
NIImportDictionary(&(d->obj), |
| 55 |
get_tsearch_config_filename(defGetString(defel), |
| 56 |
"dict")); |
| 57 |
dictloaded = true; |
| 58 |
} |
| 59 |
else if (strcmp(defel->defname, "afffile") == 0) |
| 60 |
{ |
| 61 |
if (affloaded) |
| 62 |
ereport(ERROR, |
| 63 |
(errcode(ERRCODE_INVALID_PARAMETER_VALUE), |
| 64 |
errmsg("multiple AffFile parameters"))); |
| 65 |
NIImportAffixes(&(d->obj), |
| 66 |
get_tsearch_config_filename(defGetString(defel), |
| 67 |
"affix")); |
| 68 |
affloaded = true; |
| 69 |
} |
| 70 |
else if (strcmp(defel->defname, "stopwords") == 0) |
| 71 |
{ |
| 72 |
if (stoploaded) |
| 73 |
ereport(ERROR, |
| 74 |
(errcode(ERRCODE_INVALID_PARAMETER_VALUE), |
| 75 |
errmsg("multiple StopWords parameters"))); |
| 76 |
readstoplist(defGetString(defel), &(d->stoplist), str_tolower); |
| 77 |
stoploaded = true; |
| 78 |
} |
| 79 |
else |
| 80 |
{ |
| 81 |
ereport(ERROR, |
| 82 |
(errcode(ERRCODE_INVALID_PARAMETER_VALUE), |
| 83 |
errmsg("unrecognized Ispell parameter: \"%s\"", |
| 84 |
defel->defname))); |
| 85 |
} |
| 86 |
} |
| 87 |
|
| 88 |
if (affloaded && dictloaded) |
| 89 |
{ |
| 90 |
NISortDictionary(&(d->obj)); |
| 91 |
NISortAffixes(&(d->obj)); |
| 92 |
} |
| 93 |
else if (!affloaded) |
| 94 |
{ |
| 95 |
ereport(ERROR, |
| 96 |
(errcode(ERRCODE_INVALID_PARAMETER_VALUE), |
| 97 |
errmsg("missing AffFile parameter"))); |
| 98 |
} |
| 99 |
else |
| 100 |
{ |
| 101 |
ereport(ERROR, |
| 102 |
(errcode(ERRCODE_INVALID_PARAMETER_VALUE), |
| 103 |
errmsg("missing DictFile parameter"))); |
| 104 |
} |
| 105 |
|
| 106 |
NIFinishBuild(&(d->obj)); |
| 107 |
|
| 108 |
PG_RETURN_POINTER(d); |
| 109 |
} |
| 110 |
|
| 111 |
Datum |
| 112 |
dispell_lexize(PG_FUNCTION_ARGS) |
| 113 |
{ |
| 114 |
DictISpell *d = (DictISpell *) PG_GETARG_POINTER(0); |
| 115 |
char *in = (char *) PG_GETARG_POINTER(1); |
| 116 |
int32 len = PG_GETARG_INT32(2); |
| 117 |
char *txt; |
| 118 |
TSLexeme *res; |
| 119 |
TSLexeme *ptr, |
| 120 |
*cptr; |
| 121 |
|
| 122 |
if (len <= 0) |
| 123 |
PG_RETURN_POINTER(NULL); |
| 124 |
|
| 125 |
txt = str_tolower(in, len, DEFAULT_COLLATION_OID); |
| 126 |
res = NINormalizeWord(&(d->obj), txt); |
| 127 |
|
| 128 |
if (res == NULL) |
| 129 |
PG_RETURN_POINTER(NULL); |
| 130 |
|
| 131 |
cptr = res; |
| 132 |
for (ptr = cptr; ptr->lexeme; ptr++) |
| 133 |
{ |
| 134 |
if (searchstoplist(&(d->stoplist), ptr->lexeme)) |
| 135 |
{ |
| 136 |
pfree(ptr->lexeme); |
| 137 |
ptr->lexeme = NULL; |
| 138 |
} |
| 139 |
else |
| 140 |
{ |
| 141 |
if (cptr != ptr) |
| 142 |
memcpy(cptr, ptr, sizeof(TSLexeme)); |
| 143 |
cptr++; |
| 144 |
} |
| 145 |
} |
| 146 |
cptr->lexeme = NULL; |
| 147 |
|
| 148 |
PG_RETURN_POINTER(res); |
| 149 |
} |