

ルビ振りWebサービス
漢字かな交じり文に、ひらがなとローマ字のふりがな(ルビ)を付けます。
#!/usr/bin/perl
use strict;
use warnings;
use LWP::Simple;
use XML::Simple;
use URI::Escape;
use Encode;
use utf8;
use open ':utf8';
binmode STDIN, ":utf8";
binmode STDOUT, ":utf8";
my $appid = "YahooDemo";
while (<>) {
chomp;
next unless $_;
my $r_ref = yapi({sentence => $_});
my $yomibun = "";
foreach my $w (@$r_ref) {
my $c = $w->{Furigana} || $w->{Surface};
$c = " " if ref($c) eq "HASH"; # space
$yomibun .= $c;
}
$yomibun =~ tr/ァ-ン/ぁ-ん/;
print "$yomibun\n";
}
sub yapi {
my ($args_ref) = @_;
my $s = URI::Escape::uri_escape_utf8($args_ref->{sentence}) || "";
my $g = $args_ref->{grade} || 1;
return [] unless $s;
my $url = "http://jlp.yahooapis.jp/FuriganaService/V1/furigana?"
."appid=$appid&grade=$g&sentence=$s";
my $response = get($url);
return [] unless $response;
my $xmlsimple = XML::Simple->new(ForceArray => [ 'Word' ]);
my $xml = $xmlsimple->XMLin($response);
return [] unless $xml->{Result};
return $xml->{Result}->{WordList}->{Word};
}
$appid は自分で取得したものを使ってくださいね。% cat a.txt これはマネー本で、Perl の教科書ではない。 祇園精舎の鐘の声 諸行無常の響きあり % ./yomibun.cgi a.txt これはまねーぼんで、Perl のきょうかしょではない。 ぎおんしょうじゃのかねのこえ しょぎょうむじょうのひびきあり





