Created
June 19, 2011 15:28
Revisions
-
smcl revised this gist
Jun 19, 2011 . 1 changed file with 3 additions and 2 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -88,7 +88,8 @@ def fetch(user): 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) -
smcl created this gist
Jun 19, 2011 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,94 @@ #!/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(20) while len(msg) > 140: msg = m.generate_markov_text(20) api.PostUpdate(msg)