Debian Codegraph @

lib/tokenizers.c

raw symbols SCIP
1 /*
2 Copyright (C) 2009-2018 Brazil
3 Copyright (C) 2018-2024 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 <math.h>
21 #include <string.h>
22
23 #include "grn_token.h"
24 #include "grn_token_cursor.h"
25
26 #include "grn_ctx_impl.h"
27 #include "grn_db.h"
28 #include "grn_ii.h"
29 #include "grn_onigmo.h"
30 #include "grn_plugin.h"
31 #include "grn_string.h"
32
33 grn_obj *grn_tokenizer_uvector = NULL;
34
35 typedef struct {
36 grn_tokenizer_token token;
37 byte *curr;
38 byte *tail;
39 uint32_t unit;
40 } grn_uvector_tokenizer;
41
42 static grn_obj *
43 uvector_init(grn_ctx *ctx, int nargs, grn_obj **args, grn_user_data *user_data)
44 {
45 grn_obj *str, *flags, *mode;
46 grn_uvector_tokenizer *tokenizer;
47 if (!(flags = grn_ctx_pop(ctx))) {
48 ERR(GRN_INVALID_ARGUMENT, "[tokenizer][uvector] missing argument: flags");
49 return NULL;
50 }
51 if (!(str = grn_ctx_pop(ctx))) {
52 ERR(GRN_INVALID_ARGUMENT, "[tokenizer][uvector] missing argument: string");
53 return NULL;
54 }
55 if (!(mode = grn_ctx_pop(ctx))) {
56 ERR(GRN_INVALID_ARGUMENT, "[tokenizer][uvector] missing argument: mode");
57 return NULL;
58 }
59 if (!(tokenizer = GRN_CALLOC(sizeof(grn_uvector_tokenizer)))) {
60 ERR(GRN_NO_MEMORY_AVAILABLE,
61 "[tokenizer][uvector] "
62 "memory allocation to grn_uvector_tokenizer failed");
63 return NULL;
64 }
65 user_data->ptr = tokenizer;
66
67 grn_tokenizer_token_init(ctx, &(tokenizer->token));
68 tokenizer->curr = (byte *)GRN_TEXT_VALUE(str);
69 tokenizer->tail = tokenizer->curr + GRN_TEXT_LEN(str);
70 tokenizer->unit = sizeof(grn_id);
71 return NULL;
72 }
73
74 static grn_obj *
75 uvector_next(grn_ctx *ctx, int nargs, grn_obj **args, grn_user_data *user_data)
76 {
77 grn_uvector_tokenizer *tokenizer = user_data->ptr;
78 byte *p = tokenizer->curr + tokenizer->unit;
79 if (tokenizer->tail < p) {
80 grn_tokenizer_token_push(ctx,
81 &(tokenizer->token),
82 (const char *)tokenizer->curr,
83 0,
84 GRN_TOKEN_LAST);
85 } else {
86 grn_token_status status;
87 if (tokenizer->tail == p) {
88 status = GRN_TOKEN_LAST;
89 } else {
90 status = GRN_TOKEN_CONTINUE;
91 }
92 grn_tokenizer_token_push(ctx,
93 &(tokenizer->token),
94 (const char *)tokenizer->curr,
95 tokenizer->unit,
96 status);
97 tokenizer->curr = p;
98 }
99 return NULL;
100 }
101
102 static grn_obj *
103 uvector_fin(grn_ctx *ctx, int nargs, grn_obj **args, grn_user_data *user_data)
104 {
105 grn_uvector_tokenizer *tokenizer = user_data->ptr;
106 if (!tokenizer) {
107 return NULL;
108 }
109 grn_tokenizer_token_fin(ctx, &(tokenizer->token));
110 GRN_FREE(tokenizer);
111 return NULL;
112 }
113
114 typedef struct {
115 grn_obj delimiters;
116 #ifdef GRN_SUPPORT_REGEXP
117 OnigRegex regex;
118 #endif /* GRN_SUPPORT_REGEXP */
119 } grn_delimit_options;
120
121 typedef struct {
122 const char *delimiter;
123 size_t delimiter_length;
124 } grn_delimit_options_default;
125
126 typedef struct {
127 grn_tokenizer_query *query;
128 grn_delimit_options *options;
129 bool have_tokenized_delimiter;
130 grn_encoding encoding;
131 const unsigned char *start;
132 const unsigned char *next;
133 const unsigned char *end;
134 } grn_delimit_tokenizer;
135
136 static void
137 delimit_options_init(grn_delimit_options *options)
138 {
139 GRN_TEXT_INIT(&(options->delimiters), GRN_OBJ_VECTOR);
140 #ifdef GRN_SUPPORT_REGEXP
141 options->regex = NULL;
142 #endif /* GRN_SUPPORT_REGEXP */
143 }
144
145 static void *
146 delimit_open_options(grn_ctx *ctx,
147 grn_obj *tokenizer,
148 grn_obj *raw_options,
149 void *user_data)
150 {
151 grn_delimit_options *options;
152 grn_delimit_options_default *options_default = user_data;
153 bool have_delimiter = false;
154
155 options = GRN_CALLOC(sizeof(grn_delimit_options));
156 if (!options) {
157 ERR(GRN_NO_MEMORY_AVAILABLE,
158 "[tokenizer][delimit] "
159 "failed to allocate memory for options");
160 return NULL;
161 }
162
163 delimit_options_init(options);
164
165 GRN_OPTION_VALUES_EACH_BEGIN(ctx, raw_options, i, name, name_length)
166 {
167 grn_raw_string name_raw;
168 name_raw.value = name;
169 name_raw.length = name_length;
170
171 if (GRN_RAW_STRING_EQUAL_CSTRING(name_raw, "delimiter")) {
172 const char *delimiter;
173 unsigned int delimiter_length;
174 grn_id domain;
175
176 have_delimiter = true;
177 delimiter_length =
178 grn_vector_get_element(ctx, raw_options, i, &delimiter, NULL, &domain);
179 if (grn_type_id_is_text_family(ctx, domain) && delimiter_length > 0) {
180 grn_vector_add_element(ctx,
181 &(options->delimiters),
182 delimiter,
183 delimiter_length,
184 0,
185 GRN_DB_TEXT);
186 }
187 } else if (GRN_RAW_STRING_EQUAL_CSTRING(name_raw, "pattern")) {
188 #ifdef GRN_SUPPORT_REGEXP
189 const char *pattern;
190 unsigned int pattern_length;
191 grn_id domain;
192
193 pattern_length =
194 grn_vector_get_element(ctx, raw_options, i, &pattern, NULL, &domain);
195 if (grn_type_id_is_text_family(ctx, domain) && pattern_length > 0) {
196 if (options->regex) {
197 onig_free(options->regex);
198 }
199 options->regex = grn_onigmo_new(ctx,
200 pattern,
201 pattern_length,
202 GRN_ONIGMO_OPTION_DEFAULT,
203 GRN_ONIGMO_SYNTAX_DEFAULT,
204 "[tokenizer][delimit]");
205 }
206 #endif /* GRN_SUPPORT_REGEXP */
207 }
208 }
209 GRN_OPTION_VALUES_EACH_END();
210
211 if (!have_delimiter) {
212 grn_vector_add_element(ctx,
213 &(options->delimiters),
214 options_default->delimiter,
215 (uint32_t)(options_default->delimiter_length),
216 0,
217 GRN_DB_TEXT);
218 }
219
220 return options;
221 }
222
223 static void
224 delimit_close_options(grn_ctx *ctx, void *data)
225 {
226 grn_delimit_options *options = data;
227
228 GRN_OBJ_FIN(ctx, &(options->delimiters));
229 #ifdef GRN_SUPPORT_REGEXP
230 if (options->regex) {
231 onig_free(options->regex);
232 }
233 #endif /* GRN_SUPPORT_REGEXP */
234 GRN_FREE(options);
235 }
236
237 static void *
238 delimit_init_raw(grn_ctx *ctx,
239 grn_tokenizer_query *query,
240 grn_delimit_options *options)
241 {
242 grn_delimit_tokenizer *tokenizer;
243
244 if (!(tokenizer = GRN_CALLOC(sizeof(grn_delimit_tokenizer)))) {
245 ERR(GRN_NO_MEMORY_AVAILABLE,
246 "[tokenizer][delimit] "
247 "memory allocation to grn_delimit_tokenizer failed");
248 return NULL;
249 }
250
251 tokenizer->query = query;
252 tokenizer->options = options;
253
254 {
255 const char *raw_string;
256 size_t raw_string_length;
257 grn_encoding encoding;
258
259 raw_string = grn_tokenizer_query_get_raw_string(ctx,
260 tokenizer->query,
261 &raw_string_length);
262 encoding = grn_tokenizer_query_get_encoding(ctx, tokenizer->query);
263 tokenizer->have_tokenized_delimiter =
264 grn_tokenizer_have_tokenized_delimiter(ctx,
265 raw_string,
266 (unsigned int)raw_string_length,
267 encoding);
268 tokenizer->encoding = encoding;
269 }
270 {
271 grn_obj *string;
272 const char *normalized;
273 unsigned int normalized_length_in_bytes;
274
275 string = grn_tokenizer_query_get_normalized_string(ctx, tokenizer->query);
276 grn_string_get_normalized(ctx,
277 string,
278 &normalized,
279 &normalized_length_in_bytes,
280 NULL);
281 tokenizer->start = (const unsigned char *)normalized;
282 tokenizer->next = tokenizer->start;
283 tokenizer->end = tokenizer->start + normalized_length_in_bytes;
284 }
285
286 return tokenizer;
287 }
288
289 static void *
290 delimit_init(grn_ctx *ctx, grn_tokenizer_query *query)
291 {
292 grn_obj *lexicon = grn_tokenizer_query_get_lexicon(ctx, query);
293 grn_delimit_options *options;
294 grn_delimit_options_default options_default;
295
296 options_default.delimiter = " ";
297 options_default.delimiter_length = 1;
298
299 options = grn_table_cache_default_tokenizer_options(ctx,
300 lexicon,
301 delimit_open_options,
302 delimit_close_options,
303 &options_default);
304 if (ctx->rc != GRN_SUCCESS) {
305 return NULL;
306 }
307
308 return delimit_init_raw(ctx, query, options);
309 }
310
311 static void
312 delimit_next(grn_ctx *ctx,
313 grn_tokenizer_query *query,
314 grn_token *token,
315 void *user_data)
316 {
317 grn_delimit_tokenizer *tokenizer = user_data;
318
319 if (tokenizer->have_tokenized_delimiter) {
320 ptrdiff_t rest_length;
321 rest_length = tokenizer->end - tokenizer->next;
322 tokenizer->next =
323 (unsigned char *)grn_tokenizer_next_by_tokenized_delimiter(
324 ctx,
325 token,
326 (const char *)tokenizer->next,
327 (unsigned int)rest_length,
328 tokenizer->encoding);
329 #ifdef GRN_SUPPORT_REGEXP
330 } else if (tokenizer->options->regex) {
331 OnigPosition position;
332 OnigRegion region;
333
334 onig_region_init(&region);
335 position = onig_search(tokenizer->options->regex,
336 tokenizer->start,
337 tokenizer->end,
338 tokenizer->next,
339 tokenizer->end,
340 &region,
341 ONIG_OPTION_NONE);
342 if (position == ONIG_MISMATCH) {
343 grn_token_set_data(ctx,
344 token,
345 tokenizer->next,
346 (int)(tokenizer->end - tokenizer->next));
347 grn_token_set_status(ctx, token, GRN_TOKEN_LAST);
348 } else {
349 grn_token_set_data(
350 ctx,
351 token,
352 tokenizer->next,
353 (int)((tokenizer->start + region.beg[0]) - tokenizer->next));
354 grn_token_set_status(ctx, token, GRN_TOKEN_CONTINUE);
355 tokenizer->next = tokenizer->start + region.end[0];
356 onig_region_free(&region, 0);
357 }
358 #endif /* GRN_SUPPORT_REGEXP */
359 } else {
360 int cl;
361 const unsigned char *p = tokenizer->next, *r;
362 const unsigned char *e = tokenizer->end;
363 grn_token_status status;
364 grn_obj *delimiters;
365 unsigned int i, n_delimiters;
366 delimiters = &(tokenizer->options->delimiters);
367 n_delimiters = grn_vector_size(ctx, delimiters);
368 for (r = p; r < e; r += cl) {
369 if (!(cl =
370 grn_charlen_(ctx, (char *)r, (char *)e, tokenizer->encoding))) {
371 tokenizer->next = (unsigned char *)e;
372 break;
373 }
374 {
375 bool found_delimiter = false;
376 const unsigned char *current_end = r;
377 while (true) {
378 bool found_delimiter_sub = false;
379 for (i = 0; i < n_delimiters; i++) {
380 const char *delimiter;
381 unsigned int delimiter_length;
382 delimiter_length = grn_vector_get_element(ctx,
383 delimiters,
384 i,
385 &delimiter,
386 NULL,
387 NULL);
388 if (current_end + delimiter_length <= e &&
389 memcmp(current_end, delimiter, delimiter_length) == 0) {
390 current_end += delimiter_length;
391 tokenizer->next = current_end;
392 found_delimiter = true;
393 found_delimiter_sub = true;
394 }
395 }
396 if (!found_delimiter_sub) {
397 break;
398 }
399 }
400 if (found_delimiter) {
401 break;
402 }
403 }
404 }
405 if (r == e) {
406 status = GRN_TOKEN_LAST;
407 } else {
408 status = GRN_TOKEN_CONTINUE;
409 }
410 grn_token_set_data(ctx, token, (const char *)p, (int)(r - p));
411 grn_token_set_status(ctx, token, status);
412 }
413 }
414
415 static void
416 delimit_fin(grn_ctx *ctx, void *user_data)
417 {
418 grn_delimit_tokenizer *tokenizer = user_data;
419
420 if (!tokenizer) {
421 return;
422 }
423 GRN_FREE(tokenizer);
424 }
425
426 static void *
427 delimit_null_init(grn_ctx *ctx, grn_tokenizer_query *query)
428 {
429 grn_obj *lexicon = grn_tokenizer_query_get_lexicon(ctx, query);
430 grn_delimit_options *options;
431 grn_delimit_options_default options_default;
432
433 options_default.delimiter = "\0";
434 options_default.delimiter_length = 1;
435
436 options = grn_table_cache_default_tokenizer_options(ctx,
437 lexicon,
438 delimit_open_options,
439 delimit_close_options,
440 &options_default);
441 if (ctx->rc != GRN_SUCCESS) {
442 return NULL;
443 }
444
445 return delimit_init_raw(ctx, query, options);
446 }
447
448 /* ngram tokenizer */
449
450 static bool grn_ngram_tokenizer_remove_blank_enable = true;
451
452 typedef struct {
453 uint8_t unit;
454 bool unify_alphabet;
455 bool unify_digit;
456 bool unify_symbol;
457 bool ignore_blank;
458 bool remove_blank;
459 bool loose_symbol;
460 bool loose_blank;
461 bool report_source_location;
462 bool include_removed_source_location;
463 } grn_ngram_options;
464
465 typedef struct {
466 grn_tokenizer_token token;
467 grn_tokenizer_query *query;
468 grn_ngram_options options;
469 bool overlap;
470 struct {
471 bool ing;
472 bool need;
473 bool need_end_mark;
474 grn_obj text;
475 uint_least8_t *ctypes;
476 int16_t *checks;
477 uint64_t *offsets;
478 } loose;
479 uint32_t pos;
480 uint32_t skip;
481 unsigned int n_chars;
482 const unsigned char *start;
483 const unsigned char *next;
484 const unsigned char *end;
485 const uint_least8_t *ctypes;
486 const int16_t *checks;
487 const uint64_t *offsets;
488 uint32_t tail;
489 uint64_t source_offset;
490 } grn_ngram_tokenizer;
491
492 static void
493 ngram_options_init(grn_ngram_options *options, uint8_t unit)
494 {
495 options->unit = unit;
496 options->unify_alphabet = true;
497 options->unify_digit = true;
498 options->unify_symbol = true;
499 options->ignore_blank = false;
500 options->remove_blank = grn_ngram_tokenizer_remove_blank_enable;
501 options->loose_symbol = false;
502 options->loose_blank = false;
503 options->report_source_location = false;
504 options->include_removed_source_location = true;
505 }
506
507 static void
508 ngram_switch_to_loose_mode(grn_ctx *ctx, grn_ngram_tokenizer *tokenizer)
509 {
510 grn_obj *string;
511 const char *normalized;
512 unsigned int normalized_length_in_bytes;
513 unsigned int normalized_length_in_chars;
514 const char *normalized_end;
515 const uint_least8_t *types = tokenizer->ctypes;
516 const int16_t *checks = tokenizer->checks;
517 const uint64_t *offsets = tokenizer->offsets;
518
519 string = grn_tokenizer_query_get_normalized_string(ctx, tokenizer->query);
520 grn_string_get_normalized(ctx,
521 string,
522 &normalized,
523 &normalized_length_in_bytes,
524 &normalized_length_in_chars);
525 normalized_end = normalized + normalized_length_in_bytes;
526
527 if (types) {
528 grn_encoding encoding =
529 grn_tokenizer_query_get_encoding(ctx, tokenizer->query);
530 uint_least8_t *loose_types;
531 int16_t *loose_checks = NULL;
532 uint64_t *loose_offsets = NULL;
533 const int16_t *removed_checks = NULL;
534 int64_t last_offset = -1;
535 unsigned int n_chars = 0;
536
537 tokenizer->loose.ctypes =
538 GRN_CALLOC(sizeof(uint_least8_t) * (normalized_length_in_chars + 1));
539 if (!tokenizer->loose.ctypes) {
540 ERR(GRN_NO_MEMORY_AVAILABLE,
541 "[tokenizer][ngram][loose] "
542 "failed to allocate memory for character types");
543 return;
544 }
545 loose_types = tokenizer->loose.ctypes;
546 if (checks) {
547 tokenizer->loose.checks =
548 GRN_CALLOC(sizeof(int16_t) * normalized_length_in_bytes);
549 if (!tokenizer->loose.checks) {
550 ERR(GRN_NO_MEMORY_AVAILABLE,
551 "[tokenizer][ngram][loose] "
552 "failed to allocate memory for character lengths");
553 return;
554 }
555 }
556 if (offsets) {
557 tokenizer->loose.offsets =
558 GRN_CALLOC(sizeof(uint64_t) * (normalized_length_in_chars + 1));
559 if (!tokenizer->loose.offsets) {
560 ERR(GRN_NO_MEMORY_AVAILABLE,
561 "[tokenizer][ngram][loose] "
562 "failed to allocate memory for character offsets");
563 return;
564 }
565 }
566 loose_types = tokenizer->loose.ctypes;
567 loose_checks = tokenizer->loose.checks;
568 loose_offsets = tokenizer->loose.offsets;
569 while (normalized < normalized_end) {
570 int length;
571 length =
572 grn_charlen_(ctx, (char *)normalized, (char *)normalized_end, encoding);
573 if (length == 0) {
574 break;
575 }
576 const bool is_loose_symbol = (tokenizer->options.loose_symbol &&
577 GRN_STR_CTYPE(*types) == GRN_CHAR_SYMBOL);
578 const bool is_loose_blank =
579 (!tokenizer->options.remove_blank && tokenizer->options.loose_blank &&
580 GRN_STR_ISBLANK(*types));
581 if (is_loose_symbol || is_loose_blank) {
582 if (tokenizer->options.include_removed_source_location) {
583 if (!removed_checks) {
584 removed_checks = checks;
585 }
586 }
587 if (offsets && last_offset == -1) {
588 last_offset = *offsets;
589 }
590 } else {
591 GRN_TEXT_PUT(ctx, &(tokenizer->loose.text), normalized, length);
592 *loose_types = *types;
593 loose_types++;
594 if (loose_checks) {
595 int i;
596 if (tokenizer->options.include_removed_source_location) {
597 for (; removed_checks && removed_checks < checks;
598 removed_checks++) {
599 if (*removed_checks > 0) {
600 *loose_checks += *removed_checks;
601 }
602 }
603 removed_checks = NULL;
604 }
605 for (i = 0; i < length; i++) {
606 if (checks[i] != -1) {
607 loose_checks[i] += checks[i];
608 }
609 }
610 loose_checks += length;
611 }
612 if (loose_offsets) {
613 *loose_offsets = *offsets;
614 loose_offsets++;
615 last_offset = -1;
616 }
617 n_chars++;
618 }
619 normalized += length;
620 types++;
621 if (checks) {
622 checks += length;
623 }
624 if (offsets) {
625 offsets++;
626 }
627 }
628 *loose_types = *types;
629 if (offsets) {
630 if (last_offset == -1) {
631 *loose_offsets = *offsets;
632 } else {
633 /* Remove trailing ignored characters. */
634 *loose_offsets = (uint64_t)last_offset;
635 }
636 }
637 tokenizer->start =
638 (const unsigned char *)GRN_TEXT_VALUE(&(tokenizer->loose.text));
639 tokenizer->next = tokenizer->start;
640 tokenizer->end = tokenizer->start + GRN_TEXT_LEN(&(tokenizer->loose.text));
641 tokenizer->ctypes = tokenizer->loose.ctypes;
642 tokenizer->checks = tokenizer->loose.checks;
643 tokenizer->offsets = tokenizer->loose.offsets;
644 tokenizer->n_chars = n_chars;
645 } else {
646 tokenizer->start = normalized;
647 tokenizer->next = tokenizer->start;
648 tokenizer->end = normalized_end;
649 }
650
651 tokenizer->pos = 0;
652 tokenizer->skip = 0;
653 tokenizer->overlap = false;
654 tokenizer->loose.ing = true;
655 tokenizer->source_offset = 0;
656 }
657
658 static void *
659 ngram_init_raw(grn_ctx *ctx,
660 grn_tokenizer_query *query,
661 const grn_ngram_options *options)
662 {
663 unsigned int normalize_flags = GRN_STRING_REMOVE_BLANK |
664 GRN_STRING_WITH_TYPES |
665 GRN_STRING_REMOVE_TOKENIZED_DELIMITER;
666 grn_ngram_tokenizer *tokenizer;
667
668 if (!options->remove_blank) {
669 normalize_flags &= (unsigned int)(~GRN_STRING_REMOVE_BLANK);
670 }
671 if (options->report_source_location) {
672 normalize_flags |= GRN_STRING_WITH_CHECKS;
673 }
674 grn_tokenizer_query_set_normalize_flags(ctx, query, normalize_flags);
675
676 if (!(tokenizer = GRN_CALLOC(sizeof(grn_ngram_tokenizer)))) {
677 ERR(GRN_NO_MEMORY_AVAILABLE,
678 "[tokenizer][ngram] "
679 "memory allocation to grn_ngram_tokenizer failed");
680 return NULL;
681 }
682
683 grn_tokenizer_token_init(ctx, &(tokenizer->token));
684 tokenizer->query = query;
685
686 tokenizer->options = *options;
687 tokenizer->overlap = false;
688 tokenizer->loose.ing = false;
689 tokenizer->loose.need = false;
690 tokenizer->loose.need_end_mark = false;
691 GRN_TEXT_INIT(&(tokenizer->loose.text), 0);
692 tokenizer->loose.ctypes = NULL;
693 tokenizer->loose.checks = NULL;
694 tokenizer->loose.offsets = NULL;
695 tokenizer->pos = 0;
696 tokenizer->skip = 0;
697 tokenizer->source_offset = 0;
698
699 {
700 grn_obj *string;
701 const char *normalized_raw;
702 unsigned int normalized_length_in_bytes;
703 unsigned int normalized_length_in_chars;
704 const unsigned char *start;
705 const unsigned char *next;
706 const unsigned char *end;
707 unsigned int n_chars;
708 const uint_least8_t *ctypes;
709 const int16_t *checks;
710 const uint64_t *offsets;
711
712 string = grn_tokenizer_query_get_normalized_string(ctx, tokenizer->query);
713 grn_string_get_normalized(ctx,
714 string,
715 &normalized_raw,
716 &normalized_length_in_bytes,
717 &normalized_length_in_chars);
718 tokenizer->start = start = (const unsigned char *)normalized_raw;
719 tokenizer->next = next = tokenizer->start;
720 tokenizer->end = end = tokenizer->start + normalized_length_in_bytes;
721 tokenizer->n_chars = n_chars = normalized_length_in_chars;
722 tokenizer->ctypes = ctypes = grn_string_get_types(ctx, string);
723 tokenizer->checks = checks = grn_string_get_checks(ctx, string);
724 tokenizer->offsets = offsets = grn_string_get_offsets(ctx, string);
725 if (grn_tokenizer_query_get_mode(ctx, tokenizer->query) == GRN_TOKEN_GET) {
726 ngram_switch_to_loose_mode(ctx, tokenizer);
727 if (tokenizer->n_chars == 0) {
728 tokenizer->start = start;
729 tokenizer->next = next;
730 tokenizer->end = end;
731 tokenizer->n_chars = n_chars;
732 tokenizer->ctypes = ctypes;
733 tokenizer->checks = checks;
734 tokenizer->offsets = offsets;
735 }
736 }
737 }
738
739 return tokenizer;
740 }
741
742 static grn_obj *
743 ngram_init_deprecated(grn_ctx *ctx,
744 int nargs,
745 grn_obj **args,
746 grn_user_data *user_data,
747 const grn_ngram_options *options)
748 {
749 unsigned int normalize_flags = GRN_STRING_REMOVE_BLANK |
750 GRN_STRING_WITH_TYPES |
751 GRN_STRING_REMOVE_TOKENIZED_DELIMITER;
752 grn_tokenizer_query *query;
753
754 if (!options->remove_blank) {
755 normalize_flags &= (unsigned int)(~GRN_STRING_REMOVE_BLANK);
756 }
757 query = grn_tokenizer_query_open(ctx, nargs, args, normalize_flags);
758 if (!query) {
759 return NULL;
760 }
761
762 user_data->ptr = ngram_init_raw(ctx, query, options);
763 if (!user_data->ptr) {
764 grn_tokenizer_query_close(ctx, query);
765 }
766 return NULL;
767 }
768
769 static grn_obj *
770 unigram_init(grn_ctx *ctx, int nargs, grn_obj **args, grn_user_data *user_data)
771 {
772 grn_ngram_options options;
773 ngram_options_init(&options, 1);
774 return ngram_init_deprecated(ctx, nargs, args, user_data, &options);
775 }
776
777 static grn_obj *
778 bigram_init(grn_ctx *ctx, int nargs, grn_obj **args, grn_user_data *user_data)
779 {
780 grn_ngram_options options;
781 ngram_options_init(&options, 2);
782 return ngram_init_deprecated(ctx, nargs, args, user_data, &options);
783 }
784
785 static grn_obj *
786 trigram_init(grn_ctx *ctx, int nargs, grn_obj **args, grn_user_data *user_data)
787 {
788 grn_ngram_options options;
789 ngram_options_init(&options, 3);
790 return ngram_init_deprecated(ctx, nargs, args, user_data, &options);
791 }
792
793 static grn_obj *
794 bigrams_init(grn_ctx *ctx, int nargs, grn_obj **args, grn_user_data *user_data)
795 {
796 grn_ngram_options options;
797 ngram_options_init(&options, 2);
798 options.unify_symbol = false;
799 return ngram_init_deprecated(ctx, nargs, args, user_data, &options);
800 }
801
802 static grn_obj *
803 bigramsa_init(grn_ctx *ctx, int nargs, grn_obj **args, grn_user_data *user_data)
804 {
805 grn_ngram_options options;
806 ngram_options_init(&options, 2);
807 options.unify_symbol = false;
808 options.unify_alphabet = false;
809 return ngram_init_deprecated(ctx, nargs, args, user_data, &options);
810 }
811
812 static grn_obj *
813 bigramsad_init(grn_ctx *ctx,
814 int nargs,
815 grn_obj **args,
816 grn_user_data *user_data)
817 {
818 grn_ngram_options options;
819 ngram_options_init(&options, 2);
820 options.unify_symbol = false;
821 options.unify_alphabet = false;
822 options.unify_digit = false;
823 return ngram_init_deprecated(ctx, nargs, args, user_data, &options);
824 }
825
826 static grn_obj *
827 bigrami_init(grn_ctx *ctx, int nargs, grn_obj **args, grn_user_data *user_data)
828 {
829 grn_ngram_options options;
830 ngram_options_init(&options, 2);
831 options.ignore_blank = true;
832 return ngram_init_deprecated(ctx, nargs, args, user_data, &options);
833 }
834
835 static grn_obj *
836 bigramis_init(grn_ctx *ctx, int nargs, grn_obj **args, grn_user_data *user_data)
837 {
838 grn_ngram_options options;
839 ngram_options_init(&options, 2);
840 options.ignore_blank = true;
841 options.unify_symbol = false;
842 return ngram_init_deprecated(ctx, nargs, args, user_data, &options);
843 }
844
845 static grn_obj *
846 bigramisa_init(grn_ctx *ctx,
847 int nargs,
848 grn_obj **args,
849 grn_user_data *user_data)
850 {
851 grn_ngram_options options;
852 ngram_options_init(&options, 2);
853 options.ignore_blank = true;
854 options.unify_symbol = false;
855 options.unify_alphabet = false;
856 return ngram_init_deprecated(ctx, nargs, args, user_data, &options);
857 }
858
859 static grn_obj *
860 bigramisad_init(grn_ctx *ctx,
861 int nargs,
862 grn_obj **args,
863 grn_user_data *user_data)
864 {
865 grn_ngram_options options;
866 ngram_options_init(&options, 2);
867 options.ignore_blank = true;
868 options.unify_symbol = false;
869 options.unify_alphabet = false;
870 options.unify_digit = false;
871 return ngram_init_deprecated(ctx, nargs, args, user_data, &options);
872 }
873
874 static void *
875 ngram_open_options(grn_ctx *ctx,
876 grn_obj *tokenizer,
877 grn_obj *raw_options,
878 void *user_data)
879 {
880 grn_ngram_options *options;
881
882 options = GRN_CALLOC(sizeof(grn_ngram_options));
883 if (!options) {
884 ERR(GRN_NO_MEMORY_AVAILABLE,
885 "[tokenizer][ngram] "
886 "failed to allocate memory for options");
887 return NULL;
888 }
889
890 ngram_options_init(options, 2);
891
892 GRN_OPTION_VALUES_EACH_BEGIN(ctx, raw_options, i, name, name_length)
893 {
894 grn_raw_string name_raw;
895 name_raw.value = name;
896 name_raw.length = name_length;
897
898 if (GRN_RAW_STRING_EQUAL_CSTRING(name_raw, "n")) {
899 options->unit =
900 grn_vector_get_element_uint8(ctx, raw_options, i, options->unit);
901 } else if (GRN_RAW_STRING_EQUAL_CSTRING(name_raw, "ignore_blank")) {
902 options->ignore_blank =
903 grn_vector_get_element_bool(ctx, raw_options, i, options->ignore_blank);
904 } else if (GRN_RAW_STRING_EQUAL_CSTRING(name_raw, "remove_blank")) {
905 options->remove_blank =
906 grn_vector_get_element_bool(ctx, raw_options, i, options->remove_blank);
907 } else if (GRN_RAW_STRING_EQUAL_CSTRING(name_raw, "loose_symbol")) {
908 options->loose_symbol =
909 grn_vector_get_element_bool(ctx, raw_options, i, options->loose_symbol);
910 } else if (GRN_RAW_STRING_EQUAL_CSTRING(name_raw, "loose_blank")) {
911 options->loose_blank =
912 grn_vector_get_element_bool(ctx, raw_options, i, options->loose_blank);
913 } else if (GRN_RAW_STRING_EQUAL_CSTRING(name_raw,
914 "report_source_location")) {
915 options->report_source_location =
916 grn_vector_get_element_bool(ctx,
917 raw_options,
918 i,
919 options->report_source_location);
920 } else if (GRN_RAW_STRING_EQUAL_CSTRING(
921 name_raw,
922 "include_removed_source_location")) {
923 options->include_removed_source_location =
924 grn_vector_get_element_bool(ctx,
925 raw_options,
926 i,
927 options->include_removed_source_location);
928 } else if (GRN_RAW_STRING_EQUAL_CSTRING(name_raw, "unify_alphabet")) {
929 options->unify_alphabet =
930 grn_vector_get_element_bool(ctx,
931 raw_options,
932 i,
933 options->unify_alphabet);
934 } else if (GRN_RAW_STRING_EQUAL_CSTRING(name_raw, "unify_digit")) {
935 options->unify_digit =
936 grn_vector_get_element_bool(ctx, raw_options, i, options->unify_digit);
937 } else if (GRN_RAW_STRING_EQUAL_CSTRING(name_raw, "unify_symbol")) {
938 options->unify_symbol =
939 grn_vector_get_element_bool(ctx, raw_options, i, options->unify_symbol);
940 }
941 }
942 GRN_OPTION_VALUES_EACH_END();
943
944 return options;
945 }
946
947 static void
948 ngram_close_options(grn_ctx *ctx, void *data)
949 {
950 grn_ngram_options *options = data;
951 GRN_FREE(options);
952 }
953
954 static void *
955 ngram_init(grn_ctx *ctx, grn_tokenizer_query *query)
956 {
957 grn_obj *lexicon = grn_tokenizer_query_get_lexicon(ctx, query);
958 grn_ngram_options *options;
959
960 options = grn_table_cache_default_tokenizer_options(ctx,
961 lexicon,
962 ngram_open_options,
963 ngram_close_options,
964 NULL);
965 if (ctx->rc != GRN_SUCCESS) {
966 return NULL;
967 }
968
969 return ngram_init_raw(ctx, query, options);
970 }
971
972 static void
973 ngram_next(grn_ctx *ctx,
974 grn_tokenizer_query *query,
975 grn_token *token,
976 void *user_data)
977 {
978 grn_ngram_tokenizer *tokenizer = user_data;
979 int cl;
980 const unsigned char *p = tokenizer->next, *r = p, *e = tokenizer->end;
981 uint32_t n_characters = 0;
982 uint32_t pos = tokenizer->pos + tokenizer->skip;
983 grn_token_status status = 0;
984 const uint_least8_t *cp = tokenizer->ctypes ? tokenizer->ctypes + pos : NULL;
985 const int16_t *checks = NULL;
986 const uint64_t *offsets =
987 tokenizer->offsets ? tokenizer->offsets + pos : NULL;
988 grn_encoding encoding = grn_tokenizer_query_get_encoding(ctx, query);
989 const bool in_loose_blank_mode =
990 tokenizer->loose.ing && tokenizer->options.loose_blank;
991
992 if (tokenizer->checks) {
993 checks = tokenizer->checks + (p - tokenizer->start);
994 }
995
996 if (tokenizer->loose.ing && tokenizer->loose.need_end_mark) {
997 grn_token_set_data(ctx,
998 token,
999 GRN_TOKENIZER_END_MARK_UTF8,
1000 GRN_TOKENIZER_END_MARK_UTF8_LEN);
1001 grn_token_set_status(ctx, token, status);
1002 if (offsets) {
1003 grn_token_set_source_offset(ctx,
1004 token,
1005 tokenizer->offsets[tokenizer->n_chars]);
1006 } else if (checks) {
1007 grn_token_set_source_offset(ctx, token, tokenizer->source_offset);
1008 }
1009 ngram_switch_to_loose_mode(ctx, tokenizer);
1010 tokenizer->loose.need_end_mark = false;
1011 return;
1012 }
1013
1014 #define LOOSE_NEED_CHECK(cp, tokenizer) \
1015 do { \
1016 if (cp && !tokenizer->loose.ing && !tokenizer->loose.need && \
1017 ((tokenizer->options.loose_symbol && \
1018 GRN_STR_CTYPE(*cp) == GRN_CHAR_SYMBOL) || \
1019 (tokenizer->options.loose_blank && GRN_STR_ISBLANK(*cp)))) { \
1020 tokenizer->loose.need = true; \
1021 } \
1022 } while (false)
1023
1024 LOOSE_NEED_CHECK(cp, tokenizer);
1025
1026 if (cp && tokenizer->options.unify_alphabet &&
1027 GRN_STR_CTYPE(*cp) == GRN_CHAR_ALPHA) {
1028 while ((cl = grn_charlen_(ctx, (char *)r, (char *)e, encoding))) {
1029 n_characters++;
1030 r += cl;
1031 LOOSE_NEED_CHECK(cp, tokenizer);
1032 if (!in_loose_blank_mode &&
1033 /* !tokenizer->options.ignore_blank && */ GRN_STR_ISBLANK(*cp)) {
1034 break;
1035 }
1036 if (GRN_STR_CTYPE(*++cp) != GRN_CHAR_ALPHA) {
1037 break;
1038 }
1039 }
1040 tokenizer->next = r;
1041 tokenizer->overlap = false;
1042 } else if (cp && tokenizer->options.unify_digit &&
1043 GRN_STR_CTYPE(*cp) == GRN_CHAR_DIGIT) {
1044 while ((cl = grn_charlen_(ctx, (char *)r, (char *)e, encoding))) {
1045 n_characters++;
1046 r += cl;
1047 LOOSE_NEED_CHECK(cp, tokenizer);
1048 if (!in_loose_blank_mode &&
1049 /* !tokenizer->options.ignore_blank && */ GRN_STR_ISBLANK(*cp)) {
1050 break;
1051 }
1052 if (GRN_STR_CTYPE(*++cp) != GRN_CHAR_DIGIT) {
1053 break;
1054 }
1055 }
1056 tokenizer->next = r;
1057 tokenizer->overlap = false;
1058 } else if (cp && tokenizer->options.unify_symbol &&
1059 GRN_STR_CTYPE(*cp) == GRN_CHAR_SYMBOL) {
1060 while ((cl = grn_charlen_(ctx, (char *)r, (char *)e, encoding))) {
1061 n_characters++;
1062 r += cl;
1063 LOOSE_NEED_CHECK(cp, tokenizer);
1064 if (!in_loose_blank_mode && !tokenizer->options.ignore_blank &&
1065 GRN_STR_ISBLANK(*cp)) {
1066 break;
1067 }
1068 if (GRN_STR_CTYPE(*++cp) != GRN_CHAR_SYMBOL) {
1069 break;
1070 }
1071 }
1072 tokenizer->next = r;
1073 tokenizer->overlap = false;
1074 } else {
1075 #ifdef PRE_DEFINED_UNSPLIT_WORDS
1076 const unsigned char *key = NULL;
1077 // todo : grn_pat_lcp_search
1078 if ((tid = grn_sym_common_prefix_search(sym, p))) {
1079 if (!(key = _grn_sym_key(sym, tid))) {
1080 tokenizer->status = GRN_TOKEN_CURSOR_NOT_FOUND;
1081 return;
1082 }
1083 len = grn_str_len(key, encoding, NULL);
1084 }
1085 r = p + grn_charlen_(ctx, p, e, encoding);
1086 if (tid && (len > 1 || r == p)) {
1087 if (r != p && pos + len - 1 <= tokenizer->tail) {
1088 continue;
1089 }
1090 p += strlen(key);
1091 if (!*p && tokenizer->mode == GRN_TOKEN_GET) {
1092 tokenizer->status = GRN_TOKEN_CURSOR_DONE;
1093 }
1094 }
1095 #endif /* PRE_DEFINED_UNSPLIT_WORDS */
1096 if ((cl = grn_charlen_(ctx, (char *)r, (char *)e, encoding))) {
1097 n_characters++;
1098 r += cl;
1099 tokenizer->next = r;
1100 while (n_characters < tokenizer->options.unit &&
1101 (cl = grn_charlen_(ctx, (char *)r, (char *)e, encoding))) {
1102 if (cp) {
1103 LOOSE_NEED_CHECK(cp, tokenizer);
1104 if (!in_loose_blank_mode && !tokenizer->options.ignore_blank &&
1105 GRN_STR_ISBLANK(*cp)) {
1106 break;
1107 }
1108 cp++;
1109 if ((tokenizer->options.unify_alphabet &&
1110 GRN_STR_CTYPE(*cp) == GRN_CHAR_ALPHA) ||
1111 (tokenizer->options.unify_digit &&
1112 GRN_STR_CTYPE(*cp) == GRN_CHAR_DIGIT) ||
1113 (tokenizer->options.unify_symbol &&
1114 GRN_STR_CTYPE(*cp) == GRN_CHAR_SYMBOL)) {
1115 break;
1116 }
1117 }
1118 n_characters++;
1119 r += cl;
1120 }
1121 if (tokenizer->overlap) {
1122 status |= GRN_TOKEN_OVERLAP;
1123 }
1124 if (n_characters < tokenizer->options.unit) {
1125 status |= GRN_TOKEN_UNMATURED;
1126 }
1127 tokenizer->overlap = (n_characters > 1);
1128 }
1129 }
1130 tokenizer->pos = pos;
1131 tokenizer->tail = pos + n_characters - 1;
1132 if (p == r || tokenizer->next == e) {
1133 tokenizer->skip = 0;
1134 status |= GRN_TOKEN_LAST;
1135 } else {
1136 tokenizer->skip = tokenizer->overlap ? 1 : n_characters;
1137 }
1138 if (r == e) {
1139 status |= GRN_TOKEN_REACH_END;
1140 }
1141
1142 {
1143 ptrdiff_t data_size = r - p;
1144 if ((status & (GRN_TOKEN_LAST | GRN_TOKEN_REACH_END)) &&
1145 !tokenizer->loose.ing && tokenizer->loose.need) {
1146 status &= (grn_token_status)(~(GRN_TOKEN_LAST | GRN_TOKEN_REACH_END));
1147 tokenizer->loose.ing = true;
1148 tokenizer->loose.need_end_mark = true;
1149 }
1150 grn_token_set_data(ctx, token, p, (int)data_size);
1151 grn_token_set_status(ctx, token, status);
1152 grn_token_set_overlap(ctx, token, tokenizer->overlap);
1153 if (offsets) {
1154 grn_token_set_source_offset(ctx, token, offsets[0]);
1155 if (checks) {
1156 uint32_t token_length = (uint32_t)(offsets[n_characters] - offsets[0]);
1157 grn_token_set_source_length(ctx, token, token_length);
1158 int16_t source_first_character_length = 0;
1159 if (token_length > 0) {
1160 size_t source_length;
1161 const char *source =
1162 grn_tokenizer_query_get_raw_string(ctx,
1163 tokenizer->query,
1164 &source_length);
1165 int length = grn_charlen_(ctx,
1166 source + offsets[0],
1167 source + source_length,
1168 encoding);
1169 if (length > 0) {
1170 source_first_character_length = length;
1171 }
1172 }
1173 grn_token_set_source_first_character_length(
1174 ctx,
1175 token,
1176 (uint32_t)source_first_character_length);
1177 }
1178 } else {
1179 if (checks) {
1180 int16_t source_length = 0;
1181 int16_t source_first_character_length = 0;
1182 uint64_t next_offset = tokenizer->source_offset;
1183 grn_token_set_source_offset(ctx, token, tokenizer->source_offset);
1184 if (checks[0] == -1) {
1185 ssize_t n_leading_bytes = p - tokenizer->start;
1186 ssize_t i;
1187 for (i = 1; i <= n_leading_bytes; i++) {
1188 if (checks[-i] > 0) {
1189 source_length = source_first_character_length = checks[-i];
1190 if (!tokenizer->overlap) {
1191 next_offset += (uint64_t)checks[-i];
1192 }
1193 break;
1194 }
1195 }
1196 }
1197 {
1198 int16_t first_offset = 0;
1199 ptrdiff_t i;
1200 for (i = 0; i < data_size; i++) {
1201 if (checks[i] > 0) {
1202 if ((tokenizer->overlap && first_offset == 0) ||
1203 !tokenizer->overlap) {
1204 if (first_offset == 0) {
1205 first_offset = checks[i];
1206 }
1207 next_offset += (uint64_t)checks[i];
1208 }
1209 if (source_first_character_length == 0) {
1210 source_first_character_length = checks[i];
1211 }
1212 source_length += checks[i];
1213 } else if (checks[i] < 0) {
1214 if (tokenizer->overlap) {
1215 next_offset -= (uint64_t)first_offset;
1216 }
1217 }
1218 }
1219 }
1220 grn_token_set_source_length(ctx, token, (uint32_t)source_length);
1221 grn_token_set_source_first_character_length(
1222 ctx,
1223 token,
1224 (uint32_t)source_first_character_length);
1225 tokenizer->source_offset = next_offset;
1226 }
1227 }
1228 }
1229 }
1230
1231 static grn_obj *
1232 ngram_next_deprecated(grn_ctx *ctx,
1233 int nargs,
1234 grn_obj **args,
1235 grn_user_data *user_data)
1236 {
1237 grn_ngram_tokenizer *tokenizer = user_data->ptr;
1238 grn_token token;
1239 grn_obj *token_data;
1240
1241 grn_token_init(ctx, &token);
1242 ngram_next(ctx, tokenizer->query, &token, tokenizer);
1243 token_data = grn_token_get_data(ctx, &token);
1244 grn_tokenizer_token_push(ctx,
1245 &(tokenizer->token),
1246 GRN_TEXT_VALUE(token_data),
1247 (unsigned int)GRN_TEXT_LEN(token_data),
1248 grn_token_get_status(ctx, &token));
1249 grn_token_fin(ctx, &token);
1250 return NULL;
1251 }
1252
1253 static void
1254 ngram_fin(grn_ctx *ctx, void *user_data)
1255 {
1256 grn_ngram_tokenizer *tokenizer = user_data;
1257
1258 if (!tokenizer) {
1259 return;
1260 }
1261 if (tokenizer->loose.ctypes) {
1262 GRN_FREE(tokenizer->loose.ctypes);
1263 }
1264 if (tokenizer->loose.checks) {
1265 GRN_FREE(tokenizer->loose.checks);
1266 }
1267 if (tokenizer->loose.offsets) {
1268 GRN_FREE(tokenizer->loose.offsets);
1269 }
1270 GRN_OBJ_FIN(ctx, &(tokenizer->loose.text));
1271 grn_tokenizer_token_fin(ctx, &(tokenizer->token));
1272 GRN_FREE(tokenizer);
1273 }
1274
1275 static grn_obj *
1276 ngram_fin_deprecated(grn_ctx *ctx,
1277 int nargs,
1278 grn_obj **args,
1279 grn_user_data *user_data)
1280 {
1281 grn_ngram_tokenizer *tokenizer = user_data->ptr;
1282 if (tokenizer) {
1283 grn_tokenizer_query_close(ctx, tokenizer->query);
1284 ngram_fin(ctx, tokenizer);
1285 }
1286 return NULL;
1287 }
1288
1289 /* regexp tokenizer */
1290
1291 typedef struct {
1292 grn_tokenizer_token token;
1293 grn_tokenizer_query *query;
1294 struct {
1295 uint32_t n_skip_tokens;
1296 } get;
1297 bool is_begin;
1298 bool is_end;
1299 bool is_start_token;
1300 bool is_overlapping;
1301 const char *next;
1302 const char *end;
1303 unsigned int nth_char;
1304 const uint_least8_t *char_types;
1305 grn_obj buffer;
1306 } grn_regexp_tokenizer;
1307
1308 static grn_obj *
1309 regexp_init(grn_ctx *ctx, int nargs, grn_obj **args, grn_user_data *user_data)
1310 {
1311 unsigned int normalize_flags = GRN_STRING_WITH_TYPES;
1312 grn_tokenizer_query *query;
1313 grn_regexp_tokenizer *tokenizer;
1314
1315 query = grn_tokenizer_query_open(ctx, nargs, args, normalize_flags);
1316 if (!query) {
1317 return NULL;
1318 }
1319
1320 tokenizer = GRN_CALLOC(sizeof(grn_regexp_tokenizer));
1321 if (!tokenizer) {
1322 grn_tokenizer_query_close(ctx, query);
1323 ERR(GRN_NO_MEMORY_AVAILABLE,
1324 "[tokenizer][regexp] failed to allocate memory");
1325 return NULL;
1326 }
1327 user_data->ptr = tokenizer;
1328
1329 grn_tokenizer_token_init(ctx, &(tokenizer->token));
1330 tokenizer->query = query;
1331
1332 tokenizer->get.n_skip_tokens = 0;
1333
1334 tokenizer->is_begin = true;
1335 tokenizer->is_end = false;
1336 tokenizer->is_start_token = true;
1337 tokenizer->is_overlapping = false;
1338
1339 {
1340 grn_obj *string;
1341 const char *normalized;
1342 unsigned int normalized_length_in_bytes;
1343
1344 string = grn_tokenizer_query_get_normalized_string(ctx, tokenizer->query);
1345 grn_string_get_normalized(ctx,
1346 string,
1347 &normalized,
1348 &normalized_length_in_bytes,
1349 NULL);
1350 tokenizer->next = normalized;
1351 tokenizer->end = tokenizer->next + normalized_length_in_bytes;
1352 tokenizer->nth_char = 0;
1353 tokenizer->char_types = grn_string_get_types(ctx, string);
1354 }
1355
1356 GRN_TEXT_INIT(&(tokenizer->buffer), 0);
1357
1358 return NULL;
1359 }
1360
1361 static grn_obj *
1362 regexp_next(grn_ctx *ctx, int nargs, grn_obj **args, grn_user_data *user_data)
1363 {
1364 int char_len;
1365 grn_token_status status = 0;
1366 grn_regexp_tokenizer *tokenizer = user_data->ptr;
1367 uint32_t n_characters = 0;
1368 uint32_t ngram_unit = 2;
1369 grn_obj *buffer = &(tokenizer->buffer);
1370 const char *current = tokenizer->next;
1371 const char *end = tokenizer->end;
1372 const uint_least8_t *char_types = tokenizer->char_types;
1373 const grn_tokenize_mode mode =
1374 grn_tokenizer_query_get_mode(ctx, tokenizer->query);
1375 grn_encoding encoding =
1376 grn_tokenizer_query_get_encoding(ctx, tokenizer->query);
1377 bool is_begin = tokenizer->is_begin;
1378 bool is_start_token = tokenizer->is_start_token;
1379 bool break_by_blank = false;
1380 bool break_by_end_mark = false;
1381
1382 GRN_BULK_REWIND(buffer);
1383 tokenizer->is_begin = false;
1384 tokenizer->is_start_token = false;
1385
1386 if (char_types) {
1387 char_types += tokenizer->nth_char;
1388 }
1389
1390 if (mode != GRN_TOKEN_GET) {
1391 if (is_begin) {
1392 grn_tokenizer_token_push(ctx,
1393 &(tokenizer->token),
1394 GRN_TOKENIZER_BEGIN_MARK_UTF8,
1395 GRN_TOKENIZER_BEGIN_MARK_UTF8_LEN,
1396 status);
1397 return NULL;
1398 }
1399
1400 if (tokenizer->is_end) {
1401 status |= GRN_TOKEN_LAST | GRN_TOKEN_REACH_END;
1402 grn_tokenizer_token_push(ctx,
1403 &(tokenizer->token),
1404 GRN_TOKENIZER_END_MARK_UTF8,
1405 GRN_TOKENIZER_END_MARK_UTF8_LEN,
1406 status);
1407 return NULL;
1408 }
1409 if (is_start_token) {
1410 if (char_types && GRN_STR_ISBLANK(char_types[-1])) {
1411 status |= GRN_TOKEN_SKIP;
1412 grn_tokenizer_token_push(ctx, &(tokenizer->token), "", 0, status);
1413 return NULL;
1414 }
1415 }
1416 }
1417
1418 char_len = grn_charlen_(ctx, current, end, encoding);
1419 if (char_len == 0) {
1420 status |= GRN_TOKEN_LAST | GRN_TOKEN_REACH_END;
1421 grn_tokenizer_token_push(ctx, &(tokenizer->token), "", 0, status);
1422 return NULL;
1423 }
1424
1425 if (mode == GRN_TOKEN_GET) {
1426 if (is_begin && char_len == GRN_TOKENIZER_BEGIN_MARK_UTF8_LEN &&
1427 memcmp(current, GRN_TOKENIZER_BEGIN_MARK_UTF8, (size_t)char_len) == 0) {
1428 tokenizer->is_start_token = true;
1429 n_characters++;
1430 GRN_TEXT_PUT(ctx, buffer, current, char_len);
1431 current += char_len;
1432 tokenizer->next = current;
1433 tokenizer->nth_char++;
1434 if (current == end) {
1435 status |= GRN_TOKEN_LAST | GRN_TOKEN_REACH_END;
1436 }
1437 grn_tokenizer_token_push(ctx,
1438 &(tokenizer->token),
1439 GRN_TOKENIZER_BEGIN_MARK_UTF8,
1440 GRN_TOKENIZER_BEGIN_MARK_UTF8_LEN,
1441 status);
1442 return NULL;
1443 }
1444
1445 if (current + char_len == end &&
1446 char_len == GRN_TOKENIZER_END_MARK_UTF8_LEN &&
1447 memcmp(current, GRN_TOKENIZER_END_MARK_UTF8, (size_t)char_len) == 0) {
1448 status |= GRN_TOKEN_LAST | GRN_TOKEN_REACH_END;
1449 grn_tokenizer_token_push(ctx,
1450 &(tokenizer->token),
1451 GRN_TOKENIZER_END_MARK_UTF8,
1452 GRN_TOKENIZER_END_MARK_UTF8_LEN,
1453 status);
1454 return NULL;
1455 }
1456 }
1457
1458 while (true) {
1459 n_characters++;
1460 GRN_TEXT_PUT(ctx, buffer, current, char_len);
1461 current += char_len;
1462 if (n_characters == 1) {
1463 tokenizer->next = current;
1464 tokenizer->nth_char++;
1465 }
1466
1467 if (char_types) {
1468 uint_least8_t char_type;
1469 char_type = char_types[0];
1470 char_types++;
1471 if (GRN_STR_ISBLANK(char_type)) {
1472 break_by_blank = true;
1473 }
1474 }
1475
1476 char_len =
1477 grn_charlen_(ctx, (const char *)current, (const char *)end, encoding);
1478 if (char_len == 0) {
1479 break;
1480 }
1481
1482 if (mode == GRN_TOKEN_GET && current + char_len == end &&
1483 char_len == GRN_TOKENIZER_END_MARK_UTF8_LEN &&
1484 memcmp(current, GRN_TOKENIZER_END_MARK_UTF8, (size_t)char_len) == 0) {
1485 break_by_end_mark = true;
1486 }
1487
1488 if (break_by_blank || break_by_end_mark) {
1489 break;
1490 }
1491
1492 if (n_characters == ngram_unit) {
1493 break;
1494 }
1495 }
1496
1497 if (tokenizer->is_overlapping) {
1498 status |= GRN_TOKEN_OVERLAP;
1499 }
1500 if (n_characters < ngram_unit) {
1501 status |= GRN_TOKEN_UNMATURED;
1502 }
1503 tokenizer->is_overlapping = (n_characters > 1);
1504
1505 if (mode == GRN_TOKEN_GET) {
1506 if (current == end) {
1507 tokenizer->is_end = true;
1508 status |= GRN_TOKEN_LAST | GRN_TOKEN_REACH_END;
1509 if (status & GRN_TOKEN_UNMATURED) {
1510 status |= GRN_TOKEN_FORCE_PREFIX;
1511 }
1512 } else {
1513 if (break_by_blank) {
1514 tokenizer->get.n_skip_tokens = 0;
1515 tokenizer->is_start_token = true;
1516 } else if (break_by_end_mark) {
1517 if (!is_start_token && (status & GRN_TOKEN_UNMATURED)) {
1518 status |= GRN_TOKEN_SKIP;
1519 }
1520 } else if (tokenizer->get.n_skip_tokens > 0) {
1521 tokenizer->get.n_skip_tokens--;
1522 status |= GRN_TOKEN_SKIP;
1523 } else {
1524 tokenizer->get.n_skip_tokens = ngram_unit - 1;
1525 }
1526 }
1527 } else {
1528 if (tokenizer->next == end) {
1529 tokenizer->is_end = true;
1530 }
1531 if (break_by_blank) {
1532 tokenizer->is_start_token = true;
1533 }
1534 }
1535
1536 grn_tokenizer_token_push(ctx,
1537 &(tokenizer->token),
1538 GRN_TEXT_VALUE(buffer),
1539 (unsigned int)GRN_TEXT_LEN(buffer),
1540 status);
1541
1542 return NULL;
1543 }
1544
1545 static grn_obj *
1546 regexp_fin(grn_ctx *ctx, int nargs, grn_obj **args, grn_user_data *user_data)
1547 {
1548 grn_regexp_tokenizer *tokenizer = user_data->ptr;
1549 if (!tokenizer) {
1550 return NULL;
1551 }
1552 grn_tokenizer_token_fin(ctx, &(tokenizer->token));
1553 grn_tokenizer_query_close(ctx, tokenizer->query);
1554 GRN_OBJ_FIN(ctx, &(tokenizer->buffer));
1555 GRN_FREE(tokenizer);
1556 return NULL;
1557 }
1558
1559 /* pattern tokenizer */
1560
1561 typedef struct {
1562 #ifdef GRN_SUPPORT_REGEXP
1563 OnigRegex regex;
1564 #else /* GRN_SUPPORT_REGEXP */
1565 void *regex;
1566 #endif /* GRN_SUPPORT_REGEXP */
1567 } grn_pattern_options;
1568
1569 typedef struct {
1570 grn_tokenizer_token token;
1571 grn_tokenizer_query *query;
1572 grn_pattern_options *options;
1573 bool have_tokenized_delimiter;
1574 grn_encoding encoding;
1575 const unsigned char *start;
1576 const unsigned char *next;
1577 const unsigned char *end;
1578 const unsigned char *current;
1579 size_t current_length;
1580 } grn_pattern_tokenizer;
1581
1582 static void
1583 pattern_options_init(grn_pattern_options *options)
1584 {
1585 options->regex = NULL;
1586 }
1587
1588 static void *
1589 pattern_open_options(grn_ctx *ctx,
1590 grn_obj *tokenizer,
1591 grn_obj *raw_options,
1592 void *user_data)
1593 {
1594 grn_pattern_options *options;
1595 grn_obj all_patterns;
1596
1597 options = GRN_CALLOC(sizeof(grn_pattern_options));
1598 if (!options) {
1599 ERR(GRN_NO_MEMORY_AVAILABLE,
1600 "[tokenizer][pattern] "
1601 "failed to allocate memory for options");
1602 return NULL;
1603 }
1604
1605 pattern_options_init(options);
1606 GRN_TEXT_INIT(&all_patterns, 0);
1607 GRN_OPTION_VALUES_EACH_BEGIN(ctx, raw_options, i, name, name_length)
1608 {
1609 grn_raw_string name_raw;
1610 name_raw.value = name;
1611 name_raw.length = name_length;
1612
1613 if (GRN_RAW_STRING_EQUAL_CSTRING(name_raw, "pattern")) {
1614 const char *pattern;
1615 unsigned int pattern_length;
1616 grn_id domain;
1617
1618 pattern_length =
1619 grn_vector_get_element(ctx, raw_options, i, &pattern, NULL, &domain);
1620 if (grn_type_id_is_text_family(ctx, domain) && pattern_length > 0) {
1621 if (GRN_TEXT_LEN(&all_patterns) > 0) {
1622 GRN_TEXT_PUTS(ctx, &all_patterns, "|");
1623 }
1624 GRN_TEXT_PUTS(ctx, &all_patterns, "(?:");
1625 GRN_TEXT_PUT(ctx, &all_patterns, pattern, pattern_length);
1626 GRN_TEXT_PUTS(ctx, &all_patterns, ")");
1627 }
1628 }
1629 }
1630 GRN_OPTION_VALUES_EACH_END();
1631
1632 if (GRN_TEXT_LEN(&all_patterns) > 0) {
1633 #ifdef GRN_SUPPORT_REGEXP
1634 options->regex = grn_onigmo_new(ctx,
1635 GRN_TEXT_VALUE(&all_patterns),
1636 GRN_TEXT_LEN(&all_patterns),
1637 GRN_ONIGMO_OPTION_DEFAULT,
1638 GRN_ONIGMO_SYNTAX_DEFAULT,
1639 "[tokenizer][pattern]");
1640 #endif /* GRN_SUPPORT_REGEXP */
1641 }
1642 GRN_OBJ_FIN(ctx, &all_patterns);
1643
1644 return options;
1645 }
1646
1647 static void
1648 pattern_close_options(grn_ctx *ctx, void *data)
1649 {
1650 grn_pattern_options *options = data;
1651
1652 #ifdef GRN_SUPPORT_REGEXP
1653 if (options->regex) {
1654 onig_free(options->regex);
1655 }
1656 #endif /* GRN_SUPPORT_REGEXP */
1657 GRN_FREE(options);
1658 }
1659
1660 static void *
1661 pattern_init(grn_ctx *ctx, grn_tokenizer_query *query)
1662 {
1663 grn_obj *lexicon = grn_tokenizer_query_get_lexicon(ctx, query);
1664 grn_pattern_options *options;
1665 grn_pattern_tokenizer *tokenizer;
1666
1667 options = grn_table_cache_default_tokenizer_options(ctx,
1668 lexicon,
1669 pattern_open_options,
1670 pattern_close_options,
1671 NULL);
1672 if (ctx->rc != GRN_SUCCESS) {
1673 return NULL;
1674 }
1675
1676 if (!(tokenizer = GRN_CALLOC(sizeof(grn_pattern_tokenizer)))) {
1677 ERR(GRN_NO_MEMORY_AVAILABLE,
1678 "[tokenizer][pattern] "
1679 "memory allocation to grn_pattern_tokenizer failed");
1680 return NULL;
1681 }
1682
1683 tokenizer->query = query;
1684 tokenizer->options = options;
1685
1686 {
1687 const char *raw_string;
1688 size_t raw_string_length;
1689 grn_encoding encoding;
1690
1691 raw_string = grn_tokenizer_query_get_raw_string(ctx,
1692 tokenizer->query,
1693 &raw_string_length);
1694 encoding = grn_tokenizer_query_get_encoding(ctx, tokenizer->query);
1695 tokenizer->have_tokenized_delimiter =
1696 grn_tokenizer_have_tokenized_delimiter(ctx,
1697 raw_string,
1698 (unsigned int)raw_string_length,
1699 encoding);
1700 tokenizer->encoding = encoding;
1701 }
1702 {
1703 grn_obj *string;
1704 const char *normalized;
1705 unsigned int normalized_length_in_bytes;
1706
1707 string = grn_tokenizer_query_get_normalized_string(ctx, tokenizer->query);
1708 grn_string_get_normalized(ctx,
1709 string,
1710 &normalized,
1711 &normalized_length_in_bytes,
1712 NULL);
1713 tokenizer->start = (const unsigned char *)normalized;
1714 tokenizer->next = tokenizer->start;
1715 tokenizer->end = tokenizer->start + normalized_length_in_bytes;
1716 tokenizer->current = NULL;
1717 tokenizer->current_length = 0;
1718 }
1719
1720 return tokenizer;
1721 }
1722
1723 #ifdef GRN_SUPPORT_REGEXP
1724 static void
1725 pattern_search(grn_ctx *ctx, grn_pattern_tokenizer *tokenizer)
1726 {
1727 OnigPosition position;
1728 OnigRegion region;
1729
1730 onig_region_init(&region);
1731 position = onig_search(tokenizer->options->regex,
1732 tokenizer->start,
1733 tokenizer->end,
1734 tokenizer->next,
1735 tokenizer->end,
1736 &region,
1737 ONIG_OPTION_NONE);
1738 if (position == ONIG_MISMATCH) {
1739 tokenizer->current = NULL;
1740 tokenizer->current_length = 0;
1741 tokenizer->next = tokenizer->end;
1742 } else {
1743 tokenizer->current = tokenizer->start + region.beg[0];
1744 tokenizer->current_length = (size_t)(region.end[0] - region.beg[0]);
1745 tokenizer->next = tokenizer->start + region.end[0];
1746 }
1747 onig_region_free(&region, 0);
1748 }
1749 #endif
1750
1751 static void
1752 pattern_next(grn_ctx *ctx,
1753 grn_tokenizer_query *query,
1754 grn_token *token,
1755 void *user_data)
1756 {
1757 grn_pattern_tokenizer *tokenizer = user_data;
1758
1759 if (tokenizer->have_tokenized_delimiter) {
1760 ptrdiff_t rest_length;
1761 rest_length = tokenizer->end - tokenizer->next;
1762 tokenizer->next =
1763 (unsigned char *)grn_tokenizer_next_by_tokenized_delimiter(
1764 ctx,
1765 token,
1766 (const char *)tokenizer->next,
1767 (unsigned int)rest_length,
1768 tokenizer->encoding);
1769 #ifdef GRN_SUPPORT_REGEXP
1770 } else if (tokenizer->options->regex) {
1771 grn_token_status status = GRN_TOKEN_CONTINUE;
1772 if (tokenizer->next == tokenizer->start) {
1773 pattern_search(ctx, tokenizer);
1774 }
1775 grn_token_set_data(ctx,
1776 token,
1777 tokenizer->current,
1778 (int)(tokenizer->current_length));
1779 if (tokenizer->next != tokenizer->end) {
1780 pattern_search(ctx, tokenizer);
1781 }
1782 if (tokenizer->next == tokenizer->end) {
1783 status = GRN_TOKEN_LAST;
1784 }
1785 grn_token_set_status(ctx, token, status);
1786 #endif /* GRN_SUPPORT_REGEXP */
1787 } else {
1788 grn_token_set_data(ctx, token, NULL, 0);
1789 grn_token_set_status(ctx, token, GRN_TOKEN_LAST);
1790 }
1791 }
1792
1793 static void
1794 pattern_fin(grn_ctx *ctx, void *user_data)
1795 {
1796 grn_pattern_tokenizer *tokenizer = user_data;
1797
1798 if (!tokenizer) {
1799 return;
1800 }
1801 GRN_FREE(tokenizer);
1802 }
1803
1804 /* table tokenizer */
1805
1806 typedef struct {
1807 grn_obj *table;
1808 } grn_table_options;
1809
1810 typedef struct {
1811 grn_tokenizer_token token;
1812 grn_tokenizer_query *query;
1813 grn_table_options *options;
1814 bool have_tokenized_delimiter;
1815 grn_encoding encoding;
1816 const unsigned char *start;
1817 const unsigned char *current;
1818 const unsigned char *next;
1819 const unsigned char *end;
1820 grn_pat_scan_hit hits[1024];
1821 int n_hits;
1822 int current_hit;
1823 } grn_table_tokenizer;
1824
1825 static void
1826 table_options_init(grn_table_options *options)
1827 {
1828 options->table = NULL;
1829 }
1830
1831 static void *
1832 table_open_options(grn_ctx *ctx,
1833 grn_obj *tokenizer,
1834 grn_obj *raw_options,
1835 void *user_data)
1836 {
1837 grn_table_options *options;
1838
1839 options = GRN_CALLOC(sizeof(grn_table_options));
1840 if (!options) {
1841 ERR(GRN_NO_MEMORY_AVAILABLE,
1842 "[tokenizer][table] "
1843 "failed to allocate memory for options");
1844 return NULL;
1845 }
1846
1847 table_options_init(options);
1848 GRN_OPTION_VALUES_EACH_BEGIN(ctx, raw_options, i, name, name_length)
1849 {
1850 grn_raw_string name_raw;
1851 name_raw.value = name;
1852 name_raw.length = name_length;
1853
1854 if (GRN_RAW_STRING_EQUAL_CSTRING(name_raw, "table")) {
1855 const char *name;
1856 unsigned int name_length;
1857 grn_id domain;
1858
1859 name_length =
1860 grn_vector_get_element(ctx, raw_options, i, &name, NULL, &domain);
1861 if (grn_type_id_is_text_family(ctx, domain) && name_length > 0) {
1862 options->table = grn_ctx_get(ctx, name, (int)name_length);
1863 if (!options->table) {
1864 ERR(GRN_INVALID_ARGUMENT,
1865 "[tokenizer][table] nonexistent table: <%.*s>",
1866 (int)name_length,
1867 name);
1868 break;
1869 }
1870 if (options->table->header.type != GRN_TABLE_PAT_KEY) {
1871 grn_obj inspected;
1872 GRN_TEXT_INIT(&inspected, 0);
1873 grn_inspect(ctx, &inspected, options->table);
1874 ERR(GRN_INVALID_ARGUMENT,
1875 "[tokenizer][table] table must be a patricia trie table: "
1876 "<%.*s>: <%.*s>",
1877 (int)name_length,
1878 name,
1879 (int)GRN_TEXT_LEN(&inspected),
1880 GRN_TEXT_VALUE(&inspected));
1881 GRN_OBJ_FIN(ctx, &inspected);
1882 break;
1883 }
1884 }
1885 }
1886 }
1887 GRN_OPTION_VALUES_EACH_END();
1888
1889 if (ctx->rc == GRN_SUCCESS && !options->table) {
1890 ERR(GRN_INVALID_ARGUMENT, "[tokenizer][table] table isn't specified");
1891 }
1892
1893 if (ctx->rc != GRN_SUCCESS) {
1894 GRN_FREE(options);
1895 options = NULL;
1896 }
1897
1898 return options;
1899 }
1900
1901 static void
1902 table_close_options(grn_ctx *ctx, void *data)
1903 {
1904 grn_table_options *options = data;
1905 GRN_FREE(options);
1906 }
1907
1908 static void *
1909 table_init(grn_ctx *ctx, grn_tokenizer_query *query)
1910 {
1911 grn_obj *lexicon = grn_tokenizer_query_get_lexicon(ctx, query);
1912 grn_table_options *options;
1913 grn_table_tokenizer *tokenizer;
1914
1915 options = grn_table_cache_default_tokenizer_options(ctx,
1916 lexicon,
1917 table_open_options,
1918 table_close_options,
1919 NULL);
1920 if (ctx->rc != GRN_SUCCESS) {
1921 return NULL;
1922 }
1923
1924 if (!(tokenizer = GRN_CALLOC(sizeof(grn_table_tokenizer)))) {
1925 ERR(GRN_NO_MEMORY_AVAILABLE,
1926 "[tokenizer][table] "
1927 "memory allocation to grn_table_tokenizer failed");
1928 return NULL;
1929 }
1930
1931 tokenizer->query = query;
1932 tokenizer->options = options;
1933
1934 {
1935 const char *raw_string;
1936 size_t raw_string_length;
1937 grn_encoding encoding;
1938
1939 raw_string = grn_tokenizer_query_get_raw_string(ctx,
1940 tokenizer->query,
1941 &raw_string_length);
1942 encoding = grn_tokenizer_query_get_encoding(ctx, tokenizer->query);
1943 tokenizer->have_tokenized_delimiter =
1944 grn_tokenizer_have_tokenized_delimiter(ctx,
1945 raw_string,
1946 (unsigned int)raw_string_length,
1947 encoding);
1948 tokenizer->encoding = encoding;
1949 }
1950 {
1951 grn_obj *string;
1952 const char *normalized;
1953 unsigned int normalized_length_in_bytes;
1954
1955 string = grn_tokenizer_query_get_normalized_string(ctx, tokenizer->query);
1956 grn_string_get_normalized(ctx,
1957 string,
1958 &normalized,
1959 &normalized_length_in_bytes,
1960 NULL);
1961 tokenizer->start = (const unsigned char *)normalized;
1962 tokenizer->next = tokenizer->start;
1963 tokenizer->current = tokenizer->start;
1964 tokenizer->end = tokenizer->start + normalized_length_in_bytes;
1965 }
1966
1967 tokenizer->n_hits = 0;
1968 tokenizer->current_hit = -1;
1969
1970 return tokenizer;
1971 }
1972
1973 static void
1974 table_scan(grn_ctx *ctx, grn_table_tokenizer *tokenizer)
1975 {
1976 const char *rest;
1977 tokenizer->n_hits =
1978 grn_pat_scan(ctx,
1979 (grn_pat *)(tokenizer->options->table),
1980 tokenizer->next,
1981 (unsigned int)(tokenizer->end - tokenizer->next),
1982 tokenizer->hits,
1983 sizeof(tokenizer->hits) / sizeof(*(tokenizer->hits)),
1984 &rest);
1985 tokenizer->current = tokenizer->next;
1986 tokenizer->next = rest;
1987 tokenizer->current_hit = 0;
1988 }
1989
1990 static void
1991 table_next(grn_ctx *ctx,
1992 grn_tokenizer_query *query,
1993 grn_token *token,
1994 void *user_data)
1995 {
1996 grn_table_tokenizer *tokenizer = user_data;
1997
1998 if (tokenizer->have_tokenized_delimiter) {
1999 ptrdiff_t rest_length;
2000 rest_length = tokenizer->end - tokenizer->next;
2001 tokenizer->next =
2002 (unsigned char *)grn_tokenizer_next_by_tokenized_delimiter(
2003 ctx,
2004 token,
2005 (const char *)tokenizer->next,
2006 (unsigned int)rest_length,
2007 tokenizer->encoding);
2008 } else {
2009 if (tokenizer->current_hit == -1) {
2010 table_scan(ctx, tokenizer);
2011 }
2012 if (tokenizer->current_hit < tokenizer->n_hits) {
2013 grn_pat_scan_hit *hit = &(tokenizer->hits[tokenizer->current_hit]);
2014 grn_token_set_data(ctx,
2015 token,
2016 tokenizer->current + hit->offset,
2017 (int)(hit->length));
2018 tokenizer->current_hit++;
2019 if (tokenizer->current_hit == tokenizer->n_hits) {
2020 grn_token_status status = GRN_TOKEN_CONTINUE;
2021 tokenizer->current_hit = -1;
2022 if (tokenizer->next != tokenizer->end) {
2023 table_scan(ctx, tokenizer);
2024 }
2025 if (tokenizer->next == tokenizer->end) {
2026 status = GRN_TOKEN_LAST;
2027 }
2028 grn_token_set_status(ctx, token, status);
2029 } else {
2030 grn_token_set_status(ctx, token, GRN_TOKEN_CONTINUE);
2031 }
2032 } else {
2033 grn_token_set_data(ctx, token, NULL, 0);
2034 grn_token_set_status(ctx, token, GRN_TOKEN_LAST);
2035 }
2036 }
2037 }
2038
2039 static void
2040 table_fin(grn_ctx *ctx, void *user_data)
2041 {
2042 grn_table_tokenizer *tokenizer = user_data;
2043
2044 if (!tokenizer) {
2045 return;
2046 }
2047 GRN_FREE(tokenizer);
2048 }
2049
2050 /* Common code for IDF based document vector tokenizers */
2051
2052 typedef struct {
2053 grn_obj *index_column;
2054 grn_obj *df_column;
2055 bool normalize;
2056 float k1;
2057 float b;
2058 } grn_document_vector_idf_base_options;
2059
2060 typedef struct {
2061 grn_tokenizer_token token;
2062 grn_document_vector_idf_base_options *options;
2063 grn_obj token_ids;
2064 grn_obj normalized_token_ids;
2065 size_t n_token_ids;
2066 size_t token_id_offset;
2067 grn_id current_token_id;
2068 } grn_document_vector_idf_base_tokenizer;
2069
2070 typedef enum {
2071 DOCUMENT_VECTOR_IDF_BASE_ALGORITHM_TF_IDF,
2072 DOCUMENT_VECTOR_IDF_BASE_ALGORITHM_BM25,
2073 } grn_document_vector_idf_base_algorithm;
2074
2075 typedef struct {
2076 grn_document_vector_idf_base_algorithm algorithm;
2077 grn_tokenizer_query *query;
2078 grn_obj *lexicon;
2079 grn_obj *token_table;
2080 uint32_t n_documents;
2081 float average_dl;
2082 grn_hash *token_histogram;
2083 uint32_t dl;
2084 } grn_document_vector_idf_base_metadata;
2085
2086 static void
2087 document_vector_idf_base_init_tag(grn_ctx *ctx,
2088 grn_obj *tag,
2089 const char *name,
2090 grn_tokenizer_query *query)
2091 {
2092 GRN_TEXT_INIT(tag, 0);
2093 grn_text_printf(ctx, tag, "[tokenizer][%s]", name);
2094 grn_obj *index_column = grn_tokenizer_query_get_index_column(ctx, query);
2095 if (index_column) {
2096 GRN_TEXT_PUTC(ctx, tag, '[');
2097 grn_inspect_name(ctx, tag, index_column);
2098 GRN_TEXT_PUTC(ctx, tag, ']');
2099 }
2100 GRN_TEXT_PUTC(ctx, tag, '\0');
2101 }
2102
2103 static void
2104 document_vector_idf_base_options_init(
2105 grn_ctx *ctx, grn_document_vector_idf_base_options *options)
2106 {
2107 options->index_column = NULL;
2108 options->df_column = NULL;
2109 options->normalize = true;
2110 options->k1 = 2.0;
2111 options->b = 0.75;
2112 }
2113
2114 static void
2115 document_vector_idf_base_options_fin(
2116 grn_ctx *ctx, grn_document_vector_idf_base_options *options)
2117 {
2118 if (ctx->impl->db && ((grn_db *)(ctx->impl->db))->is_closing) {
2119 return;
2120 }
2121 grn_obj_unref(ctx, options->index_column);
2122 grn_obj_unref(ctx, options->df_column);
2123 }
2124
2125 static void
2126 document_vector_idf_base_options_parse(
2127 grn_ctx *ctx,
2128 grn_document_vector_idf_base_options *options,
2129 grn_obj *raw_options,
2130 grn_obj *lexicon,
2131 grn_document_vector_idf_base_algorithm algorithm,
2132 const char *tag)
2133 {
2134 GRN_OPTION_VALUES_EACH_BEGIN(ctx, raw_options, i, name, name_length)
2135 {
2136 grn_raw_string name_raw;
2137 name_raw.value = name;
2138 name_raw.length = name_length;
2139
2140 if (GRN_RAW_STRING_EQUAL_CSTRING(name_raw, "index_column")) {
2141 const char *name;
2142 unsigned int name_length;
2143 grn_id domain;
2144
2145 name_length =
2146 grn_vector_get_element(ctx, raw_options, i, &name, NULL, &domain);
2147 if (grn_type_id_is_text_family(ctx, domain) && name_length > 0) {
2148 grn_obj *source_lexicon = grn_ctx_at(ctx, lexicon->header.domain);
2149 char source_lexicon_name[GRN_TABLE_MAX_KEY_SIZE];
2150 int source_lexicon_name_length = grn_obj_name(ctx,
2151 source_lexicon,
2152 source_lexicon_name,
2153 GRN_TABLE_MAX_KEY_SIZE);
2154 if (!grn_obj_is_table_with_key(ctx, source_lexicon)) {
2155 grn_obj inspected;
2156 GRN_TEXT_INIT(&inspected, 0);
2157 grn_inspect(ctx, &inspected, source_lexicon);
2158 ERR(GRN_INVALID_ARGUMENT,
2159 "%s table's key must be a table with key: <%.*s>",
2160 tag,
2161 (int)GRN_TEXT_LEN(&inspected),
2162 GRN_TEXT_VALUE(&inspected));
2163 GRN_OBJ_FIN(ctx, &inspected);
2164 grn_obj_unref(ctx, source_lexicon);
2165 break;
2166 }
2167 options->index_column =
2168 grn_obj_column(ctx, source_lexicon, name, name_length);
2169 grn_obj_unref(ctx, source_lexicon);
2170 if (!options->index_column) {
2171 ERR(GRN_INVALID_ARGUMENT,
2172 "%s[index_column] nonexistent index column: <%.*s.%.*s>",
2173 tag,
2174 source_lexicon_name_length,
2175 source_lexicon_name,
2176 (int)name_length,
2177 name);
2178 break;
2179 }
2180 if (!grn_obj_is_index_column(ctx, options->index_column)) {
2181 grn_obj inspected;
2182 GRN_TEXT_INIT(&inspected, 0);
2183 grn_inspect(ctx, &inspected, options->index_column);
2184 ERR(GRN_INVALID_ARGUMENT,
2185 "%s[index_column] must be an index column: <%.*s.%.*s>: <%.*s>",
2186 tag,
2187 source_lexicon_name_length,
2188 source_lexicon_name,
2189 (int)name_length,
2190 name,
2191 (int)GRN_TEXT_LEN(&inspected),
2192 GRN_TEXT_VALUE(&inspected));
2193 break;
2194 }
2195 }
2196 } else if (GRN_RAW_STRING_EQUAL_CSTRING(name_raw, "df_column")) {
2197 const char *name;
2198 unsigned int name_length;
2199 grn_id domain;
2200
2201 name_length =
2202 grn_vector_get_element(ctx, raw_options, i, &name, NULL, &domain);
2203 if (grn_type_id_is_text_family(ctx, domain) && name_length > 0) {
2204 options->df_column = grn_obj_column(ctx, lexicon, name, name_length);
2205 if (!options->df_column) {
2206 ERR(GRN_INVALID_ARGUMENT,
2207 "%s[df_column] nonexistent document frequency column: <%.*s>",
2208 tag,
2209 (int)name_length,
2210 name);
2211 break;
2212 }
2213 if (!(grn_obj_is_scalar_column(ctx, options->df_column) &&
2214 options->df_column->header.domain != GRN_DB_UINT32)) {
2215 grn_obj inspected;
2216 GRN_TEXT_INIT(&inspected, 0);
2217 grn_inspect(ctx, &inspected, options->df_column);
2218 ERR(GRN_INVALID_ARGUMENT,
2219 "%s[df_column] must be an UInt32 scalar column: <%.*s>: <%.*s>",
2220 tag,
2221 (int)name_length,
2222 name,
2223 (int)GRN_TEXT_LEN(&inspected),
2224 GRN_TEXT_VALUE(&inspected));
2225 GRN_OBJ_FIN(ctx, &inspected);
2226 break;
2227 }
2228 }
2229 } else if (GRN_RAW_STRING_EQUAL_CSTRING(name_raw, "normalize")) {
2230 options->normalize =
2231 grn_vector_get_element_bool(ctx, raw_options, i, options->normalize);
2232 } else {
2233 if (algorithm == DOCUMENT_VECTOR_IDF_BASE_ALGORITHM_BM25) {
2234 if (GRN_RAW_STRING_EQUAL_CSTRING(name_raw, "k1")) {
2235 options->k1 =
2236 grn_vector_get_element_float32(ctx, raw_options, i, options->k1);
2237 } else if (GRN_RAW_STRING_EQUAL_CSTRING(name_raw, "b")) {
2238 options->b =
2239 grn_vector_get_element_float32(ctx, raw_options, i, options->b);
2240 }
2241 }
2242 }
2243 }
2244 GRN_OPTION_VALUES_EACH_END();
2245
2246 if (ctx->rc == GRN_SUCCESS) {
2247 if (!options->index_column) {
2248 ERR(GRN_INVALID_ARGUMENT, "%s[index_column] missing", tag);
2249 } else if (!options->df_column) {
2250 ERR(GRN_INVALID_ARGUMENT, "%s[df_column] missing", tag);
2251 }
2252 }
2253 }
2254
2255 static void
2256 document_vector_idf_base_tokenizer_init(
2257 grn_ctx *ctx,
2258 grn_document_vector_idf_base_tokenizer *tokenizer,
2259 grn_document_vector_idf_base_options *options)
2260 {
2261 tokenizer->options = options;
2262
2263 GRN_RECORD_INIT(&(tokenizer->token_ids),
2264 GRN_OBJ_VECTOR,
2265 tokenizer->options->index_column->header.domain);
2266 tokenizer->token_ids.header.flags |= GRN_OBJ_WITH_WEIGHT;
2267
2268 GRN_RECORD_INIT(&(tokenizer->normalized_token_ids),
2269 GRN_OBJ_VECTOR,
2270 tokenizer->options->index_column->header.domain);
2271 tokenizer->normalized_token_ids.header.flags |= GRN_OBJ_WITH_WEIGHT;
2272
2273 tokenizer->n_token_ids = 0;
2274 tokenizer->token_id_offset = 0;
2275 tokenizer->current_token_id = GRN_ID_NIL;
2276 }
2277
2278 static void
2279 document_vector_idf_base_tokenizer_fin(
2280 grn_ctx *ctx, grn_document_vector_idf_base_tokenizer *tokenizer)
2281 {
2282 GRN_OBJ_FIN(ctx, &(tokenizer->token_ids));
2283 GRN_OBJ_FIN(ctx, &(tokenizer->normalized_token_ids));
2284 }
2285
2286 static void
2287 document_vector_idf_base_tokenizer_init_metadata(
2288 grn_ctx *ctx,
2289 grn_document_vector_idf_base_tokenizer *tokenizer,
2290 grn_document_vector_idf_base_metadata *metadata)
2291 {
2292 const char *metadata_option_name = "metadata";
2293
2294 metadata->token_table =
2295 grn_ctx_at(ctx, tokenizer->options->index_column->header.domain);
2296 metadata->token_histogram = NULL;
2297 metadata->dl = 0;
2298
2299 if (grn_table_size(ctx, metadata->lexicon) > 0) {
2300 grn_obj metadata_vector;
2301 GRN_VOID_INIT(&metadata_vector);
2302 grn_obj_get_option_values(ctx,
2303 metadata->lexicon,
2304 metadata_option_name,
2305 -1,
2306 GRN_OPTION_REVISION_NONE,
2307 &metadata_vector);
2308 size_t n_metadata = grn_vector_size(ctx, &metadata_vector);
2309
2310 metadata->n_documents = 0;
2311 if (n_metadata > 0) {
2312 metadata->n_documents =
2313 grn_vector_get_element_uint32(ctx, &metadata_vector, 0, 0);
2314 }
2315
2316 if (metadata->algorithm == DOCUMENT_VECTOR_IDF_BASE_ALGORITHM_BM25) {
2317 metadata->average_dl = 0;
2318 if (n_metadata > 1) {
2319 metadata->average_dl =
2320 grn_vector_get_element_float32(ctx, &metadata_vector, 1, 0);
2321 }
2322 }
2323
2324 GRN_OBJ_FIN(ctx, &metadata_vector);
2325
2326 return;
2327 }
2328
2329 grn_ii *ii = (grn_ii *)(tokenizer->options->index_column);
2330 uint64_t total_tf = 0;
2331 grn_obj *lexicon = metadata->lexicon;
2332 grn_obj *df_column = tokenizer->options->df_column;
2333 grn_obj df_value;
2334 GRN_UINT32_INIT(&df_value, 0);
2335 GRN_TABLE_EACH_BEGIN(ctx, metadata->token_table, cursor, source_id)
2336 {
2337 grn_id id = grn_table_add(ctx,
2338 lexicon,
2339 (const char *)&source_id,
2340 sizeof(grn_id),
2341 NULL);
2342 uint32_t df = 0;
2343 grn_ii_cursor *ii_cursor = grn_ii_cursor_open(ctx,
2344 ii,
2345 source_id,
2346 GRN_ID_NIL,
2347 GRN_ID_MAX,
2348 (int)(ii->n_elements),
2349 0);
2350 if (ii_cursor) {
2351 while (grn_ii_cursor_next(ctx, ii_cursor)) {
2352 df++;
2353 if (metadata->algorithm == DOCUMENT_VECTOR_IDF_BASE_ALGORITHM_BM25) {
2354 while (grn_ii_cursor_next_pos(ctx, ii_cursor)) {
2355 total_tf++;
2356 }
2357 }
2358 }
2359 grn_ii_cursor_close(ctx, ii_cursor);
2360 }
2361 GRN_UINT32_SET(ctx, &df_value, df);
2362 grn_obj_set_value(ctx, df_column, id, &df_value, GRN_OBJ_SET);
2363 }
2364 GRN_TABLE_EACH_END(ctx, cursor);
2365 GRN_OBJ_FIN(ctx, &df_value);
2366
2367 {
2368 const grn_id source_id =
2369 grn_obj_get_range(ctx, tokenizer->options->index_column);
2370 grn_obj *source = grn_ctx_at(ctx, source_id);
2371 metadata->n_documents = grn_table_size(ctx, source);
2372 grn_obj_unref(ctx, source);
2373
2374 grn_obj metadata_vector;
2375 GRN_VOID_INIT(&metadata_vector);
2376 grn_obj_ensure_vector(ctx, &metadata_vector);
2377 grn_vector_add_element(ctx,
2378 &metadata_vector,
2379 (const char *)&(metadata->n_documents),
2380 sizeof(uint32_t),
2381 0,
2382 GRN_DB_UINT32);
2383 if (metadata->algorithm == DOCUMENT_VECTOR_IDF_BASE_ALGORITHM_BM25) {
2384 metadata->average_dl = 0.0;
2385 if (metadata->n_documents > 0) {
2386 metadata->average_dl = (float)total_tf / (float)(metadata->n_documents);
2387 }
2388 grn_vector_add_element(ctx,
2389 &metadata_vector,
2390 (const char *)&(metadata->average_dl),
2391 sizeof(float),
2392 0,
2393 GRN_DB_FLOAT32);
2394 }
2395 grn_obj_set_option_values(ctx,
2396 lexicon,
2397 metadata_option_name,
2398 -1,
2399 &metadata_vector);
2400 GRN_OBJ_FIN(ctx, &metadata_vector);
2401 }
2402 }
2403
2404 static void
2405 document_vector_idf_base_tokenizer_fin_metadata(
2406 grn_ctx *ctx,
2407 grn_document_vector_idf_base_tokenizer *tokenizer,
2408 grn_document_vector_idf_base_metadata *metadata)
2409 {
2410 if (metadata->token_histogram) {
2411 grn_hash_close(ctx, metadata->token_histogram);
2412 }
2413 grn_obj_unref(ctx, metadata->token_table);
2414 }
2415
2416 static bool
2417 document_vector_idf_base_tokenizer_init_token_ids_token_column(
2418 grn_ctx *ctx,
2419 grn_document_vector_idf_base_tokenizer *tokenizer,
2420 grn_document_vector_idf_base_metadata *metadata,
2421 const char *tag)
2422 {
2423 grn_obj *source_column =
2424 grn_tokenizer_query_get_source_column(ctx, metadata->query);
2425 if (!source_column) {
2426 return false;
2427 }
2428 grn_id source_id = grn_tokenizer_query_get_source_id(ctx, metadata->query);
2429 if (source_id == GRN_ID_NIL) {
2430 return false;
2431 }
2432 grn_obj *index_column =
2433 grn_tokenizer_query_get_index_column(ctx, metadata->query);
2434 if (!index_column) {
2435 return false;
2436 }
2437
2438 grn_obj all_hooked_columns;
2439 GRN_PTR_INIT(&all_hooked_columns, GRN_OBJ_VECTOR, GRN_ID_NIL);
2440 grn_column_get_all_hooked_columns(ctx, source_column, &all_hooked_columns);
2441 if (ctx->rc != GRN_SUCCESS) {
2442 GRN_OBJ_FIN(ctx, &all_hooked_columns);
2443 return false;
2444 }
2445
2446 grn_obj *token_column = NULL;
2447 size_t n_hooked_columns = GRN_PTR_VECTOR_SIZE(&all_hooked_columns);
2448 size_t i;
2449 bool collected = false;
2450 for (i = 0; i < n_hooked_columns; i++) {
2451 grn_obj *column = GRN_PTR_VALUE_AT(&all_hooked_columns, i);
2452 if (column == index_column) {
2453 break;
2454 }
2455 if (grn_obj_is_token_column(ctx, column) &&
2456 (grn_obj_get_range(ctx, column) ==
2457 tokenizer->options->index_column->header.domain)) {
2458 token_column = column;
2459 break;
2460 }
2461 }
2462
2463 if (!token_column) {
2464 goto exit;
2465 }
2466
2467 if (grn_logger_pass(ctx, GRN_LOG_DEBUG)) {
2468 grn_obj message;
2469 GRN_TEXT_INIT(&message, 0);
2470 GRN_TEXT_PUTC(ctx, &message, '<');
2471 grn_inspect_name(ctx, &message, token_column);
2472 GRN_TEXT_PUTC(ctx, &message, '>');
2473 GRN_LOG(ctx,
2474 GRN_LOG_DEBUG,
2475 "%s[tokenize][token-column] %.*s",
2476 tag,
2477 (int)GRN_TEXT_LEN(&message),
2478 GRN_TEXT_VALUE(&message));
2479 GRN_OBJ_FIN(ctx, &message);
2480 }
2481 grn_obj tokens;
2482 GRN_RECORD_INIT(&tokens,
2483 GRN_OBJ_VECTOR,
2484 grn_obj_get_range(ctx, token_column));
2485 grn_obj_get_value(ctx, token_column, source_id, &tokens);
2486 metadata->dl = 0;
2487 size_t j;
2488 size_t n_tokens = GRN_RECORD_VECTOR_SIZE(&tokens);
2489 for (j = 0; j < n_tokens; j++) {
2490 grn_id token_id = GRN_RECORD_VALUE_AT(&tokens, j);
2491 if (token_id == GRN_ID_NIL) {
2492 continue;
2493 }
2494
2495 metadata->dl++;
2496
2497 void *value;
2498 grn_id id = grn_hash_add(ctx,
2499 metadata->token_histogram,
2500 &token_id,
2501 sizeof(grn_id),
2502 &value,
2503 NULL);
2504 if (id == GRN_ID_NIL) {
2505 continue;
2506 }
2507 uint32_t *tf = value;
2508 *tf += 1;
2509 }
2510 GRN_OBJ_FIN(ctx, &tokens);
2511 collected = true;
2512
2513 exit:
2514 for (i = 0; i < n_hooked_columns; i++) {
2515 grn_obj *column = GRN_PTR_VALUE_AT(&all_hooked_columns, i);
2516 grn_obj_unref(ctx, column);
2517 }
2518 GRN_OBJ_FIN(ctx, &all_hooked_columns);
2519
2520 return collected;
2521 }
2522
2523 static bool
2524 document_vector_idf_base_tokenizer_init_token_ids_token_cursor(
2525 grn_ctx *ctx,
2526 grn_document_vector_idf_base_tokenizer *tokenizer,
2527 grn_document_vector_idf_base_metadata *metadata,
2528 const char *tag)
2529 {
2530 const char *raw_string;
2531 size_t raw_string_length;
2532 raw_string = grn_tokenizer_query_get_raw_string(ctx,
2533 metadata->query,
2534 &raw_string_length);
2535 grn_token_cursor *token_cursor = grn_token_cursor_open(ctx,
2536 metadata->token_table,
2537 raw_string,
2538 raw_string_length,
2539 GRN_TOKENIZE_ADD,
2540 0);
2541 if (!token_cursor) {
2542 ERR(GRN_NO_MEMORY_AVAILABLE, "%s failed to create token cursor", tag);
2543 return false;
2544 }
2545
2546 metadata->dl = 0;
2547 while (grn_token_cursor_get_status(ctx, token_cursor) ==
2548 GRN_TOKEN_CURSOR_DOING) {
2549 grn_id token_id = grn_token_cursor_next(ctx, token_cursor);
2550 if (token_id == GRN_ID_NIL) {
2551 continue;
2552 }
2553
2554 metadata->dl++;
2555
2556 void *value;
2557 grn_id id = grn_hash_add(ctx,
2558 metadata->token_histogram,
2559 &token_id,
2560 sizeof(grn_id),
2561 &value,
2562 NULL);
2563 if (id == GRN_ID_NIL) {
2564 continue;
2565 }
2566 uint32_t *tf = value;
2567 *tf += 1;
2568 }
2569 grn_token_cursor_close(ctx, token_cursor);
2570
2571 return true;
2572 }
2573
2574 static void
2575 document_vector_idf_base_tokenizer_init_token_ids(
2576 grn_ctx *ctx,
2577 grn_document_vector_idf_base_tokenizer *tokenizer,
2578 grn_document_vector_idf_base_metadata *metadata,
2579 const char *tag)
2580 {
2581 metadata->token_histogram =
2582 grn_hash_create(ctx,
2583 NULL,
2584 sizeof(grn_id),
2585 sizeof(uint32_t),
2586 GRN_OBJ_TABLE_HASH_KEY | GRN_HASH_TINY);
2587 if (!metadata->token_histogram) {
2588 ERR(GRN_NO_MEMORY_AVAILABLE, "%s failed to create token histogram", tag);
2589 return;
2590 }
2591
2592 if (!document_vector_idf_base_tokenizer_init_token_ids_token_column(ctx,
2593 tokenizer,
2594 metadata,
2595 tag)) {
2596 if (!document_vector_idf_base_tokenizer_init_token_ids_token_cursor(
2597 ctx,
2598 tokenizer,
2599 metadata,
2600 tag)) {
2601 return;
2602 }
2603 }
2604
2605 {
2606 grn_obj *lexicon = metadata->lexicon;
2607 grn_obj *df_column = tokenizer->options->df_column;
2608 grn_obj df_value;
2609 GRN_UINT32_INIT(&df_value, 0);
2610 GRN_HASH_EACH_BEGIN(ctx,
2611 metadata->token_histogram,
2612 cursor,
2613 token_histogram_id)
2614 {
2615 void *key;
2616 unsigned int key_size;
2617 void *value;
2618 grn_hash_cursor_get_key_value(ctx, cursor, &key, &key_size, &value);
2619 const grn_id token_id = *((grn_id *)key);
2620 const uint32_t tf = *((uint32_t *)value);
2621 const grn_id id = grn_table_get(ctx, lexicon, &token_id, sizeof(grn_id));
2622 GRN_BULK_REWIND(&df_value);
2623 grn_obj_get_value(ctx, df_column, id, &df_value);
2624 uint32_t df = 0;
2625 if (GRN_BULK_VSIZE(&df_value) > 0) {
2626 df = GRN_UINT32_VALUE(&df_value);
2627 }
2628 if (df == 0) {
2629 continue;
2630 }
2631 float weight;
2632 if (metadata->algorithm == DOCUMENT_VECTOR_IDF_BASE_ALGORITHM_TF_IDF) {
2633 /* Use the same formula as gensim:
2634 * https://radimrehurek.com/gensim/models/tfidfmodel.html */
2635 const float tf_idf =
2636 (float)tf * log2f((float)(metadata->n_documents) / (float)df);
2637 weight = tf_idf;
2638 } else {
2639 /* Use the formula in Wikipedia:
2640 * https://en.wikipedia.org/wiki/Okapi_BM25 */
2641 const float idf =
2642 logf((((float)(metadata->n_documents) - (float)df + (float)0.5) /
2643 ((float)df + (float)0.5)) +
2644 1);
2645 const float k1 = tokenizer->options->k1;
2646 const float b = tokenizer->options->b;
2647 const float bm25 =
2648 idf *
2649 (((float)tf * (k1 + 1)) /
2650 ((float)tf +
2651 k1 * (1 - b + b * ((float)(metadata->dl) / metadata->average_dl))));
2652 weight = bm25;
2653 }
2654 grn_uvector_add_element_record(ctx, &(tokenizer->token_ids), id, weight);
2655 }
2656 GRN_HASH_EACH_END(ctx, cursor);
2657 GRN_OBJ_FIN(ctx, &df_value);
2658 }
2659
2660 tokenizer->n_token_ids = grn_uvector_size(ctx, &(tokenizer->token_ids));
2661
2662 if (tokenizer->options->normalize && tokenizer->n_token_ids > 0) {
2663 float l2_norm = 0.0;
2664 size_t i;
2665 for (i = 0; i < tokenizer->n_token_ids; i++) {
2666 float weight = 0.0;
2667 grn_uvector_get_element_record(ctx,
2668 &(tokenizer->token_ids),
2669 (uint32_t)i,
2670 &weight);
2671 l2_norm += weight * weight;
2672 }
2673 l2_norm = sqrtf(l2_norm);
2674 for (i = 0; i < tokenizer->n_token_ids; i++) {
2675 float weight = 0.0;
2676 grn_id id = grn_uvector_get_element_record(ctx,
2677 &(tokenizer->token_ids),
2678 (uint32_t)i,
2679 &weight);
2680 grn_uvector_add_element_record(ctx,
2681 &(tokenizer->normalized_token_ids),
2682 id,
2683 weight / l2_norm);
2684 }
2685 }
2686 }
2687
2688 static void
2689 document_vector_idf_base_tokenizer_next(
2690 grn_ctx *ctx,
2691 grn_document_vector_idf_base_tokenizer *tokenizer,
2692 grn_token *token)
2693 {
2694 if (tokenizer->n_token_ids == 0) {
2695 grn_token_set_data(ctx, token, NULL, 0);
2696 grn_token_set_status(ctx, token, GRN_TOKEN_LAST);
2697 return;
2698 }
2699
2700 grn_obj *token_ids;
2701 if (tokenizer->options->normalize) {
2702 token_ids = &(tokenizer->normalized_token_ids);
2703 } else {
2704 token_ids = &(tokenizer->token_ids);
2705 }
2706 float weight = 0.0;
2707 tokenizer->current_token_id =
2708 grn_uvector_get_element_record(ctx,
2709 token_ids,
2710 (uint32_t)(tokenizer->token_id_offset),
2711 &weight);
2712 grn_token_set_data(ctx,
2713 token,
2714 (const char *)(&(tokenizer->current_token_id)),
2715 sizeof(grn_id));
2716 grn_token_set_weight(ctx, token, weight);
2717
2718 if (tokenizer->token_id_offset == tokenizer->n_token_ids - 1) {
2719 grn_token_set_status(ctx, token, GRN_TOKEN_LAST);
2720 } else {
2721 grn_token_set_status(ctx, token, GRN_TOKEN_CONTINUE);
2722 }
2723
2724 tokenizer->token_id_offset++;
2725 }
2726
2727 /* document vector TF/IDF tokenizer */
2728
2729 static void
2730 document_vector_tf_idf_close_options(grn_ctx *ctx, void *data)
2731 {
2732 grn_document_vector_idf_base_options *options = data;
2733 document_vector_idf_base_options_fin(ctx, options);
2734 GRN_FREE(options);
2735 }
2736
2737 static void *
2738 document_vector_tf_idf_open_options(grn_ctx *ctx,
2739 grn_obj *tokenizer,
2740 grn_obj *raw_options,
2741 void *user_data)
2742 {
2743 grn_tokenizer_query *query = user_data;
2744 grn_obj *lexicon = grn_tokenizer_query_get_lexicon(ctx, query);
2745 grn_obj tag;
2746 document_vector_idf_base_init_tag(ctx, &tag, "document-vector-tf-idf", query);
2747
2748 grn_document_vector_idf_base_options *options;
2749 options = GRN_CALLOC(sizeof(grn_document_vector_idf_base_options));
2750 if (!options) {
2751 ERR(GRN_NO_MEMORY_AVAILABLE,
2752 "%s failed to allocate memory for options",
2753 GRN_TEXT_VALUE(&tag));
2754 GRN_OBJ_FIN(ctx, &tag);
2755 return NULL;
2756 }
2757
2758 document_vector_idf_base_options_init(ctx, options);
2759 document_vector_idf_base_options_parse(
2760 ctx,
2761 options,
2762 raw_options,
2763 lexicon,
2764 DOCUMENT_VECTOR_IDF_BASE_ALGORITHM_TF_IDF,
2765 GRN_TEXT_VALUE(&tag));
2766 GRN_OBJ_FIN(ctx, &tag);
2767
2768 if (ctx->rc != GRN_SUCCESS) {
2769 document_vector_tf_idf_close_options(ctx, options);
2770 options = NULL;
2771 }
2772
2773 return options;
2774 }
2775
2776 static void
2777 document_vector_tf_idf_fin(grn_ctx *ctx, void *user_data)
2778 {
2779 grn_document_vector_idf_base_tokenizer *tokenizer = user_data;
2780
2781 if (!tokenizer) {
2782 return;
2783 }
2784
2785 document_vector_idf_base_tokenizer_fin(ctx, tokenizer);
2786 GRN_FREE(tokenizer);
2787 }
2788
2789 static void *
2790 document_vector_tf_idf_init(grn_ctx *ctx, grn_tokenizer_query *query)
2791 {
2792 grn_obj *lexicon = grn_tokenizer_query_get_lexicon(ctx, query);
2793 grn_document_vector_idf_base_options *options =
2794 grn_table_cache_default_tokenizer_options(
2795 ctx,
2796 lexicon,
2797 document_vector_tf_idf_open_options,
2798 document_vector_tf_idf_close_options,
2799 query);
2800 if (ctx->rc != GRN_SUCCESS) {
2801 return NULL;
2802 }
2803
2804 grn_obj tag;
2805 document_vector_idf_base_init_tag(ctx, &tag, "document-vector-tf-idf", query);
2806 grn_document_vector_idf_base_tokenizer *tokenizer =
2807 GRN_CALLOC(sizeof(grn_document_vector_idf_base_tokenizer));
2808 if (!tokenizer) {
2809 ERR(GRN_NO_MEMORY_AVAILABLE,
2810 "%s failed to allocate tokenizer",
2811 GRN_TEXT_VALUE(&tag));
2812 GRN_OBJ_FIN(ctx, &tag);
2813 return NULL;
2814 }
2815
2816 document_vector_idf_base_tokenizer_init(ctx, tokenizer, options);
2817
2818 grn_document_vector_idf_base_metadata metadata;
2819 metadata.algorithm = DOCUMENT_VECTOR_IDF_BASE_ALGORITHM_TF_IDF;
2820 metadata.query = query;
2821 metadata.lexicon = lexicon;
2822 document_vector_idf_base_tokenizer_init_metadata(ctx, tokenizer, &metadata);
2823 if (ctx->rc != GRN_SUCCESS) {
2824 document_vector_idf_base_tokenizer_fin_metadata(ctx, tokenizer, &metadata);
2825 document_vector_tf_idf_fin(ctx, tokenizer);
2826 GRN_OBJ_FIN(ctx, &tag);
2827 return NULL;
2828 }
2829
2830 if (metadata.n_documents == 0) {
2831 document_vector_idf_base_tokenizer_fin_metadata(ctx, tokenizer, &metadata);
2832 GRN_OBJ_FIN(ctx, &tag);
2833 return tokenizer;
2834 }
2835
2836 document_vector_idf_base_tokenizer_init_token_ids(ctx,
2837 tokenizer,
2838 &metadata,
2839 GRN_TEXT_VALUE(&tag));
2840 GRN_OBJ_FIN(ctx, &tag);
2841
2842 document_vector_idf_base_tokenizer_fin_metadata(ctx, tokenizer, &metadata);
2843 if (ctx->rc != GRN_SUCCESS) {
2844 document_vector_tf_idf_fin(ctx, tokenizer);
2845 return NULL;
2846 }
2847
2848 return tokenizer;
2849 }
2850
2851 static void
2852 document_vector_tf_idf_next(grn_ctx *ctx,
2853 grn_tokenizer_query *query,
2854 grn_token *token,
2855 void *user_data)
2856 {
2857 grn_document_vector_idf_base_tokenizer *tokenizer = user_data;
2858 document_vector_idf_base_tokenizer_next(ctx, tokenizer, token);
2859 }
2860
2861 /* document vector BM25 tokenizer */
2862
2863 static void
2864 document_vector_bm25_close_options(grn_ctx *ctx, void *data)
2865 {
2866 grn_document_vector_idf_base_options *options = data;
2867 document_vector_idf_base_options_fin(ctx, options);
2868 GRN_FREE(options);
2869 }
2870
2871 static void *
2872 document_vector_bm25_open_options(grn_ctx *ctx,
2873 grn_obj *tokenizer,
2874 grn_obj *raw_options,
2875 void *user_data)
2876 {
2877 grn_tokenizer_query *query = user_data;
2878 grn_obj *lexicon = grn_tokenizer_query_get_lexicon(ctx, query);
2879 grn_obj tag;
2880 document_vector_idf_base_init_tag(ctx, &tag, "document-vector-bm25", query);
2881
2882 grn_document_vector_idf_base_options *options =
2883 GRN_CALLOC(sizeof(grn_document_vector_idf_base_options));
2884 if (!options) {
2885 ERR(GRN_NO_MEMORY_AVAILABLE,
2886 "%s failed to allocate memory for options",
2887 GRN_TEXT_VALUE(&tag));
2888 GRN_OBJ_FIN(ctx, &tag);
2889 return NULL;
2890 }
2891
2892 document_vector_idf_base_options_init(ctx, options);
2893 document_vector_idf_base_options_parse(
2894 ctx,
2895 options,
2896 raw_options,
2897 lexicon,
2898 DOCUMENT_VECTOR_IDF_BASE_ALGORITHM_BM25,
2899 GRN_TEXT_VALUE(&tag));
2900 GRN_OBJ_FIN(ctx, &tag);
2901
2902 if (ctx->rc != GRN_SUCCESS) {
2903 document_vector_tf_idf_close_options(ctx, options);
2904 options = NULL;
2905 }
2906
2907 return options;
2908 }
2909
2910 static void
2911 document_vector_bm25_fin(grn_ctx *ctx, void *user_data)
2912 {
2913 grn_document_vector_idf_base_tokenizer *tokenizer = user_data;
2914
2915 if (!tokenizer) {
2916 return;
2917 }
2918
2919 document_vector_idf_base_tokenizer_fin(ctx, tokenizer);
2920 GRN_FREE(tokenizer);
2921 }
2922
2923 static void *
2924 document_vector_bm25_init(grn_ctx *ctx, grn_tokenizer_query *query)
2925 {
2926 grn_obj *lexicon = grn_tokenizer_query_get_lexicon(ctx, query);
2927 grn_document_vector_idf_base_options *options =
2928 grn_table_cache_default_tokenizer_options(
2929 ctx,
2930 lexicon,
2931 document_vector_bm25_open_options,
2932 document_vector_bm25_close_options,
2933 query);
2934 if (ctx->rc != GRN_SUCCESS) {
2935 return NULL;
2936 }
2937
2938 grn_obj tag;
2939 document_vector_idf_base_init_tag(ctx, &tag, "document-vector-bm25", query);
2940 grn_document_vector_idf_base_tokenizer *tokenizer =
2941 GRN_CALLOC(sizeof(grn_document_vector_idf_base_tokenizer));
2942 if (!tokenizer) {
2943 ERR(GRN_NO_MEMORY_AVAILABLE,
2944 "%s failed to allocate tokenizer",
2945 GRN_TEXT_VALUE(&tag));
2946 GRN_OBJ_FIN(ctx, &tag);
2947 return NULL;
2948 }
2949
2950 document_vector_idf_base_tokenizer_init(ctx, tokenizer, options);
2951
2952 grn_document_vector_idf_base_metadata metadata;
2953 metadata.algorithm = DOCUMENT_VECTOR_IDF_BASE_ALGORITHM_BM25;
2954 metadata.query = query;
2955 metadata.lexicon = lexicon;
2956 document_vector_idf_base_tokenizer_init_metadata(ctx, tokenizer, &metadata);
2957 if (ctx->rc != GRN_SUCCESS) {
2958 document_vector_idf_base_tokenizer_fin_metadata(ctx, tokenizer, &metadata);
2959 document_vector_bm25_fin(ctx, tokenizer);
2960 GRN_OBJ_FIN(ctx, &tag);
2961 return NULL;
2962 }
2963
2964 if (metadata.n_documents == 0) {
2965 document_vector_idf_base_tokenizer_fin_metadata(ctx, tokenizer, &metadata);
2966 GRN_OBJ_FIN(ctx, &tag);
2967 return tokenizer;
2968 }
2969
2970 document_vector_idf_base_tokenizer_init_token_ids(ctx,
2971 tokenizer,
2972 &metadata,
2973 GRN_TEXT_VALUE(&tag));
2974 GRN_OBJ_FIN(ctx, &tag);
2975
2976 document_vector_idf_base_tokenizer_fin_metadata(ctx, tokenizer, &metadata);
2977 if (ctx->rc != GRN_SUCCESS) {
2978 document_vector_bm25_fin(ctx, tokenizer);
2979 return NULL;
2980 }
2981
2982 return tokenizer;
2983 }
2984
2985 static void
2986 document_vector_bm25_next(grn_ctx *ctx,
2987 grn_tokenizer_query *query,
2988 grn_token *token,
2989 void *user_data)
2990 {
2991 grn_document_vector_idf_base_tokenizer *tokenizer = user_data;
2992 document_vector_idf_base_tokenizer_next(ctx, tokenizer, token);
2993 }
2994
2995 /* external */
2996
2997 grn_rc
2998 grn_tokenizers_init(void)
2999 {
3000 static grn_proc _grn_tokenizer_uvector;
3001 _grn_tokenizer_uvector.obj.db = NULL;
3002 _grn_tokenizer_uvector.obj.id = GRN_ID_NIL;
3003 _grn_tokenizer_uvector.obj.header.domain = GRN_ID_NIL;
3004 _grn_tokenizer_uvector.obj.range = GRN_ID_NIL;
3005 _grn_tokenizer_uvector.funcs[PROC_INIT] = uvector_init;
3006 _grn_tokenizer_uvector.funcs[PROC_NEXT] = uvector_next;
3007 _grn_tokenizer_uvector.funcs[PROC_FIN] = uvector_fin;
3008 grn_tokenizer_uvector = (grn_obj *)&_grn_tokenizer_uvector;
3009 return GRN_SUCCESS;
3010 }
3011
3012 grn_rc
3013 grn_tokenizers_fin(void)
3014 {
3015 return GRN_SUCCESS;
3016 }
3017
3018 grn_rc
3019 grn_db_init_mecab_tokenizer(grn_ctx *ctx)
3020 {
3021 switch (GRN_CTX_GET_ENCODING(ctx)) {
3022 case GRN_ENC_EUC_JP:
3023 case GRN_ENC_UTF8:
3024 case GRN_ENC_SJIS:
3025 #if defined(GRN_EMBEDDED) && defined(GRN_WITH_MECAB)
3026 {
3027 GRN_PLUGIN_DECLARE_FUNCTIONS(tokenizers_mecab);
3028 grn_rc rc;
3029 rc = GRN_PLUGIN_IMPL_NAME_TAGGED(init, tokenizers_mecab)(ctx);
3030 if (rc == GRN_SUCCESS) {
3031 rc = GRN_PLUGIN_IMPL_NAME_TAGGED(register, tokenizers_mecab)(ctx);
3032 if (rc != GRN_SUCCESS) {
3033 GRN_PLUGIN_IMPL_NAME_TAGGED(fin, tokenizers_mecab)(ctx);
3034 }
3035 }
3036 return rc;
3037 }
3038 #else /* defined(GRN_EMBEDDED) && defined(GRN_WITH_MECAB) */
3039 {
3040 const char *mecab_plugin_name = "tokenizers/mecab";
3041 char *path;
3042 path = grn_plugin_find_path(ctx, mecab_plugin_name);
3043 if (path) {
3044 GRN_FREE(path);
3045 return grn_plugin_register(ctx, mecab_plugin_name);
3046 } else {
3047 return GRN_NO_SUCH_FILE_OR_DIRECTORY;
3048 }
3049 }
3050 #endif /* defined(GRN_EMBEDDED) && defined(GRN_WITH_MECAB) */
3051 break;
3052 default:
3053 return GRN_OPERATION_NOT_SUPPORTED;
3054 }
3055 }
3056
3057 #define DEF_TOKENIZER(name, init, next, fin, vars) \
3058 (grn_proc_create(ctx, \
3059 (name), \
3060 (sizeof(name) - 1), \
3061 GRN_PROC_TOKENIZER, \
3062 (init), \
3063 (next), \
3064 (fin), \
3065 3, \
3066 (vars)))
3067
3068 grn_rc
3069 grn_db_init_builtin_tokenizers(grn_ctx *ctx)
3070 {
3071 grn_obj *obj;
3072 grn_expr_var vars[3];
3073
3074 vars[0].name = NULL;
3075 vars[0].name_size = 0;
3076 GRN_TEXT_INIT(&vars[0].value, 0);
3077 vars[1].name = NULL;
3078 vars[1].name_size = 0;
3079 GRN_TEXT_INIT(&vars[1].value, 0);
3080 vars[2].name = NULL;
3081 vars[2].name_size = 0;
3082 GRN_UINT32_INIT(&vars[2].value, 0);
3083
3084 {
3085 char grn_ngram_tokenizer_remove_blank_enable_env[GRN_ENV_BUFFER_SIZE];
3086
3087 grn_getenv("GRN_NGRAM_TOKENIZER_REMOVE_BLANK_ENABLE",
3088 grn_ngram_tokenizer_remove_blank_enable_env,
3089 GRN_ENV_BUFFER_SIZE);
3090 if (grn_ngram_tokenizer_remove_blank_enable_env[0]) {
3091 if (strcmp(grn_ngram_tokenizer_remove_blank_enable_env, "no") == 0) {
3092 grn_ngram_tokenizer_remove_blank_enable = false;
3093 } else {
3094 grn_ngram_tokenizer_remove_blank_enable = true;
3095 }
3096 } else {
3097 /* Deprecated. Use GRN_NGRAM_TOKENIZER_REMOVE_BLANK_ENABLE instead. */
3098 char grn_ngram_tokenizer_remove_blank_disable_env[GRN_ENV_BUFFER_SIZE];
3099
3100 grn_getenv("GRN_NGRAM_TOKENIZER_REMOVE_BLANK_DISABLE",
3101 grn_ngram_tokenizer_remove_blank_disable_env,
3102 GRN_ENV_BUFFER_SIZE);
3103 if (grn_ngram_tokenizer_remove_blank_disable_env[0]) {
3104 grn_ngram_tokenizer_remove_blank_enable = false;
3105 }
3106 }
3107 }
3108
3109 {
3110 grn_obj *tokenizer;
3111 tokenizer = grn_tokenizer_create(ctx, "TokenDelimit", -1);
3112 if (!tokenizer || DB_OBJ(tokenizer)->id != GRN_DB_DELIMIT) {
3113 return GRN_FILE_CORRUPT;
3114 }
3115 grn_tokenizer_set_init_func(ctx, tokenizer, delimit_init);
3116 grn_tokenizer_set_next_func(ctx, tokenizer, delimit_next);
3117 grn_tokenizer_set_fin_func(ctx, tokenizer, delimit_fin);
3118 }
3119 obj = DEF_TOKENIZER("TokenUnigram",
3120 unigram_init,
3121 ngram_next_deprecated,
3122 ngram_fin_deprecated,
3123 vars);
3124 if (!obj || ((grn_db_obj *)obj)->id != GRN_DB_UNIGRAM) {
3125 return GRN_FILE_CORRUPT;
3126 }
3127 obj = DEF_TOKENIZER("TokenBigram",
3128 bigram_init,
3129 ngram_next_deprecated,
3130 ngram_fin_deprecated,
3131 vars);
3132 if (!obj || ((grn_db_obj *)obj)->id != GRN_DB_BIGRAM) {
3133 return GRN_FILE_CORRUPT;
3134 }
3135 obj = DEF_TOKENIZER("TokenTrigram",
3136 trigram_init,
3137 ngram_next_deprecated,
3138 ngram_fin_deprecated,
3139 vars);
3140 if (!obj || ((grn_db_obj *)obj)->id != GRN_DB_TRIGRAM) {
3141 return GRN_FILE_CORRUPT;
3142 }
3143
3144 DEF_TOKENIZER("TokenBigramSplitSymbol",
3145 bigrams_init,
3146 ngram_next_deprecated,
3147 ngram_fin_deprecated,
3148 vars);
3149 DEF_TOKENIZER("TokenBigramSplitSymbolAlpha",
3150 bigramsa_init,
3151 ngram_next_deprecated,
3152 ngram_fin_deprecated,
3153 vars);
3154 DEF_TOKENIZER("TokenBigramSplitSymbolAlphaDigit",
3155 bigramsad_init,
3156 ngram_next_deprecated,
3157 ngram_fin_deprecated,
3158 vars);
3159 DEF_TOKENIZER("TokenBigramIgnoreBlank",
3160 bigrami_init,
3161 ngram_next_deprecated,
3162 ngram_fin_deprecated,
3163 vars);
3164 DEF_TOKENIZER("TokenBigramIgnoreBlankSplitSymbol",
3165 bigramis_init,
3166 ngram_next_deprecated,
3167 ngram_fin_deprecated,
3168 vars);
3169 DEF_TOKENIZER("TokenBigramIgnoreBlankSplitSymbolAlpha",
3170 bigramisa_init,
3171 ngram_next_deprecated,
3172 ngram_fin_deprecated,
3173 vars);
3174 DEF_TOKENIZER("TokenBigramIgnoreBlankSplitSymbolAlphaDigit",
3175 bigramisad_init,
3176 ngram_next_deprecated,
3177 ngram_fin_deprecated,
3178 vars);
3179 {
3180 grn_obj *tokenizer;
3181 tokenizer = grn_tokenizer_create(ctx, "TokenDelimitNull", -1);
3182 grn_tokenizer_set_init_func(ctx, tokenizer, delimit_null_init);
3183 grn_tokenizer_set_next_func(ctx, tokenizer, delimit_next);
3184 grn_tokenizer_set_fin_func(ctx, tokenizer, delimit_fin);
3185 }
3186 DEF_TOKENIZER("TokenRegexp", regexp_init, regexp_next, regexp_fin, vars);
3187 {
3188 grn_obj *tokenizer;
3189 tokenizer = grn_tokenizer_create(ctx, "TokenNgram", -1);
3190 grn_tokenizer_set_init_func(ctx, tokenizer, ngram_init);
3191 grn_tokenizer_set_next_func(ctx, tokenizer, ngram_next);
3192 grn_tokenizer_set_fin_func(ctx, tokenizer, ngram_fin);
3193 }
3194 {
3195 grn_obj *tokenizer;
3196 tokenizer = grn_tokenizer_create(ctx, "TokenPattern", -1);
3197 grn_tokenizer_set_init_func(ctx, tokenizer, pattern_init);
3198 grn_tokenizer_set_next_func(ctx, tokenizer, pattern_next);
3199 grn_tokenizer_set_fin_func(ctx, tokenizer, pattern_fin);
3200 }
3201 {
3202 grn_obj *tokenizer;
3203 tokenizer = grn_tokenizer_create(ctx, "TokenTable", -1);
3204 grn_tokenizer_set_init_func(ctx, tokenizer, table_init);
3205 grn_tokenizer_set_next_func(ctx, tokenizer, table_next);
3206 grn_tokenizer_set_fin_func(ctx, tokenizer, table_fin);
3207 }
3208 {
3209 grn_obj *tokenizer;
3210 tokenizer = grn_tokenizer_create(ctx, "TokenDocumentVectorTFIDF", -1);
3211 grn_tokenizer_set_init_func(ctx, tokenizer, document_vector_tf_idf_init);
3212 grn_tokenizer_set_next_func(ctx, tokenizer, document_vector_tf_idf_next);
3213 grn_tokenizer_set_fin_func(ctx, tokenizer, document_vector_tf_idf_fin);
3214 }
3215 {
3216 grn_obj *tokenizer;
3217 tokenizer = grn_tokenizer_create(ctx, "TokenDocumentVectorBM25", -1);
3218 grn_tokenizer_set_init_func(ctx, tokenizer, document_vector_bm25_init);
3219 grn_tokenizer_set_next_func(ctx, tokenizer, document_vector_bm25_next);
3220 grn_tokenizer_set_fin_func(ctx, tokenizer, document_vector_bm25_fin);
3221 }
3222
3223 return GRN_SUCCESS;
3224 }