Make Your Computer translate different languages for You using IBM Watson.

Make Your Computer translate different languages for You using IBM Watson.

Using the IBM Language Translator API to translate and identify languages.

Subscribe to my newsletter and never miss my upcoming articles

“Some people worry that artificial intelligence will make us feel inferior, but then, anybody in his right mind should have an inferiority complex every time he looks at a flower.” —Alan Kay

Objectives

At the end of this article you should be able to:

  • Access the IBM Watson Language Translation API.
  • Use your computer to translate a sentence from one language to another.
  • Give a sentence in a foreign language to your computer and have it tell you what language it was given.

IBM Watson is a Platform As a Service that is used to develop, deploy and scale AI-powered applications. Today we are going to use IBM Watson to translate and discover languages.

Before we begin Let's give a little background on AI language Translation

Below is a picture describing Language Translation Begin Pic.jpg

In simple words, AI translators are digital tools that use advanced artificial intelligence to not only translate the words that are written or spoken but also to translate the meaning (and sometimes sentiment) of the message.

Before we begin I have a few...

Dumb Assumptions

I dumbly assume that you have:

  • Knowledge of Python and use of PIP.
  • Internet Connection.
  • An Email Address.
  • A text editor or Jupyter notebook installed.

You also need to have an IBMID, if you don't have one, click here to get one. getting an IBMID is essential because we will be using the IBM text-to-speech API to build our software if you have never used a PAAS don't fret I'll walk you through it😉.

Now you have gotten your IBMID let's ..

Get Our API Key

Below I will lead us on the steps we need to get our API key

First

You go to the IBM Cloud Catalog page

Below is a picture of the IBM Cloud Catalog Page IBM CATALOG Landing.png

Second

You click on the Services link, once you do that you will see a drop-down menu called category which lists all of the service categories offered by IBM Watson

Below is a picture showing the services link IBM CATALOG Landing.png

Third

In the category drop down you will see a lot of services like Compute, Containers, Networking, Storage etc. Click on the AI and Machine Learning link.

Below is a picture showing the AI and Machine Learning link IBM CATALOG Landing.png

Fourth

Once you click on the AI and Machine Learning link you will then be given a Catalog of Watson's AI and Machine Learning services.

Below is a picture showing IBM Watson's AI and Machine Learning services AI and ML Page .png

Fifth

If you look through the services available on the IBM Watson AI and Machine Learning services page. You'll see services like Watson Assistant, Watson Studio, Knowledge Studio, etc click on the Language Translator service.

Below is a picture showing the Language Translator service on the AI and Machine Learning page AI and ML Page .png

Sixth

Once you have clicked on the Language Translator service you will be taken to the Page shown below.

landing for Translator.png

Seventh

Now I'm going to assume you have an IBMID but if you do not click here to get one.

Now you have an IBMID you click on the log in link located at the top right corner of the page displayed below. Once you log in you will see a blue button with create on it click on it you will be taken to the page displayed below where you will be taken to the page shown below which contains your API and URL key.

endpoint for language translator.png

Now leave the page with your APIKey and URL open we will get back to it.

NB: If upon creating your instance you don't immediately get sent to the page with your URL and APIKey click on the "manage" link on the left side of your screen and you should then see your API Key and URL

Now Let's get to the interesting part and...

Get Started with our project.

Before we get building if at any point during the tutorial you get stuck or you just want to see my code😅feel free to:

  • View the tutorials GitHub repository by clicking here.
  • Play with the tutorial's code on google colab by clicking here

Let's Get to it

Project Flow

Below is the flow which the project is going to take

  • Install Dependencies:Here we are going to install the necessary python packages needed to use the API.
  • Authenticate:: Here we are going to provide our API credentials and link to the API.
  • Translate Language:Here we are going to use the Language Translator API to translate from one Language to Another.
  • Identify Languages:Here we are going to give our computer a foreign language and then make it tell us what language we gave it.

Let us Begin

Install Dependancies

I am assuming you have python, pip and a text editor or jupyter notebooks installed.

If you have the above-listed let's begin, to make use of the IBM Text-to-Speech API we are going to install the IBM-Watson library, by typing in the command listed below in your terminal or command line.

pip install ibm-watson

If you are using the jupyter notebook environment you can put down the command listed below in a cell.

