Skip to content

Instantly share code, notes, and snippets.

@hoehrmann
Created February 14, 2014 17:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hoehrmann/9004984 to your computer and use it in GitHub Desktop.
Save hoehrmann/9004984 to your computer and use it in GitHub Desktop.
Google Translate phonetic (english-only?) transliterations
#!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