| 1 | #!/bin/bash |
| 2 | # dict-lookup.sh |
| 3 | |
| 4 | # This script looks up definitions in the 1913 Webster's Dictionary. |
| 5 | # This Public Domain dictionary is available for download |
| 6 | #+ from various sites, including |
| 7 | #+ Project Gutenberg (http://www.gutenberg.org/etext/247). |
| 8 | # |
| 9 | # Convert it from DOS to UNIX format (with only LF at end of line) |
| 10 | #+ before using it with this script. |
| 11 | # Store the file in plain, uncompressed ASCII text. |
| 12 | # Set DEFAULT_DICTFILE variable below to path/filename. |
| 13 | |
| 14 | |
| 15 | E_BADARGS=85 |
| 16 | MAXCONTEXTLINES=50 # Maximum number of lines to show. |
| 17 | DEFAULT_DICTFILE="/usr/share/dict/webster1913-dict.txt" |
| 18 | # Default dictionary file pathname. |
| 19 | # Change this as necessary. |
| 20 | # Note: |
| 21 | # ---- |
| 22 | # This particular edition of the 1913 Webster's |
| 23 | #+ begins each entry with an uppercase letter |
| 24 | #+ (lowercase for the remaining characters). |
| 25 | # Only the *very first line* of an entry begins this way, |
| 26 | #+ and that's why the search algorithm below works. |
| 27 | |
| 28 | |
| 29 | |
| 30 | if [[ -z $(echo "$1" | sed -n '/^[A-Z]/p') ]] |
| 31 | # Must at least specify word to look up, and |
| 32 | #+ it must start with an uppercase letter. |
| 33 | then |
| 34 | echo "Usage: `basename $0` Word-to-define [dictionary-file]" |
| 35 | echo |
| 36 | echo "Note: Word to look up must start with capital letter," |
| 37 | echo "with the rest of the word in lowercase." |
| 38 | echo "--------------------------------------------" |
| 39 | echo "Examples: Abandon, Dictionary, Marking, etc." |
| 40 | exit $E_BADARGS |
| 41 | fi |
| 42 | |
| 43 | |
| 44 | if [ -z "$2" ] # May specify different dictionary |
| 45 | #+ as an argument to this script. |
| 46 | then |
| 47 | dictfile=$DEFAULT_DICTFILE |
| 48 | else |
| 49 | dictfile="$2" |
| 50 | fi |
| 51 | |
| 52 | # --------------------------------------------------------- |
| 53 | Definition=$(fgrep -A $MAXCONTEXTLINES "$1 \\" "$dictfile") |
| 54 | # Definitions in form "Word \..." |
| 55 | # |
| 56 | # And, yes, "fgrep" is fast enough |
| 57 | #+ to search even a very large text file. |
| 58 | |
| 59 | |
| 60 | # Now, snip out just the definition block. |
| 61 | |
| 62 | echo "$Definition" | |
| 63 | sed -n '1,/^[A-Z]/p' | |
| 64 | # Print from first line of output |
| 65 | #+ to the first line of the next entry. |
| 66 | sed '$d' | sed '$d' |
| 67 | # Delete last two lines of output |
| 68 | #+ (blank line and first line of next entry). |
| 69 | # --------------------------------------------------------- |
| 70 | |
| 71 | exit $? |
| 72 | |
| 73 | # Exercises: |
| 74 | # --------- |
| 75 | # 1) Modify the script to accept any type of alphabetic input |
| 76 | # + (uppercase, lowercase, mixed case), and convert it |
| 77 | # + to an acceptable format for processing. |
| 78 | # |
| 79 | # 2) Convert the script to a GUI application, |
| 80 | # + using something like 'gdialog' or 'zenity' . . . |
| 81 | # The script will then no longer take its argument(s) |
| 82 | # + from the command-line. |
| 83 | # |
| 84 | # 3) Modify the script to parse one of the other available |
| 85 | # + Public Domain Dictionaries, such as the U.S. Census Bureau Gazetteer. |