| 1 | /* -*- c-basic-offset: 2 -*- */ |
| 2 | /* |
| 3 | Copyright(C) 2012-2018 Brazil |
| 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 tokenizers_kytea |
| 22 | #endif |
| 23 | |
| 24 | #include <groonga/tokenizer.h> |
| 25 | |
| 26 | #include <kytea/kytea.h> |
| 27 | #include <kytea/string-util.h> |
| 28 | |
| 29 | #include <string.h> |
| 30 | |
| 31 | #include <string> |
| 32 | #include <vector> |
| 33 | |
| 34 | namespace { |
| 35 | |
| 36 | grn_plugin_mutex *kytea_mutex = NULL; |
| 37 | kytea::KyteaConfig *kytea_config = NULL; |
| 38 | kytea::Kytea *kytea_tagger = NULL; |
| 39 | kytea::StringUtil *kytea_util = NULL; |
| 40 | |
| 41 | void kytea_init(grn_ctx *ctx); |
| 42 | void kytea_fin(grn_ctx *ctx); |
| 43 | |
| 44 | void kytea_init(grn_ctx *ctx) { |
| 45 | if (kytea_mutex || kytea_config || kytea_tagger || kytea_util) { |
| 46 | GRN_PLUGIN_ERROR(ctx, GRN_TOKENIZER_ERROR, |
| 47 | "[tokenizer][kytea] " |
| 48 | "TokenKytea is already initialized"); |
| 49 | return; |
| 50 | } |
| 51 | |
| 52 | kytea_mutex = grn_plugin_mutex_open(ctx); |
| 53 | if (!kytea_mutex) { |
| 54 | kytea_fin(ctx); |
| 55 | GRN_PLUGIN_ERROR(ctx, GRN_NO_MEMORY_AVAILABLE, |
| 56 | "[tokenizer][kytea] " |
| 57 | "grn_plugin_mutex_open() failed"); |
| 58 | return; |
| 59 | } |
| 60 | |
| 61 | kytea::KyteaConfig * const config = static_cast<kytea::KyteaConfig *>( |
| 62 | GRN_PLUGIN_MALLOC(ctx, sizeof(kytea::KyteaConfig))); |
| 63 | if (!config) { |
| 64 | kytea_fin(ctx); |
| 65 | GRN_PLUGIN_ERROR(ctx, GRN_NO_MEMORY_AVAILABLE, |
| 66 | "[tokenizer][kytea] " |
| 67 | "memory allocation to kytea::KyteaConfig failed"); |
| 68 | return; |
| 69 | } |
| 70 | |
| 71 | try { |
| 72 | new (config) kytea::KyteaConfig; |
| 73 | kytea_config = config; |
| 74 | try { |
| 75 | kytea_config->setDebug(0); |
| 76 | kytea_config->setOnTraining(false); |
| 77 | kytea_config->parseRunCommandLine(0, NULL); |
| 78 | } catch (...) { |
| 79 | kytea_fin(ctx); |
| 80 | GRN_PLUGIN_ERROR(ctx, GRN_TOKENIZER_ERROR, |
| 81 | "[tokenizer][kytea] " |
| 82 | "kytea::KyteaConfig settings failed"); |
| 83 | return; |
| 84 | } |
| 85 | } catch (...) { |
| 86 | GRN_PLUGIN_FREE(ctx, config); |
| 87 | kytea_fin(ctx); |
| 88 | GRN_PLUGIN_ERROR(ctx, GRN_TOKENIZER_ERROR, |
| 89 | "[tokenizer][kytea] " |
| 90 | "kytea::KyteaConfig initialization failed"); |
| 91 | return; |
| 92 | } |
| 93 | |
| 94 | kytea::Kytea * const tagger = static_cast<kytea::Kytea *>( |
| 95 | GRN_PLUGIN_MALLOC(ctx, sizeof(kytea::Kytea))); |
| 96 | if (!tagger) { |
| 97 | kytea_fin(ctx); |
| 98 | GRN_PLUGIN_ERROR(ctx, GRN_NO_MEMORY_AVAILABLE, |
| 99 | "[tokenizer][kytea] " |
| 100 | "memory allocation to kytea::Kytea failed"); |
| 101 | return; |
| 102 | } |
| 103 | |
| 104 | try { |
| 105 | new (tagger) kytea::Kytea; |
| 106 | kytea_tagger = tagger; |
| 107 | try { |
| 108 | kytea_tagger->readModel(kytea_config->getModelFile().c_str()); |
| 109 | } catch (...) { |
| 110 | kytea_fin(ctx); |
| 111 | GRN_PLUGIN_ERROR(ctx, GRN_TOKENIZER_ERROR, |
| 112 | "[tokenizer][kytea] " |
| 113 | "kytea::Kytea::readModel() failed"); |
| 114 | return; |
| 115 | } |
| 116 | } catch (...) { |
| 117 | GRN_PLUGIN_FREE(ctx, tagger); |
| 118 | kytea_fin(ctx); |
| 119 | GRN_PLUGIN_ERROR(ctx, GRN_TOKENIZER_ERROR, |
| 120 | "[tokenizer][kytea] " |
| 121 | "kytea::Kytea initialization failed"); |
| 122 | return; |
| 123 | } |
| 124 | |
| 125 | try { |
| 126 | kytea_util = kytea_tagger->getStringUtil(); |
| 127 | } catch (...) { |
| 128 | kytea_fin(ctx); |
| 129 | GRN_PLUGIN_ERROR(ctx, GRN_TOKENIZER_ERROR, |
| 130 | "[tokenizer][kytea] " |
| 131 | "kytea::Kytea::getStringUtil() failed"); |
| 132 | return; |
| 133 | } |
| 134 | } |
| 135 | |
| 136 | void kytea_fin(grn_ctx *ctx) { |
| 137 | kytea_util = NULL; |
| 138 | |
| 139 | if (kytea_tagger) { |
| 140 | kytea_tagger->~Kytea(); |
| 141 | GRN_PLUGIN_FREE(ctx, kytea_tagger); |
| 142 | kytea_tagger = NULL; |
| 143 | } |
| 144 | |
| 145 | if (kytea_config) { |
| 146 | kytea_config->~KyteaConfig(); |
| 147 | GRN_PLUGIN_FREE(ctx, kytea_config); |
| 148 | kytea_config = NULL; |
| 149 | } |
| 150 | |
| 151 | if (kytea_mutex) { |
| 152 | grn_plugin_mutex_close(ctx, kytea_mutex); |
| 153 | kytea_mutex = NULL; |
| 154 | } |
| 155 | } |
| 156 | |
| 157 | struct grn_tokenizer_kytea { |
| 158 | grn_tokenizer_query *query; |
| 159 | kytea::KyteaSentence sentence; |
| 160 | std::vector<std::string> tokens; |
| 161 | std::size_t id; |
| 162 | grn_tokenizer_token token; |
| 163 | const char *rest_query_string; |
| 164 | unsigned int rest_query_string_length; |
| 165 | |
| 166 | grn_tokenizer_kytea() : |
| 167 | query(NULL), |
| 168 | sentence(), |
| 169 | tokens(), |
| 170 | id(0), |
| 171 | token(), |
| 172 | rest_query_string(NULL) |
| 173 | { |
| 174 | } |
| 175 | ~grn_tokenizer_kytea() {} |
| 176 | }; |
| 177 | |
| 178 | void grn_tokenizer_kytea_init(grn_ctx *ctx, grn_tokenizer_kytea *tokenizer) { |
| 179 | new (tokenizer) grn_tokenizer_kytea; |
| 180 | grn_tokenizer_token_init(ctx, &tokenizer->token); |
| 181 | } |
| 182 | |
| 183 | void grn_tokenizer_kytea_fin(grn_ctx *ctx, grn_tokenizer_kytea *tokenizer) { |
| 184 | grn_tokenizer_token_fin(ctx, &tokenizer->token); |
| 185 | if (tokenizer->query) { |
| 186 | grn_tokenizer_query_close(ctx, tokenizer->query); |
| 187 | } |
| 188 | tokenizer->~grn_tokenizer_kytea(); |
| 189 | } |
| 190 | |
| 191 | grn_obj *grn_kytea_init(grn_ctx *ctx, int num_args, grn_obj **args, |
| 192 | grn_user_data *user_data) { |
| 193 | unsigned int normalizer_flags = 0; |
| 194 | grn_tokenizer_query * const query = |
| 195 | grn_tokenizer_query_open(ctx, num_args, args, normalizer_flags); |
| 196 | if (!query) { |
| 197 | return NULL; |
| 198 | } |
| 199 | |
| 200 | grn_tokenizer_kytea * const tokenizer = static_cast<grn_tokenizer_kytea *>( |
| 201 | GRN_PLUGIN_MALLOC(ctx, sizeof(grn_tokenizer_kytea))); |
| 202 | if (!tokenizer) { |
| 203 | grn_tokenizer_query_close(ctx, query); |
| 204 | GRN_PLUGIN_ERROR(ctx, GRN_NO_MEMORY_AVAILABLE, |
| 205 | "[tokenizer][kytea] " |
| 206 | "memory allocation to grn_tokenizer_kytea failed"); |
| 207 | return NULL; |
| 208 | } |
| 209 | |
| 210 | try { |
| 211 | grn_tokenizer_kytea_init(ctx, tokenizer); |
| 212 | } catch (...) { |
| 213 | grn_tokenizer_query_close(ctx, query); |
| 214 | GRN_PLUGIN_ERROR(ctx, GRN_TOKENIZER_ERROR, |
| 215 | "[tokenizer][kytea] " |
| 216 | "tokenizer initialization failed"); |
| 217 | return NULL; |
| 218 | } |
| 219 | |
| 220 | tokenizer->query = query; |
| 221 | |
| 222 | grn_obj *string = grn_tokenizer_query_get_normalized_string(ctx, query); |
| 223 | const char *normalized_string; |
| 224 | unsigned int normalized_string_length; |
| 225 | grn_string_get_normalized(ctx, |
| 226 | string, |
| 227 | &normalized_string, |
| 228 | &normalized_string_length, |
| 229 | NULL); |
| 230 | if (grn_tokenizer_query_have_tokenized_delimiter(ctx, tokenizer->query)) { |
| 231 | tokenizer->rest_query_string = normalized_string; |
| 232 | tokenizer->rest_query_string_length = normalized_string_length; |
| 233 | } else { |
| 234 | grn_plugin_mutex_lock(ctx, kytea_mutex); |
| 235 | try { |
| 236 | const std::string str(normalized_string, normalized_string_length); |
| 237 | const kytea::KyteaString &surface_str = kytea_util->mapString(str); |
| 238 | const kytea::KyteaString &normalized_str = kytea_util->normalize(surface_str); |
| 239 | tokenizer->sentence = kytea::KyteaSentence(surface_str, normalized_str); |
| 240 | kytea_tagger->calculateWS(tokenizer->sentence); |
| 241 | } catch (...) { |
| 242 | grn_plugin_mutex_unlock(ctx, kytea_mutex); |
| 243 | GRN_PLUGIN_ERROR(ctx, GRN_TOKENIZER_ERROR, |
| 244 | "[tokenizer][kytea] " |
| 245 | "tokenization failed"); |
| 246 | return NULL; |
| 247 | } |
| 248 | grn_plugin_mutex_unlock(ctx, kytea_mutex); |
| 249 | |
| 250 | try { |
| 251 | grn_encoding encoding = grn_tokenizer_query_get_encoding(ctx, query); |
| 252 | for (std::size_t i = 0; i < tokenizer->sentence.words.size(); ++i) { |
| 253 | const std::string &token = |
| 254 | kytea_util->showString(tokenizer->sentence.words[i].surface); |
| 255 | const char *ptr = token.c_str(); |
| 256 | unsigned int left = static_cast<unsigned int>(token.length()); |
| 257 | while (left > 0) { |
| 258 | const int char_length = |
| 259 | grn_tokenizer_charlen(ctx, ptr, left, encoding); |
| 260 | if ((char_length == 0) || |
| 261 | (grn_tokenizer_isspace(ctx, ptr, left, encoding) != 0)) { |
| 262 | break; |
| 263 | } |
| 264 | ptr += char_length; |
| 265 | left -= char_length; |
| 266 | } |
| 267 | if (left == 0) { |
| 268 | tokenizer->tokens.push_back(token); |
| 269 | } |
| 270 | } |
| 271 | } catch (...) { |
| 272 | GRN_PLUGIN_ERROR(ctx, GRN_TOKENIZER_ERROR, |
| 273 | "[tokenizer][kytea] " |
| 274 | "adjustment failed"); |
| 275 | return NULL; |
| 276 | } |
| 277 | } |
| 278 | |
| 279 | user_data->ptr = tokenizer; |
| 280 | return NULL; |
| 281 | } |
| 282 | |
| 283 | grn_obj *grn_kytea_next(grn_ctx *ctx, int num_args, grn_obj **args, |
| 284 | grn_user_data *user_data) { |
| 285 | grn_tokenizer_kytea * const tokenizer = |
| 286 | static_cast<grn_tokenizer_kytea *>(user_data->ptr); |
| 287 | grn_tokenizer_query * const query = tokenizer->query; |
| 288 | |
| 289 | if (grn_tokenizer_query_have_tokenized_delimiter(ctx, query)) { |
| 290 | unsigned int rest_query_string_length = tokenizer->rest_query_string_length; |
| 291 | grn_encoding encoding = grn_tokenizer_query_get_encoding(ctx, query); |
| 292 | const char *rest_query_string = |
| 293 | grn_tokenizer_tokenized_delimiter_next(ctx, |
| 294 | &(tokenizer->token), |
| 295 | tokenizer->rest_query_string, |
| 296 | rest_query_string_length, |
| 297 | encoding); |
| 298 | if (rest_query_string) { |
| 299 | tokenizer->rest_query_string_length -= |
| 300 | rest_query_string - tokenizer->rest_query_string; |
| 301 | } |
| 302 | tokenizer->rest_query_string = rest_query_string; |
| 303 | } else { |
| 304 | const grn_tokenizer_status status = |
| 305 | ((tokenizer->id + 1) < tokenizer->tokens.size()) ? |
| 306 | GRN_TOKENIZER_CONTINUE : GRN_TOKENIZER_LAST; |
| 307 | if (tokenizer->id < tokenizer->tokens.size()) { |
| 308 | const std::string &token = tokenizer->tokens[tokenizer->id++]; |
| 309 | grn_tokenizer_token_push(ctx, &tokenizer->token, |
| 310 | token.c_str(), token.length(), status); |
| 311 | } else { |
| 312 | grn_tokenizer_token_push(ctx, &tokenizer->token, "", 0, status); |
| 313 | } |
| 314 | } |
| 315 | |
| 316 | return NULL; |
| 317 | } |
| 318 | |
| 319 | grn_obj *grn_kytea_fin(grn_ctx *ctx, int num_args, grn_obj **args, |
| 320 | grn_user_data *user_data) { |
| 321 | grn_tokenizer_kytea * const tokenizer = |
| 322 | static_cast<grn_tokenizer_kytea *>(user_data->ptr); |
| 323 | if (tokenizer) { |
| 324 | grn_tokenizer_kytea_fin(ctx, tokenizer); |
| 325 | GRN_PLUGIN_FREE(ctx, tokenizer); |
| 326 | } |
| 327 | return NULL; |
| 328 | } |
| 329 | |
| 330 | } // namespace |
| 331 | |
| 332 | extern "C" { |
| 333 | |
| 334 | /* |
| 335 | GRN_PLUGIN_INIT() is called to initialize this plugin. Note that an error |
| 336 | code must be set in `ctx->rc' on failure. |
| 337 | */ |
| 338 | grn_rc GRN_PLUGIN_INIT(grn_ctx *ctx) { |
| 339 | kytea_init(ctx); |
| 340 | return ctx->rc; |
| 341 | } |
| 342 | |
| 343 | /* |
| 344 | GRN_PLUGIN_REGISTER() registers this plugin to the database associated with |
| 345 | `ctx'. The registration requires the plugin name and the functions to be |
| 346 | called for tokenization. |
| 347 | */ |
| 348 | grn_rc GRN_PLUGIN_REGISTER(grn_ctx *ctx) { |
| 349 | return grn_tokenizer_register(ctx, "TokenKytea", 10, grn_kytea_init, |
| 350 | grn_kytea_next, grn_kytea_fin); |
| 351 | } |
| 352 | |
| 353 | /* |
| 354 | GRN_PLUGIN_FIN() is called to finalize the plugin that was initialized by |
| 355 | GRN_PLUGIN_INIT(). |
| 356 | */ |
| 357 | grn_rc GRN_PLUGIN_FIN(grn_ctx *ctx) { |
| 358 | kytea_fin(ctx); |
| 359 | return GRN_SUCCESS; |
| 360 | } |
| 361 | |
| 362 | } // extern "C" |