Created
June 25, 2010 04:11
-
-
Save poppen/452393 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env perl | |
use strict; | |
use warnings; | |
use 5.012; | |
use Config::Pit; | |
use LWP::Simple; | |
use XML::Simple; | |
use URI::Escape; | |
use Encode; | |
use utf8; | |
binmode STDOUT, ":utf8"; | |
my $app = pit_get( | |
"developer.yahoo.co.jp", | |
require => { | |
"id" => "your yahoo application id", | |
} | |
); | |
my $grade = 1; | |
while (<>) { | |
chomp; | |
my $furigana = yapi_furigana({sentence => $_, grade => $grade}); | |
say $furigana; | |
} | |
sub yapi_furigana { | |
my ($args_ref) = @_; | |
my $s = URI::Escape::uri_escape($args_ref->{sentence}) || ""; | |
my $g = $args_ref->{grade} || 1; | |
return [] unless $s; | |
my $url = "http://jlp.yahooapis.jp/FuriganaService/V1/furigana?" | |
."appid=$app->{id}&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}; | |
my $string; | |
for my $word (@{$xml->{Result}->{WordList}->{Word}}) { | |
$string .= $word->{Furigana}; | |
} | |
return $string; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment