mecab-sys 0.1.0

FFI binding and safe wrappers of MeCab
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
//
//  MeCab -- Yet Another Part-of-Speech and Morphological Analyzer
//
//  Copyright(C) 2001-2006 Taku Kudo <taku@chasen.org>
//  Copyright(C) 2004-2006 Nippon Telegraph and Telephone Corporation
#if defined(_WIN32) && !defined(__CYGWIN__)
#include <windows.h>
#endif

#include "mecab.h"
#include "tokenizer.h"
#include "utils.h"

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

namespace {
const char kUnknownError[] = "Unknown Error";
const size_t kErrorBufferSize = 256;
}

#if defined(_WIN32) && !defined(__CYGWIN__)
namespace {
DWORD g_tls_index = TLS_OUT_OF_INDEXES;
}

const char *getGlobalError() {
  LPVOID data = ::TlsGetValue(g_tls_index);
  return data == NULL ? kUnknownError : reinterpret_cast<const char *>(data);
}

void setGlobalError(const char *str) {
  char *data = reinterpret_cast<char *>(::TlsGetValue(g_tls_index));
  if (data == NULL) {
    return;
  }
  strncpy(data, str, kErrorBufferSize - 1);
  data[kErrorBufferSize - 1] = '\0';
}

HINSTANCE DllInstance = 0;

extern "C" {
  BOOL WINAPI DllMain(HINSTANCE hinst, DWORD dwReason, LPVOID) {
    LPVOID data = 0;
    if (!DllInstance) {
      DllInstance = hinst;
    }
    switch (dwReason) {
      case DLL_PROCESS_ATTACH:
        if ((g_tls_index = ::TlsAlloc()) == TLS_OUT_OF_INDEXES) {
          return FALSE;
        }
        // Not break in order to initialize the TLS.
      case DLL_THREAD_ATTACH:
        data = (LPVOID)::LocalAlloc(LPTR, kErrorBufferSize);
        if (data) {
          ::TlsSetValue(g_tls_index, data);
        }
        break;
      case DLL_THREAD_DETACH:
        data = ::TlsGetValue(g_tls_index);
        if (data) {
          ::LocalFree((HLOCAL)data);
        }
        break;
      case DLL_PROCESS_DETACH:
        data = ::TlsGetValue(g_tls_index);
        if (data) {
          ::LocalFree((HLOCAL)data);
        }
        ::TlsFree(g_tls_index);
        g_tls_index = TLS_OUT_OF_INDEXES;
        break;
      default:
        break;
    }
    return TRUE;
  }
}
#else  // _WIN32
namespace {
#ifdef HAVE_TLS_KEYWORD
__thread char kErrorBuffer[kErrorBufferSize];
#else
char kErrorBuffer[kErrorBufferSize];
#endif
}

const char *getGlobalError() {
  return kErrorBuffer;
}

void setGlobalError(const char *str) {
  strncpy(kErrorBuffer, str, kErrorBufferSize - 1);
  kErrorBuffer[kErrorBufferSize - 1] = '\0';
}
#endif

mecab_t* mecab_new(int argc, char **argv) {
  MeCab::Tagger *tagger = MeCab::createTagger(argc, argv);
  if (!tagger) {
    MeCab::deleteTagger(tagger);
    return 0;
  }
  return reinterpret_cast<mecab_t *>(tagger);
}

mecab_t* mecab_new2(const char *arg) {
  MeCab::Tagger *tagger = MeCab::createTagger(arg);
  if (!tagger) {
    MeCab::deleteTagger(tagger);
    return 0;
  }
  return reinterpret_cast<mecab_t *>(tagger);
}

const char *mecab_version() {
  return MeCab::Tagger::version();
}

const char* mecab_strerror(mecab_t *tagger) {
  if (!tagger) {
    return MeCab::getLastError();
  }
  return reinterpret_cast<MeCab::Tagger *>(tagger)->what();
}

void mecab_destroy(mecab_t *tagger) {
  MeCab::Tagger *ptr = reinterpret_cast<MeCab::Tagger *>(tagger);
  MeCab::deleteTagger(ptr);
  ptr = 0;
}

int  mecab_get_partial(mecab_t *tagger) {
  return reinterpret_cast<MeCab::Tagger *>(tagger)->partial();
}

void mecab_set_partial(mecab_t *tagger, int partial) {
  reinterpret_cast<MeCab::Tagger *>(tagger)->set_partial(partial);
}

float  mecab_get_theta(mecab_t *tagger) {
  return reinterpret_cast<MeCab::Tagger *>(tagger)->theta();
}

void mecab_set_theta(mecab_t *tagger, float theta) {
  reinterpret_cast<MeCab::Tagger *>(tagger)->set_theta(theta);
}

