-
Notifications
You must be signed in to change notification settings - Fork 2
/
markov.py
89 lines (76 loc) · 2.6 KB
/
markov.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
79
80
81
82
83
84
85
86
87
88
89
import random
import sys
import time
import PIL
import textwrap
from PIL import ImageFont
from PIL import Image
from PIL import ImageDraw
from twython import Twython
import ConfigParser
config = ConfigParser.ConfigParser()
config.read("config.cfg")
config.sections()
APP_KEY = config.get("twitter", "app_key")
APP_SECRET = config.get("twitter", "app_secret")
OAUTH_TOKEN = config.get("twitter", "oauth_token")
OAUTH_TOKEN_SECRET = config.get("twitter", "oauth_token_secret")
stopword = "\n" # Since we split on whitespace, this can never be a word
stopsentence = (".", "!", "?",) # Cause a "new sentence" if found at the end of a word
sentencesep = "\n" #String used to separate sentences
# GENERATE TABLE
w1 = stopword
w2 = stopword
table = {}
twitter = Twython(APP_KEY, APP_SECRET, OAUTH_TOKEN, OAUTH_TOKEN_SECRET)
with open("source.txt") as f:
for line in f:
for word in line.split():
if word[-1] in stopsentence:
table.setdefault( (w1, w2), [] ).append(word[0:-1])
w1, w2 = w2, word[0:-1]
word = word[-1]
table.setdefault( (w1, w2), [] ).append(word)
w1, w2 = w2, word
table.setdefault( (w1, w2), [] ).append(stopword)
MAXSENTENCES = 5
w1 = stopword
w2 = stopword
def generate_sentences():
global w1, w2
margin = offset = 10
font = ImageFont.truetype("Lora-Regular.ttf", 16)
sentence = []
sentences = []
while len(sentences) < MAXSENTENCES:
newword = random.choice(table[(w1, w2)])
if newword == stopword: sys.exit()
if newword in stopsentence:
sentences.append(" ".join(sentence) + newword)
sentence = []
else:
sentence.append(newword)
w1, w2 = w2, newword
print "tweeting..."
status = max(sentences, key=len)
try:
if len(status) > 100:
# create the image to tweet with
img = Image.new("RGBA", (400,400),(255,255,255))
draw = ImageDraw.Draw(img)
for line in textwrap.wrap(status, width=50):
draw.text((margin, offset), line, (0,0,0), font=font)
offset += font.getsize(line)[1]
draw = ImageDraw.Draw(img)
img.save('temp.png')
media = open('temp.png', 'rb')
status = (status[:100] + '...')
twitter.update_status_with_media(media=media, status=status)
else:
twitter.update_status(status=status)
except Exception as e:
print "some sort of error... don't really care...", str(e)
while True:
generate_sentences()
print "sleeping..."
time.sleep(3600)