Using Python for Sentiment Analysis in 14 lines of code
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”.
pip install TextBlob
Now, we can start writing our code, just copy and paste the below code into a file called sentiment.py
from textblob import TextBlob text = "Python is a very good language to learn"obj = TextBlob(text)sentiment = obj.sentiment.polarityprint(sentiment)
GitHub is where people build software. More than 28 million people use GitHub to discover, fork, and contribute to over…
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:
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 !
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.
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:
Video Tutorials on Recurrence Relation:
Video Tutorial on Algorithm Analysis: