Sentiment Analysis Project With Source Code Python

Hello Universe!! I welcome you to our Sentiment Analysis blog which is a part of Python Projects Series.

In this blog, we are going to work on the Sentiment Analysis Project which is an important project in Machine Learning and Natural Language Processing.

Now you will think what this sentiment analysis is? Why it is important? What’s the special thing about it? Why would we invest our precious time in this project?

Don’t worry we will have a clear image of this sentiment analysis once we go through this blog.

What is Sentiment Analysis?

Sentiment Analysis (or Opinion Mining or emotion AI) is a technique of Natural Language Processing (NLP) that is used to find the sentiment of the data that whether the data is positive or negative or neutral.

It is performed mainly on the textual data to determine its positive or negative or neutral sentiment.

In simple words, this program will take an input which could be a sentence written in any language, and then it will say whether the sentence is having a positive sentiment or negative or neutral based on its sentiment scores. Now it seems like a magic, yes this is the magic of programming.

For example: Customer’s feedback

Feedback TextSentiment
“I bought this product and it is excellent with amazing features.”Positive
“Don’t buy this product because it is terrible and expensive”Negative

It is used by the companies to find out the sentiment of the feedback given by the customers on any product to have a better understanding of the need of the customer and to provide them a better product.

Advantage of Sentiment Analysis:

We can find out lots of benefits of sentiment analysis around us. Some of the advantages of Sentiment Analysis are given below:

  • To track overall customer satisfaction: It is used to track the satisfaction of the customers by analyzing their feedback’s sentiment which helps them to understand how satisfied the customers are after purchasing their product.
  • To find out happiest customers: It can be used to find out the happiest customers after analyzing the feedbacks and it will help them to target their happiest customers to give some discounts, or any benefits or to sell more products to them.
  • To train the chatbots: It is used to train the chatbots which you can find on some websites and these chatbots reply to the users on the basis of their sentiment and this can be very helpful for the website owners.
  • Identifying key emotional triggers: It can be used to identify the emotional triggers so that the chatbots will reply with those words which can directly connect with the users. For example, it can reply “Please wait” so that the user will gather patience while chatting with the bots.

Let’s move to the coding part of this interesting project.

We will use the Python programming language to write this program and textblob library of python for sentiment analysis. This textblob library in python is used for processing textual data.

We have a text file named feedbackdata with .txt extension and we are going to use this file’s data as input in this Sentiment Analysis program.

This feedbackdata.txt file has some random sentences and our Program will determine the sentiment of each sentence of this file.

The below data is the data of the feedbackdata.txt file and the program will find out the sentiment of each sentence given below as positive or neutral or negative.

Sentiment Analysis Project With Source Code Python
cozmocard.com

Download the textblob library if it’s not downloaded there on your machine otherwise you are ready to go.

Let’s import the textblob library in our code editor.

import textblob

Now, open the feedback.txt file and read it line-by-line so that every line will behave like an input to our program and the sentiment analysis program will determine the sentiment of each line one by one.

For reading each sentence one-by-one, we are going to apply a for loop.

This for loop will help the program to read each sentence one by one and then we will print each sentence so that the user will find it easy to understand what was the sentence to be analyzed.

Once the program will start getting the input, use TextBlob (input_sentence) and store it in the object variable obj. Here, input_sentence will be each line of the feedbackdata.txt file, So we will write the code as obj=TextBlob(line).

Now, we will create a variable named sentiment which will store the polarity of the input sentence.

So, here is the code for the same.

Sentiment Analysis Project With Source Code Python
cozmocard.com

Now, the sentiment variable has the polarity value of the sentence. This polarity value lies between [ -1, 1]. The polarity value of -1 (or any value between -1 and 0) shows that the sentiment is negative while 1 (or any value between 0 and 1) shows that it is positive. The value of polarity as 0 shows that the sentence is neutral.

So, we have already written the above conditions in code form. We have used if-elif statements to find the sentiment of the sentences using their polarity values.

If the sentiment will be less than 0, the program will print “The sentence is negative”.

If the sentiment will be equal to 0, the program will print “This sentence is neutral”.

If the sentiment will be greater than 0, the program will print “This sentence is positive”.

So, here is the output of the above program which take input as feedbackdata.txt file’s data.

Output:

output sentiment analysis
cozmocard.com

Conclusion:

Hence, we successfully analyzed the sentiment of the data present in the feedbackdata.txt file. This was really easy to do. This was just like a magic. We wrote few lines of code and that program helped us to find the sentiment of each sentence present in the given file. This was a small file containing 5 sentences but you can write the same code for finding out the sentiment of thousands of sentences. If you liked this blog Sentiment Analysis Project With Source Code Python, please let me know by commenting here. If you have any suggestions to improve this program, let me know. Thanks for reading this blog. See you in the next blog.

This Post Has 4 Comments

Leave a Reply