Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Re: language translate

by localshop (Monk)
on Oct 31, 2018 at 03:55 UTC ( #1224966=note: print w/replies, xml ) Need Help??


in reply to language translate

I'm currently refining WebService::GoogleAPI::Client module that is on CPAN and can help you do this if you wish to use Google Translate API.

Here's an example of translating from English to French.

#!/usr/bin/env perl use WebService::GoogleAPI::Client; use Data::Dumper qw (Dumper); use strict; use warnings; use feature 'say'; =head1 translation_example.pl =head2 PRE-REQUISITES Setup a Google Project in the Google Console and add the Translate API + Library. You may need to enable billing to access Google Cloud Services. Setup an OAUTH Credential set and feed this into the CLI goauth included in WebService::GoogleAPI::Client and use the tool to authoris +e your user to access the project which will also create the local gapi. +json config. =over 2 =item https://www.googleapis.com/auth/cloud-translation =item https://www.googleapis.com/auth/cloud-platform =item assumes gapi.json configuration in working directory with scoped + project and user authorization =back Translation Service Specification: 'discoveryRestUrl' => 'https://translation.go +ogleapis.com/$discovery/rest?version=v2', 'description' => 'Integrates text translation + into your website or application.', 'preferred' => $VAR1->{'items'}[0]{'preferred +'}, 'kind' => 'discovery#directoryItem', 'id' => 'translate:v2', 'version' => 'v2', 'title' => 'Cloud Translation API', 'icons' => { 'x32' => 'https://www.gstatic.co +m/images/branding/product/1x/googleg_32dp.png', 'x16' => 'https://www.gstatic.co +m/images/branding/product/1x/googleg_16dp.png' }, 'name' => 'translate', 'documentationLink' => 'https://code.google.c +om/apis/language/translate/v2/getting_started.html' }, =cut ## assumes gapi.json configuration in working directory with scoped pr +oject and user authorization ## manunally sets the client user email to be the first in the gapi.js +on file my $gapi_client = WebService::GoogleAPI::Client->new( debug => 0, gapi +_json => 'gapi.json', debug=>1 ); my $aref_token_emails = $gapi_client->auth_storage->storage->get_token +_emails_from_storage; my $user = $aref_token_emails->[0]; $gapi_client->user( $user ); ## set to the first authorised user email + # my $list = $gapi_client->discover_all(); # say Dumper $list; # list available api endpoints #my $r = $gapi_client->api_query( api_endpoint_id => 'translate.langu +ages.list'); #say Dumper $r->json; ## https://cloud.google.com/translate/docs/reference/translate my $r = $gapi_client->api_query( api_endpoint_id => 'translate.transl +ations.translate', options => { q=>'this is some text to transla +te into french', target => 'fr', format => 'text', } ); say Dumper $r->json; say $r->json->{data}{translations}[0]{translatedText};

Replies are listed 'Best First'.
Re^2: language translate
by Aldebaran (Deacon) on Nov 11, 2018 at 02:13 UTC

    Thank you localshop for this write-up and your work on this module. I've been nibbling and lurking at these threads, all the while building out what I can do with them. I went ahead and signed up for google cloud services. Now that I've done it, I'm glad for it and have little fear that google is gonna run away with my credit card anytime soon. They do give you $300 worth of credit and a year for a trial period and promise not to upgrade to paying without your say-so.

    I built the sdk for debian. 5 commands: easy-peasy. The client is offered in 6 syntaxes, but perl is not one of them. For people who say "perl is dead," that might be proof, but I wonder if perl comes at it differently. Let me ask the question, short of igniting a flame war, is perl unfit for this task?

    Does this module, WebService::GoogleAPI::Client, seek to brook that gap?

    I wrote a bash driver for this script so that I could set environment variables before invocation. I get a single warning or error:

    $ ./2.google.sh >1.txt Use of uninitialized value in split at /usr/local/share/perl/5.26.1/We +bService/GoogleAPI/Client/AuthStorage/ConfigJSON.pm line 82. $

    I'll put output, perl script, and bash driver between readmore tags

    It would seem that the Oauth isn't correct. What fields should the .json file have? I think one can show the fields with redacted values and not lose his shirt:

    { "type": "service_account", "project_id": "trans1-221819", "private_key_id": ..., "private_key": "-----BEGIN PRIVATE KEY-----... "client_email": "jehosophat@trans1-221819.iam.gserviceaccount.com", "client_id": "115338096797549417908", "auth_uri": "https://accounts.google.com/o/oauth2/auth", "token_uri": "https://oauth2.googleapis.com/token", "auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1 +/certs", "client_x509_cert_url": "https://www.googleapis.com/robot/v1/metadat +a/x509/jehosophat%40trans1-221819.iam.gserviceaccount.com" }

    I don't think I made many changes from your script except to print values and I took out one of the debug => 0. Looking for a precise definition of what "scoped" project means. Thank you for your comments and scripts,

Re^2: language translate
by bigup401 (Pilgrim) on Oct 31, 2018 at 08:57 UTC

    Google translator nolonger for free

    so i wanted to use something like a module

      The free usage is reasonably generous and should be fine for a website or general ad-hoc and experimental usage.

      AutoML translation for example gives free the first 500K characters/month.

      If you just want basic usage it should be fine - there are more advanced options that include training your own data set etc.

        I'm glad to see this thread, as I do a fair amount of translating and find that machine translations make good first guesses. I posted on stackoverflow a question about what all is involved in getting a google api key like the one needed for the module you use. I got nothing but crickets. I suspect it may have something to do with google's snafu with a data breach recently, where those with api keys had access to stuff they shouldn't have. Furthermore, google is one of those companies like facebook that is ginormous but has no ordinary place for users to talk to an actual person to get the skinny on something.

        My question is what are the costs involved? I'm guessing that it would be a function of use, but does that mean you give them a credit card?

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1224966]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (6)
As of 2021-05-09 17:35 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Perl 7 will be out ...





    Results (102 votes). Check out past polls.

    Notices?