Skip to content

Instantly share code, notes, and snippets.

@manmohan24nov
Last active March 31, 2021 19:32
Show Gist options
  • Save manmohan24nov/165e1030fb31388f3d061e7c4031b7eb to your computer and use it in GitHub Desktop.
Save manmohan24nov/165e1030fb31388f3d061e7c4031b7eb to your computer and use it in GitHub Desktop.
# PRAW to interact with reddit
import praw
#install textblob if not already installed using "pip install -U textblob"
from textblob import TextBlob
import nltk
# Download VADER, if not downloaded
# nltk.download('vader_lexicon')
from nltk.sentiment.vader import SentimentIntensityAnalyzer
# create object for VADER sentiment function interaction
sia = SentimentIntensityAnalyzer()
reddit = praw.Reddit(client_id='client_id',
client_secret='client_secret',
user_agent='news on hooks')
# get 10 hot posts from the showerthoughts subreddit
top_posts = reddit.subreddit('showerthoughts').top('week', limit=5)
# Sentiment analysis function for TextBlob tools
def text_blob_sentiment(review, sub_entries_textblob):
analysis = TextBlob(review)
if analysis.sentiment.polarity >= 0.0001:
if analysis.sentiment.polarity > 0:
sub_entries_textblob['positive'] = sub_entries_textblob['positive'] + 1
return 'Positive'
elif analysis.sentiment.polarity <= -0.0001:
if analysis.sentiment.polarity <= 0:
sub_entries_textblob['negative'] = sub_entries_textblob['negative'] + 1
return 'Negative'
else:
sub_entries_textblob['neutral'] = sub_entries_textblob['neutral'] + 1
return 'Neutral'
# sentiment analysis function for VADER tool
def nltk_sentiment(review, sub_entries_nltk):
vs = sia.polarity_scores(review)
if not vs['neg'] > 0.05:
if vs['pos'] - vs['neg'] > 0:
sub_entries_nltk['positive'] = sub_entries_nltk['positive'] + 1
return 'Positive'
else:
sub_entries_nltk['neutral'] = sub_entries_nltk['neutral'] + 1
return 'Neutral'
elif not vs['pos'] > 0.05:
if vs['pos'] - vs['neg'] <= 0:
sub_entries_nltk['negative'] = sub_entries_nltk['negative'] + 1
return 'Negative'
else:
sub_entries_nltk['neutral'] = sub_entries_nltk['neutral'] + 1
return 'Neutral'
else:
sub_entries_nltk['neutral'] = sub_entries_nltk['neutral'] + 1
return 'Neutral'
# replication of comment section of reddit post
def replies_of(top_level_comment, count_comment, sub_entries_textblob, sub_entries_nltk):
if len(top_level_comment.replies) == 0:
count_comment = 0
return
else:
for num, comment in enumerate(top_level_comment.replies):
try:
count_comment += 1
print('-' * count_comment, comment.body)
text_blob_sentiment(comment.body, sub_entries_textblob)
nltk_sentiment(comment.body, sub_entries_nltk)
except:
continue
replies_of(comment, count_comment, sub_entries_textblob,sub_entries_nltk)
def main():
for submission in top_posts:
sub_entries_textblob = {'negative': 0, 'positive' : 0, 'neutral' : 0}
sub_entries_nltk = {'negative': 0, 'positive' : 0, 'neutral' : 0}
print('Title of the post :', submission.title)
text_blob_sentiment(submission.title, sub_entries_textblob)
nltk_sentiment(submission.title, sub_entries_nltk)
print("\n")
submission_comm = reddit.submission(id=submission.id)
for count, top_level_comment in enumerate(submission_comm.comments):
print(f"-------------{count} top level comment start--------------")
count_comm = 0
try :
print(top_level_comment.body)
text_blob_sentiment(top_level_comment.body, sub_entries_textblob)
nltk_sentiment(top_level_comment.body, sub_entries_nltk)
replies_of(top_level_comment,
count_comm,
sub_entries_textblob,
sub_entries_nltk)
except:
continue
print('Over all Sentiment of Topic by TextBlob :', sub_entries_textblob)
print('Over all Sentiment of Topic by VADER :', sub_entries_nltk)
print("\n\n\n")
if __name__ == '__main__' :
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment