Python Programming In def main, how can write the for loop so that it only runs
ID: 3746940 • Letter: P
Question
Python Programming
In def main, how can write the for loop so that it only runs k times.
SentimentAnalysis.py
import nltk
from nltk.sentiment.vader import SentimentIntensityAnalyzer
import praw
reddit = praw.Reddit(client_id= 'Wd-WRix4MLXfBQ',
client_secret= 'cHqEazflu9bUk_Pi8CjaF1SwTh8',
user_agent= 'utepcstest'
)
nltk.download('vader_lexicon')
sid = SentimentIntensityAnalyzer()
def get_text_negative_proba(text):
return sid.polarity_scores(text)['neg']
def get_text_neutral_proba(text):
return sid.polarity_scores(text)['neu']
def get_text_positive_proba(text):
return sid.polarity_scores(text)['pos']
def get_submission_comments(url):
submission = reddit.submission(url=url)
submission.comments.replace_more()
return submission.comments
def process_comments(each_obj):
if each_obj.replies:
for each_reply in each_obj.replies:
print(each_reply)
process_comments(each_reply)
else:
return 1
def main():
comments = get_submission_comments('https://www.reddit.com/r/learnprogramming/comments/5w50g5/eli5_what_is_recursion/')
for each in comments:
print(each.body)
process_comments(each)
# print(comments[0].body)
# print(comments[0].replies[0].body)
# print(comments[0].replies[0].replies[0].body)
#neg = get_text_negative_proba(each)
#print(neg)
main()
Explanation / Answer
import nltk
from nltk.sentiment.vader import SentimentIntensityAnalyzer
import praw
reddit = praw.Reddit(client_id= 'Wd-WRix4MLXfBQ',
client_secret= 'cHqEazflu9bUk_Pi8CjaF1SwTh8',
user_agent= 'utepcstest'
)
nltk.download('vader_lexicon')
sid = SentimentIntensityAnalyzer()
def get_text_negative_proba(text):
return sid.polarity_scores(text)['neg']
def get_text_neutral_proba(text):
return sid.polarity_scores(text)['neu']
def get_text_positive_proba(text):
return sid.polarity_scores(text)['pos']
def get_submission_comments(url):
submission = reddit.submission(url=url)
submission.comments.replace_more()
return submission.comments
def process_comments(each_obj):
if each_obj.replies:
for each_reply in each_obj.replies:
print(each_reply)
process_comments(each_reply)
else:
return 1
def main():
##Ask for how many times you want to run the loop##
loop_times=input("How many times do you want to run the loop ")
comments = get_submission_comments('https://www.reddit.com/r/learnprogramming/comments/5w50g5/eli5_what_is_recursion/')
##Outer loop to run number of times based on input##
for i in range(int(loop_times)):
for each in comments:
print(each.body)
process_comments(each)
# print(comments[0].body)
# print(comments[0].replies[0].body)
# print(comments[0].replies[0].replies[0].body)
#neg = get_text_negative_proba(each)
#print(neg)
main()
###Output##
How many times do you want to run the loop
2
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.