-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdict
More file actions
168 lines (148 loc) · 5.43 KB
/
dict
File metadata and controls
168 lines (148 loc) · 5.43 KB
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
#!/bin/bash
#
# Access dict protocol from the command line using curl
# and aspell via single interface
#
prog=${0##*/}
print_help(){
echo "
$prog [ -c [-l <language|dictionary>] -L -s -t ] \"<words|file>\" [\"<aspell options|curl options>\"]
All we can do with words:
- Spell check a file or words: needs aspell and local language dictionaries to be be installed.
- Lookup the meaning of words, default is to lookup English words in the gcide dictionary.
- Translate words into another language.
Lookup and translate need curl to be installed
Be aware spell check is done against local installed databases, to see them use \"-c -L\"
Lookups and translate are done against DICT network databases, to see them use \"-L\" without \"-c\"
options:
-c Check spelling of \"<words>\" using aspell. Be aware quotes are needed to check multiple words.
-L List local languages for aspell or network <dictionaries> for curl.
The network <dictionaries> can also be groups of dictionaries like \"all\" or \"trans\"
-l <language|dictionary>
The language or dictionary to use.
Use: \"export DICT_DICTIONARY=<dictionary>\" to set a session default.
Use: \"export DICT_DICTIONARY_TRANS=<dictionary>\" to set a session default for a translation, option -t.
Use: \"export DICT_DICTIONARY_CHECK=<dictionary>\" to set a session default for a spell checking, option -c.
-s Search the index of the \"english\" dictionary group or a specific dictionary using option -l <dictionary>
for the presence of a word.
Be aware search doesn't always return matches as it depends on the quality of the dictionary index.
So the lookup (without the -s option) might still work. Or make a spelling mistake to find
words that will lookup...
-t Search for all translations of a word if no <dictionary> is added, in other
words use dictionary group \"trans\". In stead you can use option -l <dictionary> to
translate the word against a dictionary or group of dictionaries.
If option -s is also present, search for the word. This will spit out a list of possible words
examples:
Passing a aspell option to a spell check of a html file:
dict -c c:/temp/measure_pu.html "--mode=html"
Lookup meaning of a word:
dict \"dragonfly\"
Translate it using all \"trans\" dictionaries available:
dict -t \"dragonfly\"
Translate 2 dutch words to english:
dict -l fd-nld-eng \"voet knie\"
Lookup alike dutch words:
dict -s -l fd-nld-eng \"voet knie\"
"
}
dict(){
#
# do a curl dict command
lLang="$1"
lWords="$2"
lOpts="$3"
lDict=""
curl=$(which curl 2>/dev/null)
[[ -z "$curl" ]] && echo "FATAL: curl wasn't found please install first" && exit 1
#
# reduce curl output, but not if a curl options is passed
redirect="/dev/null"
reduceCmd="grep -v -e '^250 ok' -e '^221 bye' -e '^220 dict\.dict\.org'"
[[ -n "$lOpts" ]] && redirect="/dev/stdout" reduceCmd="cat"
if [[ $listDict -eq 1 ]]; then
#
# list available dictionaries
$curl dict://dict.org/"SHOW:DB" 2>$redirect | eval $reduceCmd
else
#
# set command
lCmd="d" # show meaning or translate
[[ $search -eq 1 ]] && lCmd="m" # match (search) each dictionary for word
#
# set dictionary
[[ -n "$lLang" ]] && lDict=":$lLang"
for lWord in $lWords; do
#
# look for lWord
[[ $debug -gt 0 ]] && echo Doing: \"$lWord\"
$curl $lOpts dict://dict.org/$lCmd:"$lWord""$lDict" 2>$redirect | eval $reduceCmd
done
fi
}
spell(){
#
# spell check a word or a sentence
lLang="$1"
lWords="$2"
lOpts="$3"
aspell=$(which aspell 2>/dev/null)
[[ -z "$aspell" ]] && echo "FATAL: aspell wasn't found please install first" && exit 1
#
# special dictionary (language)
[[ -n "$lLang" ]] && lOpts="$lOpts --lang $lLang"
#
# is input a file with content
lCmd="echo"
[[ -s "$lWords" ]] && lCmd="cat"
if [[ $listDict -eq 1 ]]; then
#
# list available dictionaries
$aspell dump dicts
else
#
# do a spell check
$lCmd $lWords | eval $aspell -a $lOpts
fi
}
typeset -i debug=0 check=0 translate=0 search=0 listDict=0
while getopts "h\?DLcl:stv" s
do
case $s in
c) check=1
;;
D) debug=debug+1
;;
L) listDict=1
;;
l) dictionary=$OPTARG
;;
s) search=1
;;
t) translate=1
;;
v) set -x
;;
h|*) showHelp=1
;;
esac
done
shift $(($OPTIND - 1))
words="$1"
shift
[[ $showHelp -eq 1 ]] && print_help && exit
#
# set defaults dictionary
[[ -n "DICT_DICTIONARY" && -z "$dictionary" ]] && dictionary="$DICT_DICTIONARY"
[[ -n "DICT_DICTIONARY_TRANS" && $translate -eq 1 && -z "$dictionary" ]] && dictionary="$DICT_DICTIONARY_TRANS"
[[ -n "DICT_DICTIONARY_CHECK" && $check -eq 1 && -z "$dictionary" ]] && dictionary="$DICT_DICTIONARY_CHECK"
[[ $translate -eq 1 && -z "$dictionary" ]] && dictionary="trans"
[[ $check -eq 0 && -z "$dictionary" ]] && dictionary="english"
#
# check spelling
if [[ $check -eq 1 ]];then
spell "$dictionary" "$words" "$*"
exit $?
fi
#
# lookup using curl and dict urls
dict "$dictionary" "$words" "$*"