blob: c69f28ce0ef1657fb0a4a2494666444b56160df3 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
|
#!/usr/bin/env bash
# THESAURACLES -- synonym oracle
# Copyright (C) Case Duckworth <acdw@acdw.net>
# 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 <word>
### Code:
dict_server="dict.org"
dict_database="moby-thesaurus"
thesaurus_header_lines=3
wf=/tmp/thesauracles
hops=3
query() {
response="/tmp/$1.thesauracles"
if [ ! -f "$response" ]; then
curl "dict://$dict_server/d:$1:$dict_database" >"$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() {
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" | awk '{print "> " toupper($0) "!"}'
}
if [[ "$BASH_SOURCE" = "$0" ]]; then
main "$@"
fi
|