Question: Python You really like Reddit, but you have noticed that some subreddits are plagued with negativecomments.Tired of seeing this, you and a group of friends
Python

You really like Reddit, but you have noticed that some subreddits are plagued with negativecomments.Tired of seeing this, you and a group of friends decide to create an extension for your favorite web browser that automatically hides negative comments when you visit Reddit. As a first step to tackle this NLP problem, one of your team members decides to build a machine learning model to classify comments. She implements a method called get_text_negative_proba that receives a string containing the comment to be analyzed and outputs a number between 0 and 1. The closer the number is to 1, the more likely it is to be a negative comment. She also implemented two other methods: get_text_neutral_prob and get_text_positive_proba that do the same thing as get_text_negative_proba, but for neutral and positive sentiments.
She also writes the code to communicate with Reddit programmatically using their API. She writes a method called get_submission_comments that receives the URL of a Reddit submission as input (check the source code to see an example) and returns all the submissions replies. She asks if you can implement the process_comments method that she left blank. The purpose of this method is to traverse the replies tree of a given submission,and return three lists: negative_comments_list, neutral_comments_list and positive_comments_list. In other words, you need to traverse all of the replies in the submission,which are structured in a tree-like format. You think this is a perfect opportunity to show off your recursion skills!
Implement process_comments(use recursion) and implement multiple tests for your method.
1 import nltk 2 from nltk.sentiment.vader import SentimentIntensityAnalyzer 3 import praw 4 5 reddit-praw.Reddit(client_id-'copy and paste your client id here', clientsecret-' copy and paste your secret here', user_agent- 'my user agent' 6 8 9 10 11 nltk.download( 'vader_lexicon') 12 sid-SentimentIntensityAnalyzer() 13 14 15 def get_text_negative_proba(text): 16 17 18 19 def get_text_neutral_proba (text): 20 21 return sid.polarity_scores (text)['neg'] return sid.polarity_scores (text)['neu' 23 def get_text_positive_proba(text): 24 25 26 27 def get_submission_comments (url): 28 29 30 31 32 return sid.polarity_scores (text)[ pos'] submission- reddit.submission (url-url) submission.comments.replace_more() return submission.comments 34 def main(): 35 36 37 38 39 40 comments = get-submission-comments('https://www.reddit.com/r/learnprogramming/ comments/5W50g5/e115-what-is-recursion/') print(comments[0].body) print (comments[0].replies[0].body) neg -get_text_negative_proba(comments[0].replies[0].body) print (neg) 42 43 main) 1 import nltk 2 from nltk.sentiment.vader import SentimentIntensityAnalyzer 3 import praw 4 5 reddit-praw.Reddit(client_id-'copy and paste your client id here', clientsecret-' copy and paste your secret here', user_agent- 'my user agent' 6 8 9 10 11 nltk.download( 'vader_lexicon') 12 sid-SentimentIntensityAnalyzer() 13 14 15 def get_text_negative_proba(text): 16 17 18 19 def get_text_neutral_proba (text): 20 21 return sid.polarity_scores (text)['neg'] return sid.polarity_scores (text)['neu' 23 def get_text_positive_proba(text): 24 25 26 27 def get_submission_comments (url): 28 29 30 31 32 return sid.polarity_scores (text)[ pos'] submission- reddit.submission (url-url) submission.comments.replace_more() return submission.comments 34 def main(): 35 36 37 38 39 40 comments = get-submission-comments('https://www.reddit.com/r/learnprogramming/ comments/5W50g5/e115-what-is-recursion/') print(comments[0].body) print (comments[0].replies[0].body) neg -get_text_negative_proba(comments[0].replies[0].body) print (neg) 42 43 main)
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
