Work the Shell - Mad Libs Generator, Tweaks and Hacks
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.
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.
Realizing the promise of Apache® Hadoop® requires the effective deployment of compute, memory, storage and networking to achieve optimal results. With its flexibility and multitude of options, it is easy to over or under provision the server infrastructure, resulting in poor performance and high TCO. Join us for an in depth, technical discussion with industry experts from leading Hadoop and server companies who will provide insights into the key considerations for designing and deploying an optimal Hadoop cluster.
Sponsored by AMD
Built-in forensics, incident response, and security with Red Hat Enterprise Linux 6
Every security policy provides guidance and requirements for ensuring adequate protection of information and data, as well as high-level technical and administrative security requirements for a system in a given environment. Traditionally, providing security for a system focuses on the confidentiality of the information on it. However, protecting the data integrity and system and data availability is just as important. For example, when processing United States intelligence information, there are three attributes that require protection: confidentiality, integrity, and availability.
Learn more about catching the bad guy in this free white paper.
Sponsored by DLT Solutions
| Designing Electronics with Linux | May 22, 2013 |
| Dynamic DNS—an Object Lesson in Problem Solving | May 21, 2013 |
| Using Salt Stack and Vagrant for Drupal Development | May 20, 2013 |
| Making Linux and Android Get Along (It's Not as Hard as It Sounds) | May 16, 2013 |
| Drupal Is a Framework: Why Everyone Needs to Understand This | May 15, 2013 |
| Home, My Backup Data Center | May 13, 2013 |
- RSS Feeds
- Dynamic DNS—an Object Lesson in Problem Solving
- Making Linux and Android Get Along (It's Not as Hard as It Sounds)
- Using Salt Stack and Vagrant for Drupal Development
- New Products
- Designing Electronics with Linux
- A Topic for Discussion - Open Source Feature-Richness?
- Drupal Is a Framework: Why Everyone Needs to Understand This
- Validate an E-Mail Address with PHP, the Right Way
- What's the tweeting protocol?
Enter to Win an Adafruit Pi Cobbler Breakout Kit for Raspberry Pi

It's Raspberry Pi month at Linux Journal. Each week in May, Adafruit will be giving away a Pi-related prize to a lucky, randomly drawn LJ reader. Winners will be announced weekly.
Fill out the fields below to enter to win this week's prize-- a Pi Cobbler Breakout Kit for Raspberry Pi.
Congratulations to our winners so far:
- 5-8-13, Pi Starter Pack: Jack Davis
- 5-15-13, Pi Model B 512MB RAM: Patrick Dunn
- 5-21-13, Prototyping Pi Plate Kit: Philip Kirby
- Next winner announced on 5-27-13!
Free Webinar: Hadoop
How to Build an Optimal Hadoop Cluster to Store and Maintain Unlimited Amounts of Data Using Microservers
Realizing the promise of Apache® Hadoop® requires the effective deployment of compute, memory, storage and networking to achieve optimal results. With its flexibility and multitude of options, it is easy to over or under provision the server infrastructure, resulting in poor performance and high TCO. Join us for an in depth, technical discussion with industry experts from leading Hadoop and server companies who will provide insights into the key considerations for designing and deploying an optimal Hadoop cluster.
Some of key questions to be discussed are:
- What is the “typical” Hadoop cluster and what should be installed on the different machine types?
- Why should you consider the typical workload patterns when making your hardware decisions?
- Are all microservers created equal for Hadoop deployments?
- How do I plan for expansion if I require more compute, memory, storage or networking?




2 hours 54 min ago
7 hours 21 min ago
10 hours 57 min ago
11 hours 30 min ago
13 hours 53 min ago
13 hours 56 min ago
13 hours 58 min ago
18 hours 22 min ago
20 hours 13 min ago
1 day 1 hour ago