| 1 |
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"> |
| 2 |
<html><head> |
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><title>MeCab: Yet Another Japanese Dependency Structure Analyzer</title> |
| 7 |
|
| 8 |
<link rel="stylesheet" href="bindings_files/mecab.css"></head><body> |
| 9 |
|
| 10 |
<h1>スクリプト言語のバインディング -- Bindings for scripting languages</h1> |
| 11 |
<p>$Id: bindings.html,v 1.3 2006/01/07 14:37:40 taku-ku Exp $;</p> |
| 12 |
<p>English translation: Eric Nichols < eric-n --at-- is.naist.jp ></p> |
| 13 |
|
| 14 |
<h2>概要 -- Outline</h2> |
| 15 |
<p> |
| 16 |
各種スクリプト言語 |
| 17 |
(<a href="http://www.perl.com/">perl</a>, |
| 18 |
<a href="http://www.ruby-lang.org/">ruby</a>, |
| 19 |
<a href="http://www.python.org/">python</a>, |
| 20 |
<a href="http://java.sun.com/">Java</a>) |
| 21 |
から, MeCab が提供する形態素解析の機能を利用可能です. |
| 22 |
各バインディングは <a href="http://www.swig.org/">SWIG</a> というプログラ |
| 23 |
ムを用いて, 自動生成されています. <a href="http://www.swig.org/">SWIG</a> がサポートする他の言語も |
| 24 |
生成可能だと思われますが, 現在は, 作者の管理できる範囲内ということで, |
| 25 |
上記の4つの言語のみを提供しております. |
| 26 |
</p> |
| 27 |
|
| 28 |
<p> |
| 29 |
The functions for morphological analysis provided by MeCab can be used |
| 30 |
from various scripting languages (<a href="http://www.perl.com/">perl</a>, |
| 31 |
<a href="http://www.ruby-lang.org/">ruby</a>, |
| 32 |
<a href="http://www.python.org/">python</a>, |
| 33 |
<a href="http://java.sun.com/">Java</a>). |
| 34 |
Each of these bindings was generated automatically using a program |
| 35 |
called <a href="http://www.swig.org/">SWIG</a>. Bindings can likely be |
| 36 |
generated for the other languages supported |
| 37 |
by <a href="http://www.swig.org/">SWIG</a>, however, at this time, in |
| 38 |
order to keep things manageable, they are only provided for the above |
| 39 |
four languages. |
| 40 |
</p> |
| 41 |
|
| 42 |
<h2>インストール -- Installation</h2> |
| 43 |
<p> |
| 44 |
各言語バイディングのインストール方法は, perl/README, ruby/README, python/README, |
| 45 |
java/README を御覧下さい. |
| 46 |
</p> |
| 47 |
|
| 48 |
<p> |
| 49 |
Consult perl/README, ruby/README, python/README, and java/README for |
| 50 |
installation instructions for each language's binding. |
| 51 |
</p> |
| 52 |
|
| 53 |
|
| 54 |
<h2>とりあえず解析する -- Example usage</h2> |
| 55 |
<p> |
| 56 |
MeCab::Tagger というクラスのインスタンスを生成し, parse (もしくは |
| 57 |
parseToString) というメソッドを呼ぶことで, 解析結果が文字列として取得できます. |
| 58 |
MeCab::Tagger のコンストラクタの引数は, 基本的に mecab の実行形式に与え |
| 59 |
るパラメータと同一で, それらを文字列として与えます. |
| 60 |
</p> |
| 61 |
|
| 62 |
<p> |
| 63 |
By creating an instance of MeCab::Tagger and calling the method parse |
| 64 |
(or parseToString), parse results can be acquired in string form. |
| 65 |
The arguments of MeCab::Tagger 's constructor are basically the same |
| 66 |
as the parameters of mecab's command line form and are supplied as a |
| 67 |
string. |
| 68 |
</p> |
| 69 |
|
| 70 |
<h3>perl</h3> |
| 71 |
<pre>use MeCab; |
| 72 |
$m = new MeCab::Tagger ("-Ochasen"); |
| 73 |
print $m->parse ("今日もしないとね"); |
| 74 |
</pre> |
| 75 |
|
| 76 |
<h3>ruby</h3> |
| 77 |
<pre>require 'MeCab' |
| 78 |
m = MeCab::Tagger.new ("-Ochasen") |
| 79 |
print m.parse ("今日もしないとね") |
| 80 |
</pre> |
| 81 |
|
| 82 |
<h3>python</h3> |
| 83 |
<pre>import sys |
| 84 |
import MeCab |
| 85 |
m = MeCab.Tagger ("-Ochasen") |
| 86 |
print m.parse ("今日もしないとね") |
| 87 |
</pre> |
| 88 |
|
| 89 |
<h3>Java</h3> |
| 90 |
<pre>import org.chasen.mecab.Tagger; |
| 91 |
import org.chasen.mecab.Node |
| 92 |
public static void main(String[] argv) { |
| 93 |
Tagger tagger = new Tagger ("-Ochasen"); |
| 94 |
System.out.println (tagger.parse ("太郎は二郎にこの本を渡した.")); |
| 95 |
} |
| 96 |
</pre> |
| 97 |
|
| 98 |
<h2>各形態素の詳細情報を取得する -- Acquiring detailed information from each morpheme</h2> |
| 99 |
<p> |
| 100 |
MeCab::Tagger クラスの, parseToNode という |
| 101 |
メソッドを呼ぶことで, 「文頭」という特別な形態素が MeCab::Node クラスのインスタンスとして |
| 102 |
取得できます. |
| 103 |
</p> |
| 104 |
|
| 105 |
<p> |
| 106 |
By calling the parseToNode function from the MeCab::Tagger class, a |
| 107 |
special morpheme called "Beginning of sentence" can be acquired as an |
| 108 |
instance of the MeCab::Node class. |
| 109 |
</p> |
| 110 |
|
| 111 |
<p> |
| 112 |
MeCab::Node は, 双方向リストとして表現されており, next, prev というメン |
| 113 |
バ変数があります. それぞれ, 次の形態素, 前の形態素を MeCab::Node クラスのインスタンスとして |
| 114 |
返します. 全形態素には, next を順次呼ぶことでアクセスできます. |
| 115 |
</p> |
| 116 |
|
| 117 |
<p> |
| 118 |
MeCab::Node is represented as a doubly-linked list and has the member |
| 119 |
functions prev and next. They return the previous and following |
| 120 |
morphemes respectively as instances of the MeCab::Node class. All of |
| 121 |
the morphemes can be accessed by calling next in succession. |
| 122 |
</p> |
| 123 |
|
| 124 |
<p>MeCab::Node は C 言語のインタフェイスで提供している mecab_node_t をラッ |
| 125 |
プしたクラスです. mecab_node_t が持つほぼすべてのメンバ変数にアクセスす |
| 126 |
ることができます. ただし, surface のみ, 単語そのものが返るように変更して |
| 127 |
います.</p> |
| 128 |
|
| 129 |
<p> |
| 130 |
MeCab::Node is a wrapper class of the mecab_node_t interface provided |
| 131 |
in the C language. Almost all of mecab_node_t's member functions can |
| 132 |
be accessed with the exception that only the surface function has be |
| 133 |
altered to return the word's orthography as it is. |
| 134 |
</p> |
| 135 |
|
| 136 |
<p> |
| 137 |
以下に <a href="http://www.perl.com/">perl</a> の例を示します. この例では, |
| 138 |
各形態素を順次にアクセスし,形態素の表層文字列, 品詞, その形態素までのコストを表示します. |
| 139 |
</p> |
| 140 |
|
| 141 |
<p> |
| 142 |
Consider the following <a href="http://www.perl.com/">perl</a>. In |
| 143 |
this example, the morphemes are accessed sequentially, and the surface |
| 144 |
string (orthography), part-of-speech, and cost up to that morpheme are |
| 145 |
displayed. |
| 146 |
</p> |
| 147 |
|
| 148 |
<pre>use MeCab; |
| 149 |
my $m = new MeCab::Tagger (""); |
| 150 |
|
| 151 |
for (my $n = $m->parseToNode ("今日もしないとね"); $n ; $n = $n->{next}) { |
| 152 |
printf ("%s\t%s\t%d\n", |
| 153 |
$n->{surface}, # 表層 -- word's orthography |
| 154 |
$n->{feature}, # 現在の品詞 -- current part-of-speech |
| 155 |
$n->{cost} # その形態素までのコスト -- cost up to current morpheme |
| 156 |
); |
| 157 |
} |
| 158 |
</pre> |
| 159 |
|
| 160 |
<h2>エラー処理 -- Error handling</h2> |
| 161 |
<p> |
| 162 |
もし, コンストラクタや, 解析途中でエラーが起きた場合は, |
| 163 |
RuntimeError 例外が発生します. |
| 164 |
例外のハンドリングの方法は, 各言語のリファレンスマニュアルを |
| 165 |
ごらんください. 以下は, <a href="http://www.python.org/">python</a> の例です |
| 166 |
</p> |
| 167 |
|
| 168 |
<p> |
| 169 |
In the event an error occurs in the constructor or during parsing, a |
| 170 |
RuntimeError exception is produced. Consult the documentation for |
| 171 |
information on exception handling in each of the scripting languages. |
| 172 |
The following is an example in <a href="http://www.python.org/">python</a>. |
| 173 |
</p> |
| 174 |
|
| 175 |
<pre>try: |
| 176 |
m = MeCab.Tagger ("-d .") |
| 177 |
print m.parse ("今日もしないとね") |
| 178 |
except RuntimeError, e: |
| 179 |
print "RuntimeError:", e |
| 180 |
</pre> |
| 181 |
|
| 182 |
|
| 183 |
<h2>注意事項 -- Items of Caution</h2> |
| 184 |
<h3>文頭,文末形態素 Beginning- and end-of-sentence morphemes</h3> |
| 185 |
<p> |
| 186 |
parseToNode の返り値は, 「文頭」という特別な形態素を示す MeCab::Node |
| 187 |
インタンスです. さらに, 「文末」という特別な形態素も存在いたしますので, |
| 188 |
注意してください. もし, これらを無視したい場合は, 以下のように |
| 189 |
next でそれぞれを読み飛ばしてください. |
| 190 |
</p> |
| 191 |
|
| 192 |
<p> |
| 193 |
|
| 194 |
The return value of parseToNode is an instance of MeCab::Node that |
| 195 |
represents the special beginning-of-sentence morpheme. Furthermore, |
| 196 |
there is also a end-of-sentence morpheme, so be aware of them. If you |
| 197 |
want to ignore these, you can skip over them in the following manner: |
| 198 |
|
| 199 |
</p> |
| 200 |
|
| 201 |
<pre>my $n = $m->parseToNode ("今日もしないとね"); |
| 202 |
$n = $n->{next}; # 「文頭」を無視 -- ignore "beginning-of-sentence" |
| 203 |
|
| 204 |
while ($n->{next}) { # next を調べる -- check for next node |
| 205 |
printf ("%s\n", $n->{surface}); |
| 206 |
$n = $n->{next}; # 次に移動 -- move to next node |
| 207 |
} |
| 208 |
</pre> |
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
<h3>MeCab::Node の振舞い -- Behavior of MeCab::Node</h3> |
| 213 |
<p> |
| 214 |
MeCab::Node の実体(メモリ上にある形態素情報)は, |
| 215 |
MeCab::Tagger インスタンスが管理しています. MeCab::Node は, |
| 216 |
Node の実体を指している<b>参照</b>にすぎません. そのために, parseToNode が |
| 217 |
呼ばれる度に, 実体そのものが, 上書きされていきます. 以下のような例はソースの意図する通りには動きません. |
| 218 |
</p> |
| 219 |
|
| 220 |
<p> |
| 221 |
The contents of MeCab::Node (i.e. the morphological information stored |
| 222 |
in memory) are managed by the MeCab::Tagger interface. MeCab::Node is |
| 223 |
nothing more than a reference to the contents of a Node. Because of |
| 224 |
this, when parseToNode is called, these contents are overwritten. |
| 225 |
Thus, the example given in the following code will not work as |
| 226 |
expected. |
| 227 |
</p> |
| 228 |
|
| 229 |
<pre>m = MeCab.Tagger ("") |
| 230 |
n1 = m.parseToNode ("今日もしないとね") |
| 231 |
n2 = m.parseToNode ("さくさくさくら") |
| 232 |
|
| 233 |
# n1 の内容は無効になっている -- the contents of n1 are now inaccessible |
| 234 |
while (n1.hasNode () != 0): |
| 235 |
print n1.getSurface () |
| 236 |
n1 = n1.next () |
| 237 |
</pre> |
| 238 |
|
| 239 |
<p> |
| 240 |
上記の例では, n1 の指す中身が, 「さくさくさくら」を解析した時点で |
| 241 |
上書きされており, 使用できなくなっています. |
| 242 |
</p> |
| 243 |
|
| 244 |
<p> |
| 245 |
In the above example, the contents of n1 are overwritten as soon as " |
| 246 |
さくさくさくら" is parsed and become inaccessible. |
| 247 |
</p> |
| 248 |
|
| 249 |
<p> |
| 250 |
複数の Node を同時にアクセスしたい場合は, 複数の MeCab::Tagger インスタンスを生成してください. |
| 251 |
</p> |
| 252 |
|
| 253 |
<p> |
| 254 |
When you want to process multiple nodes simultaneously, you will need to |
| 255 |
create multiple instances of MeCab::Tagger. |
| 256 |
</p> |
| 257 |
|
| 258 |
<h2>全メソッド -- All methods</h2> |
| 259 |
<p> |
| 260 |
以下に, <a href="http://www.swig.org/">SWIG</a>用のインタフェースファイル |
| 261 |
の一部を示します. バイディングの実装言語の都合上, C++ のシンタックスで |
| 262 |
表記されていますが, 適宜読みかえてください. また, 各メソッドの動作も添え |
| 263 |
ていますので参考にしてください. |
| 264 |
</p> |
| 265 |
|
| 266 |
<p> |
| 267 |
|
| 268 |
The following is part of the interface file for <a href="http://www.swig.org/">SWIG</a>. |
| 269 |
It is shown in C++ syntax, as that is the implementation language of the bindings. |
| 270 |
Make sure to read over it appropriately. The behavior of each method |
| 271 |
is also given, so consult them as necessary. |
| 272 |
|
| 273 |
</p> |
| 274 |
|
| 275 |
<pre>namespace MeCab { |
| 276 |
|
| 277 |
class Tagger { |
| 278 |
|
| 279 |
// str を解析して文字列として結果を得ます. len は str の長さ(省略可能) |
| 280 |
// Parse str and get the results as a string. len is the length of str (can be omitted) |
| 281 |
string parse(string str, int len); |
| 282 |
|
| 283 |
// parse と同じ |
| 284 |
// Same as parse |
| 285 |
string parseToString(string str, int len); |
| 286 |
|
| 287 |
// str を解析して MeCab::Node 型の形態素を返します. |
| 288 |
// この形態素は文頭を示すもので, next を順に辿ることで全形態素にアクセスできます |
| 289 |
// Parse str and return a morpheme in the form of MeCab::Node |
| 290 |
// This morpheme represents the beginning of the sentence. |
| 291 |
// All of the morphemes can be accessed by following next in order. |
| 292 |
Node parseToNode(string str, int len); |
| 293 |
|
| 294 |
// parse の Nbest 版です. N に nbest の個数を指定します. |
| 295 |
// この機能を使う場合は, 起動時オプションとして -l 1 を指定する必要があります |
| 296 |
// An Nbest version of parse. N specifies the number of parses. |
| 297 |
// It is necessary to specify the option -l 1 at initialization when this function is used |
| 298 |
string parseNBest(int N, string str, int len); |
| 299 |
|
| 300 |
// 解析結果を, 確からしいものから順番に取得する場合にこの関数で初期化を行います. |
| 301 |
// When parse results are accessed from most likely result in succession, |
| 302 |
// this function will carry out reinitialization. |
| 303 |
bool parseNBestInit(string str, int len); |
| 304 |
|
| 305 |
// parseNbestInit() の後, この関数を順次呼ぶことで, 確からしい解析結果を, 順番に取得できます. |
| 306 |
// After calling parseNbestInit, results can be accessed in order from most likely |
| 307 |
// by calling this function |
| 308 |
string next(); |
| 309 |
|
| 310 |
// next() と同じですが, MeCab::Node を返します. |
| 311 |
// Same as next() but returns a MeCab::Node |
| 312 |
Node nextNode(); |
| 313 |
}; |
| 314 |
|
| 315 |
#define MECAB_NOR_NODE 0 |
| 316 |
#define MECAB_UNK_NODE 1 |
| 317 |
#define MECAB_BOS_NODE 2 |
| 318 |
#define MECAB_EOS_NODE 3 |
| 319 |
|
| 320 |
struct Node { |
| 321 |
|
| 322 |
struct Node prev; // 一つ前の形態素へのポインタ |
| 323 |
// pointer to previous morpheme |
| 324 |
struct Node next; // 一つ先の形態素へのポインタ |
| 325 |
// pointer to next morpheme |
| 326 |
|
| 327 |
struct Node enext; // 同じ位置で終わる形態素へのポインタ |
| 328 |
// pointer to next morpheme that ends at same location |
| 329 |
struct Node bnext; // 同じ開始位置で始まる形態素へのポインタ |
| 330 |
// pointer to next morpheme that begins at same location |
| 331 |
|
| 332 |
string surface; // 形態素の文字列情報 |
| 333 |
// orthography of morpheme in string form |
| 334 |
|
| 335 |
string feature; // CSV で表記された素性情報 |
| 336 |
// feature information in CSV format |
| 337 |
unsigned int length; // 形態素の長さ |
| 338 |
// length of morpheme |
| 339 |
unsigned int rlength; // 形態素の長さ(先頭のスペースを含む) |
| 340 |
// length of morpheme (including any leading space) |
| 341 |
unsigned int id; // 形態素に付与される ユニークID |
| 342 |
// unique ID for each morpheme |
| 343 |
unsigned short rcAttr; // 右文脈 id |
| 344 |
// ID for context on right |
| 345 |
unsigned short lcAttr; // 左文脈 id |
| 346 |
// ID for context on left |
| 347 |
unsigned short posid; // 形態素 ID (未使用) |
| 348 |
// morpheme ID (no longer used) |
| 349 |
unsigned char char_type; // 文字種情報 |
| 350 |
// character type information |
| 351 |
unsigned char stat; // 形態素の種類: 以下のマクロの値 |
| 352 |
// morpheme type: one of the following macro values |
| 353 |
// #define MECAB_NOR_NODE 0 |
| 354 |
// #define MECAB_UNK_NODE 1 |
| 355 |
// #define MECAB_BOS_NODE 2 |
| 356 |
// #define MECAB_EOS_NODE 3 |
| 357 |
unsigned char isbest; // ベスト解の場合 1, それ以外 0 |
| 358 |
// 1 when best parse, 0 otherwise |
| 359 |
|
| 360 |
float alpha; // forward backward の foward log 確率 |
| 361 |
// forward log probability of forward backward |
| 362 |
float beta; // forward backward の backward log 確率 |
| 363 |
// backword log probability of forward backward |
| 364 |
float prob; // 周辺確率 |
| 365 |
// surrounding probability |
| 366 |
// alpha, beta, prob は -l 2 オプションを指定した時に定義されます |
| 367 |
// alpha, beta, and prob are defined when -l 2 is specified |
| 368 |
|
| 369 |
short wcost; // 単語生起コスト |
| 370 |
// word occurrence cost |
| 371 |
long cost; // 累積コスト |
| 372 |
// cumulative cost |
| 373 |
}; |
| 374 |
} |
| 375 |
</pre> |
| 376 |
<h2>サンプルプログラム -- Sample programs</h2> |
| 377 |
<p> |
| 378 |
perl/test.pl, ruby/test.rb, python/test.py, java/test.java |
| 379 |
にそれぞれの言語のサンプルがありますので, 参考にしてください. |
| 380 |
</p> |
| 381 |
|
| 382 |
<p> |
| 383 |
There are sample programs in perl/test.pl, ruby/test.rb, |
| 384 |
python/test.py, and java/test.java for your reference. |
| 385 |
</p> |
| 386 |
|
| 387 |
<hr> |
| 388 |
<p> |
| 389 |
$Id: bindings.html,v 1.3 2006/01/07 14:37:40 taku-ku Exp $; |
| 390 |
</p> |
| 391 |
</body></html> |