-
Notifications
You must be signed in to change notification settings - Fork 0
/
dict_translate.c
293 lines (242 loc) · 6.2 KB
/
dict_translate.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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
/*-------------------------------------------------------------------------
*
* dict_translate.c
* Translation dictionary
*
* IDENTIFICATION
* contrib/dict_translate/dict_translate.c
*
*-------------------------------------------------------------------------
*/
#include "postgres.h"
#include "catalog/namespace.h"
#include "commands/defrem.h"
#include "nodes/pg_list.h"
#include "tsearch/ts_cache.h"
#include "tsearch/ts_locale.h"
#include "tsearch/ts_public.h"
#include "utils/builtins.h"
PG_MODULE_MAGIC;
typedef struct
{
char *key; /* Word */
char *value; /* Unparsed list of synonyms, including the
* word itself */
} TranslateEntry;
typedef struct
{
size_t len; /* length of trn array */
TranslateEntry *trn;
/* subdictionary to normalize input lexeme */
Oid inDictOid;
TSDictionaryCacheEntry *inDict;
} DictTranslate;
PG_FUNCTION_INFO_V1(dtrn_init);
PG_FUNCTION_INFO_V1(dtrn_lexize);
static char *
find_word(char *in, char **end)
{
char *start;
*end = NULL;
while (*in && t_isspace(in))
in += pg_mblen(in);
if (!*in || *in == '#')
return NULL;
start = in;
while (*in && !t_isspace(in))
in += pg_mblen(in);
*end = in;
return start;
}
static int
compareTrn(const void *a, const void *b)
{
return strcmp(((const TranslateEntry *) a)->key,
((const TranslateEntry *) b)->key);
}
static void
read_dictionary(char *filename, DictTranslate *d)
{
char *real_filename = get_tsearch_config_filename(filename, "trn");
tsearch_readline_state trst;
char *line = NULL;
size_t cur = 0;
if (!tsearch_readline_begin(&trst, real_filename))
ereport(ERROR,
(errcode(ERRCODE_CONFIG_FILE_ERROR),
errmsg("could not open translate file \"%s\": %m",
real_filename)));
while ((line = tsearch_readline(&trst)) != NULL)
{
char *lower_line,
*key,
*value,
*end;
if (*line == '\0')
continue;
lower_line = lowerstr(line);
pfree(line);
key = find_word(lower_line, &end);
if (!key)
{
/* Empty line */
goto skipline;
}
if (*end == '\0')
{
/* A line with only one word. Ignore silently. */
goto skipline;
}
/* Find start position of the key translation */
value = end;
while (*value && t_isspace(value))
value += pg_mblen(value);
if (!value)
{
/* A line with only one word (+whitespace). Ignore silently. */
goto skipline;
}
/* Enlarge trn structure if full */
if (cur == d->len)
{
d->len = (d->len > 0) ? 2 * d->len : 16;
if (d->trn)
d->trn = (TranslateEntry *) repalloc(d->trn,
sizeof(TranslateEntry) * d->len);
else
d->trn = (TranslateEntry *) palloc(sizeof(TranslateEntry) * d->len);
}
d->trn[cur].key = pnstrdup(key, end - key);
d->trn[cur].value = pstrdup(value);
cur++;
skipline:
pfree(lower_line);
}
tsearch_readline_end(&trst);
d->len = cur;
if (cur > 1)
qsort(d->trn, d->len, sizeof(TranslateEntry), compareTrn);
pfree(real_filename);
}
Datum
dtrn_init(PG_FUNCTION_ARGS)
{
List *dictoptions = (List *) PG_GETARG_POINTER(0);
ListCell *l;
DictTranslate *d;
bool fileloaded = false;
char *inputdict = NULL;
d = (DictTranslate *) palloc0(sizeof(DictTranslate));
foreach(l, dictoptions)
{
DefElem *defel = (DefElem *) lfirst(l);
if (pg_strcasecmp("DictFile", defel->defname) == 0)
{
if (fileloaded)
ereport(ERROR,
(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
errmsg("multiple DictFile parameters")));
read_dictionary(defGetString(defel), d);
fileloaded = true;
}
else if (pg_strcasecmp("InputDict", defel->defname) == 0)
{
if (inputdict)
ereport(ERROR,
(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
errmsg("multiple InputDict parameters")));
inputdict = pstrdup(defGetString(defel));
}
else
ereport(ERROR,
(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
errmsg("unrecognized synonym parameter: \"%s\"",
defel->defname)));
}
if (!fileloaded)
ereport(ERROR,
(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
errmsg("missing DictFile parameter")));
if (!inputdict)
ereport(ERROR,
(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
errmsg("missing InputDict parameter")));
d->inDictOid = get_ts_dict_oid(stringToQualifiedNameList(inputdict), false);
d->inDict = lookup_ts_dictionary_cache(d->inDictOid);
PG_RETURN_POINTER(d);
}
Datum
dtrn_lexize(PG_FUNCTION_ARGS)
{
DictTranslate *d = (DictTranslate *) PG_GETARG_POINTER(0);
char *in = (char *) PG_GETARG_POINTER(1);
int32 len = PG_GETARG_INT32(2);
TSLexeme *res = NULL,
*input_res,
*input_ptr;
TranslateEntry word,
*found;
uint16 nvariant = 1;
Size cur = 0,
lres = 0;
char *translate,
*pos,
*end;
/* note: d->len test protects against Solaris bsearch-of-no-items bug */
if (len <= 0 || d->len <= 0)
PG_RETURN_POINTER(NULL);
if (!d->inDict->isvalid)
d->inDict = lookup_ts_dictionary_cache(d->inDictOid);
input_res = (TSLexeme *) DatumGetPointer(FunctionCall4(&(d->inDict->lexize),
PointerGetDatum(d->inDict->dictData),
PointerGetDatum(in),
Int32GetDatum(len),
PointerGetDatum(NULL)));
if (!input_res)
PG_RETURN_POINTER(NULL);
if (!input_res->lexeme)
{
pfree(input_res);
PG_RETURN_POINTER(NULL);
}
input_ptr = input_res;
while (input_ptr->lexeme)
{
word.key = lowerstr(input_ptr->lexeme);
word.value = NULL;
pfree(input_ptr->lexeme);
found = (TranslateEntry *) bsearch(&word, d->trn, d->len,
sizeof(TranslateEntry), compareTrn);
pfree(word.key);
if (found)
{
pos = found->value;
while ((translate = find_word(pos, &end)) != NULL)
{
if (cur == 0 || cur == lres - 1 /* for res[cur].lexeme = NULL */)
{
lres = (lres > 0) ? 2 * lres : 8;
if (res)
res = (TSLexeme *) repalloc(res, sizeof(TSLexeme) * lres);
else
res = (TSLexeme *) palloc(sizeof(TSLexeme) * lres);
}
res[cur].nvariant = nvariant;
res[cur].lexeme = pnstrdup(translate, end - translate);
res[cur].flags = 0;
cur++;
nvariant++;
pos = end;
}
}
/*
* Compound words do not supported yet.
* So iterate without considering them.
*/
input_ptr++;
}
pfree(input_res);
if (res)
res[cur].lexeme = NULL;
PG_RETURN_POINTER(res);
}