#25 Checking the Spelling of Individual Words

High-end programs like StarOffice, OpenOffice.org, and Microsoft Word include built-in spell-checking software, but the more rudimentary command-line question of whether a single word is spelled correctly or not is beyond the ability of any of these applications.

Similarly, most Unixes include a spell-check package that works reasonably well, albeit with a crusty interface. Given an input file or data stream, the packages generate a long list of all possible misspellings. Some spell-check packages include interactive spell-check applications. Again, however, none of them offer a simple way to check the spelling of a single word.

Don't have a spell-check program installed?

For those Unix distributions that don't have a spell package — though, really, all of 'em should nowadays, with disk space so cheap — an excellent option is to install ispell, from http://fmg-www.cs.ucla.edu/geoff/ispell.html


The Code

#!/bin/sh

# checkspelling - Checks the spelling of a word.

spell="ispell -l"         # if you have ispell installed
                          # if not, just define spell=aspell or
                          # equivalent
if [ $# -lt  1 ] ; then
  echo "Usage: $0 word or words" >&2;  exit 1
fi

for word
do
  if [ -z $(echo $word | $spell) ] ; then
    echo "$word:                spelled correctly."
  else
    echo "$word:                misspelled."
  fi
done

exit 0

Running the Script

To use this script, simply specify one or more words as arguments of the checkspelling command.

The Results

It's now easy to ascertain the correct spelling of "their":

$ checkspelling thier their
thier:          misspelled.
their:          spelled correctly.

Hacking the Script

There's quite a bit you can do with a spelling utility and, for that matter, quite a bit that ispell can already accomplish. This is just the tip of the proverbial iceberg, as you'll see in the next script.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.227.46.229