{ "cells": [ { "cell_type": "markdown", "id": "f2446309-25e4-4582-a537-b8292885dcad", "metadata": {}, "source": [ "# Demo: Only positive news" ] }, { "cell_type": "markdown", "id": "123456789-930485093240532940945-0324095320945904325", "metadata": { "tags": [] }, "source": [" _Choose Social Media Platform: __Reddit__ | Discord | Bluesky | No Coding_ "] }, { "cell_type": "markdown", "id": "a2ec8fb0", "metadata": {}, "source": [ "\n", "Let's look at something we could try to do to improve the mental health for our users: Only show positive news!\n", "\n", "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.\n", "\n", "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." ] }, { "cell_type": "markdown", "id": "ae27cd44-3897-4154-b7a1-a5a807bf92c7", "metadata": {}, "source": [ "## Normal Reddit PRAW Setup" ] }, { "cell_type": "code", "execution_count": 28, "id": "0d45a981-86cd-41f0-bc0a-066afdc985b4", "metadata": { "tags": [] }, "outputs": [], "source": [ "import praw" ] }, { "cell_type": "markdown", "id": "c699f603-a604-43b9-af9e-27e3ca60f542", "metadata": {}, "source": [ "(optional) make a fake praw connection with the fake_praw library\n", "\n", "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.__" ] }, { "cell_type": "code", "execution_count": 29, "id": "24a8ae3d-bc92-4628-b348-75d1ffcf1bfe", "metadata": {}, "outputs": [ { "data": { "text/html": [ "Fake praw is replacing the praw library. Fake praw doesn't need real passwords, and prevents you from accessing real reddit" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "%run ../fake_apis/fake_praw.ipynb" ] }, { "cell_type": "code", "execution_count": 30, "id": "72567e4d-e517-43f1-a949-49fb29120ddf", "metadata": {}, "outputs": [], "source": [ "# Load all your developer access passwords into Python\n", "# TODO: Put your reddit username, password, and special developer access passwords below:\n", "username=\"fake_reddit_username\"\n", "password=\"sa@#4*fdf_fake_password_$%DSG#%DG\"\n", "client_id=\"45adf$TW_fake_client_id_JESdsg1O\"\n", "client_secret=\"56sd_fake_client_secret_%Yh%\"" ] }, { "cell_type": "code", "execution_count": 31, "id": "94c2d5da-d971-454a-aebf-3ed64e286b01", "metadata": {}, "outputs": [ { "data": { "text/html": [ "Fake praw is pretending to collect account info to use on reddit" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "# Give the praw code your reddit account info so\n", "# it can perform reddit actions\n", "reddit = praw.Reddit(\n", " username=username, password=password,\n", " client_id=client_id, client_secret=client_secret,\n", " user_agent=\"a custom python script\"\n", ")" ] }, { "cell_type": "markdown", "id": "c46fd9fd-d9d4-4966-9b3b-9c4cfd5ac6b3", "metadata": {}, "source": [ "## Load sentiment analyis code" ] }, { "cell_type": "code", "execution_count": 32, "id": "97cdf7e8-73a9-4b9f-867c-2914e1b6f85f", "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "[nltk_data] Downloading package vader_lexicon to\n", "[nltk_data] C:\\Users\\kmthayer\\AppData\\Roaming\\nltk_data...\n", "[nltk_data] Package vader_lexicon is already up-to-date!\n" ] } ], "source": [ "import nltk\n", "nltk.download([\"vader_lexicon\"])\n", "from nltk.sentiment import SentimentIntensityAnalyzer\n", "sia = SentimentIntensityAnalyzer()" ] }, { "cell_type": "markdown", "id": "544f922d-0b24-4923-b635-c5de0fb39da1", "metadata": {}, "source": [ "## Code to search and display news\n", "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." ] }, { "cell_type": "code", "execution_count": 33, "id": "7232d9db-74c3-44c6-a835-6d541fca3e0e", "metadata": {}, "outputs": [ { "data": { "text/html": [ "Fake praw is pretending to select the subreddit: news" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "name": "stdout", "output_type": "stream", "text": [ "Breaking news: A lovely cat took a nice long nap today!\n", "\n", "Breaking news: Someone said a really mean thing on the internet today!\n", "\n", "Breaking news: Some grandparents made some yummy cookies for all the kids to share!\n", "\n", "Breaking news: All the horrors of the universe revealed at last!\n", "\n" ] } ], "source": [ "# Look up the subreddit \"news\", then find the \"hot\" list, getting up to 10 submission\n", "submissions = reddit.subreddit(\"news\").hot(limit=10)\n", "\n", "# Turn the submission results into a Python List\n", "submissions_list = list(submissions)\n", "\n", "# go through each reddit submission\n", "for submission in submissions_list:\n", " print(submission.title)\n", " print()\n" ] }, { "cell_type": "markdown", "id": "21e1b71b-b881-4227-82f4-76df85e47df0", "metadata": {}, "source": [ "## Search through news submissions and only display good news\n", "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." ] }, { "cell_type": "code", "execution_count": 34, "id": "a8297615-bb0f-4728-8ff1-c891336335a9", "metadata": {}, "outputs": [ { "data": { "text/html": [ "Fake praw is pretending to select the subreddit: news" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "name": "stdout", "output_type": "stream", "text": [ "Breaking news: A lovely cat took a nice long nap today!\n", "\n", "Breaking news: Some grandparents made some yummy cookies for all the kids to share!\n", "\n" ] } ], "source": [ "# Look up the subreddit \"news\", then find the \"hot\" list, getting up to 10 submission\n", "submissions = reddit.subreddit(\"news\").hot(limit=10)\n", "\n", "# Turn the submission results into a Python List\n", "submissions_list = list(submissions)\n", "\n", "# go through each reddit submission\n", "for submission in submissions_list:\n", " \n", " #calculate sentiment\n", " title_sentiment = sia.polarity_scores(submission.title)[\"compound\"]\n", " \n", " if(title_sentiment > 0):\n", " print(submission.title)\n", " print()" ] }, { "cell_type": "markdown", "id": "1e7fe660-e47f-4c2d-acf0-c14021c2807f", "metadata": {}, "source": [ "## Try it out on real Reddit\n", "If you want, you can skip the fake_praw step and try it out on real Reddit, from whatever subreddit you want\n", "\n", "Did it work like you expected?\n", "\n", "You can also only show negative sentiment submissions (sentiment < 0) if you want to see only bad news." ] } ], "metadata": { "kernelspec": { "display_name": "Python 3 (ipykernel)", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.10.4" } }, "nbformat": 4, "nbformat_minor": 5 }