/* コンパイル cc -O2 `mecab-config --cflags` mecab1.c -o mecab1 `mecab-config --libs` 実行時 ./mecab_test -l1 */ #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[1024] = "太郎は次郎が持っている本を花子に渡した。"; mecab_t *mecab; mecab_node_t *node; const char *result; int i; mecab = mecab_new (argc, argv); CHECK(mecab); /* result = mecab_sparse_tostr(mecab, input); CHECK(result) printf ("INPUT: %s\n", input); printf ("RESULT:\n%s", result); 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 ("node->id = %d:\n%s", i, mecab_nbest_next_tostr (mecab)); } */ node = mecab_sparse_tonode(mecab, input); CHECK(node); /* for (; node; node = node->next) { fwrite (node->surface, sizeof(char), node->length, stdout); printf("node->feature = \t%s\n", node->feature); } */ node = mecab_sparse_tonode(mecab, input); CHECK(node); for (; node; node = node->next) { printf("node->id = %d :::", node->id); if (node->stat == MECAB_BOS_NODE) printf("BOS"); else if (node->stat == MECAB_EOS_NODE) printf("EOS"); else // 入力された形態素の原型を標準出力に表示. //バッファ末尾に \0 が無いので,この処理をする fwrite (node->surface, sizeof(char), node->length, stdout); printf("::: %s %d %d %d %d %d %d %d %d %f %f %f %d\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); } mecab_destroy(mecab); return 0; }
/* コンパイル g++ -O2 `mecab-config --cflags` mecab2.cpp -o mecab2 `mecab-config --libs` 実行 ./mecab2 -l1 */ #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] = "太郎は次郎が持っている本を花子に渡した。"; MeCab::Tagger *tagger = MeCab::createTagger (argc, argv); CHECK(tagger); const char *result = tagger->parse(input); CHECK(result); std::cout << "INPUT: " << input << std::endl; std::cout << "RESULT: " << result << std::endl; result = tagger->parseNBest(3, input); CHECK(result); std::cout << "NBEST: " << std::endl << result; CHECK(tagger->parseNBestInit(input)); for (int i = 0; i < 3; ++i) { std::cout << i << ":" << std::endl << tagger->next(); } // MeCab::Node* node = tagger->parseToNode(input); // はキャストエラー const MeCab::Node* node = tagger->parseToNode(input); CHECK(node); for (; node; node = node->next) { std::cout.write(node->surface, node->length); } 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; } delete tagger; return 0; }
どうも Node クラスの設定にバクがあるらしい.詳しくは[[ここ:]http://lists.sourceforge.jp/mailman/archives/mecab-devel/2007-July/000066.html].
import org.chasen.mecab.MeCab; import org.chasen.mecab.Tagger; import org.chasen.mecab.Node; public class MyMecab { static { try { System.loadLibrary("MeCab"); } catch (UnsatisfiedLinkError e) { System.err.println( "Cannot load the example native code.\n Make sure your LD_LIBRARY_PATH contains \'.\'\n" + e); System.exit(1); } } public static void main(String[] argv) { System.out.println (MeCab.VERSION); String arg = ""; for (int i = 0; i < argv.length; i++) { arg += " "; arg += argv[i]; } Tagger tagger = new Tagger(); System.out.println (tagger.parse ( "太郎は二郎にこの本を渡した.")); for (Node node = tagger.parseToNode ( "太郎は二郎にこの本を渡した."); node != null; node = node.getNext()) { System.out.println (node.getSurface() + "\t" + node.getFeature()) ; } System.out.println ("EOS\n"); } }
LD_LIBRARY_PATH の設定がなされてないとして,コンパイルの方法.
javac {-encoding utf-8} -classpath /yourPath/mecab-java-0.96/org/chasen/mecab/ MeCab.jar MyMecab.java
実行.LD_LIBRARY_PATH の設定がなされてないとして.
java -classpath .:/home/ishida/progSource/mecab/mecab-java-0.96/ MeCab.jar MyMecab
Mac OS 10.3 への導入例がここにある