Created
November 7, 2016 04:51
-
-
Save audiodude/21c1fb3ffbe3bbc90e1684171106f982 to your computer and use it in GitHub Desktop.
Random sentence mangler
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 nltk | |
import random | |
STOP_WORDS = ['the', 'a', 'and', 'of', 'in', 'with', 'for', 'on'] | |
def rhyme(inp, level): | |
entries = nltk.corpus.cmudict.entries() | |
syllables = [(word, syl) for word, syl in entries if word == inp] | |
rhymes = [] | |
for (word, syllable) in syllables: | |
rhymes += [word for word, pron in entries if pron[-level:] == syllable[-level:]] | |
return set(rhymes) | |
def quirk(sent): | |
words = nltk.word_tokenize(sent) | |
for word in words: | |
if word.lower() in STOP_WORDS or random.randint(1, 100) > 80: | |
continue | |
rhymes = rhyme(word, 2) | |
if rhymes: | |
r = random.sample(rhymes, 1).pop() | |
sent = sent.replace(word, r) | |
print(sent) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment