Guest User

Most Used Words Bot

a guest
May 27th, 2013
2,633
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.28 KB | None | 0 0
  1. import praw
  2. import time
  3. import string
  4. import logging
  5. import collections
  6. import sys
  7. from sets import Set
  8. from collections import Counter
  9. from praw import BaseReddit
  10. from pprint import pprint
  11. user_agent = ("Script by /u/PostYourSinks") #User ID Name
  12. r = praw.Reddit(user_agent=user_agent) #establish contact with reddit
  13. r.login('InfoUserBot', '******************')
  14. punctuation = " " + string.punctuation + "\n"
  15. commonWords = set()
  16. allowed_chars = Set('abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ')
  17. for line in open("common-words.txt", "r"):
  18.     commonWords.add(line.strip(punctuation).lower())
  19. while True:
  20.     newMessage = r.get_unread(limit=None)
  21.     for message in newMessage:
  22.         if "/mostusedwords" == message.body:
  23.             tfcomment = message.was_comment
  24.             if not tfcomment:
  25.                 message.mark_as_read()
  26.                 most_used_words = Counter()
  27.                 user = message.author
  28.                 comments = user.get_comments(limit=None)
  29.                 links = user.get_submitted(limit=None)
  30.                 for comment in comments:
  31.                     body = comment.body
  32.                     for word in body.split():
  33.                         if Set(word).issubset(allowed_chars) and word.isalnum():
  34.                             s = False
  35.                             sword = str(word).lower()
  36.                             if word.endswith("s") and word != 'was':
  37.                                 singular = word[:-1]
  38.                                 if most_used_words[singular] > 0:
  39.                                     most_used_words[singular] += 1
  40.                                     s = True
  41.                             if len(sword) < 16 and sword not in commonWords and s == False:
  42.                                 most_used_words[sword] += 1
  43.                 for link in links:
  44.                     body = link.selftext
  45.                     for word in body.split():
  46.                         if Set(word).issubset(allowed_chars) and word.isalnum():
  47.                             s = False
  48.                             sword = str(word).lower()
  49.                             if word.endswith("s"):
  50.                                 singular = word[:-1]
  51.                                 if most_used_words[singular] > 0:
  52.                                     most_used_words[singular] += 1
  53.                                     s = True
  54.                             if len(sword) < 16 and sword not in commonWords and s == False:
  55.                                 most_used_words[sword] += 1
  56.                 zewDict = Counter(most_used_words).most_common(10)
  57.                 newDict = dict(zewDict)
  58.                 keys = newDict.keys()
  59.                 values = newDict.values()
  60.                 msg = ('Here is your breakdown of your most used words: \n\n'
  61.                        'Word | Times Used'
  62.                        '\n:--:|:--:')
  63.                 msg = "Here is your breakdown of your most used words:\n\nWords | Times Used\n:--:|:--:\n"
  64.                 msg += '\n'.join('%s|%s' % (k.capitalize(), v) for (k, v) in most_used_words.most_common(10))
  65.                 r.send_message(user, 'Most Used Words', msg)
  66.                 print('Message sent to user %s' % (user))
  67.                 time.sleep(4)
  68.         else:
  69.             time.sleep(4)
  70.     time.sleep(4)
Advertisement
Add Comment
Please, Sign In to add comment