How to do language translation in Python

How to do language translation in Python

Hi Tech-nerds,

Today I'm going to share with you guys how to automatically perform language translation in Python programming.

Language translation concerns translating textual data from one language to textual data into another language while maintaining the semantic meaning

This is usually done in such a way the same message can be conveyed to different people speaking different languages, We now have over 6,500 Spoken languages in which more than 3,982 have developed a writing system

Therefore knowing how programmatically perform language translation can be your superpower of getting your articles, ebook, and etc to reach more people no matter what language they speak

Let's get started

We are going to cover only the very basics of doing language translation in python using pre-trained models

Python Libraries

There are several libraries in Python for performing Automatic language translation, below are some of those Libraries but almost all of them are using Google Translate API.

Just check them out in detail and choose which one you like the most to use your personal projects involving language translation, well on this blog post I will be reviewing only 3 of them

Goslate

Goslate provides you free python API to google translation service by querying the google translation website.

Installation

$~ pip install goslate

goslate will automatically detect your primary language of the text and then translate it to the secondary language you specify, during specifying language you use ISO 639-1 code of the language, you can find the whole list in Wikipedia

Let's try to translate some English text into French

You only specify the second language you're translating into and goslate will automatically detect your primary language as shown in example below

Sample Usage

>>>import goslate
>>>primary_text = 'Love you a lot '
>>>gs = goslate.Goslate()
>>>gs.translate(primary_text, 'fr')
"Je t'aime beaucoup"

Now Let's move into the next library Google translate

Googletrans

Googletrans is a free and unlimited python library that implemented Google Translate API. This uses the Google Translate Ajax API to make calls to such methods as detecting and translating.

Installation

$~ pip install googletrans

The simplicity of usage of googletrans and goslate is almost the same so it all comes to you choosing the one you like, same wise on googletrans we are using ISO 639-1 standard to represent languages.

Let's try converting some English text into Swahili

Sample Usage

>>> text = 'This site is awesome'
>>> from googletrans import Translator
>>> translator = Translator()
>>> translator.translate(text , dest ='sw').text
'Tovuti hii ni ajabu

Lastly let's do some translation using NLP library Textblob

TextBlob

TextBlob is a Python (2 and 3) library for processing textual data. It provides a simple API for diving into common natural language processing (NLP) tasks such as part-of-speech tagging, noun phrase extraction, sentiment analysis, classification, translation, and more

Installation

$~ pip install textblob

one of the good things of working with textblob is that its general-purpose natural language processing library which you can use for its variety of NLP tasks apart from translation but simplicity and syntax all most the same as the two above.

Sample Usage

Let's try converting some French text to English

>>> from textblob import TextBlob
>>> blob = TextBlob('comment ca va ?')
>>> blob.translate(to='en')
TextBlob("How is it going ?")

Well that's all, Hope you had fun learning language translation in Python

The Original Article can be found on my blog kalebujordan.com

If you find it useful don't forget to it a like and to share with your fellow developers