| 1 | /* |
| 2 | Copyright (C) 2014 Brazil |
| 3 | Copyright (C) 2018-2022 Sutou Kouhei <[email protected]> |
| 4 | |
| 5 | This library is free software; you can redistribute it and/or |
| 6 | modify it under the terms of the GNU Lesser General Public |
| 7 | License as published by the Free Software Foundation; either |
| 8 | version 2.1 of the License, or (at your option) any later version. |
| 9 | |
| 10 | This library is distributed in the hope that it will be useful, |
| 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 13 | Lesser General Public License for more details. |
| 14 | |
| 15 | You should have received a copy of the GNU Lesser General Public |
| 16 | License along with this library; if not, write to the Free Software |
| 17 | Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
| 18 | */ |
| 19 | |
| 20 | #ifdef GRN_EMBEDDED |
| 21 | # define GRN_PLUGIN_FUNCTION_TAG token_filters_stop_word |
| 22 | #endif |
| 23 | |
| 24 | #include <grn_str.h> |
| 25 | |
| 26 | #include <groonga.h> |
| 27 | #include <groonga/token_filter.h> |
| 28 | |
| 29 | #include <string.h> |
| 30 | |
| 31 | typedef struct { |
| 32 | grn_obj column; |
| 33 | } grn_stop_word_token_filter_options; |
| 34 | |
| 35 | typedef struct { |
| 36 | grn_stop_word_token_filter_options *options; |
| 37 | grn_obj *lexicon; |
| 38 | grn_obj *column; |
| 39 | grn_obj value; |
| 40 | bool is_enabled; |
| 41 | grn_tokenizer_token token; |
| 42 | } grn_stop_word_token_filter; |
| 43 | |
| 44 | static void |
| 45 | stop_word_options_init(grn_ctx *ctx, |
| 46 | grn_stop_word_token_filter_options *options) |
| 47 | { |
| 48 | GRN_TEXT_INIT(&(options->column), 0); |
| 49 | GRN_TEXT_SETS(ctx, &(options->column), "is_stop_word"); |
| 50 | } |
| 51 | |
| 52 | static void * |
| 53 | stop_word_open_options(grn_ctx *ctx, |
| 54 | grn_obj *token_filter, |
| 55 | grn_obj *raw_options, |
| 56 | void *user_data) |
| 57 | { |
| 58 | grn_stop_word_token_filter_options *options; |
| 59 | |
| 60 | options = GRN_PLUGIN_MALLOC(ctx, sizeof(grn_stop_word_token_filter_options)); |
| 61 | if (!options) { |
| 62 | GRN_PLUGIN_ERROR(ctx, |
| 63 | GRN_NO_MEMORY_AVAILABLE, |
| 64 | "[token-filter][stop-word] " |
| 65 | "failed to allocate memory for options"); |
| 66 | return NULL; |
| 67 | } |
| 68 | |
| 69 | stop_word_options_init(ctx, options); |
| 70 | |
| 71 | GRN_OPTION_VALUES_EACH_BEGIN(ctx, raw_options, i, name, name_length) { |
| 72 | grn_raw_string name_raw; |
| 73 | name_raw.value = name; |
| 74 | name_raw.length = name_length; |
| 75 | |
| 76 | if (GRN_RAW_STRING_EQUAL_CSTRING(name_raw, "column")) { |
| 77 | const char *column; |
| 78 | unsigned int length; |
| 79 | length = grn_vector_get_element(ctx, |
| 80 | raw_options, |
| 81 | i, |
| 82 | &column, |
| 83 | NULL, |
| 84 | NULL); |
| 85 | GRN_TEXT_SET(ctx, &(options->column), column, length); |
| 86 | } |
| 87 | } GRN_OPTION_VALUES_EACH_END(); |
| 88 | |
| 89 | return options; |
| 90 | } |
| 91 | |
| 92 | static void |
| 93 | stop_word_close_options(grn_ctx *ctx, void *data) |
| 94 | { |
| 95 | grn_stop_word_token_filter_options *options = data; |
| 96 | GRN_OBJ_FIN(ctx, &(options->column)); |
| 97 | GRN_PLUGIN_FREE(ctx, options); |
| 98 | } |
| 99 | |
| 100 | static void * |
| 101 | stop_word_init(grn_ctx *ctx, grn_tokenizer_query *query) |
| 102 | { |
| 103 | grn_tokenize_mode mode; |
| 104 | grn_obj *lexicon; |
| 105 | unsigned int i; |
| 106 | grn_stop_word_token_filter_options *options; |
| 107 | grn_stop_word_token_filter *token_filter; |
| 108 | |
| 109 | mode = grn_tokenizer_query_get_mode(ctx, query); |
| 110 | if (mode != GRN_TOKEN_GET) { |
| 111 | return NULL; |
| 112 | } |
| 113 | |
| 114 | lexicon = grn_tokenizer_query_get_lexicon(ctx, query); |
| 115 | i = grn_tokenizer_query_get_token_filter_index(ctx, query); |
| 116 | options = grn_table_cache_token_filters_options(ctx, |
| 117 | lexicon, |
| 118 | i, |
| 119 | stop_word_open_options, |
| 120 | stop_word_close_options, |
| 121 | NULL); |
| 122 | if (ctx->rc != GRN_SUCCESS) { |
| 123 | return NULL; |
| 124 | } |
| 125 | |
| 126 | token_filter = GRN_PLUGIN_MALLOC(ctx, sizeof(grn_stop_word_token_filter)); |
| 127 | if (!token_filter) { |
| 128 | GRN_PLUGIN_ERROR(ctx, GRN_NO_MEMORY_AVAILABLE, |
| 129 | "[token-filter][stop-word] " |
| 130 | "failed to allocate grn_stop_word_token_filter"); |
| 131 | return NULL; |
| 132 | } |
| 133 | |
| 134 | token_filter->options = options; |
| 135 | token_filter->lexicon = lexicon; |
| 136 | token_filter->column = |
| 137 | grn_obj_column(ctx, |
| 138 | token_filter->lexicon, |
| 139 | GRN_TEXT_VALUE(&(options->column)), |
| 140 | (uint32_t)GRN_TEXT_LEN(&(options->column))); |
| 141 | if (!token_filter->column) { |
| 142 | char lexicon_name[GRN_TABLE_MAX_KEY_SIZE]; |
| 143 | int lexicon_name_size; |
| 144 | |
| 145 | lexicon_name_size = grn_obj_name(ctx, |
| 146 | token_filter->lexicon, |
| 147 | lexicon_name, |
| 148 | GRN_TABLE_MAX_KEY_SIZE); |
| 149 | GRN_PLUGIN_ERROR(ctx, GRN_TOKEN_FILTER_ERROR, |
| 150 | "[token-filter][stop-word] " |
| 151 | "column for judging stop word doesn't exit: <%.*s.%.*s>", |
| 152 | lexicon_name_size, |
| 153 | lexicon_name, |
| 154 | (int)(GRN_TEXT_LEN(&(options->column))), |
| 155 | GRN_TEXT_VALUE(&(options->column))); |
| 156 | GRN_PLUGIN_FREE(ctx, token_filter); |
| 157 | return NULL; |
| 158 | } |
| 159 | |
| 160 | GRN_BOOL_INIT(&(token_filter->value), 0); |
| 161 | token_filter->is_enabled = true; |
| 162 | grn_obj *query_options = grn_tokenizer_query_get_options(ctx, query); |
| 163 | if (query_options) { |
| 164 | grn_proc_prefixed_options_parse(ctx, |
| 165 | query_options, |
| 166 | "TokenFilterStopWord.", |
| 167 | "[token-filter][stop-word]", |
| 168 | "enable", |
| 169 | GRN_PROC_OPTION_VALUE_BOOL, |
| 170 | &(token_filter->is_enabled), |
| 171 | NULL); |
| 172 | if (ctx->rc != GRN_SUCCESS) { |
| 173 | grn_obj_unlink(ctx, token_filter->column); |
| 174 | GRN_OBJ_FIN(ctx, &(token_filter->value)); |
| 175 | GRN_PLUGIN_FREE(ctx, token_filter); |
| 176 | return NULL; |
| 177 | } |
| 178 | } |
| 179 | grn_tokenizer_token_init(ctx, &(token_filter->token)); |
| 180 | |
| 181 | return token_filter; |
| 182 | } |
| 183 | |
| 184 | static void |
| 185 | stop_word_filter(grn_ctx *ctx, |
| 186 | grn_token *current_token, |
| 187 | grn_token *next_token, |
| 188 | void *user_data) |
| 189 | { |
| 190 | grn_stop_word_token_filter *token_filter = user_data; |
| 191 | grn_id id; |
| 192 | grn_obj *data; |
| 193 | |
| 194 | if (!token_filter) { |
| 195 | return; |
| 196 | } |
| 197 | |
| 198 | if (!token_filter->is_enabled) { |
| 199 | return; |
| 200 | } |
| 201 | |
| 202 | data = grn_token_get_data(ctx, current_token); |
| 203 | id = grn_table_get(ctx, |
| 204 | token_filter->lexicon, |
| 205 | GRN_TEXT_VALUE(data), |
| 206 | (unsigned int)GRN_TEXT_LEN(data)); |
| 207 | if (id != GRN_ID_NIL) { |
| 208 | GRN_BULK_REWIND(&(token_filter->value)); |
| 209 | grn_obj_get_value(ctx, |
| 210 | token_filter->column, |
| 211 | id, |
| 212 | &(token_filter->value)); |
| 213 | if (GRN_BOOL_VALUE(&(token_filter->value))) { |
| 214 | grn_tokenizer_status status; |
| 215 | status = grn_token_get_status(ctx, current_token); |
| 216 | status |= GRN_TOKEN_SKIP; |
| 217 | grn_token_set_status(ctx, next_token, status); |
| 218 | } |
| 219 | } |
| 220 | } |
| 221 | |
| 222 | static void |
| 223 | stop_word_fin(grn_ctx *ctx, void *user_data) |
| 224 | { |
| 225 | grn_stop_word_token_filter *token_filter = user_data; |
| 226 | if (!token_filter) { |
| 227 | return; |
| 228 | } |
| 229 | |
| 230 | grn_tokenizer_token_fin(ctx, &(token_filter->token)); |
| 231 | grn_obj_unlink(ctx, token_filter->column); |
| 232 | GRN_OBJ_FIN(ctx, &(token_filter->value)); |
| 233 | GRN_PLUGIN_FREE(ctx, token_filter); |
| 234 | } |
| 235 | |
| 236 | grn_rc |
| 237 | GRN_PLUGIN_INIT(grn_ctx *ctx) |
| 238 | { |
| 239 | return ctx->rc; |
| 240 | } |
| 241 | |
| 242 | grn_rc |
| 243 | GRN_PLUGIN_REGISTER(grn_ctx *ctx) |
| 244 | { |
| 245 | grn_obj *token_filter; |
| 246 | |
| 247 | token_filter = grn_token_filter_create(ctx, "TokenFilterStopWord", -1); |
| 248 | grn_token_filter_set_init_func(ctx, token_filter, stop_word_init); |
| 249 | grn_token_filter_set_filter_func(ctx, token_filter, stop_word_filter); |
| 250 | grn_token_filter_set_fin_func(ctx, token_filter, stop_word_fin); |
| 251 | |
| 252 | return ctx->rc; |
| 253 | } |
| 254 | |
| 255 | grn_rc |
| 256 | GRN_PLUGIN_FIN(grn_ctx *ctx) |
| 257 | { |
| 258 | return GRN_SUCCESS; |
| 259 | } |