Skip to content

Instantly share code, notes, and snippets.

@shaystrong
Created March 26, 2020 18:15
Show Gist options
  • Save shaystrong/4df7816117614786a5999f0f2027bd0c to your computer and use it in GitHub Desktop.
Save shaystrong/4df7816117614786a5999f0f2027bd0c to your computer and use it in GitHub Desktop.
silly madlibs with NLTK tokenizer
from nltk.tokenize import sent_tokenize, word_tokenize
from nltk.tokenize.treebank import TreebankWordDetokenizer
import argparse
#python sillylib.py --input bed button kill be black open clothes also
# inspired by https://towardsdatascience.com/a-i-plays-mad-libs-and-the-results-are-terrifying-78fa44e7f04e
parser = argparse.ArgumentParser()
parser.add_argument('--input',nargs='+',default=['bed','button','kill','be','black','open','clothes','even'])
parser.add_argument('--person',default='AI')
args = parser.parse_args()
text = (
"Today, every student has a computer small enough to fit into "+\
"his _. He can solve any math problem by simply pushing the "+\
"computer's little _. Computers can add, multiply, divide, and "+\
"_. They can also _ better than a human. Some computers are "+\
"_. Others have an _ screen that shows all kinds of _ and _ "+\
"figures. "
)
tt=word_tokenize(text)
lis=args.input
#ailis=['bed','button','kill','be','black','open','clothes','even']
ii=0
for i in range(0,len(tt)):
if tt[i] is '_':
tt[i]=lis[ii]
ii=ii+1
print(args.person+' says: '+TreebankWordDetokenizer().detokenize(tt))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment