Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
75 changes: 75 additions & 0 deletions Auto_tweeter.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
import data_finder
from pickle import dump, load
import random

class Auto_Tweeter:
def __init__ (self):
self.data_grab()
self.tweet_maker()

def data_grab(self):
# asks if you need to pull new data for this person
print ('What is the handle of the person you want to mimic?')
self.user_handle = input()

print ('Have you collected Data? (y/n)')
collect_data = input()
#pulls new data, by calling Auto_tweeter
if collect_data == 'n':
self.statuses = data_finder.data_finder(self.user_handle)
self.dictionary_maker() # calls the dictionary_maker method
def dictionary_maker(self):
#sorts data into for markov analysis
self.dictionary = {}
self.start = []
self.end = []
for tweet in self.statuses:
# collects all the starts of each tweet
self.each_start = [[tweet[0],tweet[1]]]
self.start = self.start + self.each_start
self.dictionary['START']= self.start
for tweet in self.statuses:
tweet += ['END'] # adding and end flag to the end of the tweet
i= len(tweet)
for x in range (0,(i-2)):
#forming a dictionary linking previos two to the next word
if tweet[x]+tweet[x+1] in self.dictionary:
self.dictionary[tweet[x]+tweet[x+1]] += [tweet[x+2]]
else:
self.dictionary[tweet[x]+tweet[x+1]] = [tweet[x+2]]

self.pickle()

def pickle(self):
#saves the data set in a file
Data_Set = open(str(self.user_handle)+'.txt','wb')
dump(self.dictionary, Data_Set)
Data_Set.close()


def tweet_maker(self):
Data_Set = open(str(self.user_handle)+'.txt','rb')
#loading the data
self.dictionary = load(Data_Set)
print(len(self.dictionary['START']))
self.new_tweet=[]
self.new_tweet += random.choice(self.dictionary['START'])
#starts the tweet off with a random begining
#keeps adding randomly untill we arive at an END
while not self.new_tweet[-1] == 'END':
self.new_tweet += [random.choice(self.dictionary[self.new_tweet[-2]+self.new_tweet[-1]])]
self.final_tweet = ' '
#adds the words together and adds spaces
for word in self.new_tweet:
if not word =='END':
self.final_tweet += ' ' + word
#prints the new tweet
print(str(self.user_handle)+' says:'+ self.final_tweet)

print('Make another tweet (y/n)')
another= input()
if another == 'y':
self.tweet_maker()

if __name__ == '__main__':
tweeter = Auto_Tweeter()
Binary file added Pontifex.txt
Binary file not shown.
18 changes: 18 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,21 @@
Update 5/9/17
to run
>> python3 Auto_tweeter.py

and then follow directions





# TextMining

This is the base repo for the text mining and analysis project for Software Design at Olin College.

I was told to note the hours that I worked on this assignment and that I was
instructed to continue prioritizing good code over finished code.
I spent about 9 hours on this project this week, most of that was spent
rewriting data_finder over and over again to find a method that i liked....

my write up:
https://docs.google.com/document/d/1DwI3nODol5V7itbYxsNl4fq1XazYqyo1oBaXSLcysN4/edit?usp=sharing
52 changes: 52 additions & 0 deletions data_finder.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import twitter
import pickle
#who do you want to mimic
def data_finder(user_handle):
while True:
#data finder prompst the user to input login material
print ('consumer_key:')
c_k = input()
print ('consumer_secret:')
c_s = input()
print ('access_token_key:')
a_t_k = input()
print ('access_token_secret:')
a_t_s = input()
#data fider uses these to login

api = twitter.Api(consumer_key=c_k,
consumer_secret=c_s,
access_token_key=a_t_k,
access_token_secret=a_t_s)

# test to see if this brings an error if it doesnt break! if it does,
#try autenticating again
try:
api.VerifyCredentials()
break
except twitter.error.TwitterError as e:
print(e)
data_finder(user_handle)

# how do i make a list of lists of words from each tweet?
# How do i set up a login error that brings people back to the login option and works for more people than just me
# How do i save that list so i can open it in another progam


statuses = api.GetUserTimeline(screen_name=user_handle, count=850)
statuses = [s.text for s in statuses]
#print (statuses)
words = []
for status in statuses:
tweet = [status.split()]
words = words + tweet
statuses = words
return statuses




if __name__ == '__main__':
#import doctest
#doctest.testmod(verbose=True)
data_finder('pontifex')
Loading