forked from taku910/mecab
-
-
Notifications
You must be signed in to change notification settings - Fork 15
/
libmecab.html
461 lines (390 loc) · 15.1 KB
/
libmecab.html
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
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>MeCab: Yet Another Japanese Dependency Structure Analyzer</title>
<link type="text/css" rel="stylesheet" href="mecab.css">
</head>
<body>
<h1>MeCab ライブラリ</h1>
<p>
MeCabはC/C++のライブラリを提供しています。また, SWIGを通して Perl/Ruby/Python から利用することも可能です。
</p>
<p>
シングルスレッド環境で単純な形態素解析を行う場合は、MeCab::Tagger クラスのみでほとんどのことが行えます。マルチスレッド環境で1つの辞書を共有しながら形態素解析を行いたい場合や、MeCabの辞書を解析中にアップデートするなど、高度な応用には MeCab::Taggerに加えて、 MeCab::Model, MeCab::Lattice クラスを使用します。
</p>
<p>
C++のAPIセットのドキュメントは<a href="doxygen/annotated.html">こちら</a>を参照ください。
CのAPIセットは<a href="doxygen/globals.html">こちら</a>を参照ください。
<h2>C++ サンプルコード</h2>
<p>シングルスレッド環境 (MeCab::Tagger)</p>
<ul>
<li>Taggerオブジェクトを生成すると、1つの辞書オブジェクトが生成されます。
</ul>
<pre>
#include <iostream>
#include <mecab.h>
#define CHECK(eval) if (! eval) { \
const char *e = tagger ? tagger->what() : MeCab::getTaggerError(); \
std::cerr << "Exception:" << e << std::endl; \
delete tagger; \
return -1; }
// Sample of MeCab::Tagger class.
int main (int argc, char **argv) {
char input[1024] = "太郎は次郎が持っている本を花子に渡した。";
MeCab::Tagger *tagger = MeCab::createTagger("");
CHECK(tagger);
// Gets tagged result in string format.
const char *result = tagger->parse(input);
CHECK(result);
std::cout << "INPUT: " << input << std::endl;
std::cout << "RESULT: " << result << std::endl;
// Gets N best results in string format.
result = tagger->parseNBest(3, input);
CHECK(result);
std::cout << "NBEST: " << std::endl << result;
// Gets N best results in sequence.
CHECK(tagger->parseNBestInit(input));
for (int i = 0; i < 3; ++i) {
std::cout << i << ":" << std::endl << tagger->next();
}
// Gets Node object.
const MeCab::Node* node = tagger->parseToNode(input);
CHECK(node);
for (; node; node = node->next) {
std::cout << node->id << ' ';
if (node->stat == MECAB_BOS_NODE)
std::cout << "BOS";
else if (node->stat == MECAB_EOS_NODE)
std::cout << "EOS";
else
std::cout.write (node->surface, node->length);
std::cout << ' ' << node->feature
<< ' ' << (int)(node->surface - input)
<< ' ' << (int)(node->surface - input + node->length)
<< ' ' << node->rcAttr
<< ' ' << node->lcAttr
<< ' ' << node->posid
<< ' ' << (int)node->char_type
<< ' ' << (int)node->stat
<< ' ' << (int)node->isbest
<< ' ' << node->alpha
<< ' ' << node->beta
<< ' ' << node->prob
<< ' ' << node->cost << std::endl;
}
// Dictionary info.
const MeCab::DictionaryInfo *d = tagger->dictionary_info();
for (; d; d = d->next) {
std::cout << "filename: " << d->filename << std::endl;
std::cout << "charset: " << d->charset << std::endl;
std::cout << "size: " << d->size << std::endl;
std::cout << "type: " << d->type << std::endl;
std::cout << "lsize: " << d->lsize << std::endl;
std::cout << "rsize: " << d->rsize << std::endl;
std::cout << "version: " << d->version << std::endl;
}
delete tagger;
return 0;
}
</pre>
<h2>C++ サンプルコード</h2>
<p>マルチスレッド環境 (MeCab::Tagger, MeCab::Model, MeCab::Lattice)</p>
<ul>
<li>MeCab::createModel() を使い、Modelオブジェクトを生成します
<li>model->createTagger() を使い、Taggerオブジェクトを生成します。Taggerはスレッド毎に複数作成しても、同一のmodelを共有します。Taggerがアクティブの間は、modelを削除してはなりません。
<li>model->createLattice もしくは MeCab::createLattice() を使い、Latticeオブジェクトを作成します。Latticeオプジェクトは解析に必要なすべてのローカル変数を含んでいます。必ずスレッド毎に1つのオブジェクトを作成してください
<li>model->swap(antoher_model) を呼ぶと、model から生成されたすべてのTaggerオブジェクトのモデルを another_modelに置き換えます。この操作はスレッドセーフです。
</ul>
<pre>
#include <iostream>
#include <mecab.h>
#define CHECK(eval) if (! eval) { \
const char *e = tagger ? tagger->what() : MeCab::getTaggerError(); \
std::cerr << "Exception:" << e << std::endl; \
delete tagger; \
return -1; }
int main (int argc, char **argv) {
char input[1024] = "太郎は次郎が持っている本を花子に渡した。";
// Create model object.
MeCab::Model *model = MeCab::createModel(argc, argv);
// Create Tagger
// All taggers generated by Model::createTagger() method share
// the same model/dictoinary.
MeCab::Tagger *tagger = model->createTagger();
CHECK(tagger);
// Create lattice object per thread.
MeCab::Lattice *lattice = model->createLattice();
// Gets tagged result in string
lattice->set_sentence(input);
// this method is thread safe, as long as |lattice| is thread local.
CHECK(tagger->parse(lattice));
std::cout << lattice->toString() << std::endl;
// Gets node object.
const MeCab::Node* node = lattice->bos_node();
CHECK(node);
for (; node; node = node->next) {
std::cout << node->id << ' ';
if (node->stat == MECAB_BOS_NODE)
std::cout << "BOS";
else if (node->stat == MECAB_EOS_NODE)
std::cout << "EOS";
else
std::cout.write (node->surface, node->length);
std::cout << ' ' << node->feature
<< ' ' << (int)(node->surface - input)
<< ' ' << (int)(node->surface - input + node->length)
<< ' ' << node->rcAttr
<< ' ' << node->lcAttr
<< ' ' << node->posid
<< ' ' << (int)node->char_type
<< ' ' << (int)node->stat
<< ' ' << (int)node->isbest
<< ' ' << node->alpha
<< ' ' << node->beta
<< ' ' << node->prob
<< ' ' << node->cost << std::endl;
}
// begin_nodes/end_nodes
const size_t len = lattice->size();
for (int i = 0; i <= len; ++i) {
MeCab::Node *b = lattice->begin_nodes(i);
MeCab::Node *e = lattice->end_nodes(i);
for (; b; b = b->bnext) {
printf("B[%d] %s\t%s\n", i, b->surface, b->feature);
}
for (; e; e = e->enext) {
printf("E[%d] %s\t%s\n", i, e->surface, e->feature);
}
}
// N best results
lattice->set_request_type(MECAB_NBEST);
lattice->set_sentence(input);
CHECK(tagger->parse(lattice));
for (int i = 0; i < 10; ++i) {
std::cout << "NBEST: " << i << std::endl;
std::cout << lattice->toString();
if (!lattice->next()) {
// No more results
break;
}
}
// Marginal probabilities
lattice->remove_request_type(MECAB_NBEST);
lattice->set_request_type(MECAB_MARGINAL_PROB);
lattice->set_sentence(input);
CHECK(tagger->parse(lattice));
std::cout << lattice->theta() << std::endl;
for (const MeCab::Node *node = lattice->bos_node();
node; node = node->next) {
std::cout.write(node->surface, node->length);
std::cout << "\t" << node->feature;
std::cout << "\t" << node->prob << std::endl;
}
// Dictionary info
const MeCab::DictionaryInfo *d = model->dictionary_info();
for (; d; d = d->next) {
std::cout << "filename: " << d->filename << std::endl;
std::cout << "charset: " << d->charset << std::endl;
std::cout << "size: " << d->size << std::endl;
std::cout << "type: " << d->type << std::endl;
std::cout << "lsize: " << d->lsize << std::endl;
std::cout << "rsize: " << d->rsize << std::endl;
std::cout << "version: " << d->version << std::endl;
}
// Swap model atomically.
MeCab::Model *another_model = MeCab::createModel("");
model->swap(another_model);
delete lattice;
delete tagger;
delete model;
return 0;
}
</pre>
<h2>C サンプルコード</h2>
シングルスレッド環境
<pre>
#include <mecab.h>
#include <stdio.h>
#define CHECK(eval) if (! eval) { \
fprintf (stderr, "Exception:%s\n", mecab_strerror (mecab)); \
mecab_destroy(mecab); \
return -1; }
int main (int argc, char **argv) {
char input[] = "太郎は次郎が持っている本を花子に渡した。";
mecab_t *mecab;
const mecab_node_t *node;
const char *result;
int i;
size_t len;
// Create tagger object
mecab = mecab_new(argc, argv);
CHECK(mecab);
// Gets tagged result in string.
result = mecab_sparse_tostr(mecab, input);
CHECK(result)
printf ("INPUT: %s\n", input);
printf ("RESULT:\n%s", result);
// Gets N best results
result = mecab_nbest_sparse_tostr (mecab, 3, input);
CHECK(result);
fprintf (stdout, "NBEST:\n%s", result);
CHECK(mecab_nbest_init(mecab, input));
for (i = 0; i < 3; ++i) {
printf ("%d:\n%s", i, mecab_nbest_next_tostr (mecab));
}
// Gets node object
node = mecab_sparse_tonode(mecab, input);
CHECK(node);
for (; node; node = node->next) {
if (node->stat == MECAB_NOR_NODE || node->stat == MECAB_UNK_NODE) {
fwrite (node->surface, sizeof(char), node->length, stdout);
printf("\t%s\n", node->feature);
}
}
// Dictionary info
const mecab_dictionary_info_t *d = mecab_dictionary_info(mecab);
for (; d; d = d->next) {
printf("filename: %s\n", d->filename);
printf("charset: %s\n", d->charset);
printf("size: %d\n", d->size);
printf("type: %d\n", d->type);
printf("lsize: %d\n", d->lsize);
printf("rsize: %d\n", d->rsize);
printf("version: %d\n", d->version);
}
mecab_destroy(mecab);
return 0;
}
</pre>
<h2>C サンプルコード</h2>
マルチスレッド環境
<pre>
#include <mecab.h>
#include <stdio.h>
#define CHECK(eval) if (! eval) { \
fprintf (stderr, "Exception:%s\n", mecab_strerror (mecab)); \
mecab_destroy(mecab); \
return -1; }
int main (int argc, char **argv) {
char input[] = "太郎は次郎が持っている本を花子に渡した。";
mecab_model_t *model, *another_model;
mecab_t *mecab;
mecab_lattice_t *lattice;
const mecab_node_t *node;
const char *result;
int i;
size_t len;
model = mecab_model_new(argc, argv);
CHECK(model);
mecab = mecab_model_new_tagger(model);
CHECK(mecab);
lattice = mecab_model_new_lattice(model);
CHECK(lattice);
mecab_lattice_set_sentence(lattice, input);
mecab_parse_lattice(mecab, lattice);
printf("RESULT: %s\n", mecab_lattice_tostr(lattice));
node = mecab_lattice_get_bos_node(lattice);
for (; node; node = node->next) {
printf("%d ", node->id);
if (node->stat == MECAB_BOS_NODE)
printf("BOS");
else if (node->stat == MECAB_EOS_NODE)
printf("EOS");
else
fwrite (node->surface, sizeof(char), node->length, stdout);
printf(" %s %d %d %d %d %d %d %d %d %f %f %f %ld\n",
node->feature,
(int)(node->surface - input),
(int)(node->surface - input + node->length),
node->rcAttr,
node->lcAttr,
node->posid,
(int)node->char_type,
(int)node->stat,
(int)node->isbest,
node->alpha,
node->beta,
node->prob,
node->cost);
}
len = mecab_lattice_get_size(lattice);
for (i = 0; i <= len; ++i) {
mecab_node_t *b, *e;
b = mecab_lattice_get_begin_nodes(lattice, (size_t)i);
e = mecab_lattice_get_end_nodes(lattice, (size_t)i);
for (; b; b = b->bnext) {
printf("B[%d] %s\t%s\n", i, b->surface, b->feature);
}
for (; e; e = e->enext) {
printf("E[%d] %s\t%s\n", i, e->surface, e->feature);
}
}
mecab_lattice_set_sentence(lattice, input);
mecab_lattice_set_request_type(lattice, MECAB_NBEST);
mecab_parse_lattice(mecab, lattice);
for (i = 0; i < 10; ++i) {
fprintf(stdout, "%s", mecab_lattice_tostr(lattice));
if (!mecab_lattice_next(lattice)) {
break;
}
}
mecab_lattice_set_sentence(lattice, input);
mecab_lattice_set_request_type(lattice, MECAB_MARGINAL_PROB);
mecab_lattice_set_theta(lattice, 0.001);
mecab_parse_lattice(mecab, lattice);
node = mecab_lattice_get_bos_node(lattice);
for (; node; node = node->next) {
fwrite(node->surface, sizeof(char), node->length, stdout);
fprintf(stdout, "\t%s\t%f\n", node->feature, node->prob);
}
mecab_set_lattice_level(mecab, 0);
mecab_set_all_morphs(mecab, 1);
node = mecab_sparse_tonode(mecab, input);
CHECK(node);
for (; node; node = node->next) {
fwrite (node->surface, sizeof(char), node->length, stdout);
printf("\t%s\n", node->feature);
}
const mecab_dictionary_info_t *d = mecab_dictionary_info(mecab);
for (; d; d = d->next) {
printf("filename: %s\n", d->filename);
printf("charset: %s\n", d->charset);
printf("size: %d\n", d->size);
printf("type: %d\n", d->type);
printf("lsize: %d\n", d->lsize);
printf("rsize: %d\n", d->rsize);
printf("version: %d\n", d->version);
}
mecab_destroy(mecab);
mecab_lattice_destroy(lattice);
mecab_model_destroy(model);
return 0;
}
</pre>
<h2><a name="compile">コンパイル方法</a></h2>
<ul>
<li>UNIX の場合 </li>
<pre>
% cc -O2 `mecab-config --cflags` example.c -o example \
`mecab-config --libs`
</pre>
<li>Windows の場合 </li>
<p>まず, コンパイル作業を行うディレクトリに include\mecab.h,
bin\libmecab.dll lib\libmecab.lib をコピーします.
この後の作業は, 使用するコンパイラによって微妙に変わります.</p>
<ul>
<li>cygwin/mingw 環境の場合</li>
<pre>
% gcc -DDLL_IMPORT -I. example.c -o example.exe libmecab.dll
</pre>
<li>VC++ 環境の場合</li>
<pre>
% cl -DDLL_IMPORT -I. example.c libmecab.lib
</pre>
</ul>
</ul>
<hr>
</body>
</html>