From 52f1f2744d2af3bc2e7fc95552d4da512c4ad2aa Mon Sep 17 00:00:00 2001 From: ars Date: Wed, 28 Oct 2020 20:12:05 +0000 Subject: Move thesaurus lookup to a function in order to attempt multiple lexical categories if no synonyms are found. Limit cURL attempts to 2 (if there's more than 2 then something is wrong). --- oedquery.sh | 47 +++++++++++++++++++++++++++-------------------- 1 file changed, 27 insertions(+), 20 deletions(-) diff --git a/oedquery.sh b/oedquery.sh index bb85570..3e82ba8 100755 --- a/oedquery.sh +++ b/oedquery.sh @@ -9,7 +9,7 @@ OPTS='--silent -L -H "app_id: $appId" -H "app_key: $appKey"' curlCount=0 _curl() { - if [[ $curlCount -ge 20 ]] + if [[ $curlCount -ge 2 ]] then echo "cURL loop, exiting." exit 0 @@ -97,30 +97,37 @@ _etym() { exit 0 } -# Return synonyms of first sense of first lexical entry +# Return synonyms of first sense of first lexical entry (or second if no synonyms for the first) _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 - + # Loop to get synonyms for the first sense of a given lexical entry + _thesLoop () { + count=0 + for json in $(echo "${result}" | jq -r '.results[].lexicalEntries['$1'].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 + } + # Get synonyms for first lexical entry and try the second if there aren't any + _thesLoop 0 if [[ -z $output ]] then - echo "No synonyms found." - exit 0 - else - # remove trailing comma - output=$(echo $output | sed '$s/,$//') - echo $output - exit 0 + _thesLoop 1 + if [[ -z $output ]] + then + echo "No synonyms found." + exit 0 + fi fi + # remove trailing comma + output=$(echo $output | sed '$s/,$//') + echo $output + exit 0 } # Get URL for first audio file in returned data -- cgit v1.2.3