Translating Strings in Python with TextBlob - Stack Abuse

Translating Strings in Python with TextBlob


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!")

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')

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')

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:

Ведь, если звезды зажигают -  
значит - это кому-нибудь нужно?  
Значит - кто-то хочет, чтобы они были?  
Значит - кто-то называет эти плевочки  
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  

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)

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

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

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)

The result is:

Better understand your data with visualizations

  •  30-day no-questions refunds
  •  Beginner to Advanced
  •  Updated regularly (update June 2021)
  •  New bonus resources and guides

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')

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


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.

Last Updated: August 12th, 2020

Improve your dev skills!

Get tutorials, guides, and dev jobs in your inbox.

No spam ever. Unsubscribe at any time. Read our Privacy Policy.

Want a remote job?

    Prepping for an interview?

    • Improve your skills by solving one coding problem every day
    • Get the solutions the next morning via email
    • Practice on actual problems asked by top companies, like:

    Better understand your data with visualizations

    •  30-day no-questions refunds
    •  Beginner to Advanced
    •  Updated regularly (update June 2021)
    •  New bonus resources and guides

    © 2013-2021 Stack Abuse. All rights reserved.