| 1 | /* |
| 2 | Copyright (C) 2018-2025 Sutou Kouhei <[email protected]> |
| 3 | |
| 4 | This library is free software; you can redistribute it and/or |
| 5 | modify it under the terms of the GNU Lesser General Public |
| 6 | License as published by the Free Software Foundation; either |
| 7 | version 2.1 of the License, or (at your option) any later version. |
| 8 | |
| 9 | This library is distributed in the hope that it will be useful, |
| 10 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 12 | Lesser General Public License for more details. |
| 13 | |
| 14 | You should have received a copy of the GNU Lesser General Public |
| 15 | License along with this library; if not, write to the Free Software |
| 16 | Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
| 17 | */ |
| 18 | |
| 19 | #include "grn_token_filters.h" |
| 20 | #include "grn_normalizer.h" |
| 21 | |
| 22 | #include <groonga/token_filter.h> |
| 23 | |
| 24 | typedef struct { |
| 25 | grn_nfkc_normalize_options *options; |
| 26 | grn_string string; |
| 27 | grn_tokenizer_token token; |
| 28 | } grn_nfkc_token_filter; |
| 29 | |
| 30 | static void * |
| 31 | nfkc_open_options(grn_ctx *ctx, |
| 32 | grn_obj *token_filter, |
| 33 | grn_obj *raw_options, |
| 34 | void *user_data, |
| 35 | grn_nfkc_normalize_options_init_func init_func, |
| 36 | const char *tag) |
| 37 | { |
| 38 | grn_nfkc_normalize_options *options; |
| 39 | |
| 40 | options = GRN_CALLOC(sizeof(grn_nfkc_normalize_options)); |
| 41 | if (!options) { |
| 42 | GRN_PLUGIN_ERROR(ctx, |
| 43 | GRN_NO_MEMORY_AVAILABLE, |
| 44 | "%s failed to allocate memory for options", |
| 45 | tag); |
| 46 | return NULL; |
| 47 | } |
| 48 | |
| 49 | init_func(ctx, options); |
| 50 | |
| 51 | grn_nfkc_normalize_options_apply(ctx, options, raw_options, tag); |
| 52 | |
| 53 | return options; |
| 54 | } |
| 55 | |
| 56 | |
| 57 | static void |
| 58 | nfkc_close_options(grn_ctx *ctx, void *data) |
| 59 | { |
| 60 | grn_nfkc_normalize_options *options = data; |
| 61 | grn_nfkc_normalize_options_fin(ctx, options); |
| 62 | GRN_PLUGIN_FREE(ctx, options); |
| 63 | } |
| 64 | |
| 65 | static void * |
| 66 | nfkc_init(grn_ctx *ctx, |
| 67 | grn_tokenizer_query *query, |
| 68 | grn_table_module_open_options_func open_options_func, |
| 69 | const char *normalizer_name, |
| 70 | const char *tag) |
| 71 | { |
| 72 | grn_obj *lexicon; |
| 73 | unsigned int i; |
| 74 | grn_nfkc_normalize_options *options; |
| 75 | grn_nfkc_token_filter *token_filter; |
| 76 | |
| 77 | lexicon = grn_tokenizer_query_get_lexicon(ctx, query); |
| 78 | i = grn_tokenizer_query_get_token_filter_index(ctx, query); |
| 79 | options = grn_table_cache_token_filters_options(ctx, |
| 80 | lexicon, |
| 81 | i, |
| 82 | open_options_func, |
| 83 | nfkc_close_options, |
| 84 | NULL); |
| 85 | if (ctx->rc != GRN_SUCCESS) { |
| 86 | return NULL; |
| 87 | } |
| 88 | |
| 89 | token_filter = GRN_PLUGIN_MALLOC(ctx, sizeof(grn_nfkc_token_filter)); |
| 90 | if (!token_filter) { |
| 91 | GRN_PLUGIN_ERROR(ctx, GRN_NO_MEMORY_AVAILABLE, |
| 92 | "%s failed to allocate grn_nfkc_token_filter", |
| 93 | tag); |
| 94 | return NULL; |
| 95 | } |
| 96 | |
| 97 | token_filter->options = options; |
| 98 | grn_string_init(ctx, |
| 99 | (grn_obj *)(&(token_filter->string)), |
| 100 | grn_ctx_get(ctx, normalizer_name, -1), |
| 101 | 0, |
| 102 | GRN_CTX_GET_ENCODING(ctx)); |
| 103 | grn_tokenizer_token_init(ctx, &(token_filter->token)); |
| 104 | |
| 105 | return token_filter; |
| 106 | } |
| 107 | |
| 108 | static void |
| 109 | nfkc_filter(grn_ctx *ctx, |
| 110 | grn_token *current_token, |
| 111 | grn_token *next_token, |
| 112 | void *user_data) |
| 113 | { |
| 114 | grn_nfkc_token_filter *token_filter = user_data; |
| 115 | grn_obj *data; |
| 116 | grn_obj *string; |
| 117 | |
| 118 | if (!token_filter) { |
| 119 | return; |
| 120 | } |
| 121 | |
| 122 | if (GRN_CTX_GET_ENCODING(ctx) != GRN_ENC_UTF8) { |
| 123 | return; |
| 124 | } |
| 125 | |
| 126 | data = grn_token_get_data(ctx, current_token); |
| 127 | string = (grn_obj *)(&(token_filter->string)); |
| 128 | grn_string_set_original(ctx, |
| 129 | string, |
| 130 | GRN_TEXT_VALUE(data), |
| 131 | (unsigned int)GRN_TEXT_LEN(data)); |
| 132 | grn_nfkc_normalize(ctx, |
| 133 | string, |
| 134 | token_filter->options); |
| 135 | { |
| 136 | const char *normalized; |
| 137 | unsigned int normalized_length; |
| 138 | grn_string_get_normalized(ctx, |
| 139 | string, |
| 140 | &normalized, |
| 141 | &normalized_length, |
| 142 | NULL); |
| 143 | grn_token_set_data(ctx, next_token, normalized, (int)normalized_length); |
| 144 | } |
| 145 | } |
| 146 | |
| 147 | static void |
| 148 | nfkc_fin(grn_ctx *ctx, void *user_data) |
| 149 | { |
| 150 | grn_nfkc_token_filter *token_filter = user_data; |
| 151 | |
| 152 | if (!token_filter) { |
| 153 | return; |
| 154 | } |
| 155 | |
| 156 | grn_tokenizer_token_fin(ctx, &(token_filter->token)); |
| 157 | grn_string_fin(ctx, (grn_obj *)(&(token_filter->string))); |
| 158 | GRN_PLUGIN_FREE(ctx, token_filter); |
| 159 | } |
| 160 | |
| 161 | static void * |
| 162 | nfkc100_open_options(grn_ctx *ctx, |
| 163 | grn_obj *token_filter, |
| 164 | grn_obj *raw_options, |
| 165 | void *user_data) |
| 166 | { |
| 167 | return nfkc_open_options(ctx, |
| 168 | token_filter, |
| 169 | raw_options, |
| 170 | user_data, |
| 171 | grn_nfkc100_normalize_options_init, |
| 172 | "[token-filter][nfkc100]"); |
| 173 | } |
| 174 | |
| 175 | static void * |
| 176 | nfkc100_init(grn_ctx *ctx, grn_tokenizer_query *query) |
| 177 | { |
| 178 | return nfkc_init(ctx, |
| 179 | query, |
| 180 | nfkc100_open_options, |
| 181 | "NormalierNFKC100", |
| 182 | "[token-filter][nfkc100]"); |
| 183 | } |
| 184 | |
| 185 | static void * |
| 186 | nfkc121_open_options(grn_ctx *ctx, |
| 187 | grn_obj *token_filter, |
| 188 | grn_obj *raw_options, |
| 189 | void *user_data) |
| 190 | { |
| 191 | return nfkc_open_options(ctx, |
| 192 | token_filter, |
| 193 | raw_options, |
| 194 | user_data, |
| 195 | grn_nfkc121_normalize_options_init, |
| 196 | "[token-filter][nfkc121]"); |
| 197 | } |
| 198 | |
| 199 | static void * |
| 200 | nfkc121_init(grn_ctx *ctx, grn_tokenizer_query *query) |
| 201 | { |
| 202 | return nfkc_init(ctx, |
| 203 | query, |
| 204 | nfkc121_open_options, |
| 205 | "NormalierNFKC121", |
| 206 | "[token-filter][nfkc121]"); |
| 207 | } |
| 208 | |
| 209 | static void * |
| 210 | nfkc130_open_options(grn_ctx *ctx, |
| 211 | grn_obj *token_filter, |
| 212 | grn_obj *raw_options, |
| 213 | void *user_data) |
| 214 | { |
| 215 | return nfkc_open_options(ctx, |
| 216 | token_filter, |
| 217 | raw_options, |
| 218 | user_data, |
| 219 | grn_nfkc130_normalize_options_init, |
| 220 | "[token-filter][nfkc130]"); |
| 221 | } |
| 222 | |
| 223 | static void * |
| 224 | nfkc130_init(grn_ctx *ctx, grn_tokenizer_query *query) |
| 225 | { |
| 226 | return nfkc_init(ctx, |
| 227 | query, |
| 228 | nfkc130_open_options, |
| 229 | "NormalierNFKC130", |
| 230 | "[token-filter][nfkc130]"); |
| 231 | } |
| 232 | |
| 233 | static void * |
| 234 | nfkc150_open_options(grn_ctx *ctx, |
| 235 | grn_obj *token_filter, |
| 236 | grn_obj *raw_options, |
| 237 | void *user_data) |
| 238 | { |
| 239 | return nfkc_open_options(ctx, |
| 240 | token_filter, |
| 241 | raw_options, |
| 242 | user_data, |
| 243 | grn_nfkc150_normalize_options_init, |
| 244 | "[token-filter][nfkc150]"); |
| 245 | } |
| 246 | |
| 247 | static void * |
| 248 | nfkc150_init(grn_ctx *ctx, grn_tokenizer_query *query) |
| 249 | { |
| 250 | return nfkc_init(ctx, |
| 251 | query, |
| 252 | nfkc150_open_options, |
| 253 | "NormalierNFKC150", |
| 254 | "[token-filter][nfkc150]"); |
| 255 | } |
| 256 | |
| 257 | static void * |
| 258 | nfkc_generic_open_options(grn_ctx *ctx, |
| 259 | grn_obj *token_filter, |
| 260 | grn_obj *raw_options, |
| 261 | void *user_data) |
| 262 | { |
| 263 | return nfkc_open_options(ctx, |
| 264 | token_filter, |
| 265 | raw_options, |
| 266 | user_data, |
| 267 | /* This default value is the latest NFKC |
| 268 | * version when we implement this. This |
| 269 | * must not change to keep backward |
| 270 | * compatibility. */ |
| 271 | grn_nfkc160_normalize_options_init, |
| 272 | "[token-filter][nfkc]"); |
| 273 | } |
| 274 | |
| 275 | static void * |
| 276 | nfkc_generic_init(grn_ctx *ctx, grn_tokenizer_query *query) |
| 277 | { |
| 278 | return nfkc_init(ctx, |
| 279 | query, |
| 280 | nfkc_generic_open_options, |
| 281 | "NormalierNFKC", |
| 282 | "[token-filter][nfkc]"); |
| 283 | } |
| 284 | |
| 285 | grn_rc |
| 286 | grn_db_init_builtin_token_filters(grn_ctx *ctx) |
| 287 | { |
| 288 | { |
| 289 | grn_obj *token_filter; |
| 290 | token_filter = grn_token_filter_create(ctx, "TokenFilterNFKC100", -1); |
| 291 | grn_token_filter_set_init_func(ctx, token_filter, nfkc100_init); |
| 292 | grn_token_filter_set_filter_func(ctx, token_filter, nfkc_filter); |
| 293 | grn_token_filter_set_fin_func(ctx, token_filter, nfkc_fin); |
| 294 | } |
| 295 | |
| 296 | { |
| 297 | grn_obj *token_filter; |
| 298 | token_filter = grn_token_filter_create(ctx, "TokenFilterNFKC121", -1); |
| 299 | grn_token_filter_set_init_func(ctx, token_filter, nfkc121_init); |
| 300 | grn_token_filter_set_filter_func(ctx, token_filter, nfkc_filter); |
| 301 | grn_token_filter_set_fin_func(ctx, token_filter, nfkc_fin); |
| 302 | } |
| 303 | |
| 304 | { |
| 305 | grn_obj *token_filter; |
| 306 | token_filter = grn_token_filter_create(ctx, "TokenFilterNFKC130", -1); |
| 307 | grn_token_filter_set_init_func(ctx, token_filter, nfkc130_init); |
| 308 | grn_token_filter_set_filter_func(ctx, token_filter, nfkc_filter); |
| 309 | grn_token_filter_set_fin_func(ctx, token_filter, nfkc_fin); |
| 310 | } |
| 311 | |
| 312 | { |
| 313 | grn_obj *token_filter; |
| 314 | token_filter = grn_token_filter_create(ctx, "TokenFilterNFKC150", -1); |
| 315 | grn_token_filter_set_init_func(ctx, token_filter, nfkc150_init); |
| 316 | grn_token_filter_set_filter_func(ctx, token_filter, nfkc_filter); |
| 317 | grn_token_filter_set_fin_func(ctx, token_filter, nfkc_fin); |
| 318 | } |
| 319 | |
| 320 | { |
| 321 | grn_obj *token_filter; |
| 322 | token_filter = grn_token_filter_create(ctx, "TokenFilterNFKC", -1); |
| 323 | grn_token_filter_set_init_func(ctx, token_filter, nfkc_generic_init); |
| 324 | grn_token_filter_set_filter_func(ctx, token_filter, nfkc_filter); |
| 325 | grn_token_filter_set_fin_func(ctx, token_filter, nfkc_fin); |
| 326 | } |
| 327 | |
| 328 | return GRN_SUCCESS; |
| 329 | } |