From bd9b9ed00f43e4fbca353410c237ff9988246099 Mon Sep 17 00:00:00 2001 From: Case Duckworth Date: Sat, 9 Jul 2022 23:01:52 -0500 Subject: Initial commit --- thesauracles | 60 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 60 insertions(+) create mode 100755 thesauracles diff --git a/thesauracles b/thesauracles new file mode 100755 index 0000000..3891d18 --- /dev/null +++ b/thesauracles @@ -0,0 +1,60 @@ +#!/usr/bin/env bash +# THESAURACLES -- synonym oracle +# Copyright (C) Case Duckworth +# with thanks to Will Sinatra + +### Commentary: + +### Code: + +dict_server="dict.org" +dict_database="moby-thesaurus" +thesaurus_header_lines=3 + +wf=/tmp/thesauracles + +hops=3 + +query() { + response="$(mktemp /tmp/thesauracles.XXXXXX)" + trap "rm -f $response" KILL + curl "dict://$dict_server/d:$1:$dict_database" >"$response" 2>/dev/null + 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() { + n="$(wc -l <"$wf")" + ln="$((RANDOM % n + 1))" + sed -n ${ln}p "$wf" +} + +main() { + word="$1" + words=() + hopn=0 + while [ "$hopn" -lt "$hops" ]; do + if query "$word"; then + echo "$word..." + words+=("$word") + word="$(random_word)" + : $((hopn++)) + elif [ "$hopn" -eq 0 ]; then + echo "Oops, don't know \"$word!\"" >&2 + exit 1 + else + echo "hmm." + : $((hopn--)) + word="${words[-1]}" + fi + done + echo "$word" +} + +if [[ "$BASH_SOURCE" = "$0" ]]; then + main "$@" +fi -- cgit 1.4.1-21-gabe81