Using Python for Sentiment Analysis in 14 lines of code

Image for post
Image for post

Sentiment analysis is the measurement of neutral, negative and positive language. It is a way to evaluate spoken or written language to determine if the expression is favorable (positive), unfavorable (negative), or neutral, and to what degree.- Clarabridge

There are many methods and algorithms to implement sentiment analysis systems, which can be classified as:

  • Rule-based systems that perform sentiment analysis based on a set of manually crafted rules.
  • Automatic systems that rely on machine learning techniques to learn from data.
  • Hybrid systems that combine both rule based and automatic approaches.

We will use Python (a general purpose programming language that has seen a rise in data science and data analytics) to help us perform sentiment analysis on a particular subject or topic through text.

First thing you will want to do is download the Python programming language.

Next you will want to install the dependency “TextBlob”. You can do that by running the command “pip install TextBlob”.

Now, we can start writing our code, just copy and paste the below code into a file called sentiment.py

Open up the command prompt and run the following command, make sure your file name is sentiment.py if it is another name replace sentiment.py with that name:

Result:

The results will return a number between -1.0 and 1.0, if the result is a negative number then the sentiment towards the topic was unfavorable, if the result is 0 then the sentiment towards the topic was neutral and if the result was positive then the sentiment towards the topic was favorable. The result shown here was 0.90999999999999, this indicates that the sentiment towards that text was highly positive seeing as not only is the number positive but it’s also very close to 1.0 !

Image for post

There is a great book on machine learning and sentiment analysis with Python called “Introduction to Machine Learning with Python” if you really want to dig deeper into this subject.

Image for post
Image for post
Introduction to Machine Learning with Python

Thanks for reading this article I hope its helpful to you all ! Keep up the learning, and if you would like more computer science, programming and algorithm analysis videos please visit and subscribe to my YouTube channels (randerson112358 & compsci112358 )

Check Out the following for content / videos on Computer Science, Algorithm Analysis, Programming and Logic:

YouTube Channel:
randerson112358: https://www.youtube.com/channel/UCaV_0qp2NZd319K4_K8Z5SQ

compsci112358:
https://www.youtube.com/channel/UCbmb5IoBtHZTpYZCDBOC1CA

Website:
http://everythingcomputerscience.com/

Video Tutorials on Recurrence Relation:
https://www.udemy.com/recurrence-relation-made-easy/

Video Tutorial on Algorithm Analysis:
https://www.udemy.com/algorithm-analysis/

Twitter:
https://twitter.com/CsEverything

YouTube Channel:

Image for post
Image for post

Computer Science Website:

Image for post
Image for post

Udemy Videos on Algortithm Analysis:

Image for post
Image for post

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store