1 /*-------------------------------------------------------------------------
4 * Ispell dictionary interface
6 * Portions Copyright (c) 1996-2025, PostgreSQL Global Development Group
10 * src/backend/tsearch/dict_ispell.c
12 *-------------------------------------------------------------------------
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"
31 dispell_init(PG_FUNCTION_ARGS
)
33 List
*dictoptions
= (List
*) PG_GETARG_POINTER(0);
35 bool affloaded
= false,
40 d
= (DictISpell
*) palloc0(sizeof(DictISpell
));
42 NIStartBuild(&(d
->obj
));
44 foreach(l
, dictoptions
)
46 DefElem
*defel
= (DefElem
*) lfirst(l
);
48 if (strcmp(defel
->defname
, "dictfile") == 0)
52 (errcode(ERRCODE_INVALID_PARAMETER_VALUE
),
53 errmsg("multiple DictFile parameters")));
54 NIImportDictionary(&(d
->obj
),
55 get_tsearch_config_filename(defGetString(defel
),
59 else if (strcmp(defel
->defname
, "afffile") == 0)
63 (errcode(ERRCODE_INVALID_PARAMETER_VALUE
),
64 errmsg("multiple AffFile parameters")));
65 NIImportAffixes(&(d
->obj
),
66 get_tsearch_config_filename(defGetString(defel
),
70 else if (strcmp(defel
->defname
, "stopwords") == 0)
74 (errcode(ERRCODE_INVALID_PARAMETER_VALUE
),
75 errmsg("multiple StopWords parameters")));
76 readstoplist(defGetString(defel
), &(d
->stoplist
), str_tolower
);
82 (errcode(ERRCODE_INVALID_PARAMETER_VALUE
),
83 errmsg("unrecognized Ispell parameter: \"%s\"",
88 if (affloaded
&& dictloaded
)
90 NISortDictionary(&(d
->obj
));
91 NISortAffixes(&(d
->obj
));
96 (errcode(ERRCODE_INVALID_PARAMETER_VALUE
),
97 errmsg("missing AffFile parameter")));
102 (errcode(ERRCODE_INVALID_PARAMETER_VALUE
),
103 errmsg("missing DictFile parameter")));
106 NIFinishBuild(&(d
->obj
));
108 PG_RETURN_POINTER(d
);
112 dispell_lexize(PG_FUNCTION_ARGS
)
114 DictISpell
*d
= (DictISpell
*) PG_GETARG_POINTER(0);
115 char *in
= (char *) PG_GETARG_POINTER(1);
116 int32 len
= PG_GETARG_INT32(2);
123 PG_RETURN_POINTER(NULL
);
125 txt
= str_tolower(in
, len
, DEFAULT_COLLATION_OID
);
126 res
= NINormalizeWord(&(d
->obj
), txt
);
129 PG_RETURN_POINTER(NULL
);
132 for (ptr
= cptr
; ptr
->lexeme
; ptr
++)
134 if (searchstoplist(&(d
->stoplist
), ptr
->lexeme
))
142 memcpy(cptr
, ptr
, sizeof(TSLexeme
));
148 PG_RETURN_POINTER(res
);