How to use Google Cloud Translate API in a RapidMiner Process
This is the first of several articles to help people use external APIs from within RapidMiner. There are some APIs that actually do not need any effort at all because there are extensions that make your life very easy. A good example is the NamSor extension by Elian Carsenat that will predict gender and nationalities based on first and last names. I strongly encourage people to take advantage of this resource if you work with names.
Unfortunately there are very few APIs that are as easy to use in RapidMiner as NamSor. As all APIs work differently, I'd like to show how to use some common ones and if you need to use a different one, you can likely figure it out from here.
GOOGLE CLOUD API
Google has an amazing suite of APIs that are available to developers at relatively low cost:
- Google Cloud Storage
- Google Cloud Translation
- Google Drive
- Google Maps Directions
- Google Maps JavaScript
- Google Picker
Here I am going to show how to use Google Cloud Translation to take text and use the Google Translate API to detect the language. You can of course change this to whatever you want.
1. You will need to create a Google Cloud developer account to get an API key. You do this on https://cloud.google.com. Once you do this, create an API Key. Keep this key secure as it is the way Google authenticates and allocates the billing.
2. If you have not already done so, download the Web Mining and Text Processing extensions in RapidMiner Studio.
3. Build a process that sends a text attribute to the Get Page operator (found in the Web Mining extension) and then connect to the results.
4. The only hard part here (and the only thing that changes from API to API) is how you set up this operator. For Google Cloud APIs, you will set it up like this:
The url is cut-off here but is https://translation.googleapis.com/language/translate/v2/detect?key=<your Google Cloud key>.
Note I am using POST rather that GET requests. This is due to the character limit on GET requests and, most likely, your text will exceed this character limit. Also note that I am putting the API key in the url, rather than in the header of the request ("request properties").
In the body, you will add query parameters like this:
5. Run
INPUT:
OUTPUT:
That's about it. Have fun!
Scott