TextBlob In Python

Lerekoqholosha
2 min readSep 21, 2021

TextBlob is a Python library that can be used to process textual data. Some of the tasks where it is good to use are sentiment analysis, tokenization, Spelling Correction, and many other natural language processing tasks. In this article, I’ll walk you through a tutorial on TextBlob in Python.

What is TextBlob in Python?

TextBlob is an open-source Python library that is very easy to use for processing text data. It offers many built-in methods for common natural language processing tasks. Some of the tasks where I prefer to use it over other Python libraries are spelling correction, part of speech tagging, and text classification. But it can be used for various NLP tasks like:

  1. Noun phrase extraction
  2. Part of speech tagging
  3. Sentiment Analysis
  4. Text Classification
  5. Tokenization
  6. Word and phrase frequencies
  7. Parsing
  8. n-grams
  9. Word inflexion
  10. Spelling Correction

I hope you now have understood on which types of problems we can use the TextBlob library in Python. In the section below, I will take you through a tutorial on TextBlob in Python.

TextBlob in Python (Tutorial)

If you have never used this Python library before, you can easily install it on your systems using the pip command; pip install textblob. Now let’s see how to use it by performing some common natural language processing tasks. I’ll start by using it to analyze the sentiment of a text:

#Installing textblob
!pip install textblob
from textblob import TextBlob# Sentiment Analysis
text = TextBlob("Python is a high-level, general-purpose programming language.")
print(text.sentiment)Sentiment(polarity=0.0, subjectivity=0.0)

Now let’s have a look at how to do tokenization by using this library:

Before tokenization make sure you have imported nltk

#nltk
import nltk
nltk.download('punkt')# Tokenization
text = TextBlob("Simple is better than complex.")
print(text.words)
['Simple', 'is', 'better', 'than', 'complex']

Sentiment analysis and tokenization are very common today and these features are already offered by many Python libraries. But one task that is not common in other Python NLP libraries is spelling correction. So let’s see how to correct spellings with Python:

# Spelling Correction
text = TextBlob("I love data scienc")
print(text.correct())
I love data science

Summary

So this is how you can use this library in Python to perform various tasks of natural language processing. You can learn more about this library from here. I hope you liked this article on a tutorial on TextBlob in Python. Feel free to ask your valuable questions in the comments section below.

Textblob documentation

Another great article and with more info from analyticsvidhya

--

--

Lerekoqholosha

I am a data scientist with 1 year of experience working with Python.