Python Tutorials

How to create a Translator in 4 lines (in Python w/ TextBlob)

Textblob has many useful tricks up its sleeve. Generally it’s a very useful Language Processing library that can be used with Python.

You can install it simply by executing the codes below in your Anaconda Command prompt consecutively:

conda install -c conda-forge textblob

python -m textblob.download_corpora

Used Where?

  • Communication
  • Apps
  • Book translation
  • Research
  • Entertainment
  • Literature

As usual, let’s import the library that we’re going to use:


import textblob

Estimated Time

5 mins

Skill Level

Intermediate

Modules

TextBlob

Libraries

textblob

Tutorial Provided by

HolyPython.com

Textblob in application

Now let’s create a user input that asks the user to enter a text:

f1 = input("Please enter the text you want to translate")

If you need a refresher on user inputs you can check out this lesson or these exercises:

Next, you can create a textblob object and print a translation by using the  .translate() method.

blob = textblob.TextBlob(f1)
print(blob.translate(to="en"))

Here is the full code:

import textblob
f1 = input("Please enter the text you want to translate")
blob = textblob.TextBlob(f1)
print(blob.translate(to="en"))

Output:

Failure is the mother of success

Another cool thing about this library is that, the language of the text being entered is auto detected.

Additionally you can tweak the code so that it translates a file instead of userinput.

import textblob
f1=r'C://Users/xx/Desktop/book.txt'
str = open(f1,"r")
str = str.read()
blob = textblob.TextBlob(str)
print(blob.translate(to="en"))

Conclusion

Textblob uses Google Translate as its translation engine and makes translations as easy as opening the browser and navigating to Google Translate manually and maybe even more fun.

Pixel positions

Closing, we hope you enjoyed this tutorial and found it interesting. Textblob is a super convenient text processing library based on Natural Language Toolkit (NLTK).

Usag1r

Share
Published by
Usag1r

Recent Posts

Benefits of Bokeh over Python visualization libraries like Seaborn, Matplotlib & Plotly

ContentsIntroductionBokeh vs Seaborn & MatplotlibBokeh vs PlotlySummary Introduction As computer science activity booms, you might…

12 months ago

History of Speech-to-Text AI models

History of Speech-to-Text Models​ & Current Infrastructure As speech technology continues to advance, so too…

1 year ago

Advanced Level Python Tips & Tricks

Python Tips & Tricks (Part III) Advanced Level HolyPython.com Here are more Python tips about…

1 year ago

Intermediate Level Python Tips & Tricks

Python Tips & Tricks (Part II) Intermediate Level HolyPython.com The list continues with slightly more…

1 year ago

When is Python 4 Coming Out?

Almost 15 years after the release of Python 3 many people are wondering when Python…

2 years ago

How to Create an app with a 3D model using VIKTOR

Introduction We are going to need smart engineering solutions to solve our planet's problems (and…

2 years ago