13.5. Demo: Only positive posts#
Choose Social Media Platform: Reddit | Discord | Bluesky | No Coding
Let’s look at something we could try to do to improve the mental health for our users: Only show positive posts!
We’ll use sentiment analysis again, but this time we’ll only display the tweets with a positive sentiment.
Would this actually improve someone’s mental health? It’s hard to say! But we can see something that we might try out if we wanted to improve mental health of our users.
13.5.1. Discord Setup#
# Load some code called "discord" that will help us work with Discord
import discord
# Load another library that helps the bot work in Jupyter Noteboook
import nest_asyncio
nest_asyncio.apply()
(optional) make a fake Discord connection with the fake_discord library
For testing purposes, we’ve added this line of code, which loads a fake version of discord, so it wont actually connect to Discord. If you want to try to actually connect to Discord, don’t run this line of code.
%run ../fake_apis/fake_discord.ipynb
# Set up your Discord connection
# TODO: put the discord token for your bot below
discord_token = "m#5@_fake_discord_token_$%Ds"
# set up Discord client with permissions to read message_contents
intents = discord.Intents.default()
intents.message_content = True
13.5.2. Load sentiment analyis code#
import nltk
nltk.download(["vader_lexicon"])
from nltk.sentiment import SentimentIntensityAnalyzer
sia = SentimentIntensityAnalyzer()
[nltk_data] Downloading package vader_lexicon to
[nltk_data] C:\Users\kmthayer\AppData\Roaming\nltk_data...
[nltk_data] Package vader_lexicon is already up-to-date!
13.5.3. Load posts from a discord channel#
We’ll load a set of posts from a discord channel (if you are doing this on real discord, you’ll have to put the correct channel_id
in the code). Hopefully the posts will have some images.
# set up discord connection
client = discord.Client(intents=intents)
# TODO: put the discord channel id number below for the channel you want to use
channel_id = 123456789
# Provide instructions for what your discord bot should do once it has logged in
@client.event
async def on_ready():
global recent_posts # Save the recent_posts variable outside our running bot
# Load the discord channel you want to post to
channel = client.get_channel(channel_id)
# Get the latest post in the channel history
post_history = channel.history(limit=10)
#special code to turn the post_history from discord into a python list
recent_posts = [post async for post in post_history]
# Tell your bot to stop running
await client.close()
# Now that we've defined how the bot shoould work, start running your bot
client.run(discord_token)
13.5.4. Loop through posts and only display good news#
Now we will make a modified version of the code that computed the sentiment of each post (in the Data Mining chapter) and only displays the posts with positive sentiment.
for post in recent_posts:
#calculate sentiment
post_sentiment = sia.polarity_scores(post.content)["compound"]
if(post_sentiment > 0):
print(post.content)
print()
Breaking news: A lovely cat took a nice long nap today!
Breaking news: Some grandparents made some yummy cookies for all the kids to share!
13.5.5. Try it out on real Discord#
If you want, you can skip the fake_discord step and try it out on real Discord, from whatever channel you want
Did it work like you expected?
You can also only show negative sentiment submissions (sentiment < 0) if you want to see only negative posts.