Estimated Time

10 mins

Skill Level

Intermediate

Exercises

na

Content Sections

Course Provider

Provided by HolyPython.com

Used Where?

  • Analysis of text, books, messages, posts, poems, media, news, social media…
  • Writing-support tools (auto-correction, auto-completion, spell check)
  • Sentiment analysis 
  • Financial investment decision making (through analysis of market sentiment)
  • Policy making (analysis of public reaction on specific matters)
  • Voice applications
  • Understanding of human emotions by computers
  • Search Engines
  • Business Development and Business Inteligence
  • Video Games and Virtual Reality
  • Increasingly useful and common Voice Assistants (Google Assistant, Cortana, Siri, Alexa, Athena, Mycroft, Aido, Cubic, Hound, Databot, AIVC, Butleroy, Robin, Lyra, Maluuba, Dragon, Lucida, Bixby, Vlingo, Silvia to name a few.)

How to install pip from Anaconda

First it makes sense to have pip installed (if you don’t have it already) before proceeding to add textblob to your Python library.

conda install pip

 

conda install pip (already installed)

To check if you have any of the needed libraries installed (pip, nltk, textblob) you can also try executing this command in Python:

It will list all the Python modules installed then you can scroll and check if you have the ones you need.

 

help("modules")

How to install textblob from Anaconda

 

You can install textblob from Anaconda Command Prompt. It’s simple as typing the command below:

pip install textblob

Once installed you can start importing textblob in Python using your favorite software such as Spyder, Jupyter Notebook, Pycharm, Vim, Atom, Sublime or Python in Command Prompt.

How to install Corpora Data

After you install textblob, finally, you need to make sure you have corpora files for sentiment analysis.

This can be done in numerous ways, here is a convenient one:

GUI method (NLTK Downloader)

You can type the code below in Python and execute it:

You don’t have to install nltk separately because it comes with Anaconda3 installation.

import nltk
nltk.download()

A download window will pop up.

Here you can see all the specialized corpora files that are available for installation.

NLTK Download Module for Corpora Data

Winerror 10061 (Server denies connection)

Before getting too complicated on this issue with sockets and proxies. Let’s understand why it happens and the most likely underlying cause.

CHANGE THIS
That suggests the remote machine has received your connection request, and send back a refusal (a RST packet). I don’t think this is a case of the remote machine simply not having a process listening on that port (but i could be wrong!).

That sounds like a firewall problem. It could be a firewall on the remote machine, or a filter in the network in between, or, perhaps on your local machine – are you running any kind of security software locally?

So, are you using a firewall or a security software? Anything that might obstruct the connection more than usual? For instance if you’re using a VPN connection such as NordVPN or ExpressVPN their ad blocking security system can cause this error and even if you disconnect from the VPN while the app is still running in the background connection error will occur.

The Solution:

Simply troubleshoot any extra security layer you might have in use and temporarily try disabling or terminating it.

Alternative Corpora Installation (Command-line method)

Alternatively you can install specific Corporas by executing either of the commands below:

This method will not solve Server rejection error since it’s a rather global problem regarding your computer environment. Also, NLTK Downloader really offers a nice visual experience. But just in case someone might prefer the command method here they are from the textblob official webpage:

$ python -m textblob.download_corpora

Lite corpora version: (might not cover all the needed files)

$ python -m textblob.download_corpora lite

Introduction to Sentiment Analysis and NLP

Sentiment likely comes from French word sentir which means to feel. Sentiment can be many abstract things that relate to emotions, feelings, thoughts, opinions and senses.

In computer science, sentiment analysis lives in the sweet spot where natural language processing (NLP) is carried out as a means for machines to make sense of human languages which usually involves, partially or fully; emotions, feelings, bias, conclusions, objectivity and opinions.

There are many benefits of bridging communication between humans and computers but sentiment analysis can also shed light on human to human communications as it contributes to processing capability both in terms of power, technique and efficiency.

The field is relatively new and definitely has wind in its sails since the processing capabilities keep increasing and amazing NLP opportunities continue being discovered. One can say it’s only the beginning in sentiment analysis and natural language processing.

So, it’s safe to say as Machine Learning models get more training, data keeps accumulating and more sophisticated AI modeling techniques keep coming up, the difference between computer and human communication is also getting blurry. Benefits of this technology already became enormous and will only get bigger. Let’s hope there won’t be much malintent.

Let’s discover this fantastic technology in Python with a few code examples that can be useful:

TextBlob Basics

Let’s explore some of the basic functions in textblob.

First we need to create a textblob object:

