Hack 95 Spelling Bee

figs/beginner.gif figs/hack95.gif

For those who edit their text at the command line.

Like most computer users, you probably find yourself spending a fair bit of time typing, whether responding to email, navigating the web, or working on that résumé or thesis. How often do you find yourself looking at a word, wondering if you've spelled it correctly? How often do you rack your brain trying to find a more interesting or descriptive word?

You've probably discovered that Unix doesn't come with a built-in dictionary or thesaurus. Sure, you can install a feature-rich GUI office suite, but what alternatives are there for users who prefer less bloat on their systems or are accessing systems from the command line?

9.8.1 Quick Spellcheck

If you're in doubt about the spelling of a word, try using look. Simply include as much of the word as you're sure about. For example, if you can't remember how to spell "bodacious" but you're pretty sure it starts with "boda":

% look boda bodach bodacious bodaciously

If you don't have access to a GUI, see [Hack #12] .

I find look especially helpful with suffixes. It's very handy if you can't remember when to use "ly", "ally", or "ily". For example:

% look mandator mandator mandatorily mandatory

9.8.2 Creating a Dictionary or Thesaurus

look is a useful spellchecker, but it won't show you the meanings or synonyms of a word. Accordingly, I found myself spending a fair bit of time at http://dictionary.reference.com/. While there, I noticed a pattern. Whatever word I searched for was appended to the URL as search?q=<myword>. Whenever I used the dictionary, the URL started with dictionary, which changed to the word thesaurus whenever I did a thesaurus lookup. That suggested to me that it would be very easy to generate my own custom lookup utility, so I started out with these two scripts:

% more ~/bin/dict #!/bin/sh # script to look up the definition of word from dictionary.reference.com # replaces $1 with user's search string # or gives error message if user forgets to include search string if test $1 then    w3m "http://dictionary.reference.com/search?q="$1"" else    echo "Don't forget to include the word you would like to search for"    exit 1 fi % more ~/bin/thes #!/bin/sh # script to find the synonym of word from thesaurus.reference.com # replaces $1 with user's search string # or gives error message if user forgets to include search string if test $1 then    w3m "http://thesaurus.reference.com/search?q="$1"" else    echo "Don't forget to include the word you would like to search for"    exit 1 fi

Recognize those positional parameters we saw before in [Hack #13] ? When I use either script, I include the word that I would like to look up.

The utility I chose to grab the results is the command-line browser w3m, which can be built from /usr/ports/www/w3m. If you have already installed another command-line browser, such as lynx or links, specify your browser in your own script. Don't forget to make your script executable with chmod +x. Then, to look up the meaning of a word:

% dict palladium

Or, to find its synonyms and antonyms:

% thes brusque

If you're not stuck at the command line, Mozilla-based browsers allow you to create similar shortcuts. See Asa Dotzler's article on custom keywords at http://www.mozilla.org/docs/end-user/keywords.html.

9.8.3 Improved Dictionary

Well, that's a fair start my browser now automagically takes me to the correct section of an online dictionary or thesaurus whenever I'm curious about a particular word. However, what if I want to forgo using a browser altogether? FreeBSD comes with the fetch utility specifically to retrieve web information. Why not use it to retrieve the results?

Before editing my scripts, I tried various invocations of fetch at the command line until I had achieved my desired results. I started out by replacing w3m with fetch (note that I had to supply a word, in this case test, as I was at the command line, not within a script):

% fetch "http://dictionary.reference.com/search?q=test"

This worked, but it resulted in a file called search?q=<myword>, where <myword> was the word I had supplied as the parameter. After a while, my home directory would be full of hundreds of files starting with search?q.

So, I specified the name of a file to which to write the results:

% fetch -o results "http://dictionary.reference.com/search?q=test"

Now, regardless of the number of times I use my script, I'll only have one file called results. There's a problem with that file, though. It's an HTML file, so unless I enjoy wading through HTML tags in order to read my results, I have to open up that file in a browser. That sorta defeats my goal of not using a browser.

So, I went out on the Web looking for an HTML-to-ASCII converter. I tried out several before settling on a Perl script called html2txt .

I then tried piping the results file to the converter:

% fetch -o results "http://dictionary.reference.com/search?q=test" \          | html2txt results Cannot open HTML source file : results, Error No such file or directory Receiving results: 21791 bytes

That's when I hit a timing issue. It takes a few seconds for fetch to retrieve the file, so html2txt complains when the shell asks for it to work on that (as of yet) nonexistent file. To solve that, I asked the shell to wait until after fetch was finished by using && instead of |:

% fetch -o results "http://dictionary.reference.com/search?q=test" \         && html2txt results

To finish off my command, I ask for the ASCII-fied file to be opened up in a pager so I can view the results:

% fetch -o results "http://dictionary.reference.com/search?q=test" \         && html2txt results && more results.txt

Note that this particular converter creates an ASCII file with the same name, but with a .txt extension.

9.8.4 Become a Crossword Champion

Did you know that your system has a built-in crossword-puzzle solver? You may never have to leave a square empty again if you remember this little trick.

Consider a word that resembles:

t _ _ _ k _ _ _r

This one-liner will show your possibilities, allowing you to choose the word that matches the clue definition:

% grep -wi 't...k...r' /usr/share/dict/words/   thickener trickster trinketer truckster

Here, grep searched through the dictionary words installed on your system. (This is the same file that look searches.) Use single quotes for your search phrase, and replace each blank square in your crossword with a ..

9.8.5 See Also

  • man fetch

  • The Perl HTML-to-text converter at http://www.ftls.org/en/examples/perl-tools/html2txt.shtml

  • "Wanna Cheat at Crosswords?" (http://www.osxfaq.com/tips/unix-tricks/week23/friday.ws)

BSD Hacks
BSD Hacks
ISBN: 0596006799
EAN: 2147483647
Year: 2006
Pages: 160
Authors: Lavigne

Similar book on Amazon

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net