| 1 | /* |
| 2 | Copyright (C) 2009-2018 Brazil |
| 3 | Copyright (C) 2018-2026 Sutou Kouhei <[email protected]> |
| 4 | |
| 5 | This library is free software; you can redistribute it and/or |
| 6 | modify it under the terms of the GNU Lesser General Public |
| 7 | License as published by the Free Software Foundation; either |
| 8 | version 2.1 of the License, or (at your option) any later version. |
| 9 | |
| 10 | This library is distributed in the hope that it will be useful, |
| 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 13 | Lesser General Public License for more details. |
| 14 | |
| 15 | You should have received a copy of the GNU Lesser General Public |
| 16 | License along with this library; if not, write to the Free Software |
| 17 | Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
| 18 | */ |
| 19 | |
| 20 | #include "../grn_proc.h" |
| 21 | #include "../grn_ctx.h" |
| 22 | |
| 23 | #include <groonga/plugin.h> |
| 24 | |
| 25 | grn_obj * |
| 26 | grn_proc_lexicon_open(grn_ctx *ctx, |
| 27 | grn_raw_string *tokenizer_raw, |
| 28 | grn_raw_string *normalizer_raw, |
| 29 | grn_raw_string *token_filters_raw, |
| 30 | grn_raw_string *extractors_raw, |
| 31 | const char *context_tag) |
| 32 | { |
| 33 | grn_obj *lexicon; |
| 34 | |
| 35 | lexicon = grn_table_create(ctx, NULL, 0, |
| 36 | NULL, |
| 37 | GRN_OBJ_TABLE_HASH_KEY, |
| 38 | grn_ctx_at(ctx, GRN_DB_SHORT_TEXT), |
| 39 | NULL); |
| 40 | { |
| 41 | grn_obj tokenizer; |
| 42 | GRN_TEXT_INIT(&tokenizer, GRN_OBJ_DO_SHALLOW_COPY); |
| 43 | if (tokenizer_raw) { |
| 44 | GRN_TEXT_SET(ctx, &tokenizer, tokenizer_raw->value, tokenizer_raw->length); |
| 45 | } |
| 46 | grn_obj_set_info(ctx, lexicon, GRN_INFO_DEFAULT_TOKENIZER, &tokenizer); |
| 47 | GRN_OBJ_FIN(ctx, &tokenizer); |
| 48 | if (ctx->rc != GRN_SUCCESS) { |
| 49 | grn_obj_close(ctx, lexicon); |
| 50 | GRN_PLUGIN_ERROR(ctx, ctx->rc, |
| 51 | "%s failed to set tokenizer: <%.*s>: %s", |
| 52 | context_tag, |
| 53 | (int)(tokenizer_raw->length), |
| 54 | tokenizer_raw->value, |
| 55 | ctx->errbuf); |
| 56 | return NULL; |
| 57 | } |
| 58 | } |
| 59 | |
| 60 | { |
| 61 | grn_obj normalizer; |
| 62 | GRN_TEXT_INIT(&normalizer, GRN_OBJ_DO_SHALLOW_COPY); |
| 63 | if (normalizer_raw) { |
| 64 | GRN_TEXT_SET(ctx, |
| 65 | &normalizer, |
| 66 | normalizer_raw->value, |
| 67 | normalizer_raw->length); |
| 68 | } |
| 69 | grn_obj_set_info(ctx, lexicon, GRN_INFO_NORMALIZER, &normalizer); |
| 70 | GRN_OBJ_FIN(ctx, &normalizer); |
| 71 | if (ctx->rc != GRN_SUCCESS) { |
| 72 | grn_obj_close(ctx, lexicon); |
| 73 | GRN_PLUGIN_ERROR(ctx, ctx->rc, |
| 74 | "%s failed to set normalizer: <%.*s>: %s", |
| 75 | context_tag, |
| 76 | (int)(normalizer_raw->length), |
| 77 | normalizer_raw->value, |
| 78 | ctx->errbuf); |
| 79 | return NULL; |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | if (token_filters_raw) { |
| 84 | grn_proc_table_set_token_filters(ctx, lexicon, token_filters_raw); |
| 85 | if (ctx->rc != GRN_SUCCESS) { |
| 86 | grn_obj_close(ctx, lexicon); |
| 87 | GRN_PLUGIN_ERROR(ctx, ctx->rc, |
| 88 | "%s failed to set token filters: <%.*s>: %s", |
| 89 | context_tag, |
| 90 | (int)(token_filters_raw->length), |
| 91 | token_filters_raw->value, |
| 92 | ctx->errbuf); |
| 93 | return NULL; |
| 94 | } |
| 95 | } |
| 96 | |
| 97 | if (extractors_raw) { |
| 98 | grn_proc_table_set_extractors(ctx, lexicon, extractors_raw); |
| 99 | if (ctx->rc != GRN_SUCCESS) { |
| 100 | grn_obj_close(ctx, lexicon); |
| 101 | GRN_PLUGIN_ERROR(ctx, ctx->rc, |
| 102 | "%s failed to set extractors: <%.*s>: %s", |
| 103 | context_tag, |
| 104 | (int)(extractors_raw->length), |
| 105 | extractors_raw->value, |
| 106 | ctx->errbuf); |
| 107 | return NULL; |
| 108 | } |
| 109 | } |
| 110 | |
| 111 | return lexicon; |
| 112 | } |