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