How To Use Google Cloud Translation API

Posted on Jun 14, 2018 in Project Code • 10 min read

Google Cloud Translation API

Here is the code I used to translate the LTN corpora.

In [ ]:
import pandas as pd
import numpy as np
from google.cloud import translate

Explicitly set Google Authentication Credentials

In [ ]:
import os
os.environ["GOOGLE_APPLICATION_CREDENTIALS"]="PATH TO FILE"

Instantiate the Google translation client

In [ ]:
translate_client = translate.Client()
target='en'

Read in the wrangled results from web scraping

In [ ]:
df = pd.read_excel('liberty_results_wrangled.xlsx')
text = df['articleCH']
translatedEN =[]
failed = []

Notes when using Google Translation API

  • set daily maximum quota to unlimited
  • there is a user rate limit --> use time.sleep() to buffer the quota instead rerunning the whole thing
In [ ]:
for idx, article in enumerate(text2):
    print('Now at article ' + str(idx))
    try:
        translation = translate_client.translate(article, target_language=target)
        translatedEN.append(translation['translatedText'])
    except Exception as e:
        print(e)
        translatedEN.append('ao654vm0')
        failed.append(idx)
        time.sleep(100)

Create column in dataframe to store results

In [ ]:
df['translatedEN'] = translatedEN
In [ ]:
df.head()