-
Notifications
You must be signed in to change notification settings - Fork 0
/
bot.py
78 lines (67 loc) · 2.51 KB
/
bot.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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
import requests
import time
import numpy as np
class MarkovModel:
"""Represents a Markov Model for a given text"""
def __init__(self, n, text):
"""Constructor takes n-gram length and training text
and builds dictionary mapping n-grams to
character-probability mappings."""
self.n = n
self.d = {}
for i in range(len(text)-n-1):
ngram = text[i:i+n]
nextchar = text[i+n:i+n+1]
if ngram in self.d:
if nextchar in self.d[ngram]:
self.d[ngram][nextchar] += 1
else:
self.d[ngram][nextchar] = 1
else:
self.d[ngram] = {nextchar: 1}
def test_init(self):
for x in (list(self.d.items())[:10]):
print(x)
def get_next_char(self, ngram):
"""Generates a single next character based to come after the provided n-gram,
based on the probability distribution learned from the text."""
if ngram in self.d:
dist = self.d[ngram]
distlist = list(dist.items())
keys = [k for k, _ in distlist]
vals = [v for _, v in distlist]
valsum = sum(vals)
vals = list(map(lambda x: x/valsum, vals))
return np.random.choice(keys, 1, p=vals)[0]
else:
# this should never happen if start string n-gram exists in train text
return np.random.choice([x for x in "abcdefghijklmnopqrstuvwxyz"])
def get_n_chars(self, length, ngram):
"""Returns a generated sequence of specified length,
using the given n-gram as a starting seed."""
s = []
for i in range(length):
nextchar = self.get_next_char(ngram)
ngram = ngram[1:]+nextchar
s.append(nextchar)
return ''.join(s)
def main():
"""Load the data, build the Markov Model, and generate an example."""
f = open("trump_tweets_all.txt")
text = " ".join(f.readlines())
text = " ".join(text.split())
text = text.encode("ascii", errors="ignore").decode()
text.replace("&", "&")
f.close()
ngram_length = 4
tweet_length = 120
model = MarkovModel(ngram_length, text)
initial_ngram = "Hill"[:ngram_length]
return initial_ngram + model.get_n_chars(tweet_length, initial_ngram)
while True:
tweet = main()
tweet = tweet[0:119]
print(tweet)
r = requests.post('https://twitter.fly.dev/tweets/', {'user': 3, "content": tweet})
print(r.content)
time.sleep(3)