#!/bin/sh # title: soz # license: CC0 # author: Sunur Efe Vural # version: Dec 23, 2018 # dependencies: sdcv (>=0.5.2), w3m, fold, jq, hexdump, cat... # Wrapper dictionary script for stardict (using sdcv) and web # dictionaries (using w3m). Add dictionaries to the case statement. defineWord() { if [ -z "$1" ] ; then printf "Enter word or phrase: " read -r word else word="$1" fi } interactiveMode() { [ -z "$1" ] && $0 "$dict" } queryDict() { defineWord "$2" word=$(echo "$word" | hexdump -v -e '/1 " %02x"' | sed 's/ 0a$//') site=$(echo "$1" | sed "s/\%s/$word/;y/ /%/") w3m -o display_link_number=0 "$site" | cat interactiveMode "$2" } sdcvDict() { defineWord "$2" sdcv -n -j -u "$1" "$word" | jq ' . | if length == 0 then "-->'"$1"'","There is no word similar to «'"$word"'»" elif .[0]["word"] == "'"$word"'" and length == 1 then "-->'"$1"'",.[]["word","definition"] else "-->'"$1"'", "There is no exact match for «'"$word"'». Did you mean", .[]["word"] end ' | sed -E ' s/^\"|\"$//g 1s/$/\n/ 2s/$/:/ $!{3,$s/$/,/} s/^\\n// s/\\n/\n\n/g s/\\t/\t/g ' | fold -w 72 -s interactiveMode "$2" } dict=$1 [ -z "$1" ] || shift case $dict in azen) queryDict "https://azerdict.com/az/english/%s" "$*" ;; tren) queryDict "http://www.zargan.com/tr/q/%s-ceviri-nedir" "$*" ;; en) sdcvDict "WordNet® 3.0 (En-En)" "$*" ;; fren) sdcvDict "French - English" "$*" ;; ruen) sdcvDict "Russian-English full dictionary" "$*" ;; *|-h) printf "No dictionary as %s found.\nusage: soz dictname [search term]\n" "$dict" ;; esac