Created
March 13, 2016 19:27
-
-
Save ana0/602151bc88d930ef2793 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def probabilities_matrix(text): | |
bigrams = {} | |
for i in range(len(text)): | |
try: | |
if (text[i], text[i+1]) in bigrams: | |
bigrams[(text[i], text[i+1])].append(text[i+2]) | |
else: | |
bigrams[(text[i], text[i+1])] = [text[i+2]] | |
except IndexError: | |
return bigrams | |
def make_tweet(text): | |
tweet = [] | |
get_start = random.choice(list(text.keys())) | |
while not get_start[0].istitle(): | |
get_start = random.choice(list(text.keys())) | |
for word in get_start: | |
tweet.append(word) | |
while not tweet[-1].endswith("."): | |
random_word = random.randint(0, len(text[(tweet[-2], tweet[-1])])-1) | |
tweet.append(text[(tweet[-2], tweet[-1])][random_word]) | |
return " ".join(tweet) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment