-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcountbot.py
executable file
·55 lines (39 loc) · 1.42 KB
/
countbot.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
from bot import Bot
import socket
import re
import yaml
from time import sleep
from collections import Counter
from nltk import word_tokenize
from nltk.corpus import stopwords
from string import punctuation
class CountBot(Bot):
""""""
def __init__(self, output_path=None, **kwargs):
super(CountBot, self).__init__(**kwargs)
self.output_path = output_path
self.counts = Counter()
self.recent = []
def _update_counts(self, msg):
tokens = word_tokenize(msg)
exclusions = stopwords.words('english') + list(punctuation)
tokens = [x for x in tokens if x not in exclusions]
self.counts.update(tokens)
# TODO: make n_recent an argument
if len(self.recent) >= 200:
self.recent = self.recent[200:] + tokens
else:
self.recent.extend(tokens)
if self._msg_count % 20 == 0:
n_top = 10
recent_counts = Counter(self.recent).most_common(n_top)
print('\nToken Counts @ msg_count=%s; n_keys=%s; n_counts=%s' \
% (self._msg_count, len(self.counts), sum(self.counts.values())))
for x, y in zip(self.counts.most_common(n_top), recent_counts):
print('%s | %s' % (x, y))
def action(self, username, msg):
self._msg_count += 1
self._update_counts(msg)
if __name__ == '__main__':
bot = CountBot(channel="#merlinidota")
bot.run()