Sentiment Analysis: No AI, just math | SupeRails #206

21/10/2024
AI

Sentiment analysis is a way to analyse a text and quantify its positivity negativity based on keywords from a word positivity database.

In this episode we will extract all my Udemy reviews and perform a sentiment analysis on all of them. How many are actually positive?

Maybe these days AI magic could provide a better score, but THIS here is a mathematical approach.

Episode source code: https://github.com/corsego/206-sentiment-analysis/commit/f1e8e33a34d8daf7739d31801ddb096685e0b98d

Gem sentimental: https://github.com/7compass/sentimental

Did you enjoy the episode? Leave a testimonial here: https://superails.com/testimonials/

Transcript
What is sentiment analysis? It is basically a way to rate a block of text as negative, neutral, or positive, and also maybe give a number, uh, rating like minus two or plus five. Now, how does sentiment analysis work? Basically, you can have, uh, a lot of words, and some of them are positive and negative. Let's sort them into positive and negative. And now you can, uh, understand that some words, uh, have a stronger meaning than others, like, uh, against is not as, uh, negative as fraud. And based on this, you can have a database of roads and, uh, of, uh, how strong the world is. So the negative emojis in this example, rated as minus one, the positive emoji is plus one. Uh, a word as, uh, adorable is plus three, a word as abusive is minus three. So this is a basic example of a database that can be used for sentiment analysis. Here's another example, a list of emojis. And, uh, here's a text, uh, description of each of the emojis. And all these emojis can be rated based on the text description based on, uh, what is found here. Now, in the world of ruby, there is a gem named sentimental that you can use to, uh, say the, uh, string is positive, negative, or neutral. And you can also give it an average score based on, uh, this kind of values. Now the gem comes with its own, uh, data example. So we have, um, the English words, Jason, and he has a library of words and how strongly positive or strongly negative they are. And, uh, in total there are more than, uh, 18,000 folds in this library. So let's see how the gem works here. I have already installed the gem in my application, gem sentimental, and, uh, I'm going to try to run it in the rails console. So, uh, I will say rails console. We've created the analyzer. Now this empty, I'm going to load the default, uh, roads library. So I will say analyzer, load defaults. Now, if we type analyzer, we have, uh, the defaults, we can say, do world scores dot count. There are 18,000, uh, words that they're going to, uh, use for our analysis. And, uh, let's try analyzing something like analyzer sentiment. I love ruby. Uh, I, uh, uh, hate Java, for example. This one is strongly positive. This one is strongly negative. We can also analyze, uh, do score analyzer. Score. I love U oh 0.9. I, uh, hate, uh, him, uh, minus oh point 43. This is based on the world's love and hate that were found in the sentiment, uh, dictionary. Okay, now let's do something more practical. So here, uh, is, uh, the review page, uh, that I'm looking at as an instructor in Udemy. Previously, I used to create lots of ude courses, uh, before I started a YouTube channel. And, uh, here I have, uh, all my reviews. I exported them to CSV, I received, uh, the CSV as, uh, an email. I loaded the CSV and put it into my res application in them slash db slash data. So here I have over 500 reviews that I've have imported into my application. And, uh, each review has a rating that can be like four or 4.5 or five, and many of them have comments. And based on the comments, I can, uh, perform a sentiment analysis on all my comments and see how many comments are positive, how many are negative, and how many are neutral. So let's try doing this. I will, um, actually not code it all from the beginning. I'm going to run the generate, uh, run some command that I have previously prepared to make things faster. So first of all, I will, uh, as a warm up, try to get all the ratings from, uh, all the, uh, reviews and get an average rating, uh, without doing the sentiment analysis. So I'm going to, uh, pause the CSV and get all of the rating column. I'm going to save it as, uh, uh, float. And, uh, I'm going to have an array of all the ratings from the CSV. So let's run this Ratings. We have, uh, 524 ratings, and we can, uh, uh, get the average rating by, uh, summing them up and dividing them by that quantity. So in average is 4.5 out of five. This is a very good rating, I think. Now, let's, uh, get all the comments. So in a similar way, I'm just going to get all the common trolls. Okay, we have comments dot count 115, and now we can do the sentiment analysis on all the hundred and 10 comments. So I will start with the, an, uh, empty hash. We will have, uh, zero positive, negative, and neutral. And I'm going to run the sentiment analysis on each of the comments. And, uh, based on what the analysis gives, positive, negative, and neutral, I'm going to increase the counter. So I run this analyzer, and you see it runs, uh, in, uh, basically instantly because it doesn't, uh, put any external APIs. Like if you were running it with the open ai, if you were sending each of these comments to open ai, for example, and getting the sentiment, it would take a bit longer and it would cost you money. So let's see, what is the average sentiment? Okay, you have eight, seven positive, 16 negative, and 10 neutral. Sounds quite good. Now let's get the average, uh, score. So, uh, to get the average score, I will just run, not analyze that sentiment, but analyze a score. And, uh, let's see. The total score, it is 120, but it doesn't mean anything because we want to divide it by the amount of comments. So let's get the average score. It is positive 1.06. So I think it, uh, is, uh, quite good. And this is basically how you can use sentiment analysis. Now, uh, I have to say that this, uh, data is not, uh, perfect. There are some issues here. Uh, for example, uh, uh, it says that, uh, not bad is negative, and actually it is, uh, positive. So it could be better. Uh, you can find different, uh, databases of, uh, sentiments online. You can, uh, use them. You can add, uh, files for different, uh, languages you would like need, if you are doing some analysis on French text, you would need to have a, a library of French text. And, uh, again, it is, uh, uh, good approach that you can, uh, use if you don't want to use, uh, AI for this. So thanks for, well, AI as, uh, like or something like that. Anyway, thanks for watching and see you in the next one.
0
Join the conversation
Sign in to access PRO lessons, access private repos, leave comments, create watch lists.
We collect your email address, name and username to create your account. We do not share your email address with anyone else.