int  mecab_get_lattice_level(mecab_t *tagger) {
  return reinterpret_cast<MeCab::Tagger *>(tagger)->lattice_level();
}

void mecab_set_lattice_level(mecab_t *tagger, int level) {
  reinterpret_cast<MeCab::Tagger *>(tagger)->set_lattice_level(level);
}

int mecab_get_all_morphs(mecab_t *tagger) {
  return static_cast<int>(
      reinterpret_cast<MeCab::Tagger *>(tagger)->all_morphs());
}

void mecab_set_all_morphs(mecab_t *tagger, int all_morphs) {
  reinterpret_cast<MeCab::Tagger *>(tagger)->set_all_morphs(all_morphs);
}

const char* mecab_sparse_tostr(mecab_t *tagger, const char *str) {
  return reinterpret_cast<MeCab::Tagger *>(tagger)->parse(str);
}

const char* mecab_sparse_tostr2(mecab_t *tagger, const char *str, size_t len) {
  return reinterpret_cast<MeCab::Tagger *>(tagger)->parse(str, len);
}

char* mecab_sparse_tostr3(mecab_t *tagger, const char *str, size_t len,
                          char *out, size_t len2) {
  return const_cast<char *>(
      reinterpret_cast<MeCab::Tagger *>(tagger)->parse(
          str, len, out, len2));
}

const mecab_node_t* mecab_sparse_tonode(mecab_t *tagger, const char *str) {
  return reinterpret_cast<const mecab_node_t *>(
      reinterpret_cast<MeCab::Tagger *>(tagger)->parseToNode(str));
}

const mecab_node_t* mecab_sparse_tonode2(mecab_t *tagger,
                                         const char *str, size_t len) {
  return reinterpret_cast<const mecab_node_t *>(
      reinterpret_cast<MeCab::Tagger *>(tagger)->parseToNode(str, len));
}

const char* mecab_nbest_sparse_tostr(mecab_t *tagger, size_t N,
                                     const char *str) {
  return reinterpret_cast<MeCab::Tagger *>(tagger)->parseNBest(N, str);
}

const char* mecab_nbest_sparse_tostr2(mecab_t *tagger, size_t N,
                                      const char* str, size_t len) {
  return reinterpret_cast<MeCab::Tagger *>(
      tagger)->parseNBest(N, str, len);
}

char* mecab_nbest_sparse_tostr3(mecab_t *tagger, size_t N,
                                const char *str, size_t len,
                                char *out, size_t len2) {
  return const_cast<char *>(
      reinterpret_cast<MeCab::Tagger *>(
          tagger)->parseNBest(N, str, len, out, len2));
}

int mecab_nbest_init(mecab_t *tagger, const char *str) {
  return reinterpret_cast<
      MeCab::Tagger *>(tagger)->parseNBestInit(str);
}

int mecab_nbest_init2(mecab_t *tagger, const char *str, size_t len) {
  return reinterpret_cast<
      MeCab::Tagger *>(tagger)->parseNBestInit(str, len);
}

const char* mecab_nbest_next_tostr(mecab_t *tagger) {
  return reinterpret_cast<MeCab::Tagger *>(tagger)->next();
}

char* mecab_nbest_next_tostr2(mecab_t *tagger, char *out, size_t len2) {
  return const_cast<char *>(
      reinterpret_cast<MeCab::Tagger *>(tagger)->next(out, len2));
}

const mecab_node_t* mecab_nbest_next_tonode(mecab_t *tagger) {
  return reinterpret_cast<const mecab_node_t *>(
      reinterpret_cast<MeCab::Tagger *>(tagger)->nextNode());
}

const char* mecab_format_node(mecab_t *tagger, const mecab_node_t* n) {
  return reinterpret_cast<MeCab::Tagger *>(tagger)->formatNode(n);
}

const mecab_dictionary_info_t *mecab_dictionary_info(mecab_t *tagger) {
  return reinterpret_cast<const mecab_dictionary_info_t *>(
      reinterpret_cast<MeCab::Tagger *>(tagger)->dictionary_info());
}

int mecab_parse_lattice(mecab_t *mecab, mecab_lattice_t *lattice) {
  return static_cast<int>(
      reinterpret_cast<MeCab::Tagger *>(mecab)->parse(
          reinterpret_cast<MeCab::Lattice *>(lattice)));
}

mecab_lattice_t *mecab_lattice_new() {
  return reinterpret_cast<mecab_lattice_t *>(MeCab::createLattice());
}

void mecab_lattice_destroy(mecab_lattice_t *lattice) {
  MeCab::Lattice *ptr = reinterpret_cast<MeCab::Lattice *>(lattice);
  MeCab::deleteLattice(ptr);
  ptr = 0;
}

