Skip to content

Instantly share code, notes, and snippets.

@poppen
Created June 25, 2010 04:11
Show Gist options
  • Save poppen/452393 to your computer and use it in GitHub Desktop.
Save poppen/452393 to your computer and use it in GitHub Desktop.
#!/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