LDP/LDP/guide/docbook/abs-guide/dict-lookup.sh

86 lines
2.7 KiB
Bash
Raw Normal View History

2005-03-21 13:52:30 +00:00
#!/bin/bash
# dict-lookup.sh
# This script looks up definitions in the 1913 Webster's Dictionary.
# This Public Domain dictionary is available for download
#+ from various sites, including
#+ Project Gutenberg (http://www.gutenberg.org/etext/247).
#
2008-11-23 22:43:47 +00:00
# Convert it from DOS to UNIX format (with only LF at end of line)
2005-03-21 13:52:30 +00:00
#+ before using it with this script.
2008-11-23 22:43:47 +00:00
# Store the file in plain, uncompressed ASCII text.
2005-05-08 20:09:31 +00:00
# Set DEFAULT_DICTFILE variable below to path/filename.
2005-03-21 13:52:30 +00:00
2008-11-23 22:43:47 +00:00
E_BADARGS=85
2005-03-21 13:52:30 +00:00
MAXCONTEXTLINES=50 # Maximum number of lines to show.
2005-05-08 20:09:31 +00:00
DEFAULT_DICTFILE="/usr/share/dict/webster1913-dict.txt"
# Default dictionary file pathname.
2005-03-21 13:52:30 +00:00
# Change this as necessary.
# Note:
# ----
2005-05-08 20:09:31 +00:00
# This particular edition of the 1913 Webster's
2005-03-21 13:52:30 +00:00
#+ begins each entry with an uppercase letter
#+ (lowercase for the remaining characters).
2005-05-08 20:09:31 +00:00
# Only the *very first line* of an entry begins this way,
2005-03-21 13:52:30 +00:00
#+ and that's why the search algorithm below works.
if [[ -z $(echo "$1" | sed -n '/^[A-Z]/p') ]]
# Must at least specify word to look up, and
#+ it must start with an uppercase letter.
then
echo "Usage: `basename $0` Word-to-define [dictionary-file]"
echo
echo "Note: Word to look up must start with capital letter,"
echo "with the rest of the word in lowercase."
echo "--------------------------------------------"
echo "Examples: Abandon, Dictionary, Marking, etc."
exit $E_BADARGS
fi
2005-05-08 20:09:31 +00:00
if [ -z "$2" ] # May specify different dictionary
#+ as an argument to this script.
2005-03-21 13:52:30 +00:00
then
dictfile=$DEFAULT_DICTFILE
else
dictfile="$2"
fi
# ---------------------------------------------------------
Definition=$(fgrep -A $MAXCONTEXTLINES "$1 \\" "$dictfile")
# Definitions in form "Word \..."
#
2005-05-08 20:09:31 +00:00
# And, yes, "fgrep" is fast enough
#+ to search even a very large text file.
2005-03-21 13:52:30 +00:00
# Now, snip out just the definition block.
echo "$Definition" |
sed -n '1,/^[A-Z]/p' |
# Print from first line of output
#+ to the first line of the next entry.
sed '$d' | sed '$d'
# Delete last two lines of output
#+ (blank line and first line of next entry).
# ---------------------------------------------------------
2008-11-23 22:43:47 +00:00
exit $?
2005-03-21 13:52:30 +00:00
# Exercises:
# ---------
# 1) Modify the script to accept any type of alphabetic input
# + (uppercase, lowercase, mixed case), and convert it
# + to an acceptable format for processing.
#
# 2) Convert the script to a GUI application,
2008-03-13 13:24:45 +00:00
# + using something like 'gdialog' or 'zenity' . . .
2005-03-21 13:52:30 +00:00
# The script will then no longer take its argument(s)
2008-11-23 22:43:47 +00:00
# + from the command-line.
2005-05-08 20:09:31 +00:00
#
# 3) Modify the script to parse one of the other available
# + Public Domain Dictionaries, such as the U.S. Census Bureau Gazetteer.