!pip install ibm-watson

If the package is done installing you can move on to the next step.

Authenticate

In this section, we are going to link to the IBM Watson Language Translation API.

To begin we are going to put in the URL and the API key given to us on the Language Translation API page.

below is a picture showing the Language Translation API page endpoint for language translator.png

url = 'https://api.eu-gb.text-to-speech.watson.cloud.ibm.com/instances/d04b5ea5-e6b9-4'

apikey = 'McPLvaUHWywOhceFBH4inrgnvirrwoomckw[omlap'

#I have used a fake url and apikey.

Now we are going to import the necessary tools from the IBM Watson Package

#importdependencies

from ibm_watson import LanguageTranslatorV3

from ibm_cloud_sdk_core.authenticators import IAMAuthenticator

Next up we are going to authenticate our API Key and set up our Language Translator service

#authenticate

authenticator = IAMAuthenticator(apikey)

#setup service
lt = LanguageTranslatorV3(version='2018-05-01', authenticator=authenticator)

lt.set_service_url(url)

We have successfully authenticated and set up our Language Translator service. Now let's...

Translate from One Language to Another

We are going to be translating German to English. Why German? You might ask well because I love Germany. Deal with it😎🤞.

#translating to german

translation = lt.translate(text='hello world', model_id='en-de').get_result()

#getting the resulting translation

translation

In the code above we translated the English word Hello World to German. We called the translated object and then we put in our word hello world as an argument, we then told our computer what language to translate from and what language to translate to using the model_id argument.

The model_id argument is how we tell the computer what to translate to and what to translate from. Above translated from English to German. so we passed en which stands for English at the beginning which shows that is the language we are translating from and then we put a - then we passed de which stands for Deutschland and Deutschland is essentially german for Germany (it literally means The German Lands in German).

Now I'm sure you might be wondering how you will know the language codes(theen and de thing) to use when you want to translate from a whole different language to another whole different language. Well, don't bother, IBM Watson has got you covered. The Language Translation docs has all the language codes which the Language Translator has support for down here.

When you run the code above you might notice that your result will include things called word_count and character_count if you want to remove all those and see only the translated result you do something called traversing. In simple words this means we tell the API that we only want the translated result and not the other stuff(word_count and character_count).

#traversing code below

translation['translations'][0]['translation']

In the code above put all the results given to us in a list and then we returned only the first value. The translated result (neat trick huh. I know😎)

We've seen how the Language Translator can translate languages now let's see how it...

Identifies Languages

Here we are going to give our computer a sentence in a different language and then make it tell us what language the sentence is in. (AI is cool huh😎).

language = lt.identify('This is a regular sentence' ).get_result()

language

In the first line of the code above we called the identity object and passed the sentence This is a regular sentence and then we asked the computer to tell us what language the sentence was written by calling the get_result() object.

You might be wondering how the computer discovers the correct language for the sentence, well I'll tell you

AI at it's core is all about percentages and data so if you show AI a shoe and ask it to tell you what it is. The AI goes through all the stuff it knows(data) and then looks for what is similar to the stuff (the shoe) you gave it to find and when it then decides what the stuff (the shoe) looks like through percentages. So if you trained that AI with pictures of cars, books and shoes. It will tell you that what you have just shown it is a shoe simply because when it went through the data (the pictures of cars, books and shoes) it saw that the stuff you showed it(the shoe ) matched more 0with a show than it did with a car or a book.

That is also how the Language Translator AI we just built works it goes through all the languages it had been trained with by IBM Watson and then tries to find which one matches closely with the sentence we just gave to it. Once you run you code you will see the percentages it got when it tried matching with the other languages and the percentages it shows you is something called confidence Score.

Now AI isn't all perfect you see. Let's go back to our earlier example of the shoe, care and book AI. Let's take for instance when you showed (trained) the AI on pictures of shoes(data) and you didn't show it pictures of snickers. Then the shoe you showed to it was a sneaker your AI might not be able to correctly tell you what shoe that is because of something called bias in the data. We'll talk about that another day😉.

If you want to hear more about my explanations of AI follow me on Twitter.

To learn more about the cool features of the IBM Watson Language Translator read the comprehensive docs by clicking here

Happy Building🚀.

 
Share this
Proudly part of