TextBlob 101: A sneak peek into Python’s NLP library
TexBlob is a textual data processing library that allows you to carry out some NLP tasks such a speech tagging, sentiment analysis, etc.
I have used it for less than a week and it’s pretty simple to use.
To use TextBlob, you first have to install it using pip
$ pip install -U textblob
$ python -m textblob.download_corpora
Now that we have it installed, let’s have a quick look at all the basic stuff it can do.
Create a file and call it: textblob_test.py
The first thing will, of course, be to import TextBlob
from textblob import TextBlob
Then, let’s define a bunch of text. I’ll go on and write some gibberish
text = ‘’’
From the very well acted ‘A series of Unfortunate Events’.
Life is a conundrum of esoterica. What exactly does that mean?
Count Olaf is a weird character but so is Montgomery Montgomery.
Then I am sitted here waiting for ‘Game of Thrones’.
But I am also watching ‘American Gods’. I also haven’t completed
watching ‘The Sopranos’ and am in season two of ‘Penny Dreadful’.
All these and most of my days are spent working.
I’ll be lucky if I finish them all in 2019.
‘’’
We will be performing NLP tasks on the text above now.
The first thing you can do is to get tags.
blob = TextBlob(text)
print (blob.tags)
What the above code does is it goes through our defined text and returns tags for all the words. As you can see below, words have been tagged as either noun, verbs, conjunctions, etc.
Another cool thing we can do is to get phrases.
print (blob.noun_phrases)
We can also extract sentences from the text
print (blob.sentences)
Just like in arrays, we can use indexes to fetch specific sentences
print (blob.sentences[0])
We can even break the above sentence into words
print (blob.sentences[0])for words in blob.sentences[0].words:
print (words)
We can also break the text into ngrams
print (blob.ngrams)
We can also get the overall sentiments of the text
print (blob.sentiment)
We can also choose to be specific and request either only polarity or subjectivity
print (TextBlob(text).sentiment.polarity)
Additional Resources:
Sentiment Analysis is a broad subject that can be handled in various different approaches. To get a more detailed insight into Sentiment Analysis, refer to this greatly articulated article here.
Enjoyed the read? To find more cool tech stuff, follow MindNinja on Medium.