-
Notifications
You must be signed in to change notification settings - Fork 0
/
markov.py
90 lines (63 loc) · 2.3 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
90
"""A Markov chain generator that can tweet random messages."""
import os
import twitter
import sys
from random import choice
MAX_LENGTH = 139
def open_and_read_file(filenames):
"""Take list of files. Open them, read them, and return one long string."""
body = ""
for filename in filenames:
text_file = open(filename)
body = body + text_file.read()
text_file.close()
return body
def make_chains(text_string):
"""Take input text as string; return dictionary of Markov chains."""
chains = {}
words = text_string.split()
for i in range(len(words) - 2):
key = (words[i], words[i + 1])
value = words[i + 2]
if key not in chains:
chains[key] = []
chains[key].append(value)
# or we could replace the last three lines with:
# chains.setdefault(key, []).append(value)
return chains
def make_text(chains):
"""Take dictionary of Markov chains; return random text."""
key = choice(chains.keys())
words = [key[0], key[1]]
while key in chains:
# Keep looping until we have a key that isn't in the chains
# (which would mean it was the end of our original text).
#
# Note that for long texts (like a full book), this might mean
# it would run for a very long time.
word = choice(chains[key])
words.append(word)
key = (key[1], word)
return " ".join(words)
def tweet(chains):
"""Create a tweet and send it to the Internet."""
api = twitter.Api(
consumer_key=os.environ['TWITTER_CONSUMER_KEY'],
consumer_secret=os.environ['TWITTER_CONSUMER_SECRET'],
access_token_key=os.environ['TWITTER_ACCESS_TOKEN_KEY'],
access_token_secret=os.environ['TWITTER_ACCESS_TOKEN_SECRET'])
print api.VerifyCredentials()
tweet = make_text(chains)
tweet = tweet[:140]
status = api.PostUpdate(tweet)
print status.text
# Get the filenames from the user through a command line prompt, ex:
# python markov.py green-eggs.txt shakespeare.txt
filenames = sys.argv[1:]
# Open the files and turn them into one long string
text = open_and_read_file(filenames)
# Get a Markov chain
chains = make_chains(text)
tweet(chains)
# Your task is to write a new function tweet, that will take chains as input
# tweet(chains)