From ff07143deca6191211dac4a1b3ba8f2e448dc73c Mon Sep 17 00:00:00 2001 From: Asa Venton Date: Mon, 19 Oct 2020 12:54:36 +0100 Subject: Implement Lexico's word of the day as a flag. Move functions into a more sensible order. Fix "null" being returned when an example phrase isn't available. Make code styling consistent. --- oeddefine.sh | 66 ++++++++++++++++++++++++++++++++++++++++++++++-------------- 1 file changed, 51 insertions(+), 15 deletions(-) (limited to 'oeddefine.sh') diff --git a/oeddefine.sh b/oeddefine.sh index 0208723..6bdad65 100755 --- a/oeddefine.sh +++ b/oeddefine.sh @@ -8,6 +8,11 @@ language=en-gb OPTS='--silent -L -H "app_id: $appId" -H "app_key: $appKey"' maxSynonyms=10 +# 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/.* //') @@ -38,18 +43,18 @@ _getdata() { 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 + 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')\"" + 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 @@ -59,7 +64,8 @@ _define() { _etym() { _getdata output=$(echo "${result}" | jq '.results[].lexicalEntries[0].entries[0].etymologies[0]' | tr -d \") - if [ -z "$output" ]; then + if [ -z "$output" ] + then echo "No etymology found." else echo $output @@ -75,7 +81,8 @@ _thes() { output="$output $(_jq '.text') " ((count=count+1)) # Limit output to a resonable number of synonyms - if [[ $count -ge $maxSynonyms ]]; then + if [[ $count -ge $maxSynonyms ]] + then break fi done @@ -91,22 +98,51 @@ _audio() { exit 0 } +# Return first defintion with lexical category for Lexico's word of the day. +_wod() { + # Get word from lexico + word=$( curl --silent https://www.lexico.com/ | hxnormalize -x | hxselect -i "a.linkword" | grep -o '>.*<' | sed 's/[><]//g') + _getdata + definition=$(echo $result | jq '.results[].lexicalEntries[].entries[0].senses[0].definitions[]') + lexicalCategory=$(echo $result | jq '.results[].lexicalEntries[0].lexicalCategory.text') + examplePhrase=$(echo $result | jq '.results[].lexicalEntries[0].entries[0].senses[0].examples[0].text') + # Remove surrounding quotes + definition=$(echo "${definition:1: -1}") + lexicalCategory=$(echo "${lexicalCategory:1: -1}") + if [[ "$examplePhrase" != "null" ]] + then + output=$word" - "$lexicalCategory" - "$definition" - "$examplePhrase + else + output=$word" - "$lexicalCategory" - "$definition + fi + echo $output + exit 0 +} + _help() { echo "Usage: !oeddefine [option] [word]. Options: -h help -t thesaurus -e etymology -d define -a audio -s source" exit 0 } -if [[ ${word} == "-h" ]] || [[ ${word} == "--help" ]] || [[ -z $word ]]; then +if [[ ${word} == "-h" ]] || [[ ${word} == "--help" ]] || [[ -z $word ]] +then _help -elif [[ ${word} == "--source" ]] || [[ ${word} == "-s" ]]; then +elif [[ ${word} == "--source" ]] || [[ ${word} == "-s" ]] +then echo "https://www.blatech.co.uk/ars/oeddefine" exit 0 -elif [[ ${word} =~ "-t" ]]; then +elif [[ ${word} =~ "-t" ]] +then _thes -elif [[ ${word} =~ "-et" ]]; then +elif [[ ${word} =~ "-et" ]] +then _etym -elif [[ ${word} =~ "-a" ]]; then +elif [[ ${word} =~ "-a" ]] +then _audio +elif [[ ${word} =~ "-d" ]] +then + _wod else _define fi -- cgit v1.2.3