fix #1 and fixes #2 and closes #3.

This commit is contained in:
Tom Meagher
2013-08-12 15:01:22 -04:00
parent cdc5276504
commit 178b6fcc15
2 changed files with 26 additions and 10 deletions

View File

@@ -1,8 +1,8 @@
import random import random
import markov
import twitter
import re import re
import sys import sys
import twitter
import markov
from local_settings import * from local_settings import *
def connect(): def connect():
@@ -17,6 +17,8 @@ def filter_tweet(tweet):
tweet.text = re.sub(r'(\#|@|(h\/t)|(http))\S+','',tweet.text) #Take out URLs, hashtags, hts, etc. tweet.text = re.sub(r'(\#|@|(h\/t)|(http))\S+','',tweet.text) #Take out URLs, hashtags, hts, etc.
tweet.text = re.sub(r'\n','', tweet.text) #take out new lines. tweet.text = re.sub(r'\n','', tweet.text) #take out new lines.
tweet.text = re.sub(r'\"|\(|\)', '', tweet.text) #take out quotes. tweet.text = re.sub(r'\"|\(|\)', '', tweet.text) #take out quotes.
tweet.text = re.sub(r'\xe9', 'e', tweet.text) #take out accented e
tweet.text = re.sub(r'\&', '&', tweet.text) #clean up escaped html ampersands
return tweet.text return tweet.text
def grab_tweets(api, max_id=None): def grab_tweets(api, max_id=None):
@@ -50,7 +52,12 @@ if __name__=="__main__":
sys.exit() sys.exit()
mine = markov.MarkovChainer(order) mine = markov.MarkovChainer(order)
for tweet in source_tweets: for tweet in source_tweets:
if re.search('([\.\!\?\"\']$)', tweet):
pass
else:
tweet+="."
mine.add_text(tweet) mine.add_text(tweet)
for x in range(0,10): for x in range(0,10):
ebook_tweet = mine.generate_sentence() ebook_tweet = mine.generate_sentence()
@@ -61,13 +68,19 @@ if __name__=="__main__":
print ebook_tweet print ebook_tweet
#if a tweet is very short, this will randomly add a second sentence to it. #if a tweet is very short, this will randomly add a second sentence to it.
if ebook_tweet != None and len(ebook_tweet) < 40 and random.randint(0,5) == 0: if ebook_tweet != None and len(ebook_tweet) < 40:
print "Short tweet. Adding another sentence randomly" rando = random.randint(0,7)
newer_tweet = mine.generate_sentence() if rando == 0:
if newer_tweet != None: print "Short tweet. Adding another sentence randomly"
ebook_tweet += " " + mine.generate_sentence() newer_tweet = mine.generate_sentence()
else: if newer_tweet != None:
ebook_tweet = ebook_tweet ebook_tweet += " " + mine.generate_sentence()
else:
ebook_tweet = ebook_tweet
elif rando == 1:
#say something crazy/prophetic in all caps
print "ALL THE THINGS"
ebook_tweet = ebook_tweet.upper()
#throw out tweets that match anything from the source account. #throw out tweets that match anything from the source account.
if ebook_tweet != None and len(ebook_tweet) < 110: if ebook_tweet != None and len(ebook_tweet) < 110:

View File

@@ -61,7 +61,10 @@ class MarkovChainer(object):
except: except:
nw = False nw = False
new_res = res[0:-2] new_res = res[0:-2]
new_res[0] = new_res[0].capitalize() if new_res[0].istitle() or new_res[0].isupper():
pass
else:
new_res[0] = new_res[0].capitalize()
sentence = "" sentence = ""
for word in new_res: for word in new_res:
sentence += word + " " sentence += word + " "