-
Notifications
You must be signed in to change notification settings - Fork 65
/
translate.sh
executable file
·72 lines (61 loc) · 2.15 KB
/
translate.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
70
71
72
#!/usr/bin/env bash
# Author: LKJ
# Email : [email protected]
# Date : 2013.5.14
#
################################################
# 实现从dict.baidu.com接口获取网络释义
# 实现英译汉,汉译英
# 用法:
# 汉译英:./translate -c <词语>
# ./translate -c 从标准输入翻译
# 英译汉:./translate <单词>
# ./translate 从标准输入翻译
###############################################
ENtoCN() {
#sed -i 's/<div/\n<div/g' /tmp/tempword
echo "简明释义>>"
cat /tmp/tempword | grep "dict-en-simplemeans-english" | sed 's/<strong>/\n/g' | \
sed 's/^\(.*\)<\/strong><span>\(.*\)<\/span>.*/\1 \2/g' | sed 's/<\/span>.*//g' | sed 's/.*<div.*//g';
echo;
echo "网络释义>>"
cat /tmp/tempword | grep "dict-en-netmeans-english" | sed 's/<p>/\n/g' | \
sed 's/^\(.*\)<\/p>.*/\1/g' | sed 's/<div.*//g';
echo;
echo "-----------------------------------------------------"
}
CNtoEN() {
#sed -i 's/<div/\n<div/g' /tmp/tempword
echo "简明释义>>"
cat /tmp/tempword | grep "dict-en-simplemeans" | sed 's/\/s?wd=/\n/g' | \
sed 's/^\(.*\)">.*/\1/g' | sed 's/.*<div.*//g';
echo;
echo "网络释义>>"
cat /tmp/tempword | grep "dict-en-netmeans" | sed 's/.*dict-en-netmeans/<div/g' | sed 's/<p>/\n/g' | \
sed 's/^\(.*\)<\/p>.*/\1/g' | sed 's/<div.*//g';
echo;
echo "-----------------------------------------------------"
}
Haveword() {
while [[ $# -ne 0 ]]; do
echo "======================== $1 ===========================";
wget -q "dict.baidu.com/s?wd=$1" -O /tmp/tempword
((entocn==1)) && ENtoCN;
((entocn==0)) && CNtoEN;
shift;
done
}
Noword() {
while read word; do
echo "======================== $word ===========================";
wget -q "dict.baidu.com/s?wd=$word" -O /tmp/tempword;
((entocn==1)) && ENtoCN;
((entocn==0)) && CNtoEN;
done #< <(grep -v "'" /usr/share/dict/words)
}
entocn=1;
[[ $1 = "-c" ]] && ((entocn=0))
((entocn==0)) && shift;
[[ $# -eq 0 ]] && Noword;
[[ $# -ne 0 ]] && Haveword "$@";
[[ -e /tmp/tempword ]] && rm /tmp/tempword;