Creating a discord sentiment analysis bot using VADER

Creating a discord sentiment analysis bot using VADERHow to create a discord sentiment bot using the vader sentiment analysis libraryGilbert TannerBlockedUnblockFollowFollowingMar 18Sentiment analysis refers to the use of natural language processing, text analysis, computational linguistics, and many more to identify and quantify the sentiment of some kind of text or audio.

VADER (Valence Aware Dictionary and sEntiment Reasoner) is a lexicon and rule-based sentiment analysis tool that is specifically attuned to sentiments expressed in social media.

VADER makes it easy for us to create a sentiment analysis application.

In this article, we will create a discord bot that can analyze the sentiment of the written messages.

Creating a discord botFor those of you that don’t know discord, it is a proprietary freeware VoIP application and distribution platform.

It provides you with the ability to create your own server and customize it to your liking.

Discord also allows for the creation of bots that can play music, search for things online and much more.

For creating these bots discord provides libraries in multiple programming languages.

In this article, we will use the python library called discord.

py to create our bot.

The library can be installed using the following command:With discord.

py installed we can now create our bot.

For this, we need to go to the discord application page, log in and create a new app using the “new app” button.

Figure 2: Create discord applicationAfter pressing the button you can enter the name of your application.

To now create a bot you need to navigate to the bot window and press the “Create a Bot User” button.

Afterward, you need to go to the bot permissions and give the bot some basic reading permissions.

Adding the bot to our serverWith our bot created the last step left before we can start coding is to invite the bot to our discord server.

If you don’t have a server yet you can just open discord and press “Add a server” to create a server.

After you have your server ready you will need to navigate to the OAuth2 page and select bot in the scope options.

After you have selected the bot option you just need to copy the link below and paste it into a new browser window.

Figure 3: Add discord bot to your serverUsing the botNow that we created our bot and added it to our discord server we can create an example script that just returns the same message the user wrote.

For this, we will import the discord library, create a Client object and create an on_message event-handler so we can react when a user writes a message and lastly start our bot using the run method.

The run method runs the bot and therefore the bot should be online and ready to go whilst running the script.

What is VaderAs mentioned at the start of the article VADER (Valence Aware Dictionary and sEntiment Reasoner) is a lexicon and rule-based sentiment analysis tool that is specifically attuned to sentiments expressed in social media.

It makes it easy to create a sentiment analysis application without writing a sentiment model yourself.

To use VADER we first of need to install it by typing:With VADER installed we can now import the SentimentIntensityAnalyzer and then use the polarity_scores method to get the sentiment score.

Output: {'neg': 0.

0, 'neu': 0.

377, 'pos': 0.

623, 'compound': 0.

5106}As you can see VADER doesn’t output the sentiment as a string like negative, neutral or positive but rather it gives you a dictionary with a negative, neutral, positive and compound score.

The positive, neutral and negative scores represent the proportion of the text that falls inside these categories whilst the compound score represents the sum of all the lexicon ratings where +1 represents most positive and -1 represents most negative.

If this score is between -0.

05 and 0.

05 then the sentiment is neutral.

If it is lower than -0.

05 than the sentiment is negative and if the compound score is bigger than 0.

05 it’s positive.

With that information in mind, we can now create a simple method which maps the compound score to a string representing the sentiment.

We can improve this further using the google-translator python library as described in this excellent article about sentiment analysis of tweets using vader.

Output: positivePutting things togetherNow that we know how VADER works and we have our discord bot ready we can put things together and build the whole application.

For this, we only need to copy the code for both VADER and discord.

py together.

After running this file we can communicate with our bot by writing messages on the server.

Figure 4: Bot exampleRecommended ReadingIntroduction to Uber’s LudwigCreate deep learning models without writing codetowardsdatascience.

comConclusionSentiment analysis refers to the use of natural language processing, text analysis, computational linguistics, and many more to identify and quantify the sentiment of some kind of text or audio.

The VADER sentiment analysis library allows us to easily create sentiment analysis applications that operate at almost real-time speed.

If you liked this article consider subscribing to my Youtube Channel and following me on social media.

The code covered in this article is available as a Github Repository.

If you have any questions, recommendations or critiques, I can be reached via Twitter or the comment section.

.

. More details

Leave a Reply