Groonga
Loading...
Searching...
No Matches
tokenizer.h
Go to the documentation of this file.
1/*
2 Copyright (C) 2012-2018 Brazil
3 Copyright (C) 2020-2025 Sutou Kouhei <kou@clear-code.com>
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#pragma once
21
22#include <groonga/plugin.h>
24
25#ifdef __cplusplus
26extern "C" {
27#endif /* __cplusplus */
28
29#define GRN_TOKENIZER_TOKENIZED_DELIMITER_UTF8 "\xEF\xBF\xBE"
30#define GRN_TOKENIZER_TOKENIZED_DELIMITER_UTF8_LEN 3
31
32#define GRN_TOKENIZER_BEGIN_MARK_UTF8 "\xEF\xBF\xAF"
33#define GRN_TOKENIZER_BEGIN_MARK_UTF8_LEN 3
34#define GRN_TOKENIZER_END_MARK_UTF8 "\xEF\xBF\xB0"
35#define GRN_TOKENIZER_END_MARK_UTF8_LEN 3
36
37/*
38 grn_tokenizer_charlen() returns the length (#bytes) of the first character
39 in the string specified by `str_ptr' and `str_length'. If the starting bytes
40 are invalid as a character, grn_tokenizer_charlen() returns 0. See
41 grn_encoding in "groonga.h" for more details of `encoding'
42
43 Deprecated. Use grn_plugin_charlen() instead.
44 */
45int
47 const char *str_ptr,
48 unsigned int str_length,
49 grn_encoding encoding);
50
51/*
52 grn_tokenizer_isspace() returns the length (#bytes) of the first character
53 in the string specified by `str_ptr' and `str_length' if it is a space
54 character. Otherwise, grn_tokenizer_isspace() returns 0.
55
56 Deprecated. Use grn_plugin_isspace() instead.
57 */
58int
60 const char *str_ptr,
61 unsigned int str_length,
62 grn_encoding encoding);
63
64/*
65 grn_tokenizer_is_tokenized_delimiter() returns whether is the first
66 character in the string specified by `str_ptr' and `str_length' the
67 special tokenized delimiter character or not.
68 */
69bool
71 const char *str_ptr,
72 unsigned int str_length,
73 grn_encoding encoding);
74
75/*
76 grn_tokenizer_have_tokenized_delimiter() returns whether is there
77 the special delimiter character in the string specified by `str_ptr'
78 and `str_length' the special tokenized delimiter character or not.
79 */
82 const char *str_ptr,
83 unsigned int str_length,
84 grn_encoding encoding);
85
86/*
87 grn_tokenizer_query_open() parses `args' and returns a new object of
88 grn_tokenizer_query. The new object stores information of the query.
89 grn_tokenizer_query_open() normalizes the query if the target table
90 requires normalization. grn_tokenizer_query_open() returns NULL if
91 something goes wrong. Note that grn_tokenizer_query_open() must be called
92 just once in the function that initializes a tokenizer.
93
94 See `GRN_STRING_*' flags for `normalize_flags'.
95 */
98 int num_args,
99 grn_obj **args,
100 uint32_t normalize_flags);
101
102/*
103 grn_tokenizer_query_create() is deprecated. Use grn_tokenizer_query_open()
104 instead.
105*/
106
108grn_tokenizer_query_create(grn_ctx *ctx, int num_args, grn_obj **args);
109
110/*
111 grn_tokenizer_query_close() finalizes an object of grn_tokenizer_query
112 and then frees memory allocated for that object.
113 */
116
117/*
118 grn_tokenizer_query_destroy() is deprecated. Use grn_tokenizer_query_close()
119 instead.
120 */
121void
123
126 grn_tokenizer_query *query,
127 uint32_t flags);
128GRN_PLUGIN_EXPORT uint32_t
130 grn_tokenizer_query *query);
131
134 grn_tokenizer_query *query);
135
136GRN_PLUGIN_EXPORT const char *
138 grn_tokenizer_query *query,
139 size_t *length);
140
141GRN_PLUGIN_EXPORT const char *
143 grn_tokenizer_query *query,
144 size_t *size,
145 grn_id *domain);
146
149
150GRN_PLUGIN_EXPORT uint32_t
152
155 grn_tokenizer_query *query);
156
159
162
163GRN_PLUGIN_EXPORT unsigned int
165 grn_tokenizer_query *query);
166
169
172
177
189
201
213
225
237
251 uint32_t n_records);
252
266 uint32_t n_total_records);
267
281 uint32_t n_total_records);
282
296 uint32_t n_total_records);
297
307 grn_id rid);
308
318 uint32_t sid);
319
331 grn_obj *tokens);
332
342
352
353/*
354 grn_tokenizer_token is needed to return tokens. A grn_tokenizer_token object
355 stores a token to be returned and it must be maintained until a request for
356 next token or finalization comes.
357 */
359
364
365/*
366 grn_tokenizer_token_init() initializes `token'. Note that an initialized
367 object must be finalized by grn_tokenizer_token_fin().
368 */
371
372/*
373 grn_tokenizer_token_fin() finalizes `token' that has been initialized by
374 grn_tokenizer_token_init().
375 */
378
379/*
380 * grn_tokenizer_status is a flag set for tokenizer status codes.
381 * If a document or query contains no tokens, push an empty string with
382 * GRN_TOKENIZER_TOKEN_LAST as a token.
383 *
384 * @deprecated since 4.0.8. Use grn_token_status instead.
385 */
387
388/*
389 * GRN_TOKENIZER_TOKEN_CONTINUE means that the next token is not the last one.
390 *
391 * @deprecated since 4.0.8. Use GRN_TOKEN_CONTINUE instead.
392 */
393#define GRN_TOKENIZER_TOKEN_CONTINUE GRN_TOKEN_CONTINUE
394/*
395 * GRN_TOKENIZER_TOKEN_LAST means that the next token is the last one.
396 *
397 * @deprecated since 4.0.8. Use GRN_TOKEN_LAST instead.
398 */
399#define GRN_TOKENIZER_TOKEN_LAST GRN_TOKEN_LAST
400/*
401 * GRN_TOKENIZER_TOKEN_OVERLAP means that ...
402 *
403 * @deprecated since 4.0.8. Use GRN_TOKEN_OVERLAP instead.
404 */
405#define GRN_TOKENIZER_TOKEN_OVERLAP GRN_TOKEN_OVERLAP
406/*
407 * GRN_TOKENIZER_TOKEN_UNMATURED means that ...
408 *
409 * @deprecated since 4.0.8. Use GRN_TOKEN_UNMATURED instead.
410 */
411#define GRN_TOKENIZER_TOKEN_UNMATURED GRN_TOKEN_UNMATURED
412/*
413 * GRN_TOKENIZER_TOKEN_REACH_END means that ...
414 *
415 * @deprecated since 4.0.8. Use GRN_TOKEN_REACH_END instead.
416 */
417#define GRN_TOKENIZER_TOKEN_REACH_END GRN_TOKEN_REACH_END
418/*
419 * GRN_TOKENIZER_TOKEN_SKIP means that the token is skipped
420 *
421 * @deprecated since 4.0.8. Use GRN_TOKEN_SKIP instead.
422 */
423#define GRN_TOKENIZER_TOKEN_SKIP GRN_TOKEN_SKIP
424/*
425 * GRN_TOKENIZER_TOKEN_SKIP_WITH_POSITION means that the token and postion is
426 * skipped
427 *
428 * @deprecated since 4.0.8. Use GRN_TOKEN_SKIP_WITH_POSITION instead.
429 */
430#define GRN_TOKENIZER_TOKEN_SKIP_WITH_POSITION GRN_TOKEN_SKIP_WITH_POSITION
431/*
432 * GRN_TOKENIZER_TOKEN_FORCE_PREIX that the token is used common prefix search
433 *
434 * @deprecated since 4.0.8. Use GRN_TOKEN_FORCE_PREIX instead.
435 */
436#define GRN_TOKENIZER_TOKEN_FORCE_PREFIX GRN_TOKEN_FORCE_PREFIX
437
438/*
439 * GRN_TOKENIZER_CONTINUE and GRN_TOKENIZER_LAST are deprecated. They
440 * are just for backward compatibility. Use
441 * GRN_TOKENIZER_TOKEN_CONTINUE and GRN_TOKENIZER_TOKEN_LAST
442 * instead.
443 */
444#define GRN_TOKENIZER_CONTINUE GRN_TOKENIZER_TOKEN_CONTINUE
445#define GRN_TOKENIZER_LAST GRN_TOKENIZER_TOKEN_LAST
446
447/*
448 grn_tokenizer_token_push() pushes the next token into `token'. Note that
449 grn_tokenizer_token_push() does not make a copy of the given string. This
450 means that you have to maintain a memory space allocated to the string.
451 Also note that the grn_tokenizer_token object must be maintained until the
452 request for the next token or finalization comes. See grn_token_status in
453 this header for more details of `status'.
454 */
457 grn_tokenizer_token *token,
458 const char *str_ptr,
459 unsigned int str_length,
460 grn_token_status status);
461
462/*
463 grn_tokenizer_tokenized_delimiter_next() extracts the next token
464 from the string specified by `str_ptr' and `str_length' and pushes
465 the next token into `token'. It returns the string after the next
466 token. The returned string may be `NULL' when all tokens are
467 extracted.
468
469 @deprecated since 8.0.9. It's for old tokenizer next API. Use
470 grn_tokenizer_next_by_tokenized_delimiter() for new tokenizer next
471 API (grn_tokenizer_next_func).
472 */
473GRN_PLUGIN_EXPORT const char *
475 grn_tokenizer_token *token,
476 const char *str_ptr,
477 unsigned int str_length,
478 grn_encoding encoding);
479
480/*
481 Extract the next token by delimiting by
482 GRN_TOKENIZER_TOKENIZED_DELIMITER_UTF8.
483
484 This is for grn_tokenizer_next_func.
485
486 @since 8.0.9.
487 */
488GRN_PLUGIN_EXPORT const char *
490 grn_token *token,
491 const char *str_ptr,
492 unsigned int str_length,
493 grn_encoding encoding);
494
495/*
496 grn_tokenizer_register() registers a plugin to the database which is
497 associated with `ctx'. `plugin_name_ptr' and `plugin_name_length' specify the
498 plugin name. Alphabetic letters ('A'-'Z' and 'a'-'z'), digits ('0'-'9') and
499 an underscore ('_') are capable characters. `init', `next' and `fin' specify
500 the plugin functions. `init' is called for initializing a tokenizer for a
501 document or query. `next' is called for extracting tokens one by one. `fin'
502 is called for finalizing a tokenizer. grn_tokenizer_register() returns
503 GRN_SUCCESS on success, an error code on failure. See "groonga.h" for more
504 details of grn_proc_func and grn_user_data, that is used as an argument of
505 grn_proc_func.
506
507 @deprecated since 8.0.2. Use grn_tokenizer_create() and
508 grn_tokenizer_set_*_func().
509 */
512 const char *plugin_name_ptr,
513 unsigned int plugin_name_length,
514 grn_proc_func *init,
515 grn_proc_func *next,
516 grn_proc_func *fin);
517
519grn_tokenizer_create(grn_ctx *ctx, const char *name, int name_length);
520
535typedef grn_rc
537typedef void *
539typedef void
541 grn_tokenizer_query *query,
542 grn_token *token,
543 void *user_data);
544typedef void
545grn_tokenizer_fin_func(grn_ctx *ctx, void *user_data);
546
559 grn_obj *tokenizer,
563 grn_obj *tokenizer,
567 grn_obj *tokenizer,
571 grn_obj *tokenizer,
573
574#ifdef __cplusplus
575} /* extern "C" */
576#endif /* __cplusplus */
grn_rc
Definition groonga.h:61
grn_obj * grn_proc_func(grn_ctx *ctx, int nargs, grn_obj **args, grn_user_data *user_data)
Definition groonga.h:346
uint32_t grn_id
Definition groonga.h:44
grn_encoding
Definition groonga.h:163
#define GRN_PLUGIN_EXPORT
Definition plugin.h:50
Definition groonga.h:351
Definition groonga.h:919
Definition tokenizer_query_deprecated.h:35
Definition tokenizer.h:360
grn_obj status
Definition tokenizer.h:362
grn_obj str
Definition tokenizer.h:361
uint32_t grn_token_status
Definition token.h:69
struct _grn_token grn_token
Definition token.h:127
grn_tokenize_mode
Definition token.h:37
GRN_PLUGIN_EXPORT bool grn_tokenizer_query_have_tokenized_delimiter(grn_ctx *ctx, grn_tokenizer_query *query)
GRN_PLUGIN_EXPORT void grn_tokenizer_token_fin(grn_ctx *ctx, grn_tokenizer_token *token)
void * grn_tokenizer_init_func(grn_ctx *ctx, grn_tokenizer_query *query)
Definition tokenizer.h:538
GRN_PLUGIN_EXPORT void grn_tokenizer_token_push(grn_ctx *ctx, grn_tokenizer_token *token, const char *str_ptr, unsigned int str_length, grn_token_status status)
GRN_PLUGIN_EXPORT grn_obj * grn_tokenizer_create(grn_ctx *ctx, const char *name, int name_length)
GRN_PLUGIN_EXPORT grn_tokenize_mode grn_tokenizer_query_get_mode(grn_ctx *ctx, grn_tokenizer_query *query)
GRN_PLUGIN_EXPORT grn_tokenizer_query * grn_tokenizer_query_open(grn_ctx *ctx, int num_args, grn_obj **args, uint32_t normalize_flags)
grn_tokenizer_query * grn_tokenizer_query_create(grn_ctx *ctx, int num_args, grn_obj **args)
GRN_PLUGIN_EXPORT grn_rc grn_tokenizer_build_data_start_record(grn_ctx *ctx, grn_tokenizer_build_data *data, grn_id rid)
A tokenizer must call this when it starts processing a new record.
GRN_PLUGIN_EXPORT grn_rc grn_tokenizer_build_data_start_section(grn_ctx *ctx, grn_tokenizer_build_data *data, uint32_t sid)
A tokenizer must call this when it starts processing a new section.
GRN_PLUGIN_EXPORT grn_obj * grn_tokenizer_query_get_normalized_string(grn_ctx *ctx, grn_tokenizer_query *query)
GRN_PLUGIN_EXPORT bool grn_tokenizer_have_tokenized_delimiter(grn_ctx *ctx, const char *str_ptr, unsigned int str_length, grn_encoding encoding)
GRN_PLUGIN_EXPORT grn_id grn_tokenizer_query_get_source_id(grn_ctx *ctx, grn_tokenizer_query *query)
void grn_tokenizer_next_func(grn_ctx *ctx, grn_tokenizer_query *query, grn_token *token, void *user_data)
Definition tokenizer.h:540
GRN_PLUGIN_EXPORT void grn_tokenizer_query_close(grn_ctx *ctx, grn_tokenizer_query *query)
GRN_PLUGIN_EXPORT grn_rc grn_tokenizer_set_fin_func(grn_ctx *ctx, grn_obj *tokenizer, grn_tokenizer_fin_func *fin)
GRN_PLUGIN_EXPORT grn_obj * grn_tokenizer_query_get_options(grn_ctx *ctx, grn_tokenizer_query *query)
void grn_tokenizer_fin_func(grn_ctx *ctx, void *user_data)
Definition tokenizer.h:545
GRN_PLUGIN_EXPORT const char * grn_tokenizer_query_get_raw_string(grn_ctx *ctx, grn_tokenizer_query *query, size_t *length)
GRN_PLUGIN_EXPORT grn_rc grn_tokenizer_build_data_finish_section(grn_ctx *ctx, grn_tokenizer_build_data *data)
A tokenizer must call this when it finishes processing a section.
GRN_PLUGIN_EXPORT grn_obj * grn_tokenizer_query_get_lexicon(grn_ctx *ctx, grn_tokenizer_query *query)
GRN_PLUGIN_EXPORT grn_obj * grn_tokenizer_build_data_get_source_table(grn_ctx *ctx, grn_tokenizer_build_data *data)
Return the build target table.
GRN_PLUGIN_EXPORT void grn_tokenizer_token_init(grn_ctx *ctx, grn_tokenizer_token *token)
grn_rc grn_tokenizer_build_func(grn_ctx *ctx, grn_tokenizer_build_data *data)
A function to build an index column by this tokenizer.
Definition tokenizer.h:536
int grn_tokenizer_charlen(grn_ctx *ctx, const char *str_ptr, unsigned int str_length, grn_encoding encoding)
GRN_PLUGIN_EXPORT grn_rc grn_tokenizer_register(grn_ctx *ctx, const char *plugin_name_ptr, unsigned int plugin_name_length, grn_proc_func *init, grn_proc_func *next, grn_proc_func *fin)
grn_token_status grn_tokenizer_status
Definition tokenizer.h:386
GRN_PLUGIN_EXPORT grn_rc grn_tokenizer_build_data_start_cluster(grn_ctx *ctx, grn_tokenizer_build_data *data, uint32_t n_total_records)
A tokenizer must call this when it starts clustering. This is only for progress report.
void grn_tokenizer_query_destroy(grn_ctx *ctx, grn_tokenizer_query *query)
GRN_PLUGIN_EXPORT grn_rc grn_tokenizer_set_init_func(grn_ctx *ctx, grn_obj *tokenizer, grn_tokenizer_init_func *init)
int grn_tokenizer_isspace(grn_ctx *ctx, const char *str_ptr, unsigned int str_length, grn_encoding encoding)
GRN_PLUGIN_EXPORT unsigned int grn_tokenizer_query_get_token_filter_index(grn_ctx *ctx, grn_tokenizer_query *query)
GRN_PLUGIN_EXPORT grn_obj * grn_tokenizer_query_get_index_column(grn_ctx *ctx, grn_tokenizer_query *query)
GRN_PLUGIN_EXPORT grn_obj * grn_tokenizer_build_data_get_source_columns(grn_ctx *ctx, grn_tokenizer_build_data *data)
Return the build target columns as GRN_PVECTOR.
GRN_PLUGIN_EXPORT grn_rc grn_tokenizer_build_data_append_tokens(grn_ctx *ctx, grn_tokenizer_build_data *data, grn_obj *tokens)
A tokenizer must call this when it appends tokens to the current record and section.
GRN_PLUGIN_EXPORT uint32_t grn_tokenizer_query_get_flags(grn_ctx *ctx, grn_tokenizer_query *query)
GRN_PLUGIN_EXPORT grn_rc grn_tokenizer_build_data_processed_n_records(grn_ctx *ctx, grn_tokenizer_build_data *data, uint32_t n_records)
A tokenizer may call this when it processed one or more records. This is only for progress report.
GRN_PLUGIN_EXPORT grn_obj * grn_tokenizer_build_data_get_index_column(grn_ctx *ctx, grn_tokenizer_build_data *data)
Return the index column for this tokenizer.
GRN_PLUGIN_EXPORT grn_obj * grn_tokenizer_build_data_get_lexicon(grn_ctx *ctx, grn_tokenizer_build_data *data)
Return the lexicon for this tokenizer.
bool grn_tokenizer_is_tokenized_delimiter(grn_ctx *ctx, const char *str_ptr, unsigned int str_length, grn_encoding encoding)
GRN_PLUGIN_EXPORT grn_rc grn_tokenizer_set_build_func(grn_ctx *ctx, grn_obj *tokenizer, grn_tokenizer_build_func *build)
Set build function for this tokenizer.
GRN_PLUGIN_EXPORT uint32_t grn_tokenizer_query_get_normalize_flags(grn_ctx *ctx, grn_tokenizer_query *query)
GRN_PLUGIN_EXPORT grn_rc grn_tokenizer_build_data_start_load(grn_ctx *ctx, grn_tokenizer_build_data *data, uint32_t n_total_records)
A tokenizer must call this when it starts loading. This is only for progress report.
GRN_PLUGIN_EXPORT grn_rc grn_tokenizer_build_data_finish_record(grn_ctx *ctx, grn_tokenizer_build_data *data)
A tokenizer must call this when it finishes processing a record.
GRN_PLUGIN_EXPORT grn_rc grn_tokenizer_set_next_func(grn_ctx *ctx, grn_obj *tokenizer, grn_tokenizer_next_func *next)
GRN_PLUGIN_EXPORT grn_encoding grn_tokenizer_query_get_encoding(grn_ctx *ctx, grn_tokenizer_query *query)
GRN_PLUGIN_EXPORT grn_rc grn_tokenizer_build_data_start_vectorize(grn_ctx *ctx, grn_tokenizer_build_data *data, uint32_t n_total_records)
A tokenizer must call this when it starts vectorizing. This is only for progress report.
GRN_PLUGIN_EXPORT const char * grn_tokenizer_tokenized_delimiter_next(grn_ctx *ctx, grn_tokenizer_token *token, const char *str_ptr, unsigned int str_length, grn_encoding encoding)
GRN_PLUGIN_EXPORT grn_obj * grn_tokenizer_query_get_source_column(grn_ctx *ctx, grn_tokenizer_query *query)
GRN_PLUGIN_EXPORT grn_rc grn_tokenizer_query_set_normalize_flags(grn_ctx *ctx, grn_tokenizer_query *query, uint32_t flags)
struct grn_tokenizer_build_data grn_tokenizer_build_data
This is an opaque data to pass build data to a tokenizer from Groonga.
Definition tokenizer.h:188
GRN_PLUGIN_EXPORT const char * grn_tokenizer_next_by_tokenized_delimiter(grn_ctx *ctx, grn_token *token, const char *str_ptr, unsigned int str_length, grn_encoding encoding)
GRN_PLUGIN_EXPORT const char * grn_tokenizer_query_get_data(grn_ctx *ctx, grn_tokenizer_query *query, size_t *size, grn_id *domain)