Work the Shell - Mad Libs Generator, Tweaks and Hacks

 in
We continue building a Mad Libs tool and slowly come to realize that it's a considerably harder problem than can be neatly solved in a 20-line shell script.

Last month, I ended with a script that could take an arbitrary set of sentences and randomly select, analyze and replace words with their parts of speech with the intention of creating a fun and interesting Mad Libs-style puzzle game. With a few tweaks, giving it a simple few sentences on party planning, we get something like this:


If you're ((looking:noun)) [for] a fun ((way:noun)) 
[to] celebrate your next ((birthday:noun)) how 
((about:adjective)) a pirate-themed costume
party? Start by sending ((invitations:noun)) in the 
form of ((a:noun)) <buried:verb> ((treasure:noun)) 
{map} with {X} ((marking:noun)) {the} ((location:noun)) 
[of] your house, then {put} {a} sign on the ((front:noun)) 
((door:noun)) [that] ((reads:noun)) "Ahoy, mateys" {and}
((fill:noun)) [the] ((house:noun)) [with] ((lots:noun)) 
of ((pirate:noun)) ((booty:noun))

In the current iteration of the script, it marks words chosen but discarded as being too short with {}, words where it couldn't unambiguously figure out the part of speech with [] and words that have what we defined as uninteresting parts of speech with <>.

If we display them as regular words without any indication that they've been rejected for different reasons, here's what we have left:

If you're ((looking:noun)) for a fun ((way:noun)) 
to celebrate your next ((birthday:noun)) how 
((about:adjective)) a pirate-themed costume party?
Start by sending ((invitations:noun)) in the form of 
((a:noun)) buried ((treasure:noun)) map with X 
((marking:noun)) the ((location:noun)) of your
house, then put a sign on the ((front:noun)) 
((door:noun)) that ((reads:noun)) "Ahoy, mateys" 
and ((fill:noun)) the ((house:noun)) with
((lots:noun)) of ((pirate:noun)) ((booty:noun))

Next, let's look at the output by simply blanking out the words we've chosen:

If you're ___ for a fun ___ to celebrate your next 
___ how ___ a pirate-themed costume party? Start 
by sending ___ in the form of ___ buried ___ map 
with X ___ the ___ of your house, then put a sign on 
the ___ ___ that ___ "Ahoy, mateys" and ___ the ___ 
with ___ of ___ ___.

It seems like too many words are being replaced, doesn't it? Fortunately, that's easily tweaked.

What's a bit harder to tweak is that there are two bad choices that survived the heuristics: “a” (in “form of a buried treasure map”) and “about” (in “how about a pirate-themed costume party?”). Just make three letters the minimum required for a word that can be substituted? Skip adjectives?

For the purposes of this column, let's just proceed because this is the kind of thing that's never going to be as good as a human editor taking a mundane passage of prose and pulling out the potential for amusing re-interpretation.

Prompting for Input

The next step in the evolution of the script is to prompt users for different parts of speech, then actually substitute those for the original words as the text passage is analyzed and output.

There are a couple ways to tackle this, but let's take advantage of tr and fmt to replace all spaces with carriage returns, then reassemble them neatly into formatted text again.

The problem is that both standard input and standard output already are being mapped and redirected: input is coming from the redirection of an input file, and output is going to a pipe that reassembles the individual words into a paragraph.

This means we end up needing a complicated solution like the following:


/bin/echo -n "Enter a ${pos}: " > /dev/tty
read newword < /dev/tty
echo $newword

We have to be careful not to redirect to /dev/stdout, because that's redirected, which means that a notation like &>1 would have the same problem of getting our input and output hopelessly muddled.

Instead, it actually works pretty well right off the bat:


$ sh madlib.sh < madlib-sample-text-2
Enter a noun: Starbucks
Enter a adjective: wet
Enter a adjective: sticky
Enter a noun: jeans
Enter a noun: dog
Enter a noun: window
Enter a noun: mouse
Enter a noun: bathroom
Enter a noun: Uncle Mort

That produced the following result:

If you're (( Starbucks )) for a fun way to celebrate 
your (( wet )) birthday, how (( sticky )) a pirate-themed 
costume (( jeans )) Start by sending invitations in the 
(( dog )) of a buried treasure map with X marking the 
(( window )) of your house, then put a (( mouse )) on 
the front (( bathroom )) that reads "Ahoy mateys" and fill 
the house with lots of pirate (( Uncle Mort ))

Now let's add some prompts, because if you're like me, you might not immediately remember the difference between a verb and an adjective. Here's what I came up with:

verb: an action word (eat, sleep, drink, jump)
noun: a person, place or thing (dog, Uncle Mort, Starbucks)
adjective: an attribute (red, squishy, sticky, wet)

Instead of just asking for the part of speech, we can have a simple case statement to include a useful prompt:

case $pos in
  noun ) prompt="Noun (person, place or thing: 
  ↪dog, Uncle Mort, Starbucks)" ;;
  verb ) prompt="Verb (action word: eat, 
  ↪sleep, drink, jump)" ;;
  adjective ) prompt="Adjective (attribute: red, 
  ↪squishy, sticky, wet)" ;;
  * ) prompt="$pos" ;;
esac
/bin/echo -n "${prompt}: " > /dev/tty

One more thing we need to add for completeness is to detect when we have plural versus singular, particularly with nouns. This can be done simply by looking at whether the last letter of a word is an s. It's not 100% accurate, but for our purposes, we'll slide with it being pretty good:

plural=""
if [ "$(echo $word | rev | cut -c1)" = "s" ] ; then
  plural="Plural ";
fi

Then, just modify the prompt appropriately:

/bin/echo -n "$plural${prompt}: " > /dev/tty

______________________

Dave Taylor has been hacking shell scripts for over thirty years. Really. He's the author of the popular "Wicked Cool Shell Scripts" and can be found on Twitter as @DaveTaylor and more generally at www.DaveTaylorOnline.com.

White Paper
Linux Management with Red Hat Satellite: Measuring Business Impact and ROI

Linux has become a key foundation for supporting today's rapidly growing IT environments. Linux is being used to deploy business applications and databases, trading on its reputation as a low-cost operating environment. For many IT organizations, Linux is a mainstay for deploying Web servers and has evolved from handling basic file, print, and utility workloads to running mission-critical applications and databases, physically, virtually, and in the cloud. As Linux grows in importance in terms of value to the business, managing Linux environments to high standards of service quality — availability, security, and performance — becomes an essential requirement for business success.

Learn More

Sponsored by Red Hat

White Paper
Private PaaS for the Agile Enterprise

If you already use virtualized infrastructure, you are well on your way to leveraging the power of the cloud. Virtualization offers the promise of limitless resources, but how do you manage that scalability when your DevOps team doesn’t scale? In today’s hypercompetitive markets, fast results can make a difference between leading the pack vs. obsolescence. Organizations need more benefits from cloud computing than just raw resources. They need agility, flexibility, convenience, ROI, and control.

Stackato private Platform-as-a-Service technology from ActiveState extends your private cloud infrastructure by creating a private PaaS to provide on-demand availability, flexibility, control, and ultimately, faster time-to-market for your enterprise.

Learn More

Sponsored by ActiveState