| 1 | /* |
| 2 | Copyright (C) 2009-2018 Brazil |
| 3 | Copyright (C) 2021-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 | static int |
| 26 | parse_normalize_flags(grn_ctx *ctx, grn_raw_string *flags_raw) |
| 27 | { |
| 28 | int flags = 0; |
| 29 | const char *names, *names_end; |
| 30 | |
| 31 | names = flags_raw->value; |
| 32 | names_end = names + flags_raw->length; |
| 33 | while (names < names_end) { |
| 34 | if (*names == '|' || *names == ' ') { |
| 35 | names += 1; |
| 36 | continue; |
| 37 | } |
| 38 | |
| 39 | #define CHECK_FLAG(name)\ |
| 40 | if (((size_t)(names_end - names) >= (sizeof(#name) - 1)) && \ |
| 41 | (!memcmp(names, #name, sizeof(#name) - 1))) {\ |
| 42 | flags |= GRN_STRING_ ## name;\ |
| 43 | names += sizeof(#name) - 1;\ |
| 44 | continue;\ |
| 45 | } |
| 46 | |
| 47 | CHECK_FLAG(REMOVE_BLANK); |
| 48 | CHECK_FLAG(WITH_TYPES); |
| 49 | CHECK_FLAG(WITH_CHECKS); |
| 50 | CHECK_FLAG(REMOVE_TOKENIZED_DELIMITER); |
| 51 | |
| 52 | #define GRN_STRING_NONE 0 |
| 53 | CHECK_FLAG(NONE); |
| 54 | #undef GRN_STRING_NONE |
| 55 | |
| 56 | ERR(GRN_INVALID_ARGUMENT, "[normalize] invalid flag: <%.*s>", |
| 57 | (int)(names_end - names), names); |
| 58 | return 0; |
| 59 | #undef CHECK_FLAG |
| 60 | } |
| 61 | |
| 62 | return flags; |
| 63 | } |
| 64 | |
| 65 | static grn_obj * |
| 66 | command_normalize(grn_ctx *ctx, int nargs, grn_obj **args, grn_user_data *user_data) |
| 67 | { |
| 68 | const char *context_tag = "[normalize]"; |
| 69 | grn_raw_string normalizer_raw; |
| 70 | grn_raw_string string_raw; |
| 71 | grn_raw_string flags_raw; |
| 72 | grn_raw_string normalizers_raw; |
| 73 | |
| 74 | #define GET_VALUE(name) \ |
| 75 | name ## _raw.value = \ |
| 76 | grn_plugin_proc_get_var_string(ctx, \ |
| 77 | user_data, \ |
| 78 | #name, \ |
| 79 | strlen(#name), \ |
| 80 | &(name ## _raw.length)) |
| 81 | |
| 82 | GET_VALUE(normalizer); |
| 83 | GET_VALUE(string); |
| 84 | GET_VALUE(flags); |
| 85 | GET_VALUE(normalizers); |
| 86 | |
| 87 | #undef GET_VALUE |
| 88 | |
| 89 | if (normalizers_raw.length == 0 && normalizer_raw.length > 0) { |
| 90 | normalizers_raw = normalizer_raw; |
| 91 | } |
| 92 | if (normalizers_raw.length == 0) { |
| 93 | GRN_PLUGIN_ERROR(ctx, |
| 94 | GRN_INVALID_ARGUMENT, |
| 95 | "%s normalizer name is missing", |
| 96 | context_tag); |
| 97 | return NULL; |
| 98 | } |
| 99 | |
| 100 | { |
| 101 | int flags; |
| 102 | int n_elements = 3; |
| 103 | grn_obj *lexicon; |
| 104 | grn_obj *grn_string; |
| 105 | unsigned int normalized_length_in_bytes; |
| 106 | unsigned int normalized_n_characters; |
| 107 | const uint64_t *offsets; |
| 108 | |
| 109 | flags = parse_normalize_flags(ctx, &flags_raw); |
| 110 | if (ctx->rc != GRN_SUCCESS) { |
| 111 | return NULL; |
| 112 | } |
| 113 | |
| 114 | lexicon = grn_proc_lexicon_open(ctx, |
| 115 | NULL, |
| 116 | &normalizers_raw, |
| 117 | NULL, |
| 118 | NULL, |
| 119 | context_tag); |
| 120 | if (!lexicon) { |
| 121 | return NULL; |
| 122 | } |
| 123 | |
| 124 | grn_string = grn_string_open(ctx, |
| 125 | string_raw.value, |
| 126 | (unsigned int)(string_raw.length), |
| 127 | lexicon, |
| 128 | flags); |
| 129 | |
| 130 | offsets = grn_string_get_offsets(ctx, grn_string); |
| 131 | if (offsets) { |
| 132 | n_elements++; |
| 133 | } |
| 134 | |
| 135 | grn_ctx_output_map_open(ctx, "RESULT", n_elements); |
| 136 | { |
| 137 | const char *normalized; |
| 138 | |
| 139 | grn_string_get_normalized(ctx, |
| 140 | grn_string, |
| 141 | &normalized, |
| 142 | &normalized_length_in_bytes, |
| 143 | &normalized_n_characters); |
| 144 | grn_ctx_output_cstr(ctx, "normalized"); |
| 145 | grn_ctx_output_str(ctx, normalized, normalized_length_in_bytes); |
| 146 | } |
| 147 | { |
| 148 | const unsigned char *types; |
| 149 | |
| 150 | types = grn_string_get_types(ctx, grn_string); |
| 151 | grn_ctx_output_cstr(ctx, "types"); |
| 152 | if (types) { |
| 153 | unsigned int i; |
| 154 | grn_ctx_output_array_open(ctx, "types", (int)normalized_n_characters); |
| 155 | for (i = 0; i <= normalized_n_characters; i++) { |
| 156 | grn_ctx_output_cstr(ctx, grn_char_type_to_string(types[i])); |
| 157 | } |
| 158 | grn_ctx_output_array_close(ctx); |
| 159 | } else { |
| 160 | grn_ctx_output_array_open(ctx, "types", 0); |
| 161 | grn_ctx_output_array_close(ctx); |
| 162 | } |
| 163 | } |
| 164 | { |
| 165 | const short *checks; |
| 166 | |
| 167 | checks = grn_string_get_checks(ctx, grn_string); |
| 168 | grn_ctx_output_cstr(ctx, "checks"); |
| 169 | if (checks) { |
| 170 | unsigned int i; |
| 171 | grn_ctx_output_array_open(ctx, "checks", (int)normalized_length_in_bytes); |
| 172 | for (i = 0; i < normalized_length_in_bytes; i++) { |
| 173 | grn_ctx_output_int32(ctx, checks[i]); |
| 174 | } |
| 175 | grn_ctx_output_array_close(ctx); |
| 176 | } else { |
| 177 | grn_ctx_output_array_open(ctx, "checks", 0); |
| 178 | grn_ctx_output_array_close(ctx); |
| 179 | } |
| 180 | } |
| 181 | if (offsets) { |
| 182 | unsigned int i; |
| 183 | |
| 184 | grn_ctx_output_cstr(ctx, "offsets"); |
| 185 | grn_ctx_output_array_open(ctx, "offsets", (int)normalized_n_characters); |
| 186 | for (i = 0; i <= normalized_n_characters; i++) { |
| 187 | grn_ctx_output_uint64(ctx, offsets[i]); |
| 188 | } |
| 189 | grn_ctx_output_array_close(ctx); |
| 190 | } |
| 191 | |
| 192 | grn_ctx_output_map_close(ctx); |
| 193 | |
| 194 | grn_obj_unlink(ctx, grn_string); |
| 195 | |
| 196 | grn_obj_unlink(ctx, lexicon); |
| 197 | } |
| 198 | |
| 199 | return NULL; |
| 200 | } |
| 201 | |
| 202 | void |
| 203 | grn_proc_init_normalize(grn_ctx *ctx) |
| 204 | { |
| 205 | grn_expr_var vars[4]; |
| 206 | |
| 207 | grn_plugin_expr_var_init(ctx, &(vars[0]), "normalizer", -1); |
| 208 | grn_plugin_expr_var_init(ctx, &(vars[1]), "string", -1); |
| 209 | grn_plugin_expr_var_init(ctx, &(vars[2]), "flags", -1); |
| 210 | grn_plugin_expr_var_init(ctx, &(vars[3]), "normalizers", -1); |
| 211 | grn_plugin_command_create(ctx, |
| 212 | "normalize", -1, |
| 213 | command_normalize, |
| 214 | 4, |
| 215 | vars); |
| 216 | } |