Created
March 15, 2011 06:56
-
-
Save riywo/870404 to your computer and use it in GitHub Desktop.
引数に与えた単語をGoogle翻訳で多言語に投げてみるスクリプト
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
use strict; | |
use warnings; | |
use Furl; | |
use JSON; | |
use Encode; | |
use Web::Scraper; | |
my $furl = Furl->new; | |
my $scraped = scraper { | |
process '/html/body/div[4]/div[3]/div[2]/div[2]/div[2]/div/div/div/table[2]/tbody/tr/td/code', | |
'lang[]' => 'TEXT'; | |
}->scrape($furl->get('http://code.google.com/intl/ja/apis/language/translate/v2/using_rest.html')->content); | |
print "trancelate: " . join(',', @{$scraped->{'lang'}}) . "\n"; | |
my $q = $ARGV[0] or die; | |
my $tgt = 'ja'; | |
my $url = "https://www.googleapis.com/language/translate/v2?key=XXXXXXXXXXX"; | |
for my $lang (@{$scraped->{'lang'}}) { | |
next if ($lang eq $tgt); | |
my $res = $furl->get($url."&source=$lang&target=$tgt&q=$q"); | |
my $text = decode_json(encode('utf-8', $res->content))->{'data'}->{'translations'}->[0]->{'translatedText'}; | |
next if ($text eq $q); | |
print "$lang:\t$text\n"; | |
} | |
__END__ | |
perl translate.pl oppai |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
https://code.google.com/apis/console/?api=translate
こちらで翻訳API用のapi keyを取得して$urlを書き換えて下さい。