Modules to be used:
nltk, collections, string and matplotlib modules.
nltk Module
The full form of nltk is "Natural Language Tool Kit". It is a module written in Python which works on the human language like what are the sentiments and emotions attached to it.
How can we Download it?
General Way:
pip install nltk
Pycharm Users:
Go to the project interpreter and install it.
Now after downloading this module, it does not mean that we can use its every package, for this we have to download its every package in this way,
Collections Module:
collections is an inbuilt python module that provides us various functions like the counter in which you just give our list and it will return us a dictionary with the count of the elements.
matplotlib Module:
matplotlib is a python library that will help us to plot graphs and all. In this, we will use this library to plot the emotions and sentiments graph.
How we will find the emotions and sentiments?
First of all, we will create a blank text file in which we will write or paste our speech (for which we have to find the emotions and sentiments) and we will read this file and tries to make each word a lower word(with the help of .lower() function) and now after this, we will remove the punctuations from the text and also remove the newline characters from the text and after this, we will make the text tokenize with the help of nltk module (Tokenize means splitting the text into the list (we can also do it with the help of .split() function but nltk will be faster than this) ), Now after tokenizing we will remove the stopwords (Stopwords means the words which have no emotion) with the help of nltk function and after that we will find the emotions and sentiments attached to the remaining words and will plot it with the help of matplotlib.
We will see the various functions of nltk,
The link to the emotions.txt file is: https://github.com/abhinav0606/SentimentAnalysisNLTK/blob/master/emotion.txt
Or, download here: emotion.txt
I have a taken the speech of Mark Zuckerberg which he gave at Harvard.
Let's see the code,
Output:
The plots are,
1) Emotions Graph:
2) Sentiments Graph:
need an explanation for this answer? contact us directly to get an explanation for this answer