mecab/swig で形態素のオリジナルテキスト上での位置を得る

http://lists.sourceforge.jp/mailman/archives/mecab-users/2007-February/000231.html

MeCab の出力フォーマットもしくは C/C++ API を使えば、ある単語が
元のテキストの何バイト目から何バイト目に出現したか分かるので、
元のテキスト中にスペースがあったかどうか区別することができます。

なにも考えずに swig api にも追加。

mecab/swig/MeCab.i に追加:

 %extend mecab_node_t {
   int position_start(mecab_node_t *ref)
   {
      return (int)( self->surface - (ref->surface + ref->length - ref->rlength) );
   }

   int position_end(mecab_node_t *ref)
   {
      return (int)( self->surface - (ref->surface + ref->length - ref->rlength) + self->length );
   }
 }

仕様がださいですが、一応動きます。

standoff 風出力:

my $head;
for (my $m = $head= $c->parseToNode($sentence); $m; $m = $m->{next}) {
    printf("%d %d\Token %s\t%s\n", $m->position_start($head), $m->position_end($head), $m->{surface}, $m->{feature});
}