终端下的英汉字典

仅仅用于软件推荐,不适合发求软件或软件使用问题方面的贴子
头像
steven0lisa
帖子: 68
注册时间: 2008-01-31 11:57

终端下的英汉字典

#1

帖子 steven0lisa » 2010-12-17 13:12

这个是我编写的一个基于shell的英汉网络字典,网络源是来自baidu的(没办法,我试过iciba,可惜速度和简洁程度比不上百度,罪过罪过)

感觉还是很好用,不用再开启星际或词霸了,速度快好多。

注:这里分成两个脚本,一个循环查询的,适合大家在读英文的时候时不时查下单词用,叫idic
另一个是只查询一次就退出的,适合临时查下单词,叫dic

大家如果有什么不爽的地方,既可以自己改,也可以在issue里面直接留言。

希望大家能喜欢,这个东西也能帮助大家。 :em02

这个是项目地址:http://code.google.com/p/dictionary4linux/

由于google code有时候会被重set,所以下面我就直接贴出代码:
利用notify-send输出结果

代码: 全选

#!/bin/bash
ARGS=1
E_BADARGS=65

#prepare the cache dir to save the translate offline
if [ ! -e ~/dictionary ];
then
echo "make dir...."
mkdir ~/dictionary
fi

#check the arg that user inputed
if [ $# -ne "$ARGS" ];
then
echo "Usage:`basename $0` word"
exit $E_BADARGS
fi

if [ ! -f ~/dictionary/$1 ];
then
#read the translate from dict of baidu.com
#PS: Why to use Baidu? The answer is simple, fast.
w3m -no-cookie -dump 'http://dict.baidu.com/s?wd='$1'&f=3'  \
						| sed '/以下结果来自互联网网络释义/,$d'| sed '1,15d' | tac \
									| sed '1,2d' | tac |sed -r '/^[0-9]+\./N;s/\n//' > ~/dictionary/$1
fi

msg=`cat ~/dictionary/$1`
if [ "$msg" = "" ];
then
	msg="您所查询的$1无法找到解释。"
fi
notify-send "$1" "$msg" >/dev/null 2>&1
if [ $? -eq 0 ];
then
	exit 0
else

	echo
	echo -e "--------------------\033[1;40;33m $1 \033[0m--------------------"
	echo "$msg"
	exit 0
fi


直接在终端输出结果

代码: 全选

#!/bin/bash
ARGS=1
E_BADARGS=65

#prepare the cache dir to save the translate offline
if [ ! -e ~/dictionary ];
then
echo "make dir...."
mkdir ~/dictionary
fi

#check the arg that user inputed
if [ $# -ne "$ARGS" ];
then
echo "Usage:`basename $0` word"
exit $E_BADARGS
fi

if [ ! -f ~/dictionary/$1 ];
then
#read the translate from dict of baidu.com
#PS: Why to use Baidu? The answer is simple, fast.
w3m -no-cookie -dump 'http://dict.baidu.com/s?wd='$1'&f=3'  \
						| sed '/以下结果来自互联网网络释义/,$d'| sed '1,15d' | tac \
									| sed '1,2d' | tac |sed -r '/^[0-9]+\./N;s/\n//' > ~/dictionary/$1
fi

msg=`cat ~/dictionary/$1`
if [ "$msg" = "" ];
then
	msg="您所查询的$1无法找到解释。"
fi

echo
echo -e "--------------------\033[1;40;33m $1 \033[0m--------------------"
echo "$msg"
exit 0
								

循环查询单词

代码: 全选


#!/bin/sh
if [ ! -e ~/dictionary ];
then
mkdir ~/dictionary
fi

clear

echo -e "\033[1;32;33mWelcome to use online dictionary!\033[0m"
while [ 1 = 1 ]
do
echo -en "\033[0;32;33mPlease enter the word(q:quit):\033[0m"
read word
if [ "$word" = "q" ]
then
	echo "Bye"
	exit 0
fi

if [ -z "$word" ]
then
continue
fi

if [ ! -f ~/dictionary/$word ];
then
w3m -no-cookie -dump 'http://dict.baidu.com/s?wd='$word'&f=3'  \
			| sed '/以下结果来自互联网网络释义/,$d'| sed '1,15d' | tac \
			| sed '1,2d' | tac |sed -r '/^[0-9]+\./N;s/\n//' > ~/dictionary/$word
fi
clear
echo
echo -e "--------------------\033[1;40;33m $word \033[0m--------------------"
cat ~/dictionary/$word
done
exit 0
新手指引:将代码复制一个文件,比如dic,然后chmod u+x dic,接下来

代码: 全选

sudo cp dic /usr/bin/
,这样就可以直接在命令行或者快速运行里面直接运行dic了[/color][/b]
附件
运行截图
运行截图
上次由 steven0lisa 在 2011-05-29 1:56,总共编辑 4 次。
头像
eexpress
帖子: 58428
注册时间: 2005-08-14 21:55
来自: 长沙

Re: 终端下的英汉字典

#2

帖子 eexpress » 2010-12-17 13:14

sdcv用notify显示的,如何。比较下。
● 鸣学
头像
jarryson
帖子: 4002
注册时间: 2005-08-14 19:53

Re: 终端下的英汉字典

#3

帖子 jarryson » 2010-12-17 13:16

支持一下。咋木有音标?希望弄一下输出便于使用notify-send来显示
头像
steven0lisa
帖子: 68
注册时间: 2008-01-31 11:57

Re: 终端下的英汉字典

#4

帖子 steven0lisa » 2010-12-17 14:04

eexpress 写了:sdcv用notify显示的,如何。比较下。
好好!
我去看看
头像
steven0lisa
帖子: 68
注册时间: 2008-01-31 11:57

Re: 终端下的英汉字典

#5

帖子 steven0lisa » 2010-12-17 14:08

jarryson 写了:支持一下。咋木有音标?希望弄一下输出便于使用notify-send来显示
嗯。。我当时没有想那么多。。而且百度也没有给音标
头像
steven0lisa
帖子: 68
注册时间: 2008-01-31 11:57

Re: 终端下的英汉字典

#6

帖子 steven0lisa » 2010-12-17 14:30

eexpress 写了:sdcv用notify显示的,如何。比较下。
添加了
头像
steven0lisa
帖子: 68
注册时间: 2008-01-31 11:57

Re: 终端下的英汉字典

#7

帖子 steven0lisa » 2010-12-17 14:31

jarryson 写了:支持一下。咋木有音标?希望弄一下输出便于使用notify-send来显示
不好意思。有音标。
这个我迟点加上去
头像
jarryson
帖子: 4002
注册时间: 2005-08-14 19:53

Re: 终端下的英汉字典

#9

帖子 jarryson » 2010-12-17 16:27

咋又是需要w3m。。。
头像
steven0lisa
帖子: 68
注册时间: 2008-01-31 11:57

Re: 终端下的英汉字典

#10

帖子 steven0lisa » 2010-12-17 16:32

jarryson 写了:咋又是需要w3m。。。
嗯。。。 我利用它下载网页 :em06
不知道在下有没有不用w3m就可以下载网页的方案呢?
头像
eexpress
帖子: 58428
注册时间: 2005-08-14 21:55
来自: 长沙

Re: 终端下的英汉字典

#11

帖子 eexpress » 2010-12-17 16:36

代码: 全选

● cat bin/bot/sdcv.pl 
#!/usr/bin/perl
use Getopt::Long;

# 参数:单行输出选择。屏幕提示输出选择。
GetOptions('1' => \$oneline, 'n'=>\$notify);

my $out,$in;
# 无参数时,使用剪贴板内容。
$in=$ARGV[0]; if(!$in){$in=`xsel -o`;} if(!$in){exit;}
open(SDCV,"sdcv -n $in|");
my $r;
while($l=<SDCV>){
if($l!~/^$/){$r=$l;chomp($r);$r=~s/-->//;}
else{$out="$r --> ";last;}
}
while($l=<SDCV>){
if($l=~/相关|^$/){
close(SDCV);
if($notify){`notify-send -u critical -i '/home/exp/媒体/图标●/128软件png/pidgin.png' 'sdcv翻译' "$out"`;}
else{print $out;}
exit;
}
chomp($l) if($oneline);
$out.=" ► $l";
}
● 鸣学
头像
steven0lisa
帖子: 68
注册时间: 2008-01-31 11:57

Re: 终端下的英汉字典

#12

帖子 steven0lisa » 2010-12-17 16:47

eexpress 写了:

代码: 全选

● cat bin/bot/sdcv.pl 
#!/usr/bin/perl
use Getopt::Long;

# 参数:单行输出选择。屏幕提示输出选择。
GetOptions('1' => \$oneline, 'n'=>\$notify);

my $out,$in;
# 无参数时,使用剪贴板内容。
$in=$ARGV[0]; if(!$in){$in=`xsel -o`;} if(!$in){exit;}
open(SDCV,"sdcv -n $in|");
my $r;
while($l=<SDCV>){
if($l!~/^$/){$r=$l;chomp($r);$r=~s/-->//;}
else{$out="$r --> ";last;}
}
while($l=<SDCV>){
if($l=~/相关|^$/){
close(SDCV);
if($notify){`notify-send -u critical -i '/home/exp/媒体/图标●/128软件png/pidgin.png' 'sdcv翻译' "$out"`;}
else{print $out;}
exit;
}
chomp($l) if($oneline);
$out.=" ► $l";
}

非perl派,看不懂。。。
运行没效果。
头像
eexpress
帖子: 58428
注册时间: 2005-08-14 21:55
来自: 长沙

Re: 终端下的英汉字典

#13

帖子 eexpress » 2010-12-17 16:51

你没安装过sdcv吧。stardict的cli版本。
● 鸣学
头像
steven0lisa
帖子: 68
注册时间: 2008-01-31 11:57

Re: 终端下的英汉字典

#14

帖子 steven0lisa » 2010-12-17 17:03

eexpress 写了:你没安装过sdcv吧。stardict的cli版本。
没有装过。 不过你这样一说,我倒是有印象了。有空看看吧。呵呵
owwbu
帖子: 614
注册时间: 2006-11-15 14:14

Re: 终端下的英汉字典

#15

帖子 owwbu » 2010-12-19 0:23

if [ "$msg"="" ] 应该是 if [ "$msg" = "" ]
等号两边的空格被lz丢掉了。
我说怎么老是同样的结果:您所查询的$1无法找到解释。

还有循环查询当中的 带颜色的地方 echo后lz少了一个 -e

最后,感谢lz一下,挺好用的。

另外,如果能查汉语字词就好了。我看了一下,大概还要把汉字转成十六进制的那种编码才行,比如“我”得转成“%CE%D2”才行。不知道linux下有什么工具可以实现这种转化的?
回复