package Acme::Zenbu::Waka::ko;
use strict;
use warnings;
use utf8;
use Data::Dumper;
use Encode;
use LWP::Simple;
use YAML;
use XML::Simple;
# request base
my $base_url = 'http://jlp.yahooapis.jp/MAService/V1/parse?';
# app key
my $app_id = 'your app id';
# sentence
my $sentence = $ARGV[0];
# encoding
$sentence =~ s/([^\w])/"%" . unpack("H2", $1)/eg;
# request url
my $request_url = $base_url . "appid=$app_id" . "&results=ma&sentence=" . $sentence;
# response
my $response = LWP::Simple::get($request_url) or die "Cannot get $request_url: $!";
# parse
my $parser = XML::Simple->new();
my $doc = $parser->XMLin($response);
my $words = $doc->{ma_result}->{word_list}->{word};
my $resentence = '';
while (my $word = shift(@{ $words })) {
if ($word->{pos} eq '形容詞') {
$resentence .= $word->{surface} . 'の';
}
elsif ($word->{pos} eq '名詞') {
$resentence .= $word->{surface} . ' ';
}
elsif ($word->{pos} eq '助詞') {
$resentence .= $word->{surface} . ' ';
}
else {
$resentence .= $word->{surface};
}
}
print Encode::encode('utf8', $resentence);
これの結果
「へやくるもOK」->「へ や くるも OK 」
とか
「台北一番安い店」->「台北 一番安いの店」
とか変換された。
形容詞の前に「の」を入れたり、名詞や助詞の後に空白を入れるだけで割と雰囲気でるもんですね。
ニーズはないだろうけどもうちょっと作り込むと、アジア各国で見かけるえせ日本語ジェネレータとかになるのかな〜と思う。
SEE ALSO : http://twitter.com/wakako_zenbu