OSDN Git Service

Initial commit
[wordring-tm/wordring-tm.git] / third_party / mecab-0.996 / example / example.cpp
1 #include <iostream>
2 #include <mecab.h>
3
4 #define CHECK(eval) if (! eval) { \
5    const char *e = tagger ? tagger->what() : MeCab::getTaggerError(); \
6    std::cerr << "Exception:" << e << std::endl; \
7    delete tagger; \
8    return -1; }
9
10 // Sample of MeCab::Tagger class.
11 int main (int argc, char **argv) {
12   char input[] = "太郎は次郎が持っている本を花子に渡した。";
13
14   MeCab::Tagger *tagger = MeCab::createTagger("");
15   CHECK(tagger);
16
17   // Gets tagged result in string format.
18   const char *result = tagger->parse(input);
19   CHECK(result);
20   std::cout << "INPUT: " << input << std::endl;
21   std::cout << "RESULT: " << result << std::endl;
22
23   // Gets N best results in string format.
24   result = tagger->parseNBest(3, input);
25   CHECK(result);
26   std::cout << "NBEST: " << std::endl << result;
27
28   // Gets N best results in sequence.
29   CHECK(tagger->parseNBestInit(input));
30   for (int i = 0; i < 3; ++i) {
31     std::cout << i << ":" << std::endl << tagger->next();
32   }
33
34   // Gets Node object.
35   const MeCab::Node* node = tagger->parseToNode(input);
36   CHECK(node);
37   for (; node; node = node->next) {
38     std::cout << node->id << ' ';
39     if (node->stat == MECAB_BOS_NODE)
40       std::cout << "BOS";
41     else if (node->stat == MECAB_EOS_NODE)
42       std::cout << "EOS";
43     else
44       std::cout.write (node->surface, node->length);
45
46     std::cout << ' ' << node->feature
47               << ' ' << (int)(node->surface - input)
48               << ' ' << (int)(node->surface - input + node->length)
49               << ' ' << node->rcAttr
50               << ' ' << node->lcAttr
51               << ' ' << node->posid
52               << ' ' << (int)node->char_type
53               << ' ' << (int)node->stat
54               << ' ' << (int)node->isbest
55               << ' ' << node->alpha
56               << ' ' << node->beta
57               << ' ' << node->prob
58               << ' ' << node->cost << std::endl;
59   }
60
61   // Dictionary info.
62   const MeCab::DictionaryInfo *d = tagger->dictionary_info();
63   for (; d; d = d->next) {
64     std::cout << "filename: " <<  d->filename << std::endl;
65     std::cout << "charset: " <<  d->charset << std::endl;
66     std::cout << "size: " <<  d->size << std::endl;
67     std::cout << "type: " <<  d->type << std::endl;
68     std::cout << "lsize: " <<  d->lsize << std::endl;
69     std::cout << "rsize: " <<  d->rsize << std::endl;
70     std::cout << "version: " <<  d->version << std::endl;
71   }
72
73   delete tagger;
74
75   return 0;
76 }