`

curl+sed+shell编写一个英语翻译脚本

 
阅读更多

这个事一个shell函数,使用了有道翻译提供的翻译服务

mac osx:

 

ts(){

words=""
for word in $@; 
do
    words="$words$word "
done

curl -s \
        "http://fanyi.youdao.com/translate?smartresult=dict&smartresult=rule&smartresult=ugc&sessionFrom=dict.top" \
     -d \
	"type=AUTO& i=$words&doctype=json&xmlVersion=1.4&keyfrom=fanyi.web&ue=UTF-8&typoResult=true&flag=false" \
        | sed -E -n 's/.*tgt":"([^"]+)".*/\1/p' ;

return 0;
}

 

linux:

 

ts(){

words=""
for word in $@; 
do
    words="$words$word "
done

curl -s \
        "http://fanyi.youdao.com/translate?smartresult=dict&smartresult=rule&smartresult=ugc&sessionFrom=dict.top" \
     -d \
	"type=AUTO& i=$words&doctype=json&xmlVersion=1.4&keyfrom=fanyi.web&ue=UTF-8&typoResult=true&flag=false" \
        | sed -r -n 's/.*tgt":"([^"]+)".*/\1/p' ;

return 0;
}

 

设置如下:

linux用户:

1.添加此脚本到~/.bashrc的末尾

2.source ~/.bashrc

Mac osX用户:

 

1.添加此脚本到~/.bash_profile的末尾,没有就新建

2.source ~/.bash_profile

 

使用:

 

 

ts hello
你好
ts how are you?
你好吗?
 
也可以翻译中文。

 

另外又写了一个:

linux:

ts(){
result=`curl -s \
        "http://dict.cn/ws.php?utf8=true&q=$1" `;

echo $result | sed -r -n 's/.*<def>([^<]+)<\/def>.*/\1/p'; 

#examples
echo $result \
    | sed -r -n 's/.*def> (<sent><orig>.*<\/sent>).*/\1/p' \
    | sed 's/&lt;em&gt;//g' \
    | sed 's/&lt;\/em&gt;//g' \
    | sed 's/<trans>/\n/g' \
    | sed 's/<orig>/\n/g' \
    | sed 's/<[^<>]*>//g';

return 0;

}

 

 

Mac:

 

# Mac Version
# notic: ^M^L = Ctrl+v Ctrl+Enter Ctrl+v Ctrl+l
ts(){                                                                       
result=`curl -s \
        "http://dict.cn/ws.php?utf8=true&q=$1" `;

echo $result | sed -E -n 's/.*<def>([^<]+)<\/def>.*/\1/p'; 

#examples
echo $result \
    | sed -E -n 's/.*def> (<sent><orig>.*<\/sent>).*/\1/p' \
    | sed 's/&lt;em&gt;//g' \
    | sed 's/&lt;\/em&gt;//g' \
    | sed 's/<trans>/^M^L/g' \
    | sed 's/<orig>/^M^L/g' \
    | sed 's/<[^<>]*>//g' ;

return 0;

} 

 

$ ts apple
n.苹果, 珍宝, 家伙 

My uncle has an apple orchard.
我叔叔拥有一个苹果园。 
The apple pie and custard are delicious.
苹果饼和软冻的味道好极了。 
The apple trees are blossoming.
苹果树正在开花。

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics