#!/usr/bin/env bash # THESAURACLES -- synonym oracle # Copyright (C) Case Duckworth # with thanks to Will Sinatra ### Commentary: ## The wise Thesauracles has the synonym you seek! # Ask Thesauracles about a word you'd like to synonymize. # He will think about the word and all the others nearby it, before finally # yelling out the perfect synonym. ## Usage: # thesauracles ### Code: wf=/tmp/thesauracles dict_server="dict.org" dict_database="moby-thesaurus" thesaurus_header_lines=3 usage() { cat <"$response" 2>/dev/null else sleep 0.3 fi if grep -q 552 "$response"; then return 1 fi sed -n '/^151/,/^.$/p' "$response" | tail -n+$thesaurus_header_lines | awk 'BEGIN{RS=","}{sub(/^[ \n\t\r]+/,"");print}' >"$wf" } random_word() { sort -Ru "$wf" | head -n1 } main() { degree=6 # the Kevin Bacon number ;) talkative=true while getopts hqc:s:t:h: opt; do case "$opt" in h) usage ;; q) talkative=false ;; c) degree="$OPTARG" ;; s) dict_server="$OPTARG" ;; t) dict_database="$OPTARG" ;; l) thesaurus_header_lines="$OPTARG" ;; *) usage 1 ;; esac done shift $((OPTIND - 1)) word="$1" words=() hopn=0 while [ "$hopn" -lt "$degree" ]; do #printf '%s ' "$hopn" if query "$word"; then $talkative && echo "$word..." >&2 words+=("$word") word="$(random_word)" : $((hopn++)) elif [ "$hopn" -eq 0 ]; then echo "Oops, don't know \"$word!\"" >&2 exit 2 else $talkative && echo "hmm." >&2 : $((hopn--)) word="${words[-1]}" fi done echo "$word" | if $talkative; then awk '{print "> " toupper($0) "!"}' else cat fi } if [[ "$BASH_SOURCE" = "$0" ]]; then main "$@" fi