Translating Strings in Python with TextBlob

Introduction

Text translation is a difficult computer problem that gets better and easier to solve every year. Big companies like Google are actively working on improving their text translation services which enables the rest of us to use them freely.

Apart from their great personal use, these services can be used by developers through various APIs. This article is about TextBlob which uses one such API to perform text translation.

What is TextBlob?

TextBlob is a text-processing library written in Python. According to its documentation, it can be used for part-of-speech tagging, parsing, sentiment analysis, spelling correction, translation, and more. In this article, we'll focus on text translation.

Internally, TextBlob relies on Google Translate's API. This means that an active internet connection is required for performing translations.

Installing TextBlob

Let's start off by installing TextBlob using pip, and downloading the corpora of words it needs to function:

$ pip install -U textblob
$ python -m textblob.download_corpora

Using TextBlob

Using TextBlob is straightforward and simple. We just import it, assign a string to the constructor and then translate it via the translate() function:

from textblob import TextBlob

blob = TextBlob("Buongiorno!")
print(blob.translate(to='en'))

The translate() function accepts two arguments - from_lang and to. The from_lang is automatically set depending on the language TextBlob detects.

The above example uses the Italian phrase Buongiorno which translates to Good morning in English.

Sometimes, we might wish to detect a language to decide if the text needs translation at all. To detect the language of some text, TextBlob's detect_language() function is used:

from textblob import TextBlob

blob = TextBlob("Buongiorno!")
if (blob.detect_language() != 'en')
    blob.translate(to='en'))

Translation Examples and Accuracy

Sentence Translation From English into Hindi

As our first example, we'll see how well English is translated into Hindi:

blob = TextBlob('TextBlob is a great tool for developers')
print(blob.translate(to='hi'))

The result is the following:

डेवलपर्स के लिए एक बढ़िया टूल है

Translating Russian Poetry into Croatian

Let's see how TextBlob manages poetry. The following is the work of a Russian poet Vladimir Mayakovsky, first in Russian and then in English:

Послушайте!  
Ведь, если звезды зажигают -  
значит - это кому-нибудь нужно?  
Значит - кто-то хочет, чтобы они были?  
Значит - кто-то называет эти плевочки  
жемчужиной?
Listen!  
See, if stars light up  
does it mean that there is someone who needs it?  
Does it mean that someone wants them to exist?  
It means that someone calls these little spits  
magnificent.

We'll feed the original poem in Cyrillic to TextBlob and see how well it translates into Croatian. Since both Russian and Croatian and Slavic languages, the translation is expected to be relatively good:

from textblob import TextBlob

poem = 'Послушайте! Ведь, если звезды зажигают - значит - это кому-нибудь нужно? Значит - кто-то хочет, чтобы они были? Значит - кто-то называет эти плевочки жемчужиной?'

blob = TextBlob(poem)
print(blob.translate(to='hr'))

By running the above code, we get the following output (formatted for convenience):

Slušati!
Uostalom, ako su zvijezde upaljene
znači li to nekome treba?
Dakle - netko želi da to budu?
Dakle - netko naziva ove pljuvačke
biserom?

Most of the translation is good except for the first word which would sound better if it were Slušajte instead of Slušati, in vocative. Although not perfect, you could understand the translation.

Translating Array of German Words into English

In some cases, we won't have full sentences to translate. We might have a list or an array of words. These are a lot easier to translate, since there's no context to potentially change the translation:

from textblob import TextBlob

worter =  ['einer',  'zwei',  'drei',  'vier',  'fünf',  'sechs',  'sieben',  'acht',  'neun',  'zehn']

for w in worter:
	blob = TextBlob(w)
	print(blob.translate(to='en'))

The result is:

one
two
three
four
five
six
seven
eight
nine
ten

Looks good!

Translating Food from English into French

Finally, let's translate an English word into French:

from textblob import TextBlob

blob = TextBlob('An apple')
print(blob.translate(to='fr'))

The French translation is Une pomme. Bon Appétit!

Conclusion

Translation is an interesting but difficult computer problem. Deep learning and other AI methods are becoming increasingly good at understanding language and performing automated language translation. TextBlob is one of the tools available to developers that can be used for performing such automated language translations.

There are many benefits to this kind of approach, however, not all translations are perfect. These techniques are still evolving and if you're in the need of a high-quality translation of great importance, it is always best to consult with a professional translator.

For all other purposes, however, tools such as TextBlob are more than enough to provide convenience for simple translation and satisfy the curiosity of developers using them.

Author image
About Luka Čupić
Croatia Website