Having a go at common NLP tasks using TextBlob

Senior Software Engineer. I write about practical tips and epic stories about my experiences in the field of computer science and engineering!
Search for a command to run...

Senior Software Engineer. I write about practical tips and epic stories about my experiences in the field of computer science and engineering!
Wonderfully explained, I was thinking that this will not be for someone like me who doesn't have any knowledge of NLP, but I found this very interesting. Definitely, gonna try these examples.
Thanks Yuvraj Singh Jadon
This was my only motive - to make readers introduce to the basic tasks of nlp through relatable examples :)
And you should definitely checkout more on this, NLP is one of the most interesting subset of AI
Thanks Javed!! 😊
Stop guessing your maximum-pool-size. Learn how to calculate limits backwards from your database

Learn what Jib is, how it works, and when to use Jib vs Dockerfile. A guide with real-world scenarios

A prod debugging story you’ll probably relate to

The $0 solution to a Distributed Cache Invalidation

Introduction At our current organization, earlier this year as we were looking at the errors at one of our signup API, we observed that nearly 5% of our requests were getting failed, all due to 400 BAD REQUEST errors, and the root cause was traced ba...

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 it’s also quite difficult to use. Most of the time, it’s rather slow and doesn’t match the demands of quick-paced production usage. The other famous libraries are:
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.

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()
print(output)
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.
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:
print(noun)
Output: india asia apoorv
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())
print(text.sentiment)
text = TextBlob("The movie story pinch me a lot.", analyzer=NaiveBayesAnalyzer())
print(text.sentiment)
Output: Sentiment(classification='pos', p_pos=0.7318278242290406, p_neg=0.26817217577095936) Sentiment(classification='neg', p_pos=0.258107501384339, p_neg=0.741892498764251609)

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():
antonyms.add(lemma.antonyms()[0].name())
print(antonyms)
Output:{'safety'}

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
input_word='danger'
text_word = Word(input_word)
print(text_word.definitions)
synonyms = set()
for synset in text_word.synsets:
for lemma in synset.lemmas():
if(lemma.name()!=input_word):
synonyms.add(lemma.name())
print(synonyms)
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'}
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")
print(blob.detect_language())
print(blob.translate(to='hi'))
print(blob.translate(to='fr'))
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..
