Welcome to this video.
In this video, we will continue doing natural language processing.
And particularly in this video, we will do sentiment analysis, and that is finding the mood of a piece
of text if it's positive or negative.
So let's see how we can do that.
This is the code we wrote so for, and since this output is too long, I'll just print out just the
first results of the most used words and then create a new cell here, Make it a modern cell.
And this will be sentiment.
Analysis.
What is the most positive?
The most negative.
Chapter.
To perform sentiment analysis.
We need a sentiment intensity analyzer class, which comes from an ALT key dot sentiment.
Import.
Sentiment intensity analyzer.
So I'm executing the sell.
And in the next one, since we have just imported a class in here, we need to instantiate that class
to create an object instance from that class.
So sentiment intensity analyzer with parentheses.
And that's who will create an analyzer.
Object instance, which doesn't look any exciting for now.
So you don't see any actual user friendly output.
But this object instance now has methods you can check them with dear, and among them the one we need
is a polarity score method.
So that's polarity score.
Polarity scores.
Sorry.
Yes, polarity scores.
Polarity scores now expects a string.
This could be one sentence, this could be one paragraphs, and this could also be an entire book.
But it should be a string, for example, let's say.
Hey, Luke.
Ho!
Through the trees.
Ah, I love them.
If you press control, enter now to execute this, you're going to get a dictionary as output.
And these are the so called polarities, cause we have the negativity, the neutrality and the positivity
and also a compound coefficient.
So let's look at them.
Negativity here is 0.0, obviously, because this is a very positive sentence.
So we don't have a negativity there, but it can range from 0 to 1 and then we have neutrality.
This is .46 for this can also range from 0 to 1 and then positivity is the highest score, so it's 0.53
six.
Then we have the compound.
The compound might range from -1 to 1 and if it's above zero.
As it is in this case, if it's above zero, that means that the sentence is more positive than negative.
And if it's below zero, it means the sentence.
The text is more negative than positive.
So let's try to add something else here, such as I really love.
So we're trying to make the sentence even more positive.
If I execute that, we saw a slight increase in the positivity coefficient.
But if we say, for example, how bad the trees are, I hate them and I really hate them, then we're
going to see that the negativity has increased positivity zero and the compound coefficient is now negative.
So in order to find out the moods of a text, you should use these polarity scores.
Then perhaps you can write a conditional block, such as if Let's store this in schools, variable this
dictionary and execute that.
Right.
And then you say if schools.
Pause is greater than scores.
Thank you.
Print out.
It is a positive text.
Else print.
It is a negative text.
So basically, basically you can make use of those scores and make the program decide if a text is positive
or negative.
And of course, we can also try now this code on the book.
So instead of that string, we can provide the entire variable press enter, and that should give you
the dictionary with the.
Scores.
So since this book talks both about a tragedy but also a miracle, we do have a balance here.
So the negativity is close to the positivity coefficient.
Now let's try to analyze each chapter separately.
So this here was just.
An example.
And now we need to analyze.
Chapters.
Sentiment Analysis.
Right for that.
We need the r e library and the pattern which should be r e that compile.
And that was chapter.
0 to 9 plus.
So that will help us to split the book by this by chapters.
We did cover regular expressions yesterday, so please go to yesterday's videos if you don't know what
I'm doing here.
Chapters will be are displayed.
So the r e, the regular expression library does have a split method which splits a piece of text by
pattern, so it will split the book by this pattern.
Wherever this pattern is is met.
That is where the split point will be on this book.
So note chapters will be a list of chapters.
However, the very first occurrence here is an empty string, so we need to get rid of that because
you see, the length of chapters is not ten, but it is 11 because of that first string there.
So we could say chapters is equal to chapters one.
And so so we only extract the chapters starting from the first item from this very first chapter, excluding
that.
So execute that.
Chapters now is the actual chapters right now.
We can say from four chapter in chapters.
Scores is equal to.
That's methods.
And in parentheses, we should provide the chapter.
Right.
Each chapter in here.
And then print out the scores.
So let's try this.
And there we go.
So we can look at the negativity here, so we can see, for example, that the very first chapter is
the less negative of all, actually.
And that is because the book talks about a team which was going to play a match somewhere else.
And this was like full of harmony between the team players.
They had great expectations of the game and so on.
So there was no negativity in this chapter.
But the positivity is also not the highest one because you see in this last chapter, the positivity
was the highest.
So this is the last chapter in the last chapter of the book, the ordeal ended.
So there was a lot of positivity and so on.
And I suppose that the middle chapters are the most negative one where the problems were happening in
the book and so on.
All right.
So that's what you get.
Of course, you can take this even further.
You can perhaps write some effects conditionals or you can even write full number.
So the chapter in our chapter in enumerate chapters, and then you can print out the number along the
scores.
So there you go.
Chapter number zero, chapter number one and so on.
Or you can say number plus one.
And you get the correct numbers of the chapters along the Sentiment Analysis dictionary.
So with that, I thank you for following this and I'll talk to you in the next videos.