happybot/happybot/define.sh

42 lines
1.3 KiB
Bash
Executable File

#!/usr/bin/env ash
. /home/zgrep/offtopiabday/happybot/common.sh
irc | while read -r n m; do
if hreg '^happybot[:,] define( -w)?( \S+)? (\S+)$' "$m"; then
lang="$(m 2)"
word="$(m 3)"
wikt="$(m 1)"
elif reg '^#define( -w)?( \S+)? (\S+)$' "$m"; then
lang="$(m 2)"
word="$(m 3)"
wikt="$(m 1)"
else
continue;
fi
if [ -z "$lang" ]; then
lang="en"
fi
if [ -z "$wikt" ]; then
result="$(curl -s -g -G 'https://api.dictionaryapi.dev/api/v2/entries/'"$lang"'/'"$word")"
else
result="{}"
fi
if [ "$(var "$result" | jq -r 'type')" == "object" ]; then
result="$(python3 wiktionary/wiktionary.py "$lang" "$word")"
var "$result" | if ! grep -q '^\['; then
var "$result" | zwsp | say;
else
phonetics="$(var "$result" | jq -r '.[0]|unique|join(" ")')"
definitions="$(var "$result" | jq -r '.[1]')"
extra="$(var "$result" | jq -r '.[2]')"
python3 happybot/def-limit.py "$phonetics" "$definitions" "$extra" | zwsp | say
fi
else
phonetics="$(var "$result" | jq -r 'map(.phonetics | map(.text)) | flatten | unique | join(" ")')"
definitions="$(var "$result" | jq -r 'map(.meanings | map( {(.partOfSpeech): (.definitions|map(.definition))} )) | flatten')"
extra=""
python3 happybot/def-limit.py "$phonetics" "$definitions" "$extra" | zwsp | say
fi
done