Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@audiodude
Created November 7, 2016 04:51
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save audiodude/21c1fb3ffbe3bbc90e1684171106f982 to your computer and use it in GitHub Desktop.
Save audiodude/21c1fb3ffbe3bbc90e1684171106f982 to your computer and use it in GitHub Desktop.
Random sentence mangler
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