-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathleoDict.sh
executable file
·69 lines (59 loc) · 1.81 KB
/
leoDict.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#!/bin/bash
if ! [ -x "$(command -v leo)" ]; then
sudo apt install -y libwww-dict-leo-org-perl
fi
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
LEODB="$DIR/leoCache.db"
if ! [ -f "$LEODB" ]; then
CREATE_TABLE="CREATE TABLE QUERY_CACHE ( word TEXT NOT NULL, response TEXT )"
sqlite3 "$LEODB" "$CREATE_TABLE"
fi
function search()
{
WORD=$1;
BOOKLANG="en";
if [ ! -z "$2" ]; then
BOOKLANG="${2}"
fi
CACHE_QUERY="SELECT response FROM query_cache c WHERE c.word=='${WORD}'"
RESPONSE=$(sqlite3 "$LEODB" "$CACHE_QUERY")
if [[ $RESPONSE != *"matches for"* ]]; then
#WORD_ENCODED=$(urlEncode "$WORD")
RESPONSE=$(leo -l ${BOOKLANG}2de -n "${WORD}" | sed -e 's|[\”\“]|\"|g' | iconv -f ISO-8859-1 -t utf-8 )
if [[ $RESPONSE == *"matches for"* ]]; then
WRITE_QUERY="INSERT INTO QUERY_CACHE (word,response) VALUES ('${WORD}',\"${RESPONSE}\");"
sqlite3 "$LEODB" "$WRITE_QUERY"
fi
fi
echo "${RESPONSE}"
}
function urlEncode()
{
WORD=$1;
python3 -c 'import urllib.parse, sys; print(urllib.parse.quote_plus(sys.argv[1]))' "${WORD}"
}
function cleanSearch()
{ # reduce output: only actual translation lines
search ${1} ${2} | \
sed -e 's|.*dict\.leo\.org:||g' | \
sed -e 's|^ .*||' | \
sed -E 's|^ (.*)| \1|g' | \
grep -Eo --null ' .*'
}
function resultSelect()
{
word="$1"
trans=$(( cleanSearch $1 ) 2>&1)
readarray -t lines <<< "$trans" # linewise to array
checks=( )
for line in "${lines[@]}"; do
checks+=("${line:1} " 0) # add state flag
done
# old school dialog:
lineCount=${#lines[@]}
let "height=8+$lineCount"
width=80
selected=$(whiptail --title "Best translation?" --backtitle "by leo.org" --noitem \
--checklist "results for: $word" $height $width $lineCount "${checks[@]}" 3>&1 1>&2 2>&3)
echo $selected
}