void mecab_lattice_clear(mecab_lattice_t *lattice) {
  reinterpret_cast<MeCab::Lattice *>(lattice)->clear();
}

int mecab_lattice_is_available(mecab_lattice_t *lattice) {
  return static_cast<int>(
      reinterpret_cast<MeCab::Lattice *>(lattice)->is_available());
}
mecab_node_t *mecab_lattice_get_bos_node(mecab_lattice_t *lattice) {
  return reinterpret_cast<mecab_node_t *>(
      reinterpret_cast<MeCab::Lattice *>(lattice)->bos_node());
}

mecab_node_t *mecab_lattice_get_eos_node(mecab_lattice_t *lattice) {
  return reinterpret_cast<mecab_node_t *>(
      reinterpret_cast<MeCab::Lattice *>(lattice)->eos_node());
}

mecab_node_t **mecab_lattice_get_all_begin_nodes(mecab_lattice_t *lattice) {
  return reinterpret_cast<mecab_node_t **>(
      reinterpret_cast<MeCab::Lattice *>(lattice)->begin_nodes());
}

mecab_node_t **mecab_lattice_get_all_end_nodes(mecab_lattice_t *lattice) {
  return reinterpret_cast<mecab_node_t **>(
      reinterpret_cast<MeCab::Lattice *>(lattice)->end_nodes());
}

mecab_node_t *mecab_lattice_get_begin_nodes(mecab_lattice_t *lattice,
                                            size_t pos) {
  return reinterpret_cast<mecab_node_t *>(
      reinterpret_cast<MeCab::Lattice *>(lattice)->begin_nodes(pos));
}

mecab_node_t    *mecab_lattice_get_end_nodes(mecab_lattice_t *lattice,
                                             size_t pos) {
  return reinterpret_cast<mecab_node_t *>(
      reinterpret_cast<MeCab::Lattice *>(lattice)->end_nodes(pos));
}

const char  *mecab_lattice_get_sentence(mecab_lattice_t *lattice) {
  return reinterpret_cast<MeCab::Lattice *>(lattice)->sentence();
}

void  mecab_lattice_set_sentence(mecab_lattice_t *lattice,
                                 const char *sentence) {
  reinterpret_cast<MeCab::Lattice *>(lattice)->set_sentence(sentence);
}

void mecab_lattice_set_sentence2(mecab_lattice_t *lattice,
                                 const char *sentence, size_t len) {
  reinterpret_cast<MeCab::Lattice *>(lattice)->set_sentence(
      sentence, len);
}

size_t mecab_lattice_get_size(mecab_lattice_t *lattice) {
  return reinterpret_cast<MeCab::Lattice *>(lattice)->size();
}

double mecab_lattice_get_z(mecab_lattice_t *lattice) {
  return reinterpret_cast<MeCab::Lattice *>(lattice)->Z();
}

void mecab_lattice_set_z(mecab_lattice_t *lattice, double Z) {
  reinterpret_cast<MeCab::Lattice *>(lattice)->set_Z(Z);
}

double mecab_lattice_get_theta(mecab_lattice_t *lattice) {
  return reinterpret_cast<MeCab::Lattice *>(lattice)->theta();
}

void mecab_lattice_set_theta(mecab_lattice_t *lattice, double theta) {
  reinterpret_cast<MeCab::Lattice *>(lattice)->set_theta(theta);
}

int mecab_lattice_next(mecab_lattice_t *lattice) {
  return static_cast<int>(
      reinterpret_cast<MeCab::Lattice *>(lattice)->next());
}

int mecab_lattice_get_request_type(mecab_lattice_t *lattice) {
  return reinterpret_cast<MeCab::Lattice *>(lattice)->request_type();
}

int mecab_lattice_has_request_type(mecab_lattice_t *lattice,
                                   int request_type) {
  return reinterpret_cast<MeCab::Lattice *>(
      lattice)->has_request_type(request_type);
}

void mecab_lattice_set_request_type(mecab_lattice_t *lattice,
                                    int request_type) {
  reinterpret_cast<MeCab::Lattice *>(
      lattice)->set_request_type(request_type);
}

void mecab_lattice_add_request_type(mecab_lattice_t *lattice,
                                    int request_type) {
  reinterpret_cast<MeCab::Lattice *>(
      lattice)->add_request_type(request_type);
}

void mecab_lattice_remove_request_type(mecab_lattice_t *lattice,
                                       int request_type) {
  return reinterpret_cast<MeCab::Lattice *>(
      lattice)->remove_request_type(request_type);
}

mecab_node_t    *mecab_lattice_new_node(mecab_lattice_t *lattice) {
  return reinterpret_cast<mecab_node_t *>(
      reinterpret_cast<MeCab::Lattice *>(lattice)->newNode());
}

