Debian Codegraph @

lib/proc/proc_tokenize.c

raw symbols SCIP
1 /*
2 Copyright (C) 2009-2018 Brazil
3 Copyright (C) 2018-2026 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 #include "../grn_proc.h"
21 #include "../grn_ctx.h"
22 #include "../grn_token_cursor.h"
23
24 #include <groonga/plugin.h>
25
26 typedef struct {
27 grn_id id;
28 int32_t position;
29 bool force_prefix_search;
30 uint64_t source_offset;
31 uint32_t source_length;
32 uint32_t source_first_character_length;
33 grn_obj metadata;
34 } tokenize_token;
35
36 static void
37 init_tokens(grn_ctx *ctx, grn_obj *tokens)
38 {
39 GRN_VALUE_FIX_SIZE_INIT(tokens, GRN_OBJ_VECTOR, GRN_ID_NIL);
40 }
41
42 static void
43 fin_tokens(grn_ctx *ctx, grn_obj *tokens)
44 {
45 size_t i;
46 size_t n_tokens;
47
48 n_tokens = GRN_BULK_VSIZE(tokens) / sizeof(tokenize_token);
49 for (i = 0; i < n_tokens; i++) {
50 tokenize_token *token;
51 token = ((tokenize_token *)(GRN_BULK_HEAD(tokens))) + i;
52 GRN_OBJ_FIN(ctx, &(token->metadata));
53 }
54 GRN_OBJ_FIN(ctx, tokens);
55 }
56
57 static void
58 output_tokens_simple(grn_ctx *ctx, grn_obj *tokens, grn_obj *lexicon)
59 {
60 size_t i;
61 size_t n_tokens = GRN_BULK_VSIZE(tokens) / sizeof(tokenize_token);
62
63 grn_ctx_output_array_open(ctx, "TOKENS", (int)n_tokens);
64 grn_obj key;
65 GRN_OBJ_INIT(&key, GRN_BULK, GRN_OBJ_DO_SHALLOW_COPY, lexicon->header.domain);
66 tokenize_token *tokenize_tokens = (tokenize_token *)GRN_BULK_HEAD(tokens);
67 for (i = 0; i < n_tokens; i++) {
68 tokenize_token *token = tokenize_tokens + i;
69
70 char value[GRN_TABLE_MAX_KEY_SIZE];
71 int value_size =
72 grn_table_get_key(ctx, lexicon, token->id, value, sizeof(value));
73
74 GRN_TEXT_SET(ctx, &key, value, value_size);
75 grn_ctx_output_obj(ctx, &key, NULL);
76 }
77 GRN_OBJ_FIN(ctx, &key);
78 grn_ctx_output_array_close(ctx);
79 }
80
81 static void
82 output_tokens(grn_ctx *ctx,
83 grn_obj *tokens,
84 grn_obj *lexicon,
85 grn_obj *index_column)
86 {
87 size_t i, n_tokens, n_elements;
88 grn_obj estimated_size;
89 bool have_source_location = false;
90 bool have_metadata = false;
91
92 n_tokens = GRN_BULK_VSIZE(tokens) / sizeof(tokenize_token);
93 n_elements = 4;
94 if (index_column) {
95 n_elements++;
96 GRN_UINT32_INIT(&estimated_size, 0);
97 }
98 for (i = 0; i < n_tokens; i++) {
99 tokenize_token *token;
100 token = ((tokenize_token *)(GRN_BULK_HEAD(tokens))) + i;
101 if (token->source_offset > 0 || token->source_length > 0) {
102 have_source_location = true;
103 }
104 if (grn_vector_size(ctx, &(token->metadata)) > 0) {
105 have_metadata = true;
106 }
107 }
108 if (have_source_location) {
109 n_elements += 3;
110 }
111 if (have_metadata) {
112 n_elements += 1;
113 }
114
115 grn_ctx_output_array_open(ctx, "TOKENS", (int)n_tokens);
116 for (i = 0; i < n_tokens; i++) {
117 tokenize_token *token;
118
119 token = ((tokenize_token *)(GRN_BULK_HEAD(tokens))) + i;
120
121 grn_ctx_output_map_open(ctx, "TOKEN", (int)n_elements);
122
123 grn_ctx_output_cstr(ctx, "value");
124 {
125 char value[GRN_TABLE_MAX_KEY_SIZE];
126 int value_size;
127 value_size = grn_table_get_key(ctx,
128 lexicon,
129 token->id,
130 value,
131 GRN_TABLE_MAX_KEY_SIZE);
132 grn_obj key;
133 GRN_OBJ_INIT(&key,
134 GRN_BULK,
135 GRN_OBJ_DO_SHALLOW_COPY,
136 lexicon->header.domain);
137 GRN_TEXT_SET(ctx, &key, value, value_size);
138 grn_ctx_output_obj(ctx, &key, NULL);
139 GRN_OBJ_FIN(ctx, &key);
140 }
141
142 grn_ctx_output_cstr(ctx, "position");
143 grn_ctx_output_int32(ctx, token->position);
144
145 /* For backward compatibility. */
146 grn_ctx_output_cstr(ctx, "force_prefix");
147 grn_ctx_output_bool(ctx, token->force_prefix_search);
148
149 grn_ctx_output_cstr(ctx, "force_prefix_search");
150 grn_ctx_output_bool(ctx, token->force_prefix_search);
151
152 if (index_column) {
153 GRN_BULK_REWIND(&estimated_size);
154 grn_obj_get_value(ctx, index_column, token->id, &estimated_size);
155 grn_ctx_output_cstr(ctx, "estimated_size");
156 grn_ctx_output_int64(ctx, GRN_UINT32_VALUE(&estimated_size));
157 }
158
159 if (have_source_location) {
160 grn_ctx_output_cstr(ctx, "source_offset");
161 grn_ctx_output_uint64(ctx, token->source_offset);
162
163 grn_ctx_output_cstr(ctx, "source_length");
164 grn_ctx_output_uint32(ctx, token->source_length);
165
166 grn_ctx_output_cstr(ctx, "source_first_character_length");
167 grn_ctx_output_uint32(ctx, token->source_first_character_length);
168 }
169
170 if (have_metadata) {
171 size_t i;
172 size_t n_metadata;
173 grn_obj value;
174
175 n_metadata = grn_vector_size(ctx, &(token->metadata)) / 2;
176 GRN_VOID_INIT(&value);
177 grn_ctx_output_cstr(ctx, "metadata");
178 grn_ctx_output_map_open(ctx, "METADATA", (int)n_metadata);
179 for (i = 0; i < n_metadata; i++) {
180 const char *raw_name;
181 unsigned int raw_name_length;
182 const char *raw_value;
183 unsigned int raw_value_length;
184 grn_id value_domain;
185
186 raw_name_length = grn_vector_get_element(ctx,
187 &(token->metadata),
188 (uint32_t)(i * 2),
189 &raw_name,
190 NULL,
191 NULL);
192 grn_ctx_output_str(ctx, raw_name, raw_name_length);
193
194 raw_value_length = grn_vector_get_element(ctx,
195 &(token->metadata),
196 (uint32_t)(i * 2 + 1),
197 &raw_value,
198 NULL,
199 &value_domain);
200 grn_obj_reinit(ctx, &value, value_domain, 0);
201 grn_bulk_write(ctx, &value, raw_value, raw_value_length);
202 grn_ctx_output_obj(ctx, &value, NULL);
203 }
204 grn_ctx_output_map_close(ctx);
205
206 GRN_OBJ_FIN(ctx, &value);
207 }
208
209 grn_ctx_output_map_close(ctx);
210 }
211
212 if (index_column) {
213 GRN_OBJ_FIN(ctx, &estimated_size);
214 }
215
216 grn_ctx_output_array_close(ctx);
217 }
218
219 static void
220 tokenize(grn_ctx *ctx,
221 grn_obj *lexicon,
222 grn_raw_string *string_raw,
223 grn_tokenize_mode mode,
224 unsigned int flags,
225 grn_obj *tokens)
226 {
227 grn_token_cursor *token_cursor;
228
229 token_cursor = grn_token_cursor_open(ctx,
230 lexicon,
231 string_raw->value,
232 string_raw->length,
233 mode,
234 flags);
235 if (!token_cursor) {
236 return;
237 }
238
239 grn_token_cursor_set_query_domain(ctx, token_cursor, GRN_DB_TEXT);
240 while (token_cursor->status == GRN_TOKEN_CURSOR_DOING) {
241 grn_id token_id = grn_token_cursor_next(ctx, token_cursor);
242 grn_token *token;
243 tokenize_token *current_token;
244
245 if (token_id == GRN_ID_NIL) {
246 continue;
247 }
248 token = grn_token_cursor_get_token(ctx, token_cursor);
249 grn_bulk_space(ctx, tokens, sizeof(tokenize_token));
250 current_token = ((tokenize_token *)(GRN_BULK_CURR(tokens))) - 1;
251 current_token->id = token_id;
252 current_token->position = (int32_t)grn_token_get_position(ctx, token);
253 current_token->force_prefix_search =
254 grn_token_get_force_prefix_search(ctx, token);
255 current_token->source_offset = grn_token_get_source_offset(ctx, token);
256 current_token->source_length = grn_token_get_source_length(ctx, token);
257 current_token->source_first_character_length =
258 grn_token_get_source_first_character_length(ctx, token);
259
260 {
261 grn_obj *metadata;
262 size_t n_metadata;
263 size_t i;
264 grn_obj name;
265 grn_obj value;
266
267 GRN_TEXT_INIT(&(current_token->metadata), GRN_OBJ_VECTOR);
268 metadata = grn_token_get_metadata(ctx, token);
269 n_metadata = grn_token_metadata_get_size(ctx, metadata);
270 GRN_TEXT_INIT(&name, 0);
271 GRN_VOID_INIT(&value);
272 for (i = 0; i < n_metadata; i++) {
273 GRN_BULK_REWIND(&name);
274 GRN_BULK_REWIND(&value);
275 grn_token_metadata_at(ctx, metadata, i, &name, &value);
276 if (GRN_TEXT_LEN(&name) == 0) {
277 continue;
278 }
279 grn_vector_add_element(ctx,
280 &(current_token->metadata),
281 GRN_BULK_HEAD(&name),
282 (uint32_t)GRN_BULK_VSIZE(&name),
283 0,
284 name.header.domain);
285 grn_vector_add_element(ctx,
286 &(current_token->metadata),
287 GRN_BULK_HEAD(&value),
288 (uint32_t)GRN_BULK_VSIZE(&value),
289 0,
290 value.header.domain);
291 }
292 GRN_OBJ_FIN(ctx, &name);
293 GRN_OBJ_FIN(ctx, &value);
294 }
295 }
296 grn_token_cursor_close(ctx, token_cursor);
297 }
298
299 static grn_obj *
300 command_table_tokenize(grn_ctx *ctx,
301 int nargs,
302 grn_obj **args,
303 grn_user_data *user_data)
304 {
305 grn_raw_string table_raw;
306 grn_raw_string string_raw;
307 grn_obj *flags_raw;
308 grn_obj *mode_raw;
309 grn_raw_string index_column_raw;
310 grn_raw_string output_style_raw;
311
312 #define GET_VALUE(name) \
313 name##_raw.value = grn_plugin_proc_get_var_string(ctx, \
314 user_data, \
315 #name, \
316 strlen(#name), \
317 &(name##_raw.length))
318
319 GET_VALUE(table);
320 GET_VALUE(string);
321 flags_raw = grn_plugin_proc_get_var(ctx, user_data, "flags", strlen("flags"));
322 mode_raw = grn_plugin_proc_get_var(ctx, user_data, "mode", strlen("mode"));
323 GET_VALUE(index_column);
324 GET_VALUE(output_style);
325
326 #undef GET_VALUE
327
328 if (table_raw.length == 0) {
329 GRN_PLUGIN_ERROR(ctx,
330 GRN_INVALID_ARGUMENT,
331 "[table_tokenize] table name is missing");
332 return NULL;
333 }
334
335 if (string_raw.length == 0) {
336 GRN_PLUGIN_ERROR(ctx,
337 GRN_INVALID_ARGUMENT,
338 "[table_tokenize] string is missing");
339 return NULL;
340 }
341
342 {
343 uint32_t flags;
344 grn_obj *lexicon;
345 grn_obj *index_column = NULL;
346
347 flags = grn_proc_get_value_token_cursor_flags(ctx,
348 flags_raw,
349 0,
350 "[table_tokenize][flags]");
351 if (ctx->rc != GRN_SUCCESS) {
352 return NULL;
353 }
354
355 lexicon = grn_ctx_get(ctx, table_raw.value, (int)(table_raw.length));
356 if (!lexicon) {
357 GRN_PLUGIN_ERROR(ctx,
358 GRN_INVALID_ARGUMENT,
359 "[table_tokenize] nonexistent lexicon: <%.*s>",
360 (int)table_raw.length,
361 table_raw.value);
362 return NULL;
363 }
364
365 if (index_column_raw.length > 0) {
366 index_column = grn_obj_column(ctx,
367 lexicon,
368 index_column_raw.value,
369 (uint32_t)(index_column_raw.length));
370 if (!index_column) {
371 GRN_PLUGIN_ERROR(ctx,
372 GRN_INVALID_ARGUMENT,
373 "[table_tokenize] nonexistent index column: <%.*s>",
374 (int)index_column_raw.length,
375 index_column_raw.value);
376 goto exit;
377 }
378 if (index_column->header.type != GRN_COLUMN_INDEX) {
379 GRN_PLUGIN_ERROR(ctx,
380 GRN_INVALID_ARGUMENT,
381 "[table_tokenize] "
382 "index column must be COLUMN_INDEX: <%.*s>",
383 (int)index_column_raw.length,
384 index_column_raw.value);
385 goto exit;
386 }
387 }
388
389 {
390 grn_obj tokens;
391 init_tokens(ctx, &tokens);
392 grn_tokenize_mode mode =
393 grn_proc_get_value_tokenize_mode(ctx,
394 mode_raw,
395 GRN_TOKENIZE_GET,
396 "[table_tokenize][mode]");
397 if (ctx->rc == GRN_SUCCESS) {
398 tokenize(ctx, lexicon, &string_raw, mode, flags, &tokens);
399 if (GRN_RAW_STRING_EQUAL_CSTRING(output_style_raw, "simple")) {
400 output_tokens_simple(ctx, &tokens, lexicon);
401 } else {
402 output_tokens(ctx, &tokens, lexicon, index_column);
403 }
404 }
405 fin_tokens(ctx, &tokens);
406 }
407 #undef MODE_NAME_EQUAL
408
409 exit:
410 grn_obj_unlink(ctx, lexicon);
411 if (index_column) {
412 grn_obj_unlink(ctx, index_column);
413 }
414 }
415
416 return NULL;
417 }
418
419 void
420 grn_proc_init_table_tokenize(grn_ctx *ctx)
421 {
422 #define N_ARGS 6
423 grn_expr_var vars[N_ARGS];
424
425 grn_plugin_expr_var_init(ctx, &(vars[0]), "table", -1);
426 grn_plugin_expr_var_init(ctx, &(vars[1]), "string", -1);
427 grn_plugin_expr_var_init(ctx, &(vars[2]), "flags", -1);
428 grn_plugin_expr_var_init(ctx, &(vars[3]), "mode", -1);
429 grn_plugin_expr_var_init(ctx, &(vars[4]), "index_column", -1);
430 grn_plugin_expr_var_init(ctx, &(vars[5]), "output_style", -1);
431 grn_plugin_command_create(ctx,
432 "table_tokenize",
433 -1,
434 command_table_tokenize,
435 N_ARGS,
436 vars);
437 #undef N_ARGS
438 }
439
440 static grn_obj *
441 command_tokenize(grn_ctx *ctx,
442 int nargs,
443 grn_obj **args,
444 grn_user_data *user_data)
445 {
446 const char *context_tag = "[tokenize]";
447 grn_raw_string tokenizer_raw;
448 grn_raw_string string_raw;
449 grn_raw_string normalizer_raw;
450 grn_obj *flags_raw;
451 grn_obj *mode_raw;
452 grn_raw_string token_filters_raw;
453 grn_raw_string output_style_raw;
454
455 #define GET_VALUE(name) \
456 name##_raw.value = grn_plugin_proc_get_var_string(ctx, \
457 user_data, \
458 #name, \
459 strlen(#name), \
460 &(name##_raw.length))
461
462 GET_VALUE(tokenizer);
463 GET_VALUE(string);
464 GET_VALUE(normalizer);
465 flags_raw = grn_plugin_proc_get_var(ctx, user_data, "flags", strlen("flags"));
466 mode_raw = grn_plugin_proc_get_var(ctx, user_data, "mode", strlen("mode"));
467 GET_VALUE(token_filters);
468 GET_VALUE(output_style);
469
470 #undef GET_VALUE
471
472 if (tokenizer_raw.length == 0) {
473 GRN_PLUGIN_ERROR(ctx,
474 GRN_INVALID_ARGUMENT,
475 "%s tokenizer name is missing",
476 context_tag);
477 return NULL;
478 }
479
480 if (string_raw.length == 0) {
481 GRN_PLUGIN_ERROR(ctx,
482 GRN_INVALID_ARGUMENT,
483 "%s string is missing",
484 context_tag);
485 return NULL;
486 }
487
488 {
489 uint32_t flags;
490 grn_obj *lexicon;
491
492 flags = grn_proc_get_value_token_cursor_flags(ctx,
493 flags_raw,
494 0,
495 "[tokenize][flags]");
496 if (ctx->rc != GRN_SUCCESS) {
497 return NULL;
498 }
499
500 lexicon = grn_proc_lexicon_open(ctx,
501 &tokenizer_raw,
502 &normalizer_raw,
503 &token_filters_raw,
504 NULL,
505 context_tag);
506 if (!lexicon) {
507 return NULL;
508 }
509
510 {
511 grn_obj tokens;
512 init_tokens(ctx, &tokens);
513 grn_tokenize_mode mode =
514 grn_proc_get_value_tokenize_mode(ctx,
515 mode_raw,
516 GRN_TOKENIZE_ADD,
517 "[tokenize][mode]");
518 if (ctx->rc == GRN_SUCCESS) {
519 tokenize(ctx, lexicon, &string_raw, GRN_TOKENIZE_ADD, flags, &tokens);
520 if (mode != GRN_TOKENIZE_ADD) {
521 GRN_BULK_REWIND(&tokens);
522 tokenize(ctx, lexicon, &string_raw, mode, flags, &tokens);
523 }
524 if (GRN_RAW_STRING_EQUAL_CSTRING(output_style_raw, "simple")) {
525 output_tokens_simple(ctx, &tokens, lexicon);
526 } else {
527 output_tokens(ctx, &tokens, lexicon, NULL);
528 }
529 }
530 fin_tokens(ctx, &tokens);
531 }
532 #undef MODE_NAME_EQUAL
533
534 grn_obj_unlink(ctx, lexicon);
535 }
536
537 return NULL;
538 }
539
540 void
541 grn_proc_init_tokenize(grn_ctx *ctx)
542 {
543 #define N_ARGS 7
544 grn_expr_var vars[N_ARGS];
545
546 grn_plugin_expr_var_init(ctx, &(vars[0]), "tokenizer", -1);
547 grn_plugin_expr_var_init(ctx, &(vars[1]), "string", -1);
548 grn_plugin_expr_var_init(ctx, &(vars[2]), "normalizer", -1);
549 grn_plugin_expr_var_init(ctx, &(vars[3]), "flags", -1);
550 grn_plugin_expr_var_init(ctx, &(vars[4]), "mode", -1);
551 grn_plugin_expr_var_init(ctx, &(vars[5]), "token_filters", -1);
552 grn_plugin_expr_var_init(ctx, &(vars[6]), "output_style", -1);
553 grn_plugin_command_create(ctx,
554 "tokenize",
555 -1,
556 command_tokenize,
557 N_ARGS,
558 vars);
559 #undef N_ARGS
560 }