-
Notifications
You must be signed in to change notification settings - Fork 0
/
georgeWBot.py
144 lines (105 loc) · 4.14 KB
/
georgeWBot.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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
import sys
from random import choice
#Split everything up into functions... make georgeWBot its own class??
class GeorgeWBot(object):
def __init__(self):
# Create list of words from corpus.
textfile = open("Selected_Speeches_George_.txt", "r", encoding="utf8")
text = textfile.read()
words = text.split()
#print("corpus has " + str(len(words)) + " words.")
#Need to clean up the text file a little bit.
#This gets rid of all occurences of "————" followed by the next line. Which is always a page number...I think.
while True:
try:
index = words.index("————")
del words[index]
del words[index]
except ValueError:
break
# Build the dictionary from list of words.
d = {}
for i, word in enumerate(words):
if i + 2 < len(words):
key = (words[i], words[i + 1])
if key not in d:
d[key] = []
d[key].append(words[i + 2])
self.d = d
def makeSentence(self, d):
endChars = [".", "!", "?"]
# Create a sentence using the dictionary we made above.
# Find first key that is a number since its the bible
# Key starts with a number, since bible verses start with numbers
startingWords = [key for key in d.keys() if key[0][0].isupper() and key[0][-1] not in endChars]
# Choose a random key to start from
key = choice(startingWords)
# List to store our sentence
sentence = []
first, second = key
sentence.append(first)
sentence.append(second)
while True:
third = choice(d[key])
sentence.append(third)
if third[-1] in endChars:
break
# Increment the key
key = (second, third)
if key not in d.keys():
break
first, second = key
sentenceString = ' '.join(sentence)
return sentenceString
"""
Algorithm for creating a tweet:
sentence = new sentence
if len(sentence) is < 80
sentence2 = new sentence
tweet = sentence + sentence2
while tweet > 120 and tweet < 95:
sentence2 = new sentence
tweet = sentence + sentence2
return tweet
"""
def makeTweetText(self):
tweet = self.makeSentence(self.d)
#print(len(tweet))
if len(tweet) < 80:
charsLeft = 120 - len(tweet)
tweet2 = self.makeSentence(self.d)
fullTweet = tweet + " " + tweet2
while len(tweet2) >charsLeft or len(tweet2) < 10:
tweet2 = self.makeSentence(self.d)
fullTweet = tweet + " " + tweet2
return fullTweet
elif len(tweet) > 120:
return self.makeTweetText()
else:
return tweet
#print("Length of tweet is " + str(len(tweet)) + " characters.")
#print("\n" + tweet)
#This method determines hashtags and creates the final tweet
#tweetText is the string generated from makeTweetText()
def createTweet(self,tweetText):
#TODO
triggerWords = ["terror","God","nuclear","Iraq","soldiers","freedom","al Qaeda"]
defaultHashtags = ["#America","#USA","#GodBlessAmerica","#Congress","#GeorgeBush"]
hashtag = choice(defaultHashtags)
#figure out what hashtags to use
tweetTextWords = tweetText.split()
for word in tweetTextWords:
if word in triggerWords:
hashtag = "#" + word
tweet = hashtag + " " + tweetText
return tweet
#***************************************
#TESTING
#***************************************
if __name__ == "__main__" :
myBot = GeorgeWBot()
myBotsTweetText = myBot.makeTweetText()
print(myBotsTweetText)
myTweet = myBot.createTweet(myBotsTweetText)
print(myTweet)
print("tweet length is " + str(len(myTweet)))