Last active
February 26, 2017 02:01
-
-
Save MichaelSelsky/9eca1820cd2701388a5d95d16aa013a5 to your computer and use it in GitHub Desktop.
This file contains 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
import random | |
# Most of this code is more advanced than what I'd expect from the students. Lots of list comprehension that can be replaced with loops | |
def removePunctuation(s): | |
''' | |
Remove all the punctuation and replace with spaces. | |
''' | |
return ''.join(ch if ch not in string.punctuation else ' ' for ch in s) | |
def textToMarkovModel(text): | |
''' | |
Make a dictionary that is a pair of words as a key with an array of any words that follow it as the value | |
''' | |
text = removePunctuation(text).lower().split() | |
samples = {} | |
for i in range(len(text) - 2): | |
key = (text[i], text[i+1]) | |
if key in samples: | |
samples[(text[i], text[i+1])] += [text[i+2]] | |
else: | |
sample[key] = [text[i+2]] | |
return samples | |
def getNextWord(markovModel, text): | |
''' | |
Return a random word that comes from the last 2 words in the model. Else with the last word. | |
''' | |
words = text.lower().split() | |
k = (words[-2], words[-1]) | |
if k in markovModel: | |
return random.choice(markovModel[k]) | |
possibilities = [word for ((_, w), word) in markovModel.items() if words[-1] == w] | |
if len(possibilities) > 0: | |
return random.choice(possibilities) | |
possibilities = [word for (key, word) in markovModel.items() if words[-1] in key or words[-2] in key] | |
if len(possibilities) > 0: | |
return random.choice(possibilities) | |
possibilities = [word for (_, word) in markovModel.items()] | |
return random.choice(possibilities) | |
def makeTweet(seed, baseText): | |
m = textToMarkovModel(baseText) | |
t = seed | |
while len(t) < 140: | |
w = getNextWord(m, t) | |
t += " " + w | |
return t |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment