Debian Codegraph @

doc/libmecab.html

raw
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 &lt;iostream&gt;
28 #include &lt;mecab.h&gt;
29
30 #define CHECK(eval) if (! eval) { \
31 const char *e = tagger ? tagger-&gt;what() : MeCab::getTaggerError(); \
32 std::cerr &lt;&lt; "Exception:" &lt;&lt; e &lt;&lt; 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-&gt;parse(input);
45 CHECK(result);
46 std::cout &lt;&lt; "INPUT: " &lt;&lt; input &lt;&lt; std::endl;
47 std::cout &lt;&lt; "RESULT: " &lt;&lt; result &lt;&lt; std::endl;
48
49 // Gets N best results in string format.
50 result = tagger-&gt;parseNBest(3, input);
51 CHECK(result);
52 std::cout &lt;&lt; "NBEST: " &lt;&lt; std::endl &lt;&lt; result;
53
54 // Gets N best results in sequence.
55 CHECK(tagger-&gt;parseNBestInit(input));
56 for (int i = 0; i &lt; 3; ++i) {
57 std::cout &lt;&lt; i &lt;&lt; ":" &lt;&lt; std::endl &lt;&lt; tagger-&gt;next();
58 }
59
60 // Gets Node object.
61 const MeCab::Node* node = tagger-&gt;parseToNode(input);
62 CHECK(node);
63 for (; node; node = node-&gt;next) {
64 std::cout &lt;&lt; node-&gt;id &lt;&lt; ' ';
65 if (node-&gt;stat == MECAB_BOS_NODE)
66 std::cout &lt;&lt; "BOS";
67 else if (node-&gt;stat == MECAB_EOS_NODE)
68 std::cout &lt;&lt; "EOS";
69 else
70 std::cout.write (node-&gt;surface, node-&gt;length);
71
72 std::cout &lt;&lt; ' ' &lt;&lt; node-&gt;feature
73 &lt;&lt; ' ' &lt;&lt; (int)(node-&gt;surface - input)
74 &lt;&lt; ' ' &lt;&lt; (int)(node-&gt;surface - input + node-&gt;length)
75 &lt;&lt; ' ' &lt;&lt; node-&gt;rcAttr
76 &lt;&lt; ' ' &lt;&lt; node-&gt;lcAttr
77 &lt;&lt; ' ' &lt;&lt; node-&gt;posid
78 &lt;&lt; ' ' &lt;&lt; (int)node-&gt;char_type
79 &lt;&lt; ' ' &lt;&lt; (int)node-&gt;stat
80 &lt;&lt; ' ' &lt;&lt; (int)node-&gt;isbest
81 &lt;&lt; ' ' &lt;&lt; node-&gt;alpha
82 &lt;&lt; ' ' &lt;&lt; node-&gt;beta
83 &lt;&lt; ' ' &lt;&lt; node-&gt;prob
84 &lt;&lt; ' ' &lt;&lt; node-&gt;cost &lt;&lt; std::endl;
85 }
86
87 // Dictionary info.
88 const MeCab::DictionaryInfo *d = tagger-&gt;dictionary_info();
89 for (; d; d = d-&gt;next) {
90 std::cout &lt;&lt; "filename: " &lt;&lt; d-&gt;filename &lt;&lt; std::endl;
91 std::cout &lt;&lt; "charset: " &lt;&lt; d-&gt;charset &lt;&lt; std::endl;
92 std::cout &lt;&lt; "size: " &lt;&lt; d-&gt;size &lt;&lt; std::endl;
93 std::cout &lt;&lt; "type: " &lt;&lt; d-&gt;type &lt;&lt; std::endl;
94 std::cout &lt;&lt; "lsize: " &lt;&lt; d-&gt;lsize &lt;&lt; std::endl;
95 std::cout &lt;&lt; "rsize: " &lt;&lt; d-&gt;rsize &lt;&lt; std::endl;
96 std::cout &lt;&lt; "version: " &lt;&lt; d-&gt;version &lt;&lt; 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 &lt;iostream&gt;
116 #include &lt;mecab.h&gt;
117
118 #define CHECK(eval) if (! eval) { \
119 const char *e = tagger ? tagger-&gt;what() : MeCab::getTaggerError(); \
120 std::cerr &lt;&lt; "Exception:" &lt;&lt; e &lt;&lt; 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-&gt;createTagger();
134 CHECK(tagger);
135
136 // Create lattice object per thread.
137 MeCab::Lattice *lattice = model-&gt;createLattice();
138
139 // Gets tagged result in string
140 lattice-&gt;set_sentence(input);
141
142 // this method is thread safe, as long as |lattice| is thread local.
143 CHECK(tagger-&gt;parse(lattice));
144 std::cout &lt;&lt; lattice-&gt;toString() &lt;&lt; std::endl;
145
146 // Gets node object.
147 const MeCab::Node* node = lattice-&gt;bos_node();
148 CHECK(node);
149 for (; node; node = node-&gt;next) {
150 std::cout &lt;&lt; node-&gt;id &lt;&lt; ' ';
151 if (node-&gt;stat == MECAB_BOS_NODE)
152 std::cout &lt;&lt; "BOS";
153 else if (node-&gt;stat == MECAB_EOS_NODE)
154 std::cout &lt;&lt; "EOS";
155 else
156 std::cout.write (node-&gt;surface, node-&gt;length);
157
158 std::cout &lt;&lt; ' ' &lt;&lt; node-&gt;feature
159 &lt;&lt; ' ' &lt;&lt; (int)(node-&gt;surface - input)
160 &lt;&lt; ' ' &lt;&lt; (int)(node-&gt;surface - input + node-&gt;length)
161 &lt;&lt; ' ' &lt;&lt; node-&gt;rcAttr
162 &lt;&lt; ' ' &lt;&lt; node-&gt;lcAttr
163 &lt;&lt; ' ' &lt;&lt; node-&gt;posid
164 &lt;&lt; ' ' &lt;&lt; (int)node-&gt;char_type
165 &lt;&lt; ' ' &lt;&lt; (int)node-&gt;stat
166 &lt;&lt; ' ' &lt;&lt; (int)node-&gt;isbest
167 &lt;&lt; ' ' &lt;&lt; node-&gt;alpha
168 &lt;&lt; ' ' &lt;&lt; node-&gt;beta
169 &lt;&lt; ' ' &lt;&lt; node-&gt;prob
170 &lt;&lt; ' ' &lt;&lt; node-&gt;cost &lt;&lt; std::endl;
171 }
172
173
174 // begin_nodes/end_nodes
175 const size_t len = lattice-&gt;size();
176 for (int i = 0; i &lt;= len; ++i) {
177 MeCab::Node *b = lattice-&gt;begin_nodes(i);
178 MeCab::Node *e = lattice-&gt;end_nodes(i);
179 for (; b; b = b-&gt;bnext) {
180 printf("B[%d] %s\t%s\n", i, b-&gt;surface, b-&gt;feature);
181 }
182 for (; e; e = e-&gt;enext) {
183 printf("E[%d] %s\t%s\n", i, e-&gt;surface, e-&gt;feature);
184 }
185 }
186
187 // N best results
188 lattice-&gt;set_request_type(MECAB_NBEST);
189 lattice-&gt;set_sentence(input);
190 CHECK(tagger-&gt;parse(lattice));
191 for (int i = 0; i &lt; 10; ++i) {
192 std::cout &lt;&lt; "NBEST: " &lt;&lt; i &lt;&lt; std::endl;
193 std::cout &lt;&lt; lattice-&gt;toString();
194 if (!lattice-&gt;next()) {
195 // No more results
196 break;
197 }
198 }
199
200 // Marginal probabilities
201 lattice-&gt;remove_request_type(MECAB_NBEST);
202 lattice-&gt;set_request_type(MECAB_MARGINAL_PROB);
203 lattice-&gt;set_sentence(input);
204 CHECK(tagger-&gt;parse(lattice));
205 std::cout &lt;&lt; lattice-&gt;theta() &lt;&lt; std::endl;
206 for (const MeCab::Node *node = lattice-&gt;bos_node();
207 node; node = node-&gt;next) {
208 std::cout.write(node-&gt;surface, node-&gt;length);
209 std::cout &lt;&lt; "\t" &lt;&lt; node-&gt;feature;
210 std::cout &lt;&lt; "\t" &lt;&lt; node-&gt;prob &lt;&lt; std::endl;
211 }
212
213 // Dictionary info
214 const MeCab::DictionaryInfo *d = model-&gt;dictionary_info();
215 for (; d; d = d-&gt;next) {
216 std::cout &lt;&lt; "filename: " &lt;&lt; d-&gt;filename &lt;&lt; std::endl;
217 std::cout &lt;&lt; "charset: " &lt;&lt; d-&gt;charset &lt;&lt; std::endl;
218 std::cout &lt;&lt; "size: " &lt;&lt; d-&gt;size &lt;&lt; std::endl;
219 std::cout &lt;&lt; "type: " &lt;&lt; d-&gt;type &lt;&lt; std::endl;
220 std::cout &lt;&lt; "lsize: " &lt;&lt; d-&gt;lsize &lt;&lt; std::endl;
221 std::cout &lt;&lt; "rsize: " &lt;&lt; d-&gt;rsize &lt;&lt; std::endl;
222 std::cout &lt;&lt; "version: " &lt;&lt; d-&gt;version &lt;&lt; std::endl;
223 }
224
225 // Swap model atomically.
226 MeCab::Model *another_model = MeCab::createModel("");
227 model-&gt;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 &lt;mecab.h&gt;
242 #include &lt;stdio.h&gt;
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-&gt;next) {
281 if (node-&gt;stat == MECAB_NOR_NODE || node-&gt;stat == MECAB_UNK_NODE) {
282 fwrite (node-&gt;surface, sizeof(char), node-&gt;length, stdout);
283 printf("\t%s\n", node-&gt;feature);
284 }
285 }
286
287 // Dictionary info
288 const mecab_dictionary_info_t *d = mecab_dictionary_info(mecab);
289 for (; d; d = d-&gt;next) {
290 printf("filename: %s\n", d-&gt;filename);
291 printf("charset: %s\n", d-&gt;charset);
292 printf("size: %d\n", d-&gt;size);
293 printf("type: %d\n", d-&gt;type);
294 printf("lsize: %d\n", d-&gt;lsize);
295 printf("rsize: %d\n", d-&gt;rsize);
296 printf("version: %d\n", d-&gt;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 &lt;mecab.h&gt;
310 #include &lt;stdio.h&gt;
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-&gt;next) {
343 printf("%d ", node-&gt;id);
344
345 if (node-&gt;stat == MECAB_BOS_NODE)
346 printf("BOS");
347 else if (node-&gt;stat == MECAB_EOS_NODE)
348 printf("EOS");
349 else
350 fwrite (node-&gt;surface, sizeof(char), node-&gt;length, stdout);
351
352 printf(" %s %d %d %d %d %d %d %d %d %f %f %f %ld\n",
353 node-&gt;feature,
354 (int)(node-&gt;surface - input),
355 (int)(node-&gt;surface - input + node-&gt;length),
356 node-&gt;rcAttr,
357 node-&gt;lcAttr,
358 node-&gt;posid,
359 (int)node-&gt;char_type,
360 (int)node-&gt;stat,
361 (int)node-&gt;isbest,
362 node-&gt;alpha,
363 node-&gt;beta,
364 node-&gt;prob,
365 node-&gt;cost);
366 }
367
368 len = mecab_lattice_get_size(lattice);
369 for (i = 0; i &lt;= 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-&gt;bnext) {
374 printf("B[%d] %s\t%s\n", i, b-&gt;surface, b-&gt;feature);
375 }
376 for (; e; e = e-&gt;enext) {
377 printf("E[%d] %s\t%s\n", i, e-&gt;surface, e-&gt;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 &lt; 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-&gt;next) {
397 fwrite(node-&gt;surface, sizeof(char), node-&gt;length, stdout);
398 fprintf(stdout, "\t%s\t%f\n", node-&gt;feature, node-&gt;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-&gt;next) {
406 fwrite (node-&gt;surface, sizeof(char), node-&gt;length, stdout);
407 printf("\t%s\n", node-&gt;feature);
408 }
409
410 const mecab_dictionary_info_t *d = mecab_dictionary_info(mecab);
411 for (; d; d = d-&gt;next) {
412 printf("filename: %s\n", d-&gt;filename);
413 printf("charset: %s\n", d-&gt;charset);
414 printf("size: %d\n", d-&gt;size);
415 printf("type: %d\n", d-&gt;type);
416 printf("lsize: %d\n", d-&gt;lsize);
417 printf("rsize: %d\n", d-&gt;rsize);
418 printf("version: %d\n", d-&gt;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