str = '''SHOULD WE COLONISE SPACE?

        In conclusion, I return to Einstein. If we find a planet in the 
        Alpha Centauri system, its image, captured by a camera travelling 
        at a fifth of light speed, will be slightly distorted due to the 
        effects of special relativity. It would be the first time a 
        spacecraft has flown fast enough to see such effects. In fact, 
        Einstein’s theory is central to the whole mission. Without it 
        we would have neither lasers nor the ability to perform the 
        calculations necessary for guidance, imaging and data transmission 
        over twenty-five trillion miles at a fifth of light speed. We can 
        see a pathway between that sixteen-year-old boy dreaming of riding 
        on a light beam and our own dream, which we are planning to turn 
        into a reality, of riding our own light beam to the stars. We are 
        standing at the threshold of a new era. Human colonisation on other 
        planets is no longer science fiction. It can be science fact. The 
        human race has existed as a separate species for about two million 
        years. Civilisation began about 10,000 years ago, and the rate of 
        development has been steadily increasing. If humanity is to continue 
        for another million years, our future lies in boldly going where no one 
        else has gone before. I hope for the best. I have to. We have no other 
        option. The era of civilian space travel is coming. What do you think 
        it means to us? I look forward to space travel. I would be one of the 
        first to buy a ticket. I expect that within the next hundred years we 
        will be able to travel anywhere in the solar system, except maybe the 
        outer planets. But travel to the stars will take a bit longer. I reckon 
        in 500 years, we will have visited some of the nearby stars. It won’t be 
        like Star Trek . We won’t be able to travel at warp speed. So a round trip 
        will take at least ten years and probably much longer.
        
        From the book: Brief Answers to the Big Questions – Stephen Hawking
'''

    
    
blob = textblob.TextBlob(str)

.words (list of words)

.words property will return all the words from the text in a list.

a = blob.words
print(a)
['SHOULD', 'WE', 'COLONISE', 'SPACE', 'In', 'conclusion', 'I', 'return', 'to', 'Einstein', 'If', 'we', 'find', 'a', 'planet', 'in', 'the', 'Alpha', 'Centauri', 'system', 'its', 'image', 'captured', 'by', 'a', 'camera', 'travelling', 'at', 'a', 'fifth', 'of', 'light', 'speed', 'will', 'be', 'slightly', 'distorted', 'due', 'to', 'the', 'effects', 'of', 'special', 'relativity', 'It', 'would', 'be', 'the', 'first', 'time', 'a', 'spacecraft', 'has', 'flown', 'fast', 'enough', 'to', 'see', 'such', 'effects', 'In', 'fact', 'Einstein', '’', 's', 'theory', 'is', 'central', 'to', 'the', 'whole', 'mission', 'Without', 'it', 'we', 'would', 'have', 'neither', 'lasers', 'nor', 'the', 'ability', 'to', 'perform', 'the', 'calculations', 'necessary', 'for', 'guidance', 'imaging', 'and', 'data', 'transmission', 'over', 'twenty-five', 'trillion', 'miles', 'at', 'a', 'fifth', 'of', 'light', 'speed', 'We', 'can', 'see', 'a', 'pathway', 'between', 'that', 'sixteen-year-old', 'boy', 'dreaming', 'of', 'riding', 'on', 'a', 'light', 'beam', 'and', 'our', 'own', 'dream', 'which', 'we', 'are', 'planning', 'to', 'turn', 'into', 'a', 'reality', 'of', 'riding', 'our', 'own', 'light', 'beam', 'to', 'the', 'stars', 'We', 'are', 'standing', 'at', 'the', 'threshold', 'of', 'a', 'new', 'era', 'Human', 'colonisation', 'on', 'other', 'planets', 'is', 'no', 'longer', 'science', 'fiction', 'It', 'can', 'be', 'science', 'fact', 'The', 'human', 'race', 'has', 'existed', 'as', 'a', 'separate', 'species', 'for', 'about', 'two', 'million', 'years', 'Civilisation', 'began', 'about', '10,000', 'years', 'ago', 'and', 'the', 'rate', 'of', 'development', 'has', 'been', 'steadily', 'increasing', 'If', 'humanity', 'is', 'to', 'continue', 'for', 'another', 'million', 'years', 'our', 'future', 'lies', 'in', 'boldly', 'going', 'where', 'no', 'one', 'else', 'has', 'gone', 'before', 'I', 'hope', 'for', 'the', 'best', 'I', 'have', 'to', 'We', 'have', 'no', 'other', 'option', 'The', 'era', 'of', 'civilian', 'space', 'travel', 'is', 'coming', 'What', 'do', 'you', 'think', 'it', 'means', 'to', 'us', 'I', 'look', 'forward', 'to', 'space', 'travel', 'I', 'would', 'be', 'one', 'of', 'the', 'first', 'to', 'buy', 'a', 'ticket', 'I', 'expect', 'that', 'within', 'the', 'next', 'hundred', 'years', 'we', 'will', 'be', 'able', 'to', 'travel', 'anywhere', 'in', 'the', 'solar', 'system', 'except', 'maybe', 'the', 'outer', 'planets', 'But', 'travel', 'to', 'the', 'stars', 'will', 'take', 'a', 'bit', 'longer', 'I', 'reckon', 'in', '500', 'years', 'we', 'will', 'have', 'visited', 'some', 'of', 'the', 'nearby', 'stars', 'It', 'won', '’', 't', 'be', 'like', 'Star', 'Trek', 'We', 'won', '’', 't', 'be', 'able', 'to', 'travel', 'at', 'warp', 'speed', 'So', 'a', 'round', 'trip', 'will', 'take', 'at', 'least', 'ten', 'years', 'and', 'probably', 'much', 'longer', 'From', 'the', 'book', 'Brief', 'Answers', 'to', 'the', 'Big', 'Questions', '–', 'Stephen', 'Hawking']