const char *mecab_lattice_tostr(mecab_lattice_t *lattice) {
  return reinterpret_cast<MeCab::Lattice *>(lattice)->toString();
}

const char *mecab_lattice_tostr2(mecab_lattice_t *lattice,
                                 char *buf, size_t size) {
  return reinterpret_cast<MeCab::Lattice *>(
      lattice)->toString(buf, size);
}
const char *mecab_lattice_nbest_tostr(mecab_lattice_t *lattice,
                                      size_t N) {
  return reinterpret_cast<MeCab::Lattice *>(
      lattice)->enumNBestAsString(N);
}
const char *mecab_lattice_nbest_tostr2(mecab_lattice_t *lattice,
                                       size_t N, char *buf, size_t size) {
  return reinterpret_cast<MeCab::Lattice *>(
      lattice)->enumNBestAsString(N, buf, size);
}

int mecab_lattice_has_constraint(mecab_lattice_t *lattice) {
  return static_cast<bool>(reinterpret_cast<MeCab::Lattice *>(
                               lattice)->has_constraint());
}

int mecab_lattice_get_boundary_constraint(mecab_lattice_t *lattice,
                                          size_t pos) {
  return reinterpret_cast<MeCab::Lattice *>(
      lattice)->boundary_constraint(pos);
}

const char *mecab_lattice_get_feature_constraint(mecab_lattice_t *lattice,
                                                 size_t pos) {
  return reinterpret_cast<MeCab::Lattice *>(
      lattice)->feature_constraint(pos);
}

void mecab_lattice_set_boundary_constraint(mecab_lattice_t *lattice,
                                           size_t pos, int boundary_type) {
  return reinterpret_cast<MeCab::Lattice *>(
      lattice)->set_boundary_constraint(pos, boundary_type);
}

void mecab_lattice_set_feature_constraint(mecab_lattice_t *lattice,
                                          size_t begin_pos, size_t end_pos,
                                          const char *feature) {
  return reinterpret_cast<MeCab::Lattice *>(
      lattice)->set_feature_constraint(begin_pos, end_pos, feature);
}

void mecab_lattice_set_result(mecab_lattice_t *lattice,
                              const char *result) {
  return reinterpret_cast<MeCab::Lattice *>(lattice)->set_result(result);
}

const char *mecab_lattice_strerror(mecab_lattice_t *lattice) {
  return reinterpret_cast<MeCab::Lattice *>(lattice)->what();
}

mecab_model_t *mecab_model_new(int argc, char **argv) {
  MeCab::Model *model = MeCab::createModel(argc, argv);
  if (!model) {
    MeCab::deleteModel(model);
    return 0;
  }
  return reinterpret_cast<mecab_model_t *>(model);
}

mecab_model_t *mecab_model_new2(const char *arg) {
  MeCab::Model *model = MeCab::createModel(arg);
  if (!model) {
    MeCab::deleteModel(model);
    return 0;
  }
  return reinterpret_cast<mecab_model_t *>(model);
}

void mecab_model_destroy(mecab_model_t *model) {
  MeCab::Model *ptr = reinterpret_cast<MeCab::Model *>(model);
  MeCab::deleteModel(ptr);
  ptr = 0;
}

mecab_t *mecab_model_new_tagger(mecab_model_t *model) {
  return reinterpret_cast<mecab_t *>(
      reinterpret_cast<MeCab::Model *>(model)->createTagger());
}

mecab_lattice_t *mecab_model_new_lattice(mecab_model_t *model) {
  return reinterpret_cast<mecab_lattice_t *>(
      reinterpret_cast<MeCab::Model *>(model)->createLattice());
}

int mecab_model_swap(mecab_model_t *model, mecab_model_t *new_model) {
  return static_cast<int>(
      reinterpret_cast<MeCab::Model *>(model)->swap(
          reinterpret_cast<MeCab::Model *>(new_model)));
}

const mecab_dictionary_info_t* mecab_model_dictionary_info(
    mecab_model_t *model) {
  return reinterpret_cast<const mecab_dictionary_info_t *>(
      reinterpret_cast<MeCab::Model *>(model)->dictionary_info());
}

int mecab_model_transition_cost(mecab_model_t *model,
                                unsigned short rcAttr,
                                unsigned short lcAttr) {
  return reinterpret_cast<MeCab::Model *>(model)->transition_cost(
      rcAttr, lcAttr);
}

mecab_node_t *mecab_model_lookup(mecab_model_t *model,
                                 const char *begin,
                                 const char *end,
                                 mecab_lattice_t *lattice) {
  return reinterpret_cast<mecab_node_t *>(
      reinterpret_cast<MeCab::Model *>(model)->lookup(
          begin, end,
          reinterpret_cast<MeCab::Lattice *>(lattice)));
}