summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorars <asav1410@gmail.com>2020-10-18 16:08:24 +0100
committerars <asav1410@gmail.com>2020-10-18 16:08:24 +0100
commit6c7a1a705cebf56d0b3a69e94cdb8cf2d3fc6182 (patch)
tree02ccb708292835a29412a33cd7860a326141f2c1
parenteb4becd0407063213b2394df490014c6074d609b (diff)
Add etymology, thesaurus, help, source features. Refactor existing code.
-rwxr-xr-xoeddefine.sh118
1 files changed, 92 insertions, 26 deletions
diff --git a/oeddefine.sh b/oeddefine.sh
index 46d06a3..aa76bde 100755
--- a/oeddefine.sh
+++ b/oeddefine.sh
@@ -5,32 +5,98 @@ word=$(echo $word | tr '[:upper:]' '[:lower:]')
appId=
key=
language=en-gb
-lemmasUrl=https://od-api.oxforddictionaries.com/api/v2/lemmas/$language/$word
OPTS='--silent -L -H "app_id: $appId" -H "app_key: $key"'
+maxSynonyms=10
-# 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
-headWord=$(echo $result | jq '.results[0].lexicalEntries[0].inflectionOf[0].id')
-if [ "$headWord" == "null" ]
-then
- echo "Word not found."
- exit
-fi
-entriesUrl=https://od-api.oxforddictionaries.com/api/v2/entries/$language/$headWord
-
-# get definition of headword
-result=$(eval curl $OPTS $entriesUrl)
-ret=$?
-if [ "$ret" -ne 0 ]
-then
- echo "cURL error $ret when fetching."
- exit
+_getdata() {
+ # Take everything after final space as the requested word (assuming everything before the last space is a flag)
+ word=$(echo $word | sed 's/.* //')
+ 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
+}
+
+# function to provide data to a for loop
+_jq() {
+ echo ${json} | base64 --decode | jq -r ${1}
+}
+
+# Return definition for the first sense of each lexical entry and create output string
+_define() {
+ _getdata
+ 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]'). "
+ output="$output \"$(_jq '.entries[0].senses[0].examples[0].text')\""
+ done
+ echo $output
+ exit 0
+}
+
+# Return etymology if available
+_etym() {
+ _getdata
+ output=$(echo "${result}" | jq '.results[].lexicalEntries[0].Entries[0].etymologies[0]')
+ if [ -z "$output"]; 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
+ echo $output
+ exit 0
+}
+
+_help() {
+ echo "Usage: !oeddefine [option] [word]. Options: -h help -t thesaurus -e etymology -d define -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} == *"-e"* ]]; then
+ _etym
+else
+ _define
fi
-definition=$(echo $result | jq '.results[0].lexicalEntries[0].entries[].senses[0].definitions')
-echo $definition