Follow my blog with Bloglovin

Monday, December 16, 2013

Sentiment Analysis and Text Processing with Python : TextBlob

Sentiment analysis is used to find sentiment of author from a text, like tweets, posts or blog. Lets do this in Python:

1. Python is already installed on Linux distributions and can be install on windows using installer http://www.python.org/download/releases/2.7.6/

2. Install easy install and pip package manager:
Download python script from https://bitbucket.org/pypa/setuptools/raw/bootstrap/ez_setup.py and execute using "python <path/to/ez_setup.py>"

Install pip using "sudo easy_install pip"

3. Install TextBlob using "pip install -U textblob"

Python script to do sentiment analysis:


from textblob import TextBlob

class TextTool:



    def analyseText(self, text,  sourceLang='en', translateTo='en', spellCorrection=False):

        blob = TextBlob(text)

        if spellCorrection: #correct spelling

            correctedTxt = blob.correct()

        if translateTo!=None and sourceLang!=translateTo: #translate text

            if sourceLang!=None:

                translatedTxt = blob.translate(sourceLang, translateTo)

            else:

                translatedTxt = blob.translate(to=translateTo)

        #return sentiment score, 0-10 (negative to positive, 5 as neutral)

        return blob.sentiment, correctedTxt.__str__() if spellCorrection else None, translatedTxt.__str__() if translateTo!=None and sourceLang!=translateTo else None



textTool = TextTool()

analysis = textTool.analyseText(“Its great to see this again … wow”)


This Python script,
Corrects spelling or can suggest correction (check other methods for that).
Translates text using Google translate.
Analyzes sentiment of text (authors' feeling).


This function can do sentiment analysis, translation and spelling corrections. For more text processing see TextBlob website.

1 comment:

Popular Posts