| 1 |
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" |
| 2 |
"http://www.w3.org/TR/html4/strict.dtd"> |
| 3 |
<html> |
| 4 |
<head> |
| 5 |
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> |
| 6 |
<title>MeCab: Yet Another Japanese Dependency Structure Analyzer</title> |
| 7 |
<link type="text/css" rel="stylesheet" href="mecab.css"> |
| 8 |
</head> |
| 9 |
<body> |
| 10 |
<h1>MeCab ライブラリ</h1> |
| 11 |
<p> |
| 12 |
MeCabはC/C++のライブラリを提供しています。また, SWIGを通して Perl/Ruby/Python から利用することも可能です。 |
| 13 |
</p> |
| 14 |
<p> |
| 15 |
シングルスレッド環境で単純な形態素解析を行う場合は、MeCab::Tagger クラスのみでほとんどのことが行えます。マルチスレッド環境で1つの辞書を共有しながら形態素解析を行いたい場合や、MeCabの辞書を解析中にアップデートするなど、高度な応用には MeCab::Taggerに加えて、 MeCab::Model, MeCab::Lattice クラスを使用します。 |
| 16 |
</p> |
| 17 |
<p> |
| 18 |
C++のAPIセットのドキュメントは<a href="doxygen/annotated.html">こちら</a>を参照ください。 |
| 19 |
CのAPIセットは<a href="doxygen/globals.html">こちら</a>を参照ください。 |
| 20 |
|
| 21 |
<h2>C++ サンプルコード</h2> |
| 22 |
<p>シングルスレッド環境 (MeCab::Tagger)</p> |
| 23 |
<ul> |
| 24 |
<li>Taggerオブジェクトを生成すると、1つの辞書オブジェクトが生成されます。 |
| 25 |
</ul> |
| 26 |
<pre> |
| 27 |
#include <iostream> |
| 28 |
#include <mecab.h> |
| 29 |
|
| 30 |
#define CHECK(eval) if (! eval) { \ |
| 31 |
const char *e = tagger ? tagger->what() : MeCab::getTaggerError(); \ |
| 32 |
std::cerr << "Exception:" << e << std::endl; \ |
| 33 |
delete tagger; \ |
| 34 |
return -1; } |
| 35 |
|
| 36 |
// Sample of MeCab::Tagger class. |
| 37 |
int main (int argc, char **argv) { |
| 38 |
char input[1024] = "太郎は次郎が持っている本を花子に渡した。"; |
| 39 |
|
| 40 |
MeCab::Tagger *tagger = MeCab::createTagger(""); |
| 41 |
CHECK(tagger); |
| 42 |
|
| 43 |
// Gets tagged result in string format. |
| 44 |
const char *result = tagger->parse(input); |
| 45 |
CHECK(result); |
| 46 |
std::cout << "INPUT: " << input << std::endl; |
| 47 |
std::cout << "RESULT: " << result << std::endl; |
| 48 |
|
| 49 |
// Gets N best results in string format. |
| 50 |
result = tagger->parseNBest(3, input); |
| 51 |
CHECK(result); |
| 52 |
std::cout << "NBEST: " << std::endl << result; |
| 53 |
|
| 54 |
// Gets N best results in sequence. |
| 55 |
CHECK(tagger->parseNBestInit(input)); |
| 56 |
for (int i = 0; i < 3; ++i) { |
| 57 |
std::cout << i << ":" << std::endl << tagger->next(); |
| 58 |
} |
| 59 |
|
| 60 |
// Gets Node object. |
| 61 |
const MeCab::Node* node = tagger->parseToNode(input); |
| 62 |
CHECK(node); |
| 63 |
for (; node; node = node->next) { |
| 64 |
std::cout << node->id << ' '; |
| 65 |
if (node->stat == MECAB_BOS_NODE) |
| 66 |
std::cout << "BOS"; |
| 67 |
else if (node->stat == MECAB_EOS_NODE) |
| 68 |
std::cout << "EOS"; |
| 69 |
else |
| 70 |
std::cout.write (node->surface, node->length); |
| 71 |
|
| 72 |
std::cout << ' ' << node->feature |
| 73 |
<< ' ' << (int)(node->surface - input) |
| 74 |
<< ' ' << (int)(node->surface - input + node->length) |
| 75 |
<< ' ' << node->rcAttr |
| 76 |
<< ' ' << node->lcAttr |
| 77 |
<< ' ' << node->posid |
| 78 |
<< ' ' << (int)node->char_type |
| 79 |
<< ' ' << (int)node->stat |
| 80 |
<< ' ' << (int)node->isbest |
| 81 |
<< ' ' << node->alpha |
| 82 |
<< ' ' << node->beta |
| 83 |
<< ' ' << node->prob |
| 84 |
<< ' ' << node->cost << std::endl; |
| 85 |
} |
| 86 |
|
| 87 |
// Dictionary info. |
| 88 |
const MeCab::DictionaryInfo *d = tagger->dictionary_info(); |
| 89 |
for (; d; d = d->next) { |
| 90 |
std::cout << "filename: " << d->filename << std::endl; |
| 91 |
std::cout << "charset: " << d->charset << std::endl; |
| 92 |
std::cout << "size: " << d->size << std::endl; |
| 93 |
std::cout << "type: " << d->type << std::endl; |
| 94 |
std::cout << "lsize: " << d->lsize << std::endl; |
| 95 |
std::cout << "rsize: " << d->rsize << std::endl; |
| 96 |
std::cout << "version: " << d->version << std::endl; |
| 97 |
} |
| 98 |
|
| 99 |
delete tagger; |
| 100 |
|
| 101 |
return 0; |
| 102 |
} |
| 103 |
|
| 104 |
</pre> |
| 105 |
|
| 106 |
<h2>C++ サンプルコード</h2> |
| 107 |
<p>マルチスレッド環境 (MeCab::Tagger, MeCab::Model, MeCab::Lattice)</p> |
| 108 |
<ul> |
| 109 |
<li>MeCab::createModel() を使い、Modelオブジェクトを生成します |
| 110 |
<li>model->createTagger() を使い、Taggerオブジェクトを生成します。Taggerはスレッド毎に複数作成しても、同一のmodelを共有します。Taggerがアクティブの間は、modelを削除してはなりません。 |
| 111 |
<li>model->createLattice もしくは MeCab::createLattice() を使い、Latticeオブジェクトを作成します。Latticeオプジェクトは解析に必要なすべてのローカル変数を含んでいます。必ずスレッド毎に1つのオブジェクトを作成してください |
| 112 |
<li>model->swap(antoher_model) を呼ぶと、model から生成されたすべてのTaggerオブジェクトのモデルを another_modelに置き換えます。この操作はスレッドセーフです。 |
| 113 |
</ul> |
| 114 |
<pre> |
| 115 |
#include <iostream> |
| 116 |
#include <mecab.h> |
| 117 |
|
| 118 |
#define CHECK(eval) if (! eval) { \ |
| 119 |
const char *e = tagger ? tagger->what() : MeCab::getTaggerError(); \ |
| 120 |
std::cerr << "Exception:" << e << std::endl; \ |
| 121 |
delete tagger; \ |
| 122 |
return -1; } |
| 123 |
|
| 124 |
int main (int argc, char **argv) { |
| 125 |
char input[1024] = "太郎は次郎が持っている本を花子に渡した。"; |
| 126 |
|
| 127 |
// Create model object. |
| 128 |
MeCab::Model *model = MeCab::createModel(argc, argv); |
| 129 |
|
| 130 |
// Create Tagger |
| 131 |
// All taggers generated by Model::createTagger() method share |
| 132 |
// the same model/dictoinary. |
| 133 |
MeCab::Tagger *tagger = model->createTagger(); |
| 134 |
CHECK(tagger); |
| 135 |
|
| 136 |
// Create lattice object per thread. |
| 137 |
MeCab::Lattice *lattice = model->createLattice(); |
| 138 |
|
| 139 |
// Gets tagged result in string |
| 140 |
lattice->set_sentence(input); |
| 141 |
|
| 142 |
// this method is thread safe, as long as |lattice| is thread local. |
| 143 |
CHECK(tagger->parse(lattice)); |
| 144 |
std::cout << lattice->toString() << std::endl; |
| 145 |
|
| 146 |
// Gets node object. |
| 147 |
const MeCab::Node* node = lattice->bos_node(); |
| 148 |
CHECK(node); |
| 149 |
for (; node; node = node->next) { |
| 150 |
std::cout << node->id << ' '; |
| 151 |
if (node->stat == MECAB_BOS_NODE) |
| 152 |
std::cout << "BOS"; |
| 153 |
else if (node->stat == MECAB_EOS_NODE) |
| 154 |
std::cout << "EOS"; |
| 155 |
else |
| 156 |
std::cout.write (node->surface, node->length); |
| 157 |
|
| 158 |
std::cout << ' ' << node->feature |
| 159 |
<< ' ' << (int)(node->surface - input) |
| 160 |
<< ' ' << (int)(node->surface - input + node->length) |
| 161 |
<< ' ' << node->rcAttr |
| 162 |
<< ' ' << node->lcAttr |
| 163 |
<< ' ' << node->posid |
| 164 |
<< ' ' << (int)node->char_type |
| 165 |
<< ' ' << (int)node->stat |
| 166 |
<< ' ' << (int)node->isbest |
| 167 |
<< ' ' << node->alpha |
| 168 |
<< ' ' << node->beta |
| 169 |
<< ' ' << node->prob |
| 170 |
<< ' ' << node->cost << std::endl; |
| 171 |
} |
| 172 |
|
| 173 |
|
| 174 |
// begin_nodes/end_nodes |
| 175 |
const size_t len = lattice->size(); |
| 176 |
for (int i = 0; i <= len; ++i) { |
| 177 |
MeCab::Node *b = lattice->begin_nodes(i); |
| 178 |
MeCab::Node *e = lattice->end_nodes(i); |
| 179 |
for (; b; b = b->bnext) { |
| 180 |
printf("B[%d] %s\t%s\n", i, b->surface, b->feature); |
| 181 |
} |
| 182 |
for (; e; e = e->enext) { |
| 183 |
printf("E[%d] %s\t%s\n", i, e->surface, e->feature); |
| 184 |
} |
| 185 |
} |
| 186 |
|
| 187 |
// N best results |
| 188 |
lattice->set_request_type(MECAB_NBEST); |
| 189 |
lattice->set_sentence(input); |
| 190 |
CHECK(tagger->parse(lattice)); |
| 191 |
for (int i = 0; i < 10; ++i) { |
| 192 |
std::cout << "NBEST: " << i << std::endl; |
| 193 |
std::cout << lattice->toString(); |
| 194 |
if (!lattice->next()) { |
| 195 |
// No more results |
| 196 |
break; |
| 197 |
} |
| 198 |
} |
| 199 |
|
| 200 |
// Marginal probabilities |
| 201 |
lattice->remove_request_type(MECAB_NBEST); |
| 202 |
lattice->set_request_type(MECAB_MARGINAL_PROB); |
| 203 |
lattice->set_sentence(input); |
| 204 |
CHECK(tagger->parse(lattice)); |
| 205 |
std::cout << lattice->theta() << std::endl; |
| 206 |
for (const MeCab::Node *node = lattice->bos_node(); |
| 207 |
node; node = node->next) { |
| 208 |
std::cout.write(node->surface, node->length); |
| 209 |
std::cout << "\t" << node->feature; |
| 210 |
std::cout << "\t" << node->prob << std::endl; |
| 211 |
} |
| 212 |
|
| 213 |
// Dictionary info |
| 214 |
const MeCab::DictionaryInfo *d = model->dictionary_info(); |
| 215 |
for (; d; d = d->next) { |
| 216 |
std::cout << "filename: " << d->filename << std::endl; |
| 217 |
std::cout << "charset: " << d->charset << std::endl; |
| 218 |
std::cout << "size: " << d->size << std::endl; |
| 219 |
std::cout << "type: " << d->type << std::endl; |
| 220 |
std::cout << "lsize: " << d->lsize << std::endl; |
| 221 |
std::cout << "rsize: " << d->rsize << std::endl; |
| 222 |
std::cout << "version: " << d->version << std::endl; |
| 223 |
} |
| 224 |
|
| 225 |
// Swap model atomically. |
| 226 |
MeCab::Model *another_model = MeCab::createModel(""); |
| 227 |
model->swap(another_model); |
| 228 |
|
| 229 |
delete lattice; |
| 230 |
delete tagger; |
| 231 |
delete model; |
| 232 |
|
| 233 |
return 0; |
| 234 |
} |
| 235 |
|
| 236 |
</pre> |
| 237 |
|
| 238 |
<h2>C サンプルコード</h2> |
| 239 |
シングルスレッド環境 |
| 240 |
<pre> |
| 241 |
#include <mecab.h> |
| 242 |
#include <stdio.h> |
| 243 |
|
| 244 |
#define CHECK(eval) if (! eval) { \ |
| 245 |
fprintf (stderr, "Exception:%s\n", mecab_strerror (mecab)); \ |
| 246 |
mecab_destroy(mecab); \ |
| 247 |
return -1; } |
| 248 |
|
| 249 |
int main (int argc, char **argv) { |
| 250 |
char input[] = "太郎は次郎が持っている本を花子に渡した。"; |
| 251 |
mecab_t *mecab; |
| 252 |
const mecab_node_t *node; |
| 253 |
const char *result; |
| 254 |
int i; |
| 255 |
size_t len; |
| 256 |
|
| 257 |
// Create tagger object |
| 258 |
mecab = mecab_new(argc, argv); |
| 259 |
CHECK(mecab); |
| 260 |
|
| 261 |
// Gets tagged result in string. |
| 262 |
result = mecab_sparse_tostr(mecab, input); |
| 263 |
CHECK(result) |
| 264 |
printf ("INPUT: %s\n", input); |
| 265 |
printf ("RESULT:\n%s", result); |
| 266 |
|
| 267 |
// Gets N best results |
| 268 |
result = mecab_nbest_sparse_tostr (mecab, 3, input); |
| 269 |
CHECK(result); |
| 270 |
fprintf (stdout, "NBEST:\n%s", result); |
| 271 |
|
| 272 |
CHECK(mecab_nbest_init(mecab, input)); |
| 273 |
for (i = 0; i < 3; ++i) { |
| 274 |
printf ("%d:\n%s", i, mecab_nbest_next_tostr (mecab)); |
| 275 |
} |
| 276 |
|
| 277 |
// Gets node object |
| 278 |
node = mecab_sparse_tonode(mecab, input); |
| 279 |
CHECK(node); |
| 280 |
for (; node; node = node->next) { |
| 281 |
if (node->stat == MECAB_NOR_NODE || node->stat == MECAB_UNK_NODE) { |
| 282 |
fwrite (node->surface, sizeof(char), node->length, stdout); |
| 283 |
printf("\t%s\n", node->feature); |
| 284 |
} |
| 285 |
} |
| 286 |
|
| 287 |
// Dictionary info |
| 288 |
const mecab_dictionary_info_t *d = mecab_dictionary_info(mecab); |
| 289 |
for (; d; d = d->next) { |
| 290 |
printf("filename: %s\n", d->filename); |
| 291 |
printf("charset: %s\n", d->charset); |
| 292 |
printf("size: %d\n", d->size); |
| 293 |
printf("type: %d\n", d->type); |
| 294 |
printf("lsize: %d\n", d->lsize); |
| 295 |
printf("rsize: %d\n", d->rsize); |
| 296 |
printf("version: %d\n", d->version); |
| 297 |
} |
| 298 |
|
| 299 |
mecab_destroy(mecab); |
| 300 |
|
| 301 |
return 0; |
| 302 |
} |
| 303 |
|
| 304 |
</pre> |
| 305 |
|
| 306 |
<h2>C サンプルコード</h2> |
| 307 |
マルチスレッド環境 |
| 308 |
<pre> |
| 309 |
#include <mecab.h> |
| 310 |
#include <stdio.h> |
| 311 |
|
| 312 |
#define CHECK(eval) if (! eval) { \ |
| 313 |
fprintf (stderr, "Exception:%s\n", mecab_strerror (mecab)); \ |
| 314 |
mecab_destroy(mecab); \ |
| 315 |
return -1; } |
| 316 |
|
| 317 |
int main (int argc, char **argv) { |
| 318 |
char input[] = "太郎は次郎が持っている本を花子に渡した。"; |
| 319 |
mecab_model_t *model, *another_model; |
| 320 |
mecab_t *mecab; |
| 321 |
mecab_lattice_t *lattice; |
| 322 |
const mecab_node_t *node; |
| 323 |
const char *result; |
| 324 |
int i; |
| 325 |
size_t len; |
| 326 |
|
| 327 |
model = mecab_model_new(argc, argv); |
| 328 |
CHECK(model); |
| 329 |
|
| 330 |
mecab = mecab_model_new_tagger(model); |
| 331 |
CHECK(mecab); |
| 332 |
|
| 333 |
lattice = mecab_model_new_lattice(model); |
| 334 |
CHECK(lattice); |
| 335 |
|
| 336 |
mecab_lattice_set_sentence(lattice, input); |
| 337 |
mecab_parse_lattice(mecab, lattice); |
| 338 |
|
| 339 |
printf("RESULT: %s\n", mecab_lattice_tostr(lattice)); |
| 340 |
|
| 341 |
node = mecab_lattice_get_bos_node(lattice); |
| 342 |
for (; node; node = node->next) { |
| 343 |
printf("%d ", node->id); |
| 344 |
|
| 345 |
if (node->stat == MECAB_BOS_NODE) |
| 346 |
printf("BOS"); |
| 347 |
else if (node->stat == MECAB_EOS_NODE) |
| 348 |
printf("EOS"); |
| 349 |
else |
| 350 |
fwrite (node->surface, sizeof(char), node->length, stdout); |
| 351 |
|
| 352 |
printf(" %s %d %d %d %d %d %d %d %d %f %f %f %ld\n", |
| 353 |
node->feature, |
| 354 |
(int)(node->surface - input), |
| 355 |
(int)(node->surface - input + node->length), |
| 356 |
node->rcAttr, |
| 357 |
node->lcAttr, |
| 358 |
node->posid, |
| 359 |
(int)node->char_type, |
| 360 |
(int)node->stat, |
| 361 |
(int)node->isbest, |
| 362 |
node->alpha, |
| 363 |
node->beta, |
| 364 |
node->prob, |
| 365 |
node->cost); |
| 366 |
} |
| 367 |
|
| 368 |
len = mecab_lattice_get_size(lattice); |
| 369 |
for (i = 0; i <= len; ++i) { |
| 370 |
mecab_node_t *b, *e; |
| 371 |
b = mecab_lattice_get_begin_nodes(lattice, (size_t)i); |
| 372 |
e = mecab_lattice_get_end_nodes(lattice, (size_t)i); |
| 373 |
for (; b; b = b->bnext) { |
| 374 |
printf("B[%d] %s\t%s\n", i, b->surface, b->feature); |
| 375 |
} |
| 376 |
for (; e; e = e->enext) { |
| 377 |
printf("E[%d] %s\t%s\n", i, e->surface, e->feature); |
| 378 |
} |
| 379 |
} |
| 380 |
|
| 381 |
mecab_lattice_set_sentence(lattice, input); |
| 382 |
mecab_lattice_set_request_type(lattice, MECAB_NBEST); |
| 383 |
mecab_parse_lattice(mecab, lattice); |
| 384 |
for (i = 0; i < 10; ++i) { |
| 385 |
fprintf(stdout, "%s", mecab_lattice_tostr(lattice)); |
| 386 |
if (!mecab_lattice_next(lattice)) { |
| 387 |
break; |
| 388 |
} |
| 389 |
} |
| 390 |
|
| 391 |
mecab_lattice_set_sentence(lattice, input); |
| 392 |
mecab_lattice_set_request_type(lattice, MECAB_MARGINAL_PROB); |
| 393 |
mecab_lattice_set_theta(lattice, 0.001); |
| 394 |
mecab_parse_lattice(mecab, lattice); |
| 395 |
node = mecab_lattice_get_bos_node(lattice); |
| 396 |
for (; node; node = node->next) { |
| 397 |
fwrite(node->surface, sizeof(char), node->length, stdout); |
| 398 |
fprintf(stdout, "\t%s\t%f\n", node->feature, node->prob); |
| 399 |
} |
| 400 |
|
| 401 |
mecab_set_lattice_level(mecab, 0); |
| 402 |
mecab_set_all_morphs(mecab, 1); |
| 403 |
node = mecab_sparse_tonode(mecab, input); |
| 404 |
CHECK(node); |
| 405 |
for (; node; node = node->next) { |
| 406 |
fwrite (node->surface, sizeof(char), node->length, stdout); |
| 407 |
printf("\t%s\n", node->feature); |
| 408 |
} |
| 409 |
|
| 410 |
const mecab_dictionary_info_t *d = mecab_dictionary_info(mecab); |
| 411 |
for (; d; d = d->next) { |
| 412 |
printf("filename: %s\n", d->filename); |
| 413 |
printf("charset: %s\n", d->charset); |
| 414 |
printf("size: %d\n", d->size); |
| 415 |
printf("type: %d\n", d->type); |
| 416 |
printf("lsize: %d\n", d->lsize); |
| 417 |
printf("rsize: %d\n", d->rsize); |
| 418 |
printf("version: %d\n", d->version); |
| 419 |
} |
| 420 |
|
| 421 |
|
| 422 |
mecab_destroy(mecab); |
| 423 |
mecab_lattice_destroy(lattice); |
| 424 |
mecab_model_destory(model); |
| 425 |
|
| 426 |
return 0; |
| 427 |
} |
| 428 |
|
| 429 |
</pre> |
| 430 |
|
| 431 |
<h2><a name="compile">コンパイル方法</a></h2> |
| 432 |
|
| 433 |
<ul> |
| 434 |
<li>UNIX の場合 </li> |
| 435 |
|
| 436 |
<pre> |
| 437 |
% cc -O2 `mecab-config --cflags` example.c -o example \ |
| 438 |
`mecab-config --libs` |
| 439 |
</pre> |
| 440 |
|
| 441 |
<li>Windows の場合 </li> |
| 442 |
<p>まず, コンパイル作業を行うディレクトリに include\mecab.h, |
| 443 |
bin\libmecab.dll lib\libmecab.lib をコピーします. |
| 444 |
この後の作業は, 使用するコンパイラによって微妙に変わります.</p> |
| 445 |
|
| 446 |
<ul> |
| 447 |
<li>cygwin/mingw 環境の場合</li> |
| 448 |
<pre> |
| 449 |
% gcc -DDLL_IMPORT -I. example.c -o example.exe libmecab.dll |
| 450 |
</pre> |
| 451 |
|
| 452 |
<li>VC++ 環境の場合</li> |
| 453 |
<pre> |
| 454 |
% cl -DDLL_IMPORT -I. example.c libmecab.lib |
| 455 |
</pre> |
| 456 |
</ul> |
| 457 |
</ul> |
| 458 |
<hr> |
| 459 |
</body> |
| 460 |
</html> |
| 461 |
|