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.
Today’s modular x86 servers are compute-centric, designed as a least common denominator to support a wide range of IT workloads. Those generic, virtualized IT workloads have much different resource optimization requirements than hyperscale and cloud applications. They have resulted in a “one size fits all” enterprise IT architecture that is not optimized for a specific set of IT workloads, and especially not emerging hyperscale workloads, such as web applications, big data, and object storage. In this report, you will learn how shifting the focus from traditional compute-centric IT architectures to an innovative disaggregated fabric-based architecture can optimize and scale your data center.
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
| 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 |
| Non-Linux FOSS: Seashore | May 10, 2013 |
| Trying to Tame the Tablet | May 08, 2013 |
| Dart: a New Web Programming Experience | May 07, 2013 |
- RSS Feeds
- New Products
- Making Linux and Android Get Along (It's Not as Hard as It Sounds)
- A Topic for Discussion - Open Source Feature-Richness?
- Drupal Is a Framework: Why Everyone Needs to Understand This
- Home, My Backup Data Center
- New Products
- Paranoid Penguin - Building a Secure Squid Web Proxy, Part IV
- Trying to Tame the Tablet
- Developer Poll
Enter to Win an Adafruit Prototyping Pi Plate 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 Prototyping Pi Plate 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
- Next winner announced on 5-21-13!
Free Webinar: Linux Backup and Recovery
Most companies incorporate backup procedures for critical data, which can be restored quickly if a loss occurs. However, fewer companies are prepared for catastrophic system failures, in which they lose all data, the entire operating system, applications, settings, patches and more, reducing their system(s) to “bare metal.” After all, before data can be restored to a system, there must be a system to restore it to.
In this one hour webinar, learn how to enhance your existing backup strategies for better disaster recovery preparedness using Storix System Backup Administrator (SBAdmin), a highly flexible bare-metal recovery solution for UNIX and Linux systems.




42 min 23 sec ago
4 hours 56 min ago
7 hours 28 min ago
12 hours 7 min ago
14 hours 30 min ago
1 day 7 hours ago
1 day 9 hours ago
1 day 11 hours ago
1 day 11 hours ago
1 day 12 hours ago