-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathdict_hspell.c
226 lines (185 loc) · 5.49 KB
/
dict_hspell.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
/*
* dict_hspell.c
*
* Copyright (c) 2022 Igor Khanin
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
*/
// PostgreSQL server API includes
#include <postgres.h>
#include <commands/defrem.h>
#include <fmgr.h>
#include <mb/pg_wchar.h>
#include <nodes/pg_list.h>
#include <tsearch/ts_public.h>
#include <utils/elog.h>
// hspell includes
#include <hspell.h>
#include <linginfo.h>
PG_MODULE_MAGIC;
typedef struct DictHspell
{
struct dict_radix* dict;
StopList stoplist;
}
DictHspell;
static const unsigned char MISC_STEM[] = {0xF9, 0xE5, 0xF0, 0xE5, 0xFA, 0};
static List* stem_list;
PG_FUNCTION_INFO_V1(dhspell_init);
Datum
dhspell_init(PG_FUNCTION_ARGS)
{
List* options = (List *)PG_GETARG_POINTER(0);
bool stopwords_loaded = false;
DictHspell* d;
ListCell* l;
int rc;
d = (DictHspell*)palloc0(sizeof(DictHspell));
rc = hspell_init(&d->dict, HSPELL_OPT_LINGUISTICS);
if (rc) {
ereport(ERROR,
(errcode(ERRCODE_CONFIG_FILE_ERROR),
errmsg("Can't load hspell dictionary from \"%s\", rc = %d",
hspell_get_dictionary_path(), rc)));
}
foreach(l, options) {
DefElem* defel = (DefElem *)lfirst(l);
if (strcmp(defel->defname, "stopwords") == 0) {
if (stopwords_loaded) {
ereport(ERROR,
(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
errmsg("Multiple stopwords parameters to hspell")));
}
readstoplist(defGetString(defel), &d->stoplist, NULL);
stopwords_loaded = true;
}
else {
ereport(ERROR,
(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
errmsg("Unrecognized hspell parameter: \"%s\"", defel->defname)));
}
}
PG_RETURN_POINTER(d);
}
static bool
has_hebrew(const char* word_iso)
{
const unsigned char* ptr;
if (word_iso == NULL) {
return false;
}
ptr = (const unsigned char*)word_iso;
while (*ptr) {
if (*ptr >= 0xE0 && *ptr <= 0xFA) { // Alef to Tav in ISO-8859-8
return true;
}
ptr++;
}
return false;
}
static char*
get_hspell_token(const char* token, int token_len)
{
char* recoded_token = NULL;
PG_TRY();
{
recoded_token = pg_server_to_any(token, token_len, PG_ISO_8859_8);
}
PG_CATCH();
{
// Failure to convert token into ISO-8859-8 means that hspell
// won't be able to handle it, so don't recognize the token.
return NULL;
}
PG_END_TRY();
if (!has_hebrew(recoded_token)) {
// Don't recognize tokens that don't contain any Hebrew letters,
// we want those to possibly go to another dictionary.
if (recoded_token != token) {
pfree(recoded_token);
}
return NULL;
}
return recoded_token;
}
static int
hspell_callback(const char* word, const char* baseword, int preflen, int prefspec)
{
char* desc;
char* stem;
if (*baseword == '\0') {
// Don't index prefix only matches
return 0;
}
linginfo_lookup(baseword, &desc, &stem);
for (int i = 0; ; i++) {
char* stem_text = linginfo_stem2text(stem, i);
if (!stem_text) {
break;
}
if ((linginfo_desc2ps(desc, i) & prefspec) == 0) {
continue;
}
if (memcmp(stem_text, MISC_STEM, sizeof(MISC_STEM)) == 0) {
continue;
}
if (!list_member_ptr(stem_list, stem_text)) {
stem_list = lappend(stem_list, stem_text);
}
}
return 0;
}
PG_FUNCTION_INFO_V1(dhspell_lexize);
Datum
dhspell_lexize(PG_FUNCTION_ARGS)
{
DictHspell* d = (DictHspell*)PG_GETARG_POINTER(0);
char* in = (char*)PG_GETARG_POINTER(1);
int32 in_len = PG_GETARG_INT32(2);
char* token;
char* recoded_token;
TSLexeme* res;
size_t i;
ListCell* l;
token = palloc0(in_len + 1);
strncpy(token, in, in_len);
recoded_token = get_hspell_token(token, in_len);
if (recoded_token == NULL) {
pfree(token);
PG_RETURN_POINTER(NULL);
}
// Search stop words with the original token in the server encoding, but do
// it after get_hspell_token() so we are sure to ignore non-Hebrew
if (searchstoplist(&d->stoplist, token)) {
res = palloc0(sizeof(TSLexeme));
}
else {
stem_list = NIL;
hspell_enum_splits(d->dict, recoded_token, hspell_callback);
if (list_length(stem_list) == 0) {
// Since we know the token has some Hebrew, and it is not a
// valid word in hspell's dictionary - accept it as-is
stem_list = lappend(stem_list, recoded_token);
}
res = palloc0(sizeof(TSLexeme) * (list_length(stem_list) + 1));
i = 0;
foreach(l, stem_list) {
char* stem = (char *)lfirst(l);
char* stem_recoded = pg_any_to_server(stem, strlen(stem), PG_ISO_8859_8);
if (stem_recoded == stem) {
stem_recoded = pstrdup(stem);
}
res[i].lexeme = stem_recoded;
res[i].nvariant = i;
i++;
}
list_free(stem_list);
}
if (recoded_token != token) {
pfree(recoded_token);
}
pfree(token);
PG_RETURN_POINTER(res);
}