Replace words in a sentence with words chosen from a Markov chain generated from your favorite source material. Or, in non-technical terms, automatic Mad Libs!
Provide a source file of thousands of words of text; Madman will use this as inspiration when choosing its Mad Libs.
Requires NLTK.
- Install NLTK for your Python version.
- Install the
wordnet
,maxent_treebank_pos_tagger
, andpunkt
datasets for NLTK, usingnltk.download
. (That is, run the Python CLI,import nltk
, and then runnltk.download()
and select the datasets.) - Place your favorite source material in
sources/
as plain text files, named in the formatsourcename-raw.txt
. Large files (50,000 words or more) are good. As an example, Madman comes with some Mark Twain novels (twain.txt
) to try out. - Use the
sources/format.pl
script to turn the files into one-sentence-per-line files (e.g./format.pl sourcename
). - Run
python makeChains.py sourcename
to build the Markov chain.
Now you're ready to make some mad libs. You can do this two ways: interactively in Python, or at the command line.
For interactive mode, just run python
or ipython
in this directory, and then
just use
import madman
m = madman.Madman("sourcename")
m.madlib("Your text goes here.") # Only one sentence at a time
Alternately, to madlibify an entire text file in one shot, just run
python madfile.py yourfile.txt
Each sentence will be printed with strategic mad lib replacements.
Enjoy.
Using Mark Twain for the mad libs:
>>> m.madlib("This is a test, so please do not panic.")
'This is a giant, so please do there well.'
>>> m.madlib("Four score and seven years ago our forefathers brought forth on this continent a new nation.")
'Four score and seven dollars there our forefathers brought forth on this sign a runaway nation.'
>>> m.madlib("Ask not what your country can do for you -- ask what you can do for your country.")
'Ask not what your country can get for you-- ask what you can do for your uncles.'
For extra blasphemy, here's something based on source material I collected myself:
'Ask not what your country can do for you-- ask what you can do for your thighs.'