Having a go at common NLP tasks using TextBlob

Having a go at common NLP tasks using TextBlob

4 min read

What is NLP

NLP is the subset if AI which enables computers to understand, interpret, and manipulate human natural languages.

The history of NLP started in the early 1950s(although work can be found from earlier periods too) when Alan Turing published an article titled "Computer machinery and intelligence" which proposed a criterion of intelligence which is now known as the Turing test.

NLP contains many interesting libraries, the most basic one is NLTK, this library is pretty versatile but its also quite difficult to use. Most of the time, its rather slow and doesnt match the demands of quick-paced production usage. The other famous libraries are:

  • TextBlob
  • CoreNLP
  • polyglot
  • spaCy
  • Gensim

Out of all these libraries i mentioned, TextBlob is my personal favorite. It basically provides beginners with an easy interface to help them learn most basic NLP tasks like sentiment analysis, POS-tagging, or noun phrase extraction.

Let's see some of them here.


1. Correcting the spelling

Often people tend do a lot of typos. In this case the TextBlob library can come pretty handy let's take a look at a program to see how it works :

from textblob import TextBlob
incorrect_line = "Heello my naem is john and i lobe nathural langeguage procesing"
output = TextBlob(incorrect_line).correct()

Output: Hello my name is john and i love natural language processing

In the above program we have first imported the library then wrote a sentence having many incorrect words after which we have just called a correct() function of the library and finally get our output.

2. Extracting the Noun

Whenever we want to do some manipulation of a natural language using a computer we generally have to extract a lot of things from a sentence out of which one important thing is extraction of nouns and TextBlob is a perfect for this task too:

from textblob import TextBlob
nouns = TextBlob("India is a country in the Asia. This is where Apoorv lives")
for noun in nouns.noun_phrases:

Output: india asia apoorv

3. Sentimental Analysis

It is the process of computationally identifying and categorizing opinions expressed in a piece of text, especially in order to determine whether the writer's attitude towards a particular topic, product, etc. is positive, negative, or neutral. This is used by all the companies all over the world to to get the review about their products

from textblob import TextBlob
from textblob.sentiments import NaiveBayesAnalyzer
text = TextBlob("The movie was excellent!", analyzer=NaiveBayesAnalyzer())
text = TextBlob("The movie story pinch me a lot.", analyzer=NaiveBayesAnalyzer())

Output: Sentiment(classification='pos', p_pos=0.7318278242290406, p_neg=0.26817217577095936) Sentiment(classification='neg', p_pos=0.258107501384339, p_neg=0.741892498764251609)


4. Antonyms of a word

Whenever we want to know the Antonym of a word we either look up to that word in a dictionary(old school) or give the word a search on the internet but have you ever wonder how the online search is able to answer you queries, yes they also use the NLP for this and TextBlob again can help us do the same:

from textblob import Word
text_word = Word('danger')

if lemma.antonyms():


5. Synonyms of a word


Just like we can find the antonym we can also find the synonym but let me tell you one more interesting aspect of this library which is, it can also define a word for you just by typing one extra statement

text_word = Word(input_word)


synonyms = set()
for synset in text_word.synsets:
    for lemma in synset.lemmas():

Output: ['the condition of being susceptible to harm or injury', 'a venture undertaken without regard to possible loss or injury', 'a cause of pain or injury or loss', 'a dangerous place'] {'peril', 'risk'}

6. Language detection and translation

This is by far the best part of this library and i have made my own language translator and detector application with the help of the textblob library.

blob = TextBlob("My name is Apoorv Tyagi")



To which we get output as: en
鄐桌鄐啤冗 鄐兒冗鄐 鄐鄐芹鄐啤鄐 鄐戈鄐能冗鄐鄍 鄐嫩
Je m'appelle Apoorv Tyagi

So that's it for now, i hope that you now have the idea of how powerful NLP libraries are. Hopefully you learned something new today! Happy Coding..


Other articles you might like

Did you find this article valuable?

Support Apoorv Tyagi by becoming a sponsor. Any amount is appreciated!