forked from cannedtomatoes/TwitterHaikuBot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHaikuBot.py
134 lines (100 loc) · 3.22 KB
/
HaikuBot.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
# Haiku bot - Chris Pocock
# Reads some file of random text and detects sentences with 5 and 7 syllables
# and turns them into Haiku poems
import sys
import re
import textstat
from textstat.textstat import textstat
import random
from twython import Twython
from twython import TwythonStreamer
import json
#Function to count syllables
def CountSyllables(word, isName=True):
vowels = "aeiouy"
#single syllables in words like bread and lead, but split in names like Breanne and Adreann
specials = ["ia","ea"] if isName else ["ia"]
specials_except_end = ["ie","ya","es","ed"] #seperate syllables unless ending the word
currentWord = word.lower()
numVowels = 0
lastWasVowel = False
last_letter = ""
for letter in currentWord:
if letter in vowels:
#don't count diphthongs unless special cases
combo = last_letter+letter
if lastWasVowel and combo not in specials and combo not in specials_except_end:
lastWasVowel = True
else:
numVowels += 1
lastWasVowel = True
else:
lastWasVowel = False
last_letter = letter
#remove es & ed which are usually silent
if len(currentWord) > 2 and currentWord[-2:] in specials_except_end:
numVowels -= 1
#remove silent single e, but not ee since it counted it before and we should be correct
elif len(currentWord) > 2 and currentWord[-1:] == "e" and currentWord[-2:] != "ee":
numVowels -= 1
return numVowels
#End function
#success = False
#count = 0
#Define our constant variables, this is all the data we wrote down in the first part of the tutorial.
CONSUMER_KEY =
CONSUMER_SECRET =
ACCESS_KEY =
ACCESS_SECRET =
#Create a copy of the Twython object with all our keys and secrets to allow easy commands.
api = Twython(CONSUMER_KEY,CONSUMER_SECRET,ACCESS_KEY,ACCESS_SECRET)
#optionally generate random word
#word = random.choice(open('20k.txt').readlines())[:-1]
word = 'Trump'
print 'Random word: ', word
twitter=Twython(
CONSUMER_KEY,
CONSUMER_SECRET,
ACCESS_KEY,
ACCESS_SECRET
)
# Find first line - 5 syllables
success = False
while success == False:
results = twitter.search(q = word, count = 100)
tweets = results['statuses']
for tweet in tweets:
tw = tweet['text']
tw_clean = re.sub(r"http\S+", "", tw)
if CountSyllables(tw_clean) == 5:
print(tw_clean)
success = True
#break
else:
count += 1
if count > 100
break
# Find second line - 7 syllables
success = False
while success == False:
results = twitter.search(q = word, count = 100)
tweets = results['statuses']
for tweet in tweets:
tw = tweet['text']
tw_clean = re.sub(r"http\S+", "", tw)
if CountSyllables(tw_clean) == 7:
print(tw_clean)
success = True
# Find third line - 5 syllables
success = False
while success == False:
results = twitter.search(q = word, count = 100)
tweets = results['statuses']
for tweet in tweets:
tw = tweet['text']
tw_clean = re.sub(r"http\S+", "", tw)
if CountSyllables(tw_clean) == 5:
print(tw_clean)
success = True
#Using our newly created object, utilize the update_status to send in the text passed in through CMD
#api.update_status(status=s1)