-
Notifications
You must be signed in to change notification settings - Fork 3
/
et
executable file
·258 lines (203 loc) · 6.76 KB
/
et
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
#!/bin/sh
html_entities_to_ascii () {
echo -n "$1" | sed -E "s/&\;/\& /g"
}
get_abbreviations () {
ABBRS_URL="https://www.abbreviations.com/$1"
USERAGENT='Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:92.0) Gecko/20100101 Firefox/92.0'
ABBREVIATIONS=$(
echo -n "$(curl -s -A "$USERAGENT" $ABBRS_URL)" |\
sed -E "/<table class=\"table tdata no-margin\">.+<\/table>/ !d" |\
perl -pe "s/(<td class=\"tal dx fsl\">.+?<\/td>)/\n\$1\n/g" |\
sed -E "/^<td class=\"tal dx fsl\">/ !d;\
s/<td class=\"tal dx fsl\"><p class=\"desc\">(.*)<\/p><p class=\"path\">(.*)<\/p><\/td>/[\2] \1/;\
s/<\/{0,1}a[^>]*>//g;\
s/»\;/»/g;\
s/ -- and more...//g"
)
ABBREVIATIONS=$(html_entities_to_ascii "$ABBREVIATIONS")
echo "$ABBREVIATIONS"
}
get_antonyms () {
WORD="$1"
spell $WORD 0
ANTONYMS_URL="https://words.bighugelabs.com/$WORD"
ANTONYMS_RES="$(curl -s $ANTONYMS_URL)"
ANTONYMS=""
declare -a ANT_TYPES=("noun" "verb" "adjective" "adverb")
for ANT_TYPE in "${ANT_TYPES[@]}"; do
ANTONYMS+=$(
cat << EOF
$(echo -n "$ANTONYMS_RES" |\
sed -E "/<h3>$ANT_TYPE<\/h3>/,/(<h3>)|(<\/div>)/ !d;\
/<h4>antonyms<\/h4>/,/<\/ul>/ !d;\
/<li>/ !d;\
s/\s*<li><a.*>(.*)<\/a><\/li>/($ANT_TYPE) \1/g"
)
EOF
)
done
ANTONYMS=$(echo -n "$ANTONYMS" | sed '1 d')
echo "$ANTONYMS"
}
define () {
spell $1 0
WORDNIK_URL="https://www.wordnik.com/words/$WORD"
USERAGENT='Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:92.0) Gecko/20100101 Firefox/92.0'
DEFINITIONS=$(
echo -n "$(curl -s -A "$USERAGENT" $WORDNIK_URL)" |\
sed "/guts active/,/<\/ul>/!d;\
/<li>/!d" |\
sed -E "s/\s*<li><abbr title=\"partOfSpeech\">(.+)<\/abbr> <i>(.*)<\/i> (.*)<\/li>/(\1) [\2] \3/;\
s/ \[\]//g;\
s/\s{2,}/ /g;\
s/<[^\;][^>]*>//g;"
)
DEFINITIONS=$(html_entities_to_ascii "$DEFINITIONS")
echo "$DEFINITIONS"
}
pronounce () {
WORD="$1"
SOURCE="$2"
spell $WORD 0
SOUND_URL=""
if [[ "$SOURCE" == "macmillan" ]]; then
USERAGENT='Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:92.0) Gecko/20100101 Firefox/92.0'
SOUND_URL=$(
curl -s -A "$USERAGENT" "https://www.wordnik.com/fragments/sounds/$WORD" | \
tac | \
sed -n '/macmillandictionary/, /play_sound/ p' | \
tail -n 1 | \
sed -E 's/.*doc-fileUrl="([^"]+)".*/\1/; s/&\;/\&/g'
)
else
SOUND_URL=$(
curl -s "https://api.dictionaryapi.dev/api/v2/entries/en/$WORD" | \
grep -oE '"audio":"[^"]+"' | \
cut -d ':' -f 2- | \
sort | \
uniq | \
sed -E 's/^"|"$//g' | \
grep "$WORD-$SOURCE\.mp3"
)
if [[ -z "$SOUND_URL" ]]; then
echo "Invalid source. See available ones with -lst|--list-sources."
exit 1
fi
fi
play -q -t mp3 "$SOUND_URL"
}
list_pronunciation_sources () {
WORD="$1"
spell $WORD 0
DICTAPI_SOURCES=$(
curl -s "https://api.dictionaryapi.dev/api/v2/entries/en/$WORD" | \
grep -oE '"audio":"[^"]+"' | \
cut -d ':' -f 2- | \
sed -E "s/.*$WORD-(.+)\.mp3\"$/\1/" | \
sort | \
uniq
)
echo "- macmillan (default)"
echo "$DICTAPI_SOURCES" | sed 's/^/- /'
}
spell () {
agrep -iyB "$1" /usr/share/wordlists/et_wordlist.txt | grep -iw "$1" > /dev/null
FOUND=$?
PROCEED_TO_DEFINE=$2
if [ $FOUND -eq 0 ]; then
if [ $PROCEED_TO_DEFINE -ne 0 ]; then
echo "\"$1\" appears to be spelled correctly"
fi
else
SUGGESTIONS=$(agrep -iyB "$1" /usr/share/wordlists/et_wordlist.txt | awk '{print length, $0}' | sort -n | cut -d " " -f2-)
echo "$SUGGESTIONS"
exit 1
fi
}
get_synonyms () {
WORD="$1"
spell $WORD 0
SYNONYMS_URL="https://words.bighugelabs.com/$WORD"
SYNONYMS_RES="$(curl -s $SYNONYMS_URL)"
SYNONYMS=""
declare -a SYN_TYPES=("noun" "verb" "adjective" "adverb")
for SYN_TYPE in "${SYN_TYPES[@]}"; do
SYNONYMS+=$(
cat << EOF
$(echo -n "$SYNONYMS_RES" |\
sed -E "/<h3>$SYN_TYPE<\/h3>/,/<\/ul>/ !d;\
/<li>/ !d;\
s/\s*<li><a.*>(.*)<\/a><\/li>/($SYN_TYPE) \1/g"
)
EOF
)
done
SYNONYMS=$(echo -n "$SYNONYMS" | sed '1 d')
echo "$SYNONYMS"
}
usage () {
cat << EOF
Usage: et OPTION...
Options:
-abr, --abbreviations <abr>
Print what <abr> might stand for from abbreviations.com.
-ant, --antonyms <word>
Print antonyms for <word> from bighugelabs.com.
-def, --define <word>
Print definitions for <word> from wordnik.com if correctly spelled, otherwise print spell suggestions.
-pro, --pronounce <word>
Play pronunciation of <word>.
Defaults to American English pronunciation (listed as macmillan in sources list) from wordnik.com (which in turn depends on macmillandictionary). Other pronunciation sources/accents (from dictionaryapi.dev) can be listed with -lst|--list-sources and specified with -src|--source (must come after -pro|--pronounce).
-lst, --list-sources <word>
List available pronunciation sources/accents for <word>.
-src, --source <source>
Specify the <source> to use for pronunciation. This option must come after -pro|--pronounce.
-spl, --spell <word>
Print spell suggestions for <word> from wordlist if not spelled correctly (exits with 1 as exit code), otherwise print a message indicating that <word> is spelled correctly.
-syn, --synonyms <word>
Print synonyms for <word> from bighugelabs.com.
-h, --help
Print this help message and exit
EOF
}
if [[ "$@" ]]; then
OPTION=$(echo "$@" | cut -d " " -f 1)
WORD=$(echo "$@" | cut -d " " -f 2)
if [ "$OPTION" == "-h" ] || [ "$OPTION" == "--help" ]; then
usage
else
SPACE_COUNT=$(echo "$@" | tr -cd " \t" | wc -c)
if [ "$WORD" == "" ] || [ $SPACE_COUNT -lt 1 ]; then
echo "You need to specifiy an option and a word"
exit 1
else
WORD=$(echo "$WORD" | awk '{print tolower($0)}')
if [ "$OPTION" == "-def" ] || [ "$OPTION" == "--define" ]; then
define $WORD
elif [ "$OPTION" == "-spl" ] || [ "$OPTION" == "--spell" ]; then
spell $WORD 1
elif [ "$OPTION" == "-abr" ] || [ "$OPTION" == "--abbreviations" ]; then
get_abbreviations $WORD
elif [ "$OPTION" == "-syn" ] || [ "$OPTION" == "--synonyms" ]; then
get_synonyms $WORD
elif [ "$OPTION" == "-ant" ] || [ "$OPTION" == "--antonyms" ]; then
get_antonyms $WORD
elif [ "$OPTION" == "-pro" ] || [ "$OPTION" == "--pronounce" ]; then
SOURCE="macmillan"
OPTION="$(echo "$@" | cut -d ' ' -f 3)"
if [[ "$OPTION" == "-src" ]] || [[ "$OPTION" == "--source" ]]; then
SOURCE="$(echo "$@" | cut -d ' ' -f 4 | awk '{print tolower($0)}')"
fi
pronounce "$WORD" "$SOURCE"
elif [ "$OPTION" == "-lst" ] || [ "$OPTION" == "--list-sources" ]; then
list_pronunciation_sources $WORD
else
echo "Unkown option \"$OPTION\""
exit 1
fi
fi
fi
else
usage
fi