.sentences (list of sentences)

.sentences property will return all the sentences from the text in a list.

a = blob.sentences
print(a)
       [Sentence("SHOULD WE COLONISE SPACE?"), 
Sentence("In conclusion, I return to Einstein."), Sentence("If we find a planet in the
Alpha Centauri system, its image, captured by a camera travelling
at a fifth of light speed, will be slightly distorted due to the
effects of special relativity."), Sentence("It would be the first time a
spacecraft has flown fast enough to see such effects."), Sentence("In fact,
Einstein’s theory is central to the whole mission."), Sentence("Without it
we would have neither lasers nor the ability to perform the
calculations necessary for guidance, imaging and data transmission
over twenty-five trillion miles at a fifth of light speed."), Sentence("We can
see a pathway between that sixteen-year-old boy dreaming of riding
on a light beam and our own dream, which we are planning to turn
into a reality, of riding our own light beam to the stars."), Sentence("We are
standing at the threshold of a new era."), Sentence("Human colonisation on other
planets is no longer science fiction."), Sentence("It can be science fact."), Sentence("The
human race has existed as a separate species for about two million
years."), Sentence("Civilisation began about 10,000 years ago, and the rate of
development has been steadily increasing."), Sentence("If humanity is to continue
for another million years, our future lies in boldly going where no one
else has gone before."), Sentence("I hope for the best."), Sentence("I have to."), Sentence("We have no other
option."), Sentence("The era of civilian space travel is coming."), Sentence("What do you think
it means to us?"), Sentence("I look forward to space travel."), Sentence("I would be one of the
first to buy a ticket."), Sentence("I expect that within the next hundred years we
will be able to travel anywhere in the solar system, except maybe the
outer planets."), Sentence("But travel to the stars will take a bit longer."), Sentence("I reckon
in 500 years, we will have visited some of the nearby stars."), Sentence("It won’t be
like Star Trek ."), Sentence("We won’t be able to travel at warp speed."), Sentence("So a round trip
will take at least ten years and probably much longer."),
Sentence("From the book: Brief Answers to the Big Questions – Stephen Hawking")]

.noun_phrases (list of phrases)

.noun_phrases property will return all the noun phrases from the text in a list.

a = blob.noun_phrases
print(a)
['should we colonise space', 'einstein', 'alpha centauri', 'light speed', 'special relativity', 'such effects', 'einstein', '’ s theory', 'whole mission', 'data transmission', 'twenty-five trillion miles', 'light speed', 'sixteen-year-old boy', 'light beam', 'own dream', 'own light beam', 'new era', 'human colonisation', 'science fiction', 'science fact', 'human race', 'separate species', 'civilisation', 'civilian space travel', 'space travel', 'solar system', 'outer planets', 'nearby stars', '’ t', 'trek', '’ t', 'warp speed', 'round trip', 'brief answers', 'stephen hawking']

.ngrams(n=4) (n consecutive words)

.ngrams property will return successive words in a tuple. You can adjust the succession amount by assigning a different value to n parameter.

a = blob.ngrams(n=4)
print(a)
[WordList(['SHOULD', 'WE', 'COLONISE']), WordList(['WE', 'COLONISE', 'SPACE']), WordList(['COLONISE', 'SPACE', 'In']), WordList(['SPACE', 'In', 'conclusion']), WordList(['In', 'conclusion', 'I']), WordList(['conclusion', 'I', 'return']), WordList(['I', 'return', 'to']), WordList(['return', 'to', 'Einstein']), WordList(['to', 'Einstein', 'If']), WordList(['Einstein', 'If', 'we']), WordList(['If', 'we', 'find']), WordList(['we', 'find', 'a']), WordList(['find', 'a', 'planet']), WordList(['a', 'planet', 'in']), WordList(['planet', 'in', 'the']), WordList(['in', 'the', 'Alpha']), WordList(['the', 'Alpha', 'Centauri']), WordList(['Alpha', 'Centauri', 'system']), WordList(['Centauri', 'system', 'its']), WordList(['system', 'its', 'image']), WordList(['its', 'image', 'captured']), WordList(['image', 'captured', 'by']), WordList(['captured', 'by', 'a']), WordList(['by', 'a', 'camera']), WordList(['a', 'camera', 'travelling']), WordList(['camera', 'travelling', 'at']), WordList(['travelling', 'at', 'a']), WordList(['at', 'a', 'fifth']), WordList(['a', 'fifth', 'of']), WordList(['fifth', 'of', 'light']), WordList(['of', 'light', 'speed']), WordList(['light', 'speed', 'will']), WordList(['speed', 'will', 'be']), WordList(['will', 'be', 'slightly']), WordList(['be', 'slightly', 'distorted']), WordList(['slightly', 'distorted', 'due']), WordList(['distorted', 'due', 'to']), WordList(['due', 'to', 'the']), WordList(['to', 'the', 'effects']), WordList(['the', 'effects', 'of']), WordList(['effects', 'of', 'special']), WordList(['of', 'special', 'relativity']), WordList(['special', 'relativity', 'It']), WordList(['relativity', 'It', 'would']), WordList(['It', 'would', 'be']), WordList(['would', 'be', 'the']), WordList(['be', 'the', 'first']), WordList(['the', 'first', 'time']), WordList(['first', 'time', 'a']), WordList(['time', 'a', 'spacecraft']), WordList(['a', 'spacecraft', 'has']), WordList(['spacecraft', 'has', 'flown']), WordList(['has', 'flown', 'fast']), WordList(['flown', 'fast', 'enough']), WordList(['fast', 'enough', 'to']), WordList(['enough', 'to', 'see']), WordList(['to', 'see', 'such']), WordList(['see', 'such', 'effects']), WordList(['such', 'effects', 'In']), WordList(['effects', 'In', 'fact']), WordList(['In', 'fact', 'Einstein']), WordList(['fact', 'Einstein', '’']), WordList(['Einstein', '’', 's']), WordList(['’', 's', 'theory']), WordList(['s', 'theory', 'is']), WordList(['theory', 'is', 'central']), WordList(['is', 'central', 'to']), WordList(['central', 'to', 'the']), WordList(['to', 'the', 'whole']), WordList(['the', 'whole', 'mission']), WordList(['whole', 'mission', 'Without']), WordList(['mission', 'Without', 'it']), WordList(['Without', 'it', 'we']), WordList(['it', 'we', 'would']), WordList(['we', 'would', 'have']), WordList(['would', 'have', 'neither']), WordList(['have', 'neither', 'lasers']), WordList(['neither', 'lasers', 'nor']), WordList(['lasers', 'nor', 'the']), WordList(['nor', 'the', 'ability']), WordList(['the', 'ability', 'to']), WordList(['ability', 'to', 'perform']), WordList(['to', 'perform', 'the']), WordList(['perform', 'the', 'calculations']), WordList(['the', 'calculations', 'necessary']), WordList(['calculations', 'necessary', 'for']), WordList(['necessary', 'for', 'guidance']), WordList(['for', 'guidance', 'imaging']), WordList(['guidance', 'imaging', 'and']), WordList(['imaging', 'and', 'data']), WordList(['and', 'data', 'transmission']), WordList(['data', 'transmission', 'over']), WordList(['transmission', 'over', 'twenty-five']), WordList(['over', 'twenty-five', 'trillion']), WordList(['twenty-five', 'trillion', 'miles']), WordList(['trillion', 'miles', 'at']), WordList(['miles', 'at', 'a']), WordList(['at', 'a', 'fifth']), WordList(['a', 'fifth', 'of']), WordList(['fifth', 'of', 'light']), WordList(['of', 'light', 'speed']), WordList(['light', 'speed', 'We']), WordList(['speed', 'We', 'can']), WordList(['We', 'can', 'see']), WordList(['can', 'see', 'a']), WordList(['see', 'a', 'pathway']), WordList(['a', 'pathway', 'between']), WordList(['pathway', 'between', 'that']), WordList(['between', 'that', 'sixteen-year-old']), WordList(['that', 'sixteen-year-old', 'boy']), WordList(['sixteen-year-old', 'boy', 'dreaming']), WordList(['boy', 'dreaming', 'of']), WordList(['dreaming', 'of', 'riding']), WordList(['of', 'riding', 'on']), WordList(['riding', 'on', 'a']), WordList(['on', 'a', 'light']), WordList(['a', 'light', 'beam']), WordList(['light', 'beam', 'and']), WordList(['beam', 'and', 'our']), WordList(['and', 'our', 'own']), WordList(['our', 'own', 'dream']), WordList(['own', 'dream', 'which']), WordList(['dream', 'which', 'we']), WordList(['which', 'we', 'are']), WordList(['we', 'are', 'planning']), WordList(['are', 'planning', 'to']), WordList(['planning', 'to', 'turn']), WordList(['to', 'turn', 'into']), WordList(['turn', 'into', 'a']), WordList(['into', 'a', 'reality']), WordList(['a', 'reality', 'of']), WordList(['reality', 'of', 'riding']), WordList(['of', 'riding', 'our']), WordList(['riding', 'our', 'own']), WordList(['our', 'own', 'light']), WordList(['own', 'light', 'beam']), WordList(['light', 'beam', 'to']), WordList(['beam', 'to', 'the']), WordList(['to', 'the', 'stars']), WordList(['the', 'stars', 'We']), WordList(['stars', 'We', 'are']), WordList(['We', 'are', 'standing']), WordList(['are', 'standing', 'at']), WordList(['standing', 'at', 'the']), WordList(['at', 'the', 'threshold']), WordList(['the', 'threshold', 'of']), WordList(['threshold', 'of', 'a']), WordList(['of', 'a', 'new']), WordList(['a', 'new', 'era']), WordList(['new', 'era', 'Human']), WordList(['era', 'Human', 'colonisation']), WordList(['Human', 'colonisation', 'on']), WordList(['colonisation', 'on', 'other']), WordList(['on', 'other', 'planets']), WordList(['other', 'planets', 'is']), WordList(['planets', 'is', 'no']), WordList(['is', 'no', 'longer']), WordList(['no', 'longer', 'science']), WordList(['longer', 'science', 'fiction']), WordList(['science', 'fiction', 'It']), WordList(['fiction', 'It', 'can']), WordList(['It', 'can', 'be']), WordList(['can', 'be', 'science']), WordList(['be', 'science', 'fact']), WordList(['science', 'fact', 'The']), WordList(['fact', 'The', 'human']), WordList(['The', 'human', 'race']), WordList(['human', 'race', 'has']), WordList(['race', 'has', 'existed']), WordList(['has', 'existed', 'as']), WordList(['existed', 'as', 'a']), WordList(['as', 'a', 'separate']), WordList(['a', 'separate', 'species']), WordList(['separate', 'species', 'for']), WordList(['species', 'for', 'about']), WordList(['for', 'about', 'two']), WordList(['about', 'two', 'million']), WordList(['two', 'million', 'years']), WordList(['million', 'years', 'Civilisation']), WordList(['years', 'Civilisation', 'began']), WordList(['Civilisation', 'began', 'about']), WordList(['began', 'about', '10,000']), WordList(['about', '10,000', 'years']), WordList(['10,000', 'years', 'ago']), WordList(['years', 'ago', 'and']), WordList(['ago', 'and', 'the']), WordList(['and', 'the', 'rate']), WordList(['the', 'rate', 'of']), WordList(['rate', 'of', 'development']), WordList(['of', 'development', 'has']), WordList(['development', 'has', 'been']), WordList(['has', 'been', 'steadily']), WordList(['been', 'steadily', 'increasing']), WordList(['steadily', 'increasing', 'If']), WordList(['increasing', 'If', 'humanity']), WordList(['If', 'humanity', 'is']), WordList(['humanity', 'is', 'to']), WordList(['is', 'to', 'continue']), WordList(['to', 'continue', 'for']), WordList(['continue', 'for', 'another']), WordList(['for', 'another', 'million']), WordList(['another', 'million', 'years']), WordList(['million', 'years', 'our']), WordList(['years', 'our', 'future']), WordList(['our', 'future', 'lies']), WordList(['future', 'lies', 'in']), WordList(['lies', 'in', 'boldly']), WordList(['in', 'boldly', 'going']), WordList(['boldly', 'going', 'where']), WordList(['going', 'where', 'no']), WordList(['where', 'no', 'one']), WordList(['no', 'one', 'else']), WordList(['one', 'else', 'has']), WordList(['else', 'has', 'gone']), WordList(['has', 'gone', 'before']), WordList(['gone', 'before', 'I']), WordList(['before', 'I', 'hope']), WordList(['I', 'hope', 'for']), WordList(['hope', 'for', 'the']), WordList(['for', 'the', 'best']), WordList(['the', 'best', 'I']), WordList(['best', 'I', 'have']), WordList(['I', 'have', 'to']), WordList(['have', 'to', 'We']), WordList(['to', 'We', 'have']), WordList(['We', 'have', 'no']), WordList(['have', 'no', 'other']), WordList(['no', 'other', 'option']), WordList(['other', 'option', 'The']), WordList(['option', 'The', 'era']), WordList(['The', 'era', 'of']), WordList(['era', 'of', 'civilian']), WordList(['of', 'civilian', 'space']), WordList(['civilian', 'space', 'travel']), WordList(['space', 'travel', 'is']), WordList(['travel', 'is', 'coming']), WordList(['is', 'coming', 'What']), WordList(['coming', 'What', 'do']), WordList(['What', 'do', 'you']), WordList(['do', 'you', 'think']), WordList(['you', 'think', 'it']), WordList(['think', 'it', 'means']), WordList(['it', 'means', 'to']), WordList(['means', 'to', 'us']), WordList(['to', 'us', 'I']), WordList(['us', 'I', 'look']), WordList(['I', 'look', 'forward']), WordList(['look', 'forward', 'to']), WordList(['forward', 'to', 'space']), WordList(['to', 'space', 'travel']), WordList(['space', 'travel', 'I']), WordList(['travel', 'I', 'would']), WordList(['I', 'would', 'be']), WordList(['would', 'be', 'one']), WordList(['be', 'one', 'of']), WordList(['one', 'of', 'the']), WordList(['of', 'the', 'first']), WordList(['the', 'first', 'to']), WordList(['first', 'to', 'buy']), WordList(['to', 'buy', 'a']), WordList(['buy', 'a', 'ticket']), WordList(['a', 'ticket', 'I']), WordList(['ticket', 'I', 'expect']), WordList(['I', 'expect', 'that']), WordList(['expect', 'that', 'within']), WordList(['that', 'within', 'the']), WordList(['within', 'the', 'next']), WordList(['the', 'next', 'hundred']), WordList(['next', 'hundred', 'years']), WordList(['hundred', 'years', 'we']), WordList(['years', 'we', 'will']), WordList(['we', 'will', 'be']), WordList(['will', 'be', 'able']), WordList(['be', 'able', 'to']), WordList(['able', 'to', 'travel']), WordList(['to', 'travel', 'anywhere']), WordList(['travel', 'anywhere', 'in']), WordList(['anywhere', 'in', 'the']), WordList(['in', 'the', 'solar']), WordList(['the', 'solar', 'system']), WordList(['solar', 'system', 'except']), WordList(['system', 'except', 'maybe']), WordList(['except', 'maybe', 'the']), WordList(['maybe', 'the', 'outer']), WordList(['the', 'outer', 'planets']), WordList(['outer', 'planets', 'But']), WordList(['planets', 'But', 'travel']), WordList(['But', 'travel', 'to']), WordList(['travel', 'to', 'the']), WordList(['to', 'the', 'stars']), WordList(['the', 'stars', 'will']), WordList(['stars', 'will', 'take']), WordList(['will', 'take', 'a']), WordList(['take', 'a', 'bit']), WordList(['a', 'bit', 'longer']), WordList(['bit', 'longer', 'I']), WordList(['longer', 'I', 'reckon']), WordList(['I', 'reckon', 'in']), WordList(['reckon', 'in', '500']), WordList(['in', '500', 'years']), WordList(['500', 'years', 'we']), WordList(['years', 'we', 'will']), WordList(['we', 'will', 'have']), WordList(['will', 'have', 'visited']), WordList(['have', 'visited', 'some']), WordList(['visited', 'some', 'of']), WordList(['some', 'of', 'the']), WordList(['of', 'the', 'nearby']), WordList(['the', 'nearby', 'stars']), WordList(['nearby', 'stars', 'It']), WordList(['stars', 'It', 'won']), WordList(['It', 'won', '’']), WordList(['won', '’', 't']), WordList(['’', 't', 'be']), WordList(['t', 'be', 'like']), WordList(['be', 'like', 'Star']), WordList(['like', 'Star', 'Trek']), WordList(['Star', 'Trek', 'We']), WordList(['Trek', 'We', 'won']), WordList(['We', 'won', '’']), WordList(['won', '’', 't']), WordList(['’', 't', 'be']), WordList(['t', 'be', 'able']), WordList(['be', 'able', 'to']), WordList(['able', 'to', 'travel']), WordList(['to', 'travel', 'at']), WordList(['travel', 'at', 'warp']), WordList(['at', 'warp', 'speed']), WordList(['warp', 'speed', 'So']), WordList(['speed', 'So', 'a']), WordList(['So', 'a', 'round']), WordList(['a', 'round', 'trip']), WordList(['round', 'trip', 'will']), WordList(['trip', 'will', 'take']), WordList(['will', 'take', 'at']), WordList(['take', 'at', 'least']), WordList(['at', 'least', 'ten']), WordList(['least', 'ten', 'years']), WordList(['ten', 'years', 'and']), WordList(['years', 'and', 'probably']), WordList(['and', 'probably', 'much']), WordList(['probably', 'much', 'longer']), WordList(['much', 'longer', 'From']), WordList(['longer', 'From', 'the']), WordList(['From', 'the', 'book']), WordList(['the', 'book', 'Brief']), WordList(['book', 'Brief', 'Answers']), WordList(['Brief', 'Answers', 'to']), WordList(['Answers', 'to', 'the']), WordList(['to', 'the', 'Big']), WordList(['the', 'Big', 'Questions']), WordList(['Big', 'Questions', '–']), WordList(['Questions', '–', 'Stephen']), WordList(['–', 'Stephen', 'Hawking'])]

.tags (Part-of-speech tags)

.tags property is very useful and it will provide part-of-speech properties.

You can see a full list of part-of-speech tags at this page from University of Pennsylvania.

a = blob.tags
print(a)
[('SHOULD', 'MD'), ('WE', 'NNP'), ('COLONISE', 'NNP'), ('SPACE', 'NNP'), ('In', 'IN'), ('conclusion', 'NN'), ('I', 'PRP'), ('return', 'VBP'), ('to', 'TO'), ('Einstein', 'NNP'), ('If', 'IN'), ('we', 'PRP'), ('find', 'VBP'), ('a', 'DT'), ('planet', 'NN'), ('in', 'IN'), ('the', 'DT'), ('Alpha', 'NNP'), ('Centauri', 'NNP'), ('system', 'NN'), ('its', 'PRP$'), ('image', 'NN'), ('captured', 'VBN'), ('by', 'IN'), ('a', 'DT'), ('camera', 'NN'), ('travelling', 'NN'), ('at', 'IN'), ('a', 'DT'), ('fifth', 'NN'), ('of', 'IN'), ('light', 'JJ'), ('speed', 'NN'), ('will', 'MD'), ('be', 'VB'), ('slightly', 'RB'), ('distorted', 'VBN'), ('due', 'JJ'), ('to', 'TO'), ('the', 'DT'), ('effects', 'NNS'), ('of', 'IN'), ('special', 'JJ'), ('relativity', 'NN'), ('It', 'PRP'), ('would', 'MD'), ('be', 'VB'), ('the', 'DT'), ('first', 'JJ'), ('time', 'NN'), ('a', 'DT'), ('spacecraft', 'NN'), ('has', 'VBZ'), ('flown', 'VBN'), ('fast', 'RB'), ('enough', 'JJ'), ('to', 'TO'), ('see', 'VB'), ('such', 'JJ'), ('effects', 'NNS'), ('In', 'IN'), ('fact', 'NN'), ('Einstein', 'NNP'), ('’', 'NNP'), ('s', 'NN'), ('theory', 'NN'), ('is', 'VBZ'), ('central', 'JJ'), ('to', 'TO'), ('the', 'DT'), ('whole', 'JJ'), ('mission', 'NN'), ('Without', 'IN'), ('it', 'PRP'), ('we', 'PRP'), ('would', 'MD'), ('have', 'VB'), ('neither', 'CC'), ('lasers', 'NNS'), ('nor', 'CC'), ('the', 'DT'), ('ability', 'NN'), ('to', 'TO'), ('perform', 'VB'), ('the', 'DT'), ('calculations', 'NNS'), ('necessary', 'JJ'), ('for', 'IN'), ('guidance', 'NN'), ('imaging', 'VBG'), ('and', 'CC'), ('data', 'NNS'), ('transmission', 'NN'), ('over', 'IN'), ('twenty-five', 'JJ'), ('trillion', 'CD'), ('miles', 'NNS'), ('at', 'IN'), ('a', 'DT'), ('fifth', 'NN'), ('of', 'IN'), ('light', 'JJ'), ('speed', 'NN'), ('We', 'PRP'), ('can', 'MD'), ('see', 'VB'), ('a', 'DT'), ('pathway', 'NN'), ('between', 'IN'), ('that', 'DT'), ('sixteen-year-old', 'JJ'), ('boy', 'NN'), ('dreaming', 'NN'), ('of', 'IN'), ('riding', 'VBG'), ('on', 'IN'), ('a', 'DT'), ('light', 'JJ'), ('beam', 'NN'), ('and', 'CC'), ('our', 'PRP$'), ('own', 'JJ'), ('dream', 'NN'), ('which', 'WDT'), ('we', 'PRP'), ('are', 'VBP'), ('planning', 'VBG'), ('to', 'TO'), ('turn', 'VB'), ('into', 'IN'), ('a', 'DT'), ('reality', 'NN'), ('of', 'IN'), ('riding', 'VBG'), ('our', 'PRP$'), ('own', 'JJ'), ('light', 'NN'), ('beam', 'NN'), ('to', 'TO'), ('the', 'DT'), ('stars', 'NNS'), ('We', 'PRP'), ('are', 'VBP'), ('standing', 'VBG'), ('at', 'IN'), ('the', 'DT'), ('threshold', 'NN'), ('of', 'IN'), ('a', 'DT'), ('new', 'JJ'), ('era', 'NN'), ('Human', 'NNP'), ('colonisation', 'NN'), ('on', 'IN'), ('other', 'JJ'), ('planets', 'NNS'), ('is', 'VBZ'), ('no', 'DT'), ('longer', 'JJR'), ('science', 'NN'), ('fiction', 'NN'), ('It', 'PRP'), ('can', 'MD'), ('be', 'VB'), ('science', 'JJ'), ('fact', 'NN'), ('The', 'DT'), ('human', 'JJ'), ('race', 'NN'), ('has', 'VBZ'), ('existed', 'VBN'), ('as', 'IN'), ('a', 'DT'), ('separate', 'JJ'), ('species', 'NNS'), ('for', 'IN'), ('about', 'IN'), ('two', 'CD'), ('million', 'CD'), ('years', 'NNS'), ('Civilisation', 'NN'), ('began', 'VBD'), ('about', 'IN'), ('10,000', 'CD'), ('years', 'NNS'), ('ago', 'RB'), ('and', 'CC'), ('the', 'DT'), ('rate', 'NN'), ('of', 'IN'), ('development', 'NN'), ('has', 'VBZ'), ('been', 'VBN'), ('steadily', 'RB'), ('increasing', 'VBG'), ('If', 'IN'), ('humanity', 'NN'), ('is', 'VBZ'), ('to', 'TO'), ('continue', 'VB'), ('for', 'IN'), ('another', 'DT'), ('million', 'CD'), ('years', 'NNS'), ('our', 'PRP$'), ('future', 'NN'), ('lies', 'VBZ'), ('in', 'IN'), ('boldly', 'RB'), ('going', 'VBG'), ('where', 'WRB'), ('no', 'DT'), ('one', 'NN'), ('else', 'RB'), ('has', 'VBZ'), ('gone', 'VBN'), ('before', 'RB'), ('I', 'PRP'), ('hope', 'VBP'), ('for', 'IN'), ('the', 'DT'), ('best', 'JJS'), ('I', 'PRP'), ('have', 'VBP'), ('to', 'TO'), ('We', 'PRP'), ('have', 'VBP'), ('no', 'DT'), ('other', 'JJ'), ('option', 'NN'), ('The', 'DT'), ('era', 'NN'), ('of', 'IN'), ('civilian', 'JJ'), ('space', 'NN'), ('travel', 'NN'), ('is', 'VBZ'), ('coming', 'VBG'), ('What', 'WP'), ('do', 'VBP'), ('you', 'PRP'), ('think', 'VB'), ('it', 'PRP'), ('means', 'VBZ'), ('to', 'TO'), ('us', 'PRP'), ('I', 'PRP'), ('look', 'VBP'), ('forward', 'RB'), ('to', 'TO'), ('space', 'NN'), ('travel', 'NN'), ('I', 'PRP'), ('would', 'MD'), ('be', 'VB'), ('one', 'CD'), ('of', 'IN'), ('the', 'DT'), ('first', 'JJ'), ('to', 'TO'), ('buy', 'VB'), ('a', 'DT'), ('ticket', 'NN'), ('I', 'PRP'), ('expect', 'VBP'), ('that', 'IN'), ('within', 'IN'), ('the', 'DT'), ('next', 'JJ'), ('hundred', 'CD'), ('years', 'NNS'), ('we', 'PRP'), ('will', 'MD'), ('be', 'VB'), ('able', 'JJ'), ('to', 'TO'), ('travel', 'VB'), ('anywhere', 'RB'), ('in', 'IN'), ('the', 'DT'), ('solar', 'JJ'), ('system', 'NN'), ('except', 'IN'), ('maybe', 'RB'), ('the', 'DT'), ('outer', 'NN'), ('planets', 'NNS'), ('But', 'CC'), ('travel', 'NN'), ('to', 'TO'), ('the', 'DT'), ('stars', 'NNS'), ('will', 'MD'), ('take', 'VB'), ('a', 'DT'), ('bit', 'NN'), ('longer', 'RBR'), ('I', 'PRP'), ('reckon', 'VBP'), ('in', 'IN'), ('500', 'CD'), ('years', 'NNS'), ('we', 'PRP'), ('will', 'MD'), ('have', 'VB'), ('visited', 'VBN'), ('some', 'DT'), ('of', 'IN'), ('the', 'DT'), ('nearby', 'JJ'), ('stars', 'NNS'), ('It', 'PRP'), ('won', 'VBD'), ('’', 'NNP'), ('t', 'RB'), ('be', 'VB'), ('like', 'IN'), ('Star', 'NNP'), ('Trek', 'NNP'), ('We', 'PRP'), ('won', 'VBD'), ('’', 'NNP'), ('t', 'NN'), ('be', 'VB'), ('able', 'JJ'), ('to', 'TO'), ('travel', 'VB'), ('at', 'IN'), ('warp', 'JJ'), ('speed', 'NN'), ('So', 'RB'), ('a', 'DT'), ('round', 'NN'), ('trip', 'NN'), ('will', 'MD'), ('take', 'VB'), ('at', 'IN'), ('least', 'JJS'), ('ten', 'CD'), ('years', 'NNS'), ('and', 'CC'), ('probably', 'RB'), ('much', 'RB'), ('longer', 'RBR'), ('From', 'IN'), ('the', 'DT'), ('book', 'NN'), ('Brief', 'JJ'), ('Answers', 'NNS'), ('to', 'TO'), ('the', 'DT'), ('Big', 'NNP'), ('Questions', 'NNP'), ('–', 'NNP'), ('Stephen', 'NNP'), ('Hawking', 'NNP')]

.definitions (list of definitions)

.definitions property can be applied to words and it will return the definition of the word. But, first you’ll need to import the Word module from textblob.

from textblob import Word
a = Word("prolific")
print(a.definitions)
['intellectually productive', 'bearing in abundance especially offspring']

.spellcheck (Spell check with confidence level)

.spellcheck will return the correct spelling of a word with confidence value.

from textblob import Word
a = Word("profitible")
print(a.spellcheck)
[('profitable', 1.0)]

.correct (Autocorrection)

.correct will check a word and correct it if it’s written wrong with the highest confidence word option.

from textblob import Word
a = Word("profitible")
print(a.correct)
profitable

.word_count(["word"]) (frequency of a word)

.word_count() will return the frequency of a word.

print(blob.word_counts["space"])
3

Polarity & Subjectivity

Sentiment analysis can be carried out with these properties of textblob:

.sentiment (sentiment analysis)

.sentiment will return 2 values in a tuple:

Polarity: Takes a value between -1 and +1. -1 suggests a very negative language and +1 suggests a very positive language. This part of the analysis is the heart of sentiment analysis and can be supported, advanced or elaborated further.

Subjectivity: Takes a value between 0 and +1. 0 suggests maximum objectivity possible and +1 suggests a very subjective text.

a = blob.sentiment
    
print(a)
Sentiment(polarity=0.1725525664811379, subjectivity=0.44312306740878166)

.sentiment.subjectivity (only subjectivity)

a = blob.sentiment.subjectivity
    
print(a)
0.44312306740878166

.sentiment.polarity (only polarity)

a = blob.sentiment.polarity
    
print(a)
0.1725525664811379

Recommended Posts