Advertisement
abdelrahman_orief

Untitled

Sep 28th, 2020 (edited)
1,162
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.29 KB | None | 0 0
  1. import praw
  2. import matplotlib.pyplot as plt
  3.  
  4. reddit = praw.Reddit(
  5.  client_id="client_id",
  6.  client_secret="client_secret",
  7.  user_agent="user_agent"
  8. )
  9.  
  10. AllSubmissions = []
  11. mp = {}
  12. words = []
  13.  
  14. for submission in reddit.subreddit("dataisbeautiful").hot(limit=1000):
  15.     AllSubmissions.append((submission.title, submission.score))
  16.     if "[Topic][Open]" not in submission.title:
  17.         for i in submission.title.split():
  18.             if i.isalpha():
  19.                 words.append(i.lower())
  20.  
  21. WordUpvote = []
  22. stopWords = ["me", "my", "myself", "we", "our", "ours", "ourselves", "you", "your", "yours", "yourself", "yourselves", "he", "him", "his", "himself", "she", "her", "hers", "herself", "it", "its", "itself", "they", "them", "their", "theirs", "themselves", "what", "which", "who", "whom", "this", "that", "these", "those", "am", "is", "are", "was", "were", "be", "been", "being", "have", "has", "had", "having", "do", "does", "did", "doing", "a", "an", "the", "and", "but", "if", "or", "because", "as", "until", "while", "of", "at", "by", "for", "with", "about", "against", "between", "into", "through", "during", "before", "after", "above", "below", "to", "from", "up", "down", "in", "out", "on", "off", "over", "under", "again", "further", "then", "once", "here", "there", "when", "where", "why", "how", "all", "any", "both", "each", "few", "more", "most", "other", "some", "such", "no", "nor", "not", "only", "own", "same", "so", "than", "too", "very", "s", "t", "can", "will", "just", "don", "should", "now"]
  23.  
  24. words = list(set(words))
  25.  
  26. for word in words:
  27.     scores = []
  28.     for sub in AllSubmissions:
  29.         t = sub[0]
  30.         s = sub[1]
  31.         if word in t:
  32.             scores.append(s)
  33.     if len(scores)>5 and word.lower() not in stopWords:
  34.         avg = int(sum(scores)/len(scores))
  35.         WordUpvote.append([avg, word])
  36.  
  37. WordUpvote.sort(key=lambda x:int(x[0]))
  38.  
  39. length = 20
  40. x = [b for a, b in WordUpvote[len(WordUpvote)-length:]]
  41. y = [a for a, b in WordUpvote[len(WordUpvote)-length:]]
  42.  
  43. plt.bar(x, y)
  44.  
  45. plt.ylim(0,)
  46.  
  47. ax = plt.axes()
  48.  
  49.  
  50.  
  51. plt.title("These words were included in the title of the most upvoted posts (given that they appeared in at least 5 posts)")
  52. plt.xlabel("Words")
  53. plt.ylabel("Number of upvotes")
  54.  
  55. ax.set_xticklabels(labels=x, fontsize = 8)
  56.  
  57.  
  58. plt.show()
  59.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement