Build a crossword puzzle assistant

Do you enjoy solving crossword puzzles, but often find yourself wishing you had an assistant at hand to help you complete those tough partial words? Hmm, I need a nine-letter word for “brand of fruit or type of computer,” and all I have is “_ _ c i _ _ o _ h”…just what could it be? At any rate, you can actually write your own simple crossword assistant using Terminal, a few lines of shell script, and the built-in Unix dictionary. (Note that the Unix dictionary differs from the dictionary used by the OS X Dictionary application—in particular, it’s missing many new words, as it’s based on a 1934 dictionary.)

If you’d rather stay away from Terminal, but like the idea of a crossword puzzle assistant, check out the free XWordLookup Widget from HRSoftWorks—it’s built using the same techniques shown in this hint, then wrapped in a nice Dashboard Widget front-end. If you’d like to know how to build your own version, though, keep reading.

To start, launch Terminal, in Applications -> Utilities, and type nano crosswd then press Return. This will launch the nano text editor with a blank screen. Copy the following text, and paste it into nano:

#!/bin/bash

echo -n "Please enter a word pattern to search for (use a . for unknowns): " 
read text

grep -i -w $text /usr/share/dict/words | tr '\n' ' '
echo

After pasting, press Control-X (for Exit), then press Y (to confirm saving changes) and Return (to accept the filename). Congratulations, you’ve now created a shell script. To use it, we need to make it executable (so that it can run). Type chmod a+x crosswd and press Return to do just that.

Now that you have an executable script, you can use it to help fill in the missing blanks in your crossword. Run it by typing ./crosswd (the ./ is required whenever you’re running a program within the current directory), then press Return. Now enter your partial word, using periods to indicate missing characters. Press Return, and your program will then list any matches:

$ ./crosswd

Please enter a word pattern to search for (use a . for unknowns): th..e
thane thave theme there these thine thoke thole thone thore those three throe Thule thyme

How it works

The shell script is pretty simple; the echo line asks for the word to find, which is then stored in a variable named text by the read command on the next line. The value stored in the variable is then sent to grep, which is used to find lines that match the stored variable. The -w option makes grep only look for matches that form whole words. Without this option, grep would find your specified pattern anywhere, even within longer words. With the option, only complete words of the length you’ve specified will be returned. The -i option just tells grep to ignore case, so it won’t try to match based on capitalization. Finally, the tr bit simply replaces line breaks (the \n) with spaces, so the results are presented without a lot of wasted space. The last line adds one line break before the command prompt is shown again.

Make it a bit easier to use

As things are set up now, you’d have to make sure you always specified the path to your script whenever you wanted to use it—that is, unless you were in your home directory, you’d need to type /Users/youruser/crosswd to run your program. You can fix this problem by modifying your user’s $PATH variable, which tells the system where to look for executable code. To start, type cd and press Return—this will insure you’re in your user’s home directory. To keep things neat, we’ll make a new directory (folder) to hold the crossword script, and you can then use it in the future for any additional scripts you create. Type mkdir bin and press Return to create a new directory named bin.

Next, we need to tell OS X that we’re going to use this folder for executable code, and we’d like the system to look there for programs. To do this, you need to modify the $PATH variable, and the best way to do that is via your user’s .profile file. Still in your user’s home folder, type nano .profile to edit the file. If you don’t have an existing file, you’ll be looking at a blank screen. If you do have this file, you’ll be looking at its contents. Copy and paste these two lines into the editor:

PATH="$HOME/bin:$PATH"
export PATH

(If you have an existing file, you may already have your own PATH statement in it. If you do, you’ll want to modify it to include the $HOME/bin bit from above.) Save the file as you did the other before—press Control-X, Y, and Return. Now close your Terminal window and open a new one, and type echo $PATH then press Return. You should see output like this:

/Users/robg/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/usr/X11/bin

If you don’t, check the above command in the .profile file and make sure it’s correct. If you see your user’s bin folder listed, there’s only one thing left to do: move your program into the bin folder. Type cd and press Return again to make sure you’re in your home folder, then type:

mv crosswd bin/crosswd

You can now run your crossword program any time in Terminal by simply typing crosswd. In the future, place new scripts in your bin folder, make them executable, and you can then run them from anywhere, too.

While this isn't the ideal crossword assistant, it works surprisingly well. Just don't try to use it on any modern computing-related puzzles, as you'll come up blank on nearly every word--not too surprising, given the age of the dictionary in use!

  
Shop Tech Products at Amazon