blob: 239d2cf917510fa49d3e2aad34c9716878381bcb (
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
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
|
#!/bin/bash
read word
word=$(echo $word | tr '[:upper:]' '[:lower:]')
# Create array of variable to read from config file
config=(
appId
appKey
language
maxSynonyms
)
# curl options
OPTS='--silent -L -H "app_id: $appId" -H "app_key: $appKey"'
source oedquery.conf
# function to provide data to a for loop
_jq() {
echo ${json} | base64 --decode | jq -r ${1}
}
_getdata() {
# Take everything after final space as the requested word (assuming everything before the last space is a flag)
word=$(echo $word | sed 's/.* //')
if [[ "$word" = "null" ]]
then
echo "Missing word. Usage: !oed [option] [word]. Use -h for help."
fi
lemmasUrl=https://od-api.oxforddictionaries.com/api/v2/lemmas/$language/$word
# get lemmas to link an inflected form back to its headword (required to get definition)
result=$(eval curl $OPTS $lemmasUrl)
ret=$?
if [ "$ret" -ne 0 ]
then
echo "cURL error $ret when fetching."
exit
fi
# get definition of headword if it exists
headWord=$(echo $result | jq '.results[0].lexicalEntries[0].inflectionOf[0].id')
if [ "$headWord" == "null" ]
then
echo "Word not found. Use -h for help."
exit
fi
entriesUrl=https://od-api.oxforddictionaries.com/api/v2/entries/$language/$headWord
result=$(eval curl $OPTS $entriesUrl)
ret=$?
if [ "$ret" -ne 0 ]
then
echo "cURL error $ret when fetching."
exit
fi
}
# Return definition for the first sense of each lexical entry and create output string
_define() {
_getdata
if [[ $wod == true ]]
then
output="$output $word - "
fi
for json in $(echo "${result}" | jq -r '.results[].lexicalEntries[] | @base64')
do
output="$output $(_jq '.lexicalCategory.text'): "
output="$output $(_jq '.entries[0].senses[0].definitions[0]') "
examplePhrase=$(_jq '.entries[0].senses[0].examples[0].text')
if [[ "$examplePhrase" != "null" ]]
then
output=$output" \"$examplePhrase\""
fi
done
echo $output
exit 0
}
# Return etymology if available
_etym() {
_getdata
output=$(echo "${result}" | jq '.results[].lexicalEntries[0].entries[0].etymologies[0]' | tr -d \")
if [[ "$output" = "null" ]]
then
echo "No etymology found."
else
echo $output
fi
exit 0
}
# Return synonyms of first sense of first lexical entry
_thes() {
_getdata
count=0
for json in $(echo "${result}" | jq -r '.results[].lexicalEntries[0].entries[0].senses[0].synonyms[]? | @base64'); do
output="$output $(_jq '.text'), "
((count=count+1))
# Limit output to a resonable number of synonyms
if [[ $count -ge $maxSynonyms ]]
then
break
fi
done
if [[ -z $output ]]
then
echo "No synonyms found."
exit 0
else
# remove trailing comma
output=$(echo $output | sed '$s/,$//')
echo $output
exit 0
fi
}
# Get URL for first audio file in returned data
_audio() {
_getdata
output=$(echo "${result}" | jq '.results[0].lexicalEntries[0].entries[0].pronunciations[0].audioFile' | tr -d \")
if [[ "$output" != "null" ]]
then
echo $output
else
echo "No audio file found."
fi
exit 0
}
# Return first defintion with lexical category for Lexico's word of the day.
_wod() {
wod=true
# Get word from lexico
word=$( curl --silent https://www.lexico.com/ | hxnormalize -x | hxselect -i "a.linkword" | grep -o '>.*<' | sed 's/[><]//g')
_getdata
_define
}
_help() {
echo "Usage: !oed [option] [word]. Options: -h help, -t thesaurus, -e etymology, -d define, -a audio, -wod word of the day, -s source."
exit 0
}
if [[ ${word} == "-h" ]] || [[ ${word} == "--help" ]] || [[ -z $word ]]
then
_help
elif [[ ${word} == "--source" ]] || [[ ${word} == "-s" ]]
then
echo "https://www.blatech.co.uk/ars/oeddefine"
exit 0
elif [[ ${word} =~ "-t" ]]
then
_thes
elif [[ ${word} =~ "-et" ]]
then
_etym
elif [[ ${word} =~ "-a" ]]
then
_audio
elif [[ ${word} =~ "-wod" ]]
then
_wod
else
_define
fi
|