-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmarkov.py
More file actions
42 lines (33 loc) · 1009 Bytes
/
markov.py
File metadata and controls
42 lines (33 loc) · 1009 Bytes
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
import random
import json
import configparser
import Twitter
import tweet
import random
def buildChain(markov_array):
TWEET_MAX = 140
current_tweet = ""
finished_tweet = False
first_tuple = random.choice(markov_array)
current_tweet += first_tuple[0]
next_word = first_tuple[1]
start_word = next_word
while not finished_tweet:
finished_tweet = True
next_word = findWord(start_word, markov_array)
if len(current_tweet)+len(next_word)+1 < 140:
finished_tweet = False
current_tweet = current_tweet + " " + next_word
start_word = next_word
tweet.tweetString(current_tweet)
def findWord(last_word, markov_array):
candidate_words = []
for i in markov_array:
if i[0] == last_word:
candidate_words.append(i[1])
if len(candidate_words) == 0:
return " "
else:
return random.choice(candidate_words)
#markov_array = [("hi","dog")]
#buildChain(markov_array)