Skip to content

Instantly share code, notes, and snippets.

@ana0
Created March 13, 2016 19:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ana0/602151bc88d930ef2793 to your computer and use it in GitHub Desktop.
Save ana0/602151bc88d930ef2793 to your computer and use it in GitHub Desktop.
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