13.5. Demo: Only positive news#
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 news!
We’ll use sentiment analysis again, but this time we’ll get posts from the news subreddit, but only display the posts 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. Normal Reddit PRAW Setup#
import praw
(optional) make a fake praw connection with the fake_praw library
For testing purposes, we’ve added this line of code, which loads a fake version of praw, so it wont actually connect to reddit. If you want to try to actually connect to reddit, don’t run this line of code.
%run ../fake_apis/fake_praw.ipynb
# Load all your developer access passwords into Python
# TODO: Put your reddit username, password, and special developer access passwords below:
username="fake_reddit_username"
password="sa@#4*fdf_fake_password_$%DSG#%DG"
client_id="45adf$TW_fake_client_id_JESdsg1O"
client_secret="56sd_fake_client_secret_%Yh%"
# Give the praw code your reddit account info so
# it can perform reddit actions
reddit = praw.Reddit(
username=username, password=password,
client_id=client_id, client_secret=client_secret,
user_agent="a custom python script"
)
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. Code to search and display news#
Now let’s make code that will get submissions from the news subreddit (or from a fake_news_site), and display all of them. We will then make a modified version below to compare the results.
# Look up the subreddit "news", then find the "hot" list, getting up to 10 submission
submissions = reddit.subreddit("news").hot(limit=10)
# Turn the submission results into a Python List
submissions_list = list(submissions)
# go through each reddit submission
for submission in submissions_list:
print(submission.title)
print()
Breaking news: A lovely cat took a nice long nap today!
Breaking news: Someone said a really mean thing on the internet today!
Breaking news: Some grandparents made some yummy cookies for all the kids to share!
Breaking news: All the horrors of the universe revealed at last!
13.5.4. Search through news submissions and only display good news#
Now we will make a different version of the code that computes the sentiment of each submission title and only displays the ones with positive sentiment.
# Look up the subreddit "news", then find the "hot" list, getting up to 10 submission
submissions = reddit.subreddit("news").hot(limit=10)
# Turn the submission results into a Python List
submissions_list = list(submissions)
# go through each reddit submission
for submission in submissions_list:
#calculate sentiment
title_sentiment = sia.polarity_scores(submission.title)["compound"]
if(title_sentiment > 0):
print(submission.title)
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 Reddit#
If you want, you can skip the fake_praw step and try it out on real Reddit, from whatever subreddit you want
Did it work like you expected?
You can also only show negative sentiment submissions (sentiment < 0) if you want to see only bad news.