Debian Codegraph @

plugins/token_filters/stem.c

raw symbols SCIP
1 /*
2 Copyright (C) 2014 Brazil
3 Copyright (C) 2018-2025 Sutou Kouhei <[email protected]>
4
5 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
9
10 This library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General Public
16 License along with this library; if not, write to the Free Software
17 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18 */
19
20 #ifdef GRN_EMBEDDED
21 # define GRN_PLUGIN_FUNCTION_TAG token_filters_stem
22 #endif
23
24 #include <grn_str.h>
25
26 #include <groonga.h>
27 #include <groonga/token_filter.h>
28
29 #include <ctype.h>
30 #include <string.h>
31
32 #include <libstemmer.h>
33
34 typedef struct {
35 grn_obj algorithm;
36 bool keep_original;
37 } grn_stem_token_filter_options;
38
39 typedef struct {
40 grn_stem_token_filter_options *options;
41 grn_tokenize_mode mode;
42 bool is_enabled;
43 struct sb_stemmer *stemmer;
44 grn_tokenizer_token token;
45 grn_obj buffer;
46 } grn_stem_token_filter;
47
48 static void
49 stem_options_init(grn_ctx *ctx, grn_stem_token_filter_options *options)
50 {
51 GRN_TEXT_INIT(&(options->algorithm), 0);
52 GRN_TEXT_SETS(ctx, &(options->algorithm), "english");
53 GRN_TEXT_PUTC(ctx, &(options->algorithm), '\0');
54 options->keep_original = false;
55 }
56
57 static void *
58 stem_open_options(grn_ctx *ctx,
59 grn_obj *token_filter,
60 grn_obj *raw_options,
61 void *user_data)
62 {
63 grn_stem_token_filter_options *options;
64
65 options = GRN_PLUGIN_MALLOC(ctx, sizeof(grn_stem_token_filter_options));
66 if (!options) {
67 GRN_PLUGIN_ERROR(ctx,
68 GRN_NO_MEMORY_AVAILABLE,
69 "[token-filter][stem] "
70 "failed to allocate memory for options");
71 return NULL;
72 }
73
74 stem_options_init(ctx, options);
75
76 GRN_OPTION_VALUES_EACH_BEGIN(ctx, raw_options, i, name, name_length) {
77 grn_raw_string name_raw;
78 name_raw.value = name;
79 name_raw.length = name_length;
80
81 if (GRN_RAW_STRING_EQUAL_CSTRING(name_raw, "algorithm")) {
82 const char *algorithm;
83 unsigned int length;
84 length = grn_vector_get_element(ctx,
85 raw_options,
86 i,
87 &algorithm,
88 NULL,
89 NULL);
90 GRN_TEXT_SET(ctx, &(options->algorithm), algorithm, length);
91 GRN_TEXT_PUTC(ctx, &(options->algorithm), '\0');
92 } else if (GRN_RAW_STRING_EQUAL_CSTRING(name_raw, "keep_original")) {
93 options->keep_original =
94 grn_vector_get_element_bool(ctx,
95 raw_options,
96 i,
97 options->keep_original);
98 }
99 } GRN_OPTION_VALUES_EACH_END();
100
101 return options;
102 }
103
104 static void
105 stem_close_options(grn_ctx *ctx, void *data)
106 {
107 grn_stem_token_filter_options *options = data;
108 GRN_OBJ_FIN(ctx, &(options->algorithm));
109 GRN_PLUGIN_FREE(ctx, options);
110 }
111
112 static void *
113 stem_init(grn_ctx *ctx, grn_tokenizer_query *query)
114 {
115 grn_obj *lexicon;
116 unsigned int i;
117 grn_stem_token_filter_options *options;
118 grn_stem_token_filter *token_filter;
119
120 lexicon = grn_tokenizer_query_get_lexicon(ctx, query);
121 i = grn_tokenizer_query_get_token_filter_index(ctx, query);
122 options = grn_table_cache_token_filters_options(ctx,
123 lexicon,
124 i,
125 stem_open_options,
126 stem_close_options,
127 NULL);
128 if (ctx->rc != GRN_SUCCESS) {
129 return NULL;
130 }
131
132 token_filter = GRN_PLUGIN_MALLOC(ctx, sizeof(grn_stem_token_filter));
133 if (!token_filter) {
134 GRN_PLUGIN_ERROR(ctx, GRN_NO_MEMORY_AVAILABLE,
135 "[token-filter][stem] "
136 "failed to allocate grn_stem_token_filter");
137 return NULL;
138 }
139 token_filter->options = options;
140 token_filter->mode = grn_tokenizer_query_get_mode(ctx, query);
141 token_filter->is_enabled = true;
142 grn_obj *query_options = grn_tokenizer_query_get_options(ctx, query);
143 if (query_options) {
144 grn_proc_prefixed_options_parse(ctx,
145 query_options,
146 "TokenFilterStem.",
147 "[token-filter][stem]",
148 "enable",
149 GRN_PROC_OPTION_VALUE_BOOL,
150 &(token_filter->is_enabled),
151 NULL);
152 if (ctx->rc != GRN_SUCCESS) {
153 GRN_PLUGIN_FREE(ctx, token_filter);
154 return NULL;
155 }
156 }
157
158 {
159 const char *algorithm = GRN_TEXT_VALUE(&(token_filter->options->algorithm));
160 /* TODO: Support other encoding. */
161 const char *encoding = "UTF_8";
162 token_filter->stemmer = sb_stemmer_new(algorithm, encoding);
163 if (!token_filter->stemmer) {
164 GRN_PLUGIN_FREE(ctx, token_filter);
165 GRN_PLUGIN_ERROR(ctx, GRN_INVALID_ARGUMENT,
166 "[token-filter][stem] "
167 "failed to create stemmer: "
168 "algorithm=<%s>, encoding=<%s>",
169 algorithm, encoding);
170 return NULL;
171 }
172 }
173 grn_tokenizer_token_init(ctx, &(token_filter->token));
174 GRN_TEXT_INIT(&(token_filter->buffer), 0);
175
176 return token_filter;
177 }
178
179 static bool
180 is_stemmable(grn_ctx *ctx, grn_obj *data, bool *is_all_ascii_upper)
181 {
182 const char *current, *end;
183 bool have_ascii_lower = false;
184 bool have_ascii_upper = false;
185 bool have_non_ascii_alphabet = false;
186
187 *is_all_ascii_upper = false;
188
189 switch (data->header.domain) {
190 case GRN_DB_SHORT_TEXT :
191 case GRN_DB_TEXT :
192 case GRN_DB_LONG_TEXT :
193 break;
194 default :
195 return false;
196 }
197
198 current = GRN_TEXT_VALUE(data);
199 end = current + GRN_TEXT_LEN(data);
200 int length = 0;
201 for (; current < end; current += length) {
202 length = grn_plugin_charlen(ctx, current, end - current, GRN_ENC_UTF8);
203 if (length == 0) {
204 return false;
205 } else if (length == 1) {
206 if (islower((unsigned char)*current)) {
207 have_ascii_lower = true;
208 continue;
209 }
210 if (isupper((unsigned char)*current)) {
211 have_ascii_upper = true;
212 continue;
213 }
214 if (isdigit((unsigned char)*current)) {
215 continue;
216 }
217 if (isdigit((unsigned char)*current)) {
218 continue;
219 }
220 switch (*current) {
221 case '-' :
222 case '\'' :
223 break;
224 default :
225 return false;
226 }
227 } else {
228 grn_char_type type = grn_nfkc_latest_char_type(current);
229 if (type == GRN_CHAR_ALPHA) {
230 have_non_ascii_alphabet = true;
231 continue;
232 } else {
233 return false;
234 }
235 }
236 }
237
238 if (!have_ascii_lower && have_ascii_upper && !have_non_ascii_alphabet) {
239 *is_all_ascii_upper = true;
240 }
241
242 return true;
243 }
244
245 static void
246 normalize_ascii(grn_ctx *ctx,
247 const char *string, unsigned int length,
248 grn_obj *normalized)
249 {
250 const char *current, *end;
251 const char *unwritten;
252
253 current = unwritten = string;
254 end = current + length;
255
256 for (; current < end; current++) {
257 if (isupper((unsigned char)*current)) {
258 if (current > unwritten) {
259 GRN_TEXT_PUT(ctx, normalized, unwritten, current - unwritten);
260 }
261 GRN_TEXT_PUTC(ctx, normalized, (char)tolower((unsigned char)*current));
262 unwritten = current + 1;
263 }
264 }
265
266 if (current != unwritten) {
267 GRN_TEXT_PUT(ctx, normalized, unwritten, current - unwritten);
268 }
269 }
270
271 static void
272 unnormalize_ascii(grn_ctx *ctx,
273 const char *string, unsigned int length,
274 grn_obj *normalized)
275 {
276 const char *current, *end;
277 const char *unwritten;
278
279 current = unwritten = string;
280 end = current + length;
281
282 for (; current < end; current++) {
283 if (islower((unsigned char)*current)) {
284 if (current > unwritten) {
285 GRN_TEXT_PUT(ctx, normalized, unwritten, current - unwritten);
286 }
287 GRN_TEXT_PUTC(ctx, normalized, (char)toupper((unsigned char)*current));
288 unwritten = current + 1;
289 }
290 }
291
292 if (current != unwritten) {
293 GRN_TEXT_PUT(ctx, normalized, unwritten, current - unwritten);
294 }
295 }
296
297 static void
298 stem_filter(grn_ctx *ctx,
299 grn_token *current_token,
300 grn_token *next_token,
301 void *user_data)
302 {
303 grn_stem_token_filter *token_filter = user_data;
304 grn_obj *data;
305
306 if (!token_filter->is_enabled) {
307 return;
308 }
309
310 if (GRN_CTX_GET_ENCODING(ctx) != GRN_ENC_UTF8) {
311 return;
312 }
313
314 data = grn_token_get_data(ctx, current_token);
315 bool is_all_ascii_upper = false;
316 if (!is_stemmable(ctx, data, &is_all_ascii_upper)) {
317 return;
318 }
319
320 {
321 const sb_symbol *stemmed;
322
323 if (is_all_ascii_upper) {
324 grn_obj *buffer;
325 buffer = &(token_filter->buffer);
326 GRN_BULK_REWIND(buffer);
327 normalize_ascii(ctx,
328 GRN_TEXT_VALUE(data),
329 (unsigned int)GRN_TEXT_LEN(data),
330 buffer);
331 stemmed = sb_stemmer_stem(token_filter->stemmer,
332 GRN_TEXT_VALUE(buffer),
333 (int)GRN_TEXT_LEN(buffer));
334 if (!stemmed) {
335 GRN_PLUGIN_ERROR(ctx, GRN_NO_MEMORY_AVAILABLE,
336 "[token-filter][stem] "
337 "failed to allocate memory for stemmed word: <%.*s> "
338 "(normalized: <%.*s>)",
339 (int)GRN_TEXT_LEN(data), GRN_TEXT_VALUE(data),
340 (int)GRN_TEXT_LEN(buffer), GRN_TEXT_VALUE(buffer));
341 return;
342 }
343 GRN_BULK_REWIND(buffer);
344 unnormalize_ascii(ctx,
345 stemmed,
346 (unsigned int)sb_stemmer_length(token_filter->stemmer),
347 buffer);
348 grn_token_set_data(ctx,
349 next_token,
350 GRN_TEXT_VALUE(buffer),
351 (int)GRN_TEXT_LEN(buffer));
352 } else {
353 stemmed = sb_stemmer_stem(token_filter->stemmer,
354 GRN_TEXT_VALUE(data),
355 (int)GRN_TEXT_LEN(data));
356 if (!stemmed) {
357 GRN_PLUGIN_ERROR(ctx, GRN_NO_MEMORY_AVAILABLE,
358 "[token-filter][stem] "
359 "failed to allocate memory for stemmed word: <%.*s>",
360 (int)GRN_TEXT_LEN(data), GRN_TEXT_VALUE(data));
361 return;
362 }
363 grn_token_set_data(ctx, next_token,
364 stemmed,
365 sb_stemmer_length(token_filter->stemmer));
366 }
367 }
368
369 if (token_filter->mode == GRN_TOKENIZE_ADD &&
370 token_filter->options->keep_original) {
371 grn_token_add_status(ctx, next_token, GRN_TOKEN_KEEP_ORIGINAL);
372 }
373 }
374
375 static void
376 stem_fin(grn_ctx *ctx, void *user_data)
377 {
378 grn_stem_token_filter *token_filter = user_data;
379 if (!token_filter) {
380 return;
381 }
382
383 grn_tokenizer_token_fin(ctx, &(token_filter->token));
384 if (token_filter->stemmer) {
385 sb_stemmer_delete(token_filter->stemmer);
386 }
387 GRN_OBJ_FIN(ctx, &(token_filter->buffer));
388 GRN_PLUGIN_FREE(ctx, token_filter);
389 }
390
391 grn_rc
392 GRN_PLUGIN_INIT(grn_ctx *ctx)
393 {
394 return ctx->rc;
395 }
396
397 grn_rc
398 GRN_PLUGIN_REGISTER(grn_ctx *ctx)
399 {
400 grn_obj *token_filter;
401
402 token_filter = grn_token_filter_create(ctx, "TokenFilterStem", -1);
403 grn_token_filter_set_init_func(ctx, token_filter, stem_init);
404 grn_token_filter_set_filter_func(ctx, token_filter, stem_filter);
405 grn_token_filter_set_fin_func(ctx, token_filter, stem_fin);
406
407 return ctx->rc;
408 }
409
410 grn_rc
411 GRN_PLUGIN_FIN(grn_ctx *ctx)
412 {
413 return GRN_SUCCESS;
414 }