Sentiment Analysis Project Using TextBlob | Python NLP Project for Beginners

Akansha Yadav builds a sentiment analyzer in Python using TextBlob, which scores text on polarity (-1 to +1) and subjectivity (0 to 1). The project reads 1000+ course reviews from a CSV, classifies each as positive, negative, or neutral, and outputs results to a new file. A command-line interface lets you analyze single texts or entire datasets.
Chapters
This Python library can even predict your breakup. Well, not exactly, but it can read the emotions behind the text. Yes, in today's video we are going to build something that is called sentiment analyzer using Python. I'm talking about the library which is called TextBlob, and TextBlob library can be used for doing sentiment analysis. But before we dive into coding, let's understand what is sentiment analysis. Sentiment analysis is a technique in natural language processing. You might have heard about natural language processing, but this is the complete video where we will talk about it and we create a practical project. This is a technique where we try to determine the emotions behind the text. It can be any text and we are going to determine the emotion. So let's get back on the screen and understand it better. So as I said, this is a part of NLTK. NLTK means Natural Language Processing Toolkit that we are going to use. I'm going to give you some examples of sentiment analysis, how sentiment analysis looks like and how your sentiment analyzer is going to— the very first example is, let's say you are reading some of the reviews of courses or some of the reviews of product. So the courses and the reviews seem something like this. Suppose I am writing the basic one: this course is amazing. So which type of feedback is it? Is it positive or is it negative? So we can simply say that it includes the word amazing, which shows that this is a positive feedback. And this is what we call as a sentiment analysis. It is basically understanding the emotions behind the text. So TextBlob library does the same. Just like we humans can understand the emotion, same way the natural language processing techniques, which like one part of it is a sentiment analysis, can also understand the emotion behind the text. And that is very important to know because in 2025, creating a brilliant AI tool is always on top. So you have to learn about the sentiment analysis. So when I talk about the TextBlob, so TextBlob is a Python library which you can simply install with pip install command. I will show you that. But before we dive into the coding, as I said, basic understanding is very important. So what TextBlob is going to give us— TextBlob is obviously not going to give us the direct result, but it will give us two values. First is going to be the polarity, and the second is going to be the subjective. Subjectivity is also a number, and polarity is also going to be a number, but then we will convert them. Okay, let's first understand what is polarity. When we talk about polarity, it means this is going to give us a number from -1 to +1, okay? And when we talk about the subjectivity, the subjectivity is going to give us the number from 0 to 1. These are the numbers that you will get, and the sentiment analysis, as I said, is important to understand each and everything. Whether you are reviewing the product review, whether you are going through the tweets, whether you are reading the news headlines Each and everywhere we have to go through the emotions, what the sentence trying to say, what that person trying to say. Same way, creating an AI and giving it as emotional intelligence is also important. And TextBlob is created for the same purpose. So TextBlob is helping you with the emotional intelligence in any of the thing. It can be news headline, it can be student feedback, it can be product reviews, it can be tweet as well. If you are into marketing, if you are into politics, if you are into e-commerce, or if even if you are into finance, understanding the sentiment is important. So for marketing, if I say marketing, then it means to understand the customer behavior. It is very important. When we talk about finance, to understand the market mood, it is very important. When we talk about e-commerce, to have that satisfaction level from the customers, it is again important. So there are so many places where sentiment analysis plays a role. So learning the sentiment analysis and creating a sentiment analyzer is also one of the top projects that you can add in your resume. So if you are looking forward to that, this video is going to give you the practical knowledge of how exactly you can create a project, how exactly you can create a project for your resume on the basis of NLP, or NLP, or I would say NLTK. So let us begin with that, with the coding part. So now for the coding, what do I need? So very first thing is I need some data, obviously. So I have generated one sample data which I will show you right away. So I have already created one folder on my system which is called sentiment analysis. This sentiment analysis folder is over here. This folder I'm going to use to write down my Python code, and while writing the Python code, I'm going to show you the data. Okay, so I'm just going to open it. This is the reviews.csv file which I have created with the help of the ChatGPT, I will say, or you can even use the Gemini. Okay, these all are the reviews of the courses that I have seen on Google, on edX, on Coursera, and so on. So you will find I have added 1000+ reviews from multiple websites. 1000+ reviews you will find on this. I will add the same in the description also for the— for you. Okay, so I just have one column in it which is review, and where we have a multiple review. Now I'm going to show you how to start with the coding. Okay, so you simply open your Visual Studio Code and open the same folder in it. So just like I'm going to do, opening a folder which I have just created on the desktop itself, going to search for it with, with the name. It is like sentiment analysis, so I can easily search for it. I have selected that folder. Once I have opened it, I already have the data in it. If you don't have, you simply cut, copy, paste in the same folder. Then creating a new file code.py, or you can even create code.ipynb. So when I'm creating code.ipynb, it means it is a place where I will be writing my entire codes. Okay, so let us begin with the very first code which everyone needs, which is to install the library, to import the libraries that we need. So as I said, we have to get a library which is called TextBlob library. Without TextBlob library, you cannot do sentiment analysis. All right, so to get that installation, to get that library in your system, you simply have to write down a command: pip install textblob. Now this will install the TextBlob library on your system. The second library that we need in this project is called the Pandas library so that we can read our CSV file. So it is again important to have it. After that, I will also use the os library. So you are going to install all 3 libraries, pip install os. So os is like already there in the Python, so you don't need to do this. But yeah, for pandas and for TextBlob, you have to do that. Now let us begin with the main code that we need. So the main code obviously begins with writing the import part. It means I'm going to say from TextBlob import TextBlock. Okay, so same, I will say import pandas as pd, import os. These are the 3 libraries which are required for this, right? All right, so let us start with the code now. I have already imported the lines that I want. Now I am going to do what? I am going to create one function which is going to convert my sentiments. Okay, as I told you, the output are going to be on the basis of polarity. Okay, so So I'm going to just take the polarity and discuss some few things. Like if say, let's say the polarity that we are going to get is greater than 0, so it means that is something which is called positive. So I just wanted to print that, okay, this is positive with a smiley emoji. So I can add an emoji over here with this one right now. Same thing, if I will say else, elif polarity is less than 0, it means return negative with a sad emoji. And if I say that else, if none of the case, then it means if it is 0, then it simply returns neutral. These are the 3 things that I want. After this, what I'm going to do is I'm going to analyze the text. How do we analyze the text? As I said, we will use a function. So I'm going to say def analyze_text. I'm going to pass a text to it, and using the built-in object of TextBlob, which is then passing the text Then as I said, it is going to give you two outputs. So blob.sentiment.polarity, I'm going to take it in the variable polarity. The second is going to be the subjectivity. So subjectivity, I will write and I will use the same thing, blob.sentiment.subjectivity, because this is how we will get the values in it, right? Subjectivity, polarity and subjectivity I have taken in the variable. Now I'm going to apply the sentiment, okay? Sentiment is going to be get sentiment and then taking the polarity from there, okay, applying the polarity value over there, correct? Same way I will— what I will do is I will print. I will print what? I will print the sentiment that we have within the sentiment, from the sentiment variable that we have created simply. And I will use the formatting, f formatting over here. So I'm using the sentiment. Then same way I will print the polarity and the subjectivity as well. So copy paste, enter. And this is going to be the polarity. Same over here also, it is going to be polarity. I will change this to subjectivity. And again, for the value from the variable also, I have to change it to subjective. This is our analyze text function. After making an analysis, I want that there should be new file which is going to be review updated.csv, something. So I have to get that file as well. So to get that file created, I have to append certain data in it. So what I am going to do is I am going to create analyze_csv function, which is going to generate that CSV file for us as a final output. Okay, so file path I have to take, so I will pass the file path over here. Then df is equal to pd.read_csv, I will take and pass that file path over here. And then I will say df of polarity, you will take from here polarity, and then equal to df of the reviews that we have— we are going to take. Okay, let me come out of it. Polarity is equal to df of reviews, I want to mention. And then I will just apply the apply function so that I can apply the lambda one. Okay. So that we will have the detailed values. So text blob is the data, but I want what? I want str to be there so that it will be string, and taking the first value out of it, then applying the .sentiment over here, .polarity. Simple. And then I will do for the sentiment because I just want two things, like if it is 0, 1, like, like the one above I have done. The same thing I want to apply over here. So I will do sentiment— the third— second column that I am creating is of sentiment. Then taking the polarity.apply— now which function we will apply? We will apply the function that we have created above, which is get_sentiment. And then I will simply say that print_df of— I want reviews to be there, then I want polarity to be there, so that whoever is from the coding background can easily understand it. That, okay, we have used something out of Python, and then .head so that we can check whether our values are adding perfectly or not. Then we are converting it into _csv, and I will simply say that, okay, review_sentiment_output should be the file name, .csv is the extension that I want. Do I want index? No, I don't want index, so I will keep it false. And then same way, what I will do is I will simply print full output or full sentiment output saved to review sentiment output and .csv. Simply this print statement will be printed. Now it's going to be our main function or main code, okay? So it will run, okay? So I'm using this name underscore underscore and then underscore underscore main underscore underscore to have the data print sentiment analysis tool powered by TextBlob. And then I can simply mention what do you want, like do you— I am going to give it a choice, okay? So is equal to input. Do you want to read, or I will say, do you want to analyze text file or CSV file? So what I will do is I will give them options. Enter 1 for text file and 2 for CSV file. And then I will do what? I will just evaluate if choice is equal to equal to 1, then what to do? Take the user text is equal to input of enter your text. It means this time they are going to enter the text file name. Okay, I will enter text Filename, okay, or just taking a single text, okay, let's say not file but just a text, okay, so let's remove it. CSV file or a simple text that you wanted to evaluate, it can be any sentence, okay, and then analyze. We have one function which is called analyze text over here. Which we have created, this one. So we will use this function. I have, I have to add analyze, and then I will add it up. I will use it up over here. Analyze text, passing the user text which user has passed, and then going to simply go to the next one, which is going to be elif choice is equal to equal to 2. Then simply go to CSV file because now I want to read the CSV file that user is going to provide. So I will take the input of the data, input of the data saying that enter the CSV file name. So I will give an example as well that it should be with the name like review.csv. Like this you have to enter the file name. Okay, now we already have a CSV Well, os library in this, so os library will easily identify. So I'm using os library, os.path.exists, search for that file which is the CSV file, then passing the CSV file like analyze_csv to the CSV file, and then else print file not found. And same way I will say that else print invalid choice. So this is our entire code. Let me just walk you through again. First, we have imported all the libraries that we need. Then we have written one sentiment analysis function where I'm just working on this polarity, okay? Then I have created two functions. First is the analyze text, second is the analyze CSV. Why analyze CSV and why analyze text is basically, suppose you wanted to just pass on the message that you got from your ex So you simply can choose the text version and it will give you the analysis for the same. Now if you want the CSV, like you are working professional and you wanted to review, uh, you wanted to work on reviews, product reviews, sentiment things kind of, so then you can use with this one method. And then this is the main code where I have given the choice. Now we just execute it. Now what will happen? First it will connect to the kernel and then it will go to the Okay, so you can see here it is showing us an option. Do you want to analyze text or CSV? Let's say I wanted to analyze a text. Okay, so what I have to enter is I have to enter 1. So now it says that enter your text. Now I have to pass on text. So let's say I'm typing, I think we are not on the same page. So it says that sentiment is positive, polarity is 0.25, subjective is this. Now I'm going to run this. It says 2. Now you have to give it a name. So what is my file name? I will just check it. It is reviews. So I will write reviews.csv. Then it will evaluate each and everything. It will go through each and every sentence or each and every review that I have. Then it will go to the polarity and will give us the sentiment on the basis of the polarity. Now if you will open this CSV file, which is the new file which is generated, we have review, polarity, and sentiment. And this is the entire thing. You can even give it a UI as well. By using— you can use Streamlit library or a Flask library to give it a front end and then passing on this particular code as a backend. So this is how you can do a sentiment analysis. I hope you found this video helpful. I hope you found this video informative. Do let me know in the comments if you are looking forward to create more projects like this. We'll meet in the next session. Till then, bye-bye, stay tuned, and happy learning!










