Yahoo!日本語形態素解析APIを使う

今さら感はありますが、日本語形態素解析APIを使ってみました。

  • サンプルコード
#!/usr/local/bin/perl

use strict;
use warnings;
use Encode qw/encode_utf8/;
use LWP::UserAgent;
use XML::Simple;

my $app_id = '前準備で登録したアプリケーションID';
my $api_uri = 'http://jlp.yahooapis.jp/MAService/V1/parse';
my $text = '庭には二羽ニワトリがいる';

my $ua = LWP::UserAgent->new();
my $res = $ua->post(
    $api_uri,
    {
        appid => $app_id,
        sentence => $text,
    },
);

my $ref = XMLin($res->content);
for my $word (@{$ref->{ma_result}->{word_list}->{word}}) {
    print encode_utf8(sprintf "%s: %s,%s\n", $word->{surface}, $word->{pos}, $word->{reading});
}

__END__

実行すると以下のようになります。

庭: 名詞,にわ
に: 助詞,に
は: 助詞,は
二: 名詞,2
羽: 接尾辞,わ
ニワトリ: 名詞,にわとり
が: 助詞,が
いる: 動詞,いる


テキスト解析:日本語形態素解析 - Yahoo!デベロッパーネットワーク