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