Skip to content

Instantly share code, notes, and snippets.

@smcl
Created June 19, 2011 15:28
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save smcl/1034413 to your computer and use it in GitHub Desktop.
Save smcl/1034413 to your computer and use it in GitHub Desktop.
Markov Chain tweeter for @briancox
#!/usr/bin/python
import random
import oauth2 as oauth
import sys
import twitter
class Markov(object):
def __init__(self):
self.cache = {}
self.words = fetch("profbriancox")
self.word_size = len(self.words)
self.database()
def triples(self):
""" Generates triples from the given data string. So if our string were
"What a lovely day", we'd generate (What, a, lovely) and then
(a, lovely, day).
"""
if len(self.words) < 3:
return
for i in range(len(self.words) - 2):
yield (self.words[i], self.words[i+1], self.words[i+2])
def database(self):
for w1, w2, w3 in self.triples():
key = (w1, w2)
if key in self.cache:
self.cache[key].append(w3)
else:
self.cache[key] = [w3]
def generate_markov_text(self, size=20):
seed = random.randint(0, self.word_size-3)
seed_word, next_word = self.words[seed], self.words[seed+1]
w1, w2 = seed_word, next_word
gen_words = []
for i in xrange(size):
gen_words.append(w1)
w1, w2 = w2, random.choice(self.cache[(w1, w2)])
gen_words.append(w2)
return ' '.join(gen_words)
ACCESS_TOKEN_KEY="accesstokenkey"
ACCESS_TOKEN_SECRET="accesstokenkeysecret"
CONSUMER_KEY="consumerkey"
CONSUMER_SECRET="consumersecret"
api = twitter.Api(
consumer_key=CONSUMER_KEY,
consumer_secret=CONSUMER_SECRET,
access_token_key=ACCESS_TOKEN_KEY,
access_token_secret=ACCESS_TOKEN_SECRET)
######
def fetch(user):
data = {}
api = twitter.Api()
max_id = None
total = 0
words = []
while True:
try:
statuses = api.GetUserTimeline(user, count=200, max_id=max_id)
newCount = ignCount = 0
for s in statuses:
if s.id in data:
ignCount += 1
else:
data[s.id] = s
newCount += 1
total += newCount
print >>sys.stderr, "Fetched %d/%d/%d new/old/total." % (
newCount, ignCount, total)
if newCount == 0:
break
max_id = min([s.id for s in statuses]) - 1
for s in statuses:
words.extend(s.text.split())
except:
return words
return words
m = Markov()
def tweet():
msg = m.generate_markov_text(random.randint(5,20))
while len(msg) > 140:
msg = m.generate_markov_text(random.randint(5,20))
api.PostUpdate(msg)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment