Created
February 14, 2014 17:11
-
-
Save hoehrmann/9004984 to your computer and use it in GitHub Desktop.
Google Translate phonetic (english-only?) transliterations
This file contains hidden or 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
#!perl -w | |
use strict; | |
use warnings; | |
use LWP::UserAgent; | |
use URI; | |
use JSON; | |
use YAML::XS; | |
my $uri = URI->new('http://translate.google.com/translate_a/t'); | |
my $source = 'ru'; | |
my $target = 'de'; | |
my $text = 'Северная Фризия'; | |
##################################################################### | |
# Setup request | |
##################################################################### | |
$uri->query_form( | |
'client' => 't', | |
'hl' => 'en', | |
'sl' => $source, | |
'tl' => $target, | |
'multires' => '1', | |
'otf' => '1', | |
'ssel' => '0', | |
'tsel' => '0', | |
# 'uptl' => 'en', # seems to override `tl` and unnecessary | |
'sc' => '1', | |
'text' => $text | |
); | |
my $ua = LWP::UserAgent->new(agent => | |
'Mozilla/5.0 (X11; U; Linux i686) Gecko/20071127 Firefox/2.0.0.11'); | |
my $response = $ua->get($uri); | |
unless ($response->is_success) { | |
... | |
} | |
my $response_text = $response->decoded_content(charset => 'none'); | |
##################################################################### | |
# Quick and dirty JSON repairs. | |
##################################################################### | |
while ($response_text =~ /,,/) { | |
$response_text =~ s/,,/,null,/g; | |
} | |
$response_text =~ s/\[,/\[null,/g; | |
$response_text =~ s/,\]/,null]/g; | |
my $d = JSON->new->decode($response_text); | |
print Dump { phonetic_transliteration_of_output => $d->[0][0][2] }; | |
print Dump { phonetic_transliteration_of_input => $d->[0][0][3] }; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment