Chapter 12 Google Translation API
You will need to install the following packages for this chapter (run the code):
# install.packages('pacman')
library(pacman)
p_load('googleLanguageR')
12.1 Provided services/data
- What data/service is provided by the API?
The API is provided by Google.
Google’s Translation API translates texts into more than one hundred languages. Note that the approach via the API is a lot more refined than the free version on Googles’ translation website and of course comes in very useful if text in large scale needs to be translated (possibly with longer and more complex content or syntax). For instance, you can choose to specify a certain model to further improve the translation (Neural Machine Translation vs. Phrase-Based Machine Translation).
The API limits in three ways: characters per day, characters per 100 seconds, and API requests per 100 seconds. All can be set in the API manager of your Google Cloud Project.
Consider that additionally to the Translation API which we demonstrate in this review, Google provides us with two further APIs for translation: AutoML Translation and the Advanced Translation API (see here for a short comparison).
12.2 Prerequisites
- What are the prerequisites to access the API (authentication)?
To access and to use the API the following steps are necessary:
Create a google account (if you do not already have one).
Using this google account login to the google cloud platform and create a Google Cloud Project.
Within this Google Cloud Project enable the Google Translation API.
For authentication you will need to create an API key (which you additionally should restrict to the Translation API). If however, you are planning to request the Natural Language API from outside a Google Cloud environment (e.g., R) you will be required to use a private (service account) key. This can be achieved by creating a service account which in turn will allow you to download your private key as a JSON file (we show an example below).
12.3 Simple API call
- What does a simple API call look like?
Here we describe how a simple API call can be made from within the Google Cloud Platform environment via the Google Cloud Shell:
To activate your Cloud Shell, inspect the upper right-hand corner of your Google Cloud Platform Console and click the icon called “Activate Shell”. Google Cloud Shell is a command line environment running in the cloud.
Via the built-in Editor in Cloud Shell create a JSON file (call it for instance ‘request.json’) with the text that you would like to perform analysis on. Consider that text can be uploaded in the request (shown below) or integrated with Cloud Storage. Supported types of your text are PLAIN_TEXT (shown below) or HTML.
{
"q": ["To administer medicine to animals is frequently a very difficult matter, and yet sometimes it’s necessary to do so"],
"target": "de"
}
For sending your data, pass a curl command to your Cloud Shell command line where you refer (via @) to your request.json file from the previous step.
Don’t forget to insert your individual API key in the curl command (alternatively, you could define it beforehand via adding a global variable to your environment → see example in the API call for Googles’ NLP API earlier in this document).
12.4 API access in R
- How can we access the API from R (httr + other packages)?
The following example makes use of the ‘googleLanguageR’ package from R which among other options (e.g., syntax analysis → see Chapter 2 of this review) allows calling the Cloud Translation API.
In this small example we demonstrate how to…
… authenticate with your Google Cloud Account within R
… translate an exemplary sentence
Step 1: Load package
Step 2: Authentication
Step 3: Analysis - API call and Translation
First, we create exemplary data. For demonstration purposes, we here again make use of the sentence from above. Of course, for your own project use a complete vector containing text data from within your data.
df_original <- data.frame(text = "To administer medicine to animals is frequently a very difficult matter, and yet sometimes it’s necessary to do so")
df_original$text <- as.character(df_original$text)
Next, using this data, call the API via the function ‘gl_translate()’ and importantly specify the target language (german in this example) within the ‘target’ argument.
This API call eventually provides results in a dataframe with three columns: translatedText ( → contains the translation), detectedSourceLangauge ( → contains a label for the original language being detected) and text ( → contains the original text).
Let’s check the translation.
Tieren Medikamente zu verabreichen ist oft eine sehr schwierige Angelegenheit, und doch ist es manchmal notwendig
12.5 Social science examples
Searching on literature using Google Translation Services we found a study by Prates, Avelar, and Lamb (2018) (Assessing gender bias in machine translation: a case study with Google Translate).
We are not aware of any further research / publications that made explicit usage of Google’s Translation API. However, we assume that the API (or at least Google’s Free Translation Service) is involved in many research projects. Generally, we are convinced that making use of automated translation (as well as converting speech to text ( → example also in this review) eventually in combination with translation) can be of great advantage for all kinds of qualitative or mixed-methods research projects. For instance, automated translation could be very useful for easily and very reliably translating data from qualitative interviews or other (field) experiments or observational data (where data might be collected in a foreign language). Also consider the other way around where data is available in the principal language of the research project but some of the textual data has to be communicated and presented in (for instance) english language.