注册 登录  
 加关注
   显示下一条  |  关闭
温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!立即重新绑定新浪微博》  |  关闭

Bioinformatics home

 
 
 

日志

 
 

Translator.pl 带自我学习功能的翻译工具 (perl群的 )  

2010-01-06 11:59:23|  分类: Perl |  标签: |举报 |字号 订阅

  下载LOFTER 我的照片书  |

#!/usr/local/bin/perl
#By xti9er www.xtiger.net
use IO::Socket::INET;
use Encode qw/encode decode/;
use Encode::CN;
use Color::Output;
Color::Output::Init;

`title   --= S t u d i o u s T r a n s l a t o r =--`;
cprin("\tS t u d i o u s\tT r a n s l a t o r\n",7);
cprin("\t\tBy Google&Xti9er \n\t translate.google.com&www.xtiger.net\n",13);
my $host="translate.google.com";
print "_"x50,"\n";
START:
cprin("Give me You Word:",13);
my $english=<STDIN>;
until($english)
{
cprin("\n[!]\tPLs input a word!\n",5);
goto START;
}

chomp ($english);

until($english and $english=~/[a-z]/i)
{
cprin("\n[!]\tDon't Fool me, It's Not a word!\n",5);
goto START;
}

unless(-d "./dic")
{
cprin("DIC directory not found,make it NOW!\n",5);
mkdir("./dic") or print $!,"\n";
}

print "\n","+"x50,"\n";
cprin("\tWord = < $english >\n",13);

my $flt=substr($english,0,1);

if(fromlocal($english))
{
cprin("Ah! I Remember It!\n",13);
}
else
{
open(LOCALDIC,"+>>./dic/$flt.dic");
if(fromgoogle($english))
{
       cprin("Ah! I Googled It!\n",13);
}
else
{
       cprin("So Sorry Get dic failed!\n",13);
}
close LOCALDIC;
}

sub fromlocal
{
my $getword=shift;
my $lword=length($getword)+1;
if(-e "./dic/$flt.dic")
{
open(LOCALDIC,"./dic/$flt.dic") or (print $! and goto LOCALEND);
while(my $nowstr=<LOCALDIC>)
{
         if(substr($nowstr,0,$lword) eq "$getword=")
         {
            putdic($nowstr);
            return 1;
         }
         else
         {
            next;
         }
}
close LOCALDIC;
}

else
{
return 0;
}
LOCALEND:
return 0;
}

sub fromgoogle
{
my $co=0;
my $english=shift;
if($sock=IO::Socket::INET->new(PeerAddr=>$host,PeerPort=>80,Proto=>'tcp'))
{
     print $sock "GET /translate_a/t?client=t&text=$english&sl=en&tl=zh-CN&swap=1 HTTP/1.1
Accept: */*
Accept-Language: zh-cn
Referer: http://translate.google.cn/translate_t?hl=zh-CN#en|zh-CN|good
UA-CPU: x86
Accept-Encoding: deflate
User-Agent: Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; .NET CLR 2.0.50727; InfoPath.1; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729)
Host: translate.google.cn
Connection: Keep-Alive
Cookie: PREF=ID=43dee4faa0c164ea:NW=1:TM=1232591661:LM=1232591661:S=QBHliTyaa2tE54Pw; NID=20=JX7tVJE1QRqMtvxmDHY0oUsM93zsUBPNAQaUGm2h-uIC8RYKxl0hDQamcNFagW9rZ7sfTldUdyaQtsGYYgfknMhUOsyaIj3ljNlPJrFWIhWrMg30UO2mKijsFVxcZqX7; __utma=3261045.390960281383473000.1234836497.1235467139.1235527712.12; __utmz=3261045.1235527712.12.9.utmcsr=google.cn|utmccn=(referral)|utmcmd=referral|utmcct=/intl/zh-cn/options/; __utmc=3261045

";

while($getinfo=(<$sock>))
{
$co++;
chomp($getinfo);
last if $getinfo=~/404\sNot\sFound/;
if ($getinfo=~/^\[|^\"/)
{
close $sock;
print LOCALDIC "$english=$getinfo\n";
putdic($getinfo);
return 1;
}
}
}
else
{
cprin("[Connect Fail!] $host:80\n",5);
return 0;
}
return 0;
}

sub putdic
{
my $dicinfo=shift;
@pinfo=split(/\,\[/,$dicinfo);

cprin("\t翻译:< ".utf2gbk($pinfo[0])." >\n",7);
print "-"x50,"\n";
cprin("\t字典:\n",11);

for(1..scalar(@pinfo)-1)
{
chomp($pinfo[$_]);
@dinfo=split(/\,/,$pinfo[$_]);
cprin("\t\t| ".utf2gbk($dinfo[0])." |\n",11);
for my $no (1..scalar(@dinfo)-1)
{
chomp($dinfo[$no]);
cprin("\t\t\t$no.".utf2gbk($dinfo[$no])."\n",11);
}
}
}

print "\n","+"x50,"\n";

goto START;

sub utf2gbk
{
my $str=shift;
$str=~ s/\,|\[|\]|\"|//gi;
my $euc_cn=decode("utf-8", $str);
my $deuc_cn=encode("GBK", $euc_cn);
return $deuc_cn;
}

sub cprin
{
($str,$i)=@_;
cprint("\x03" . $i . "$str\x030");
}

  评论这张
 
阅读(626)| 评论(0)
推荐 转载

历史上的今天

在LOFTER的更多文章

评论

<#--最新日志,群博日志--> <#--推荐日志--> <#--引用记录--> <#--博主推荐--> <#--随机阅读--> <#--首页推荐--> <#--历史上的今天--> <#--被推荐日志--> <#--上一篇,下一篇--> <#-- 热度 --> <#-- 网易新闻广告 --> <#--右边模块结构--> <#--评论模块结构--> <#--引用模块结构--> <#--博主发起的投票-->
 
 
 
 
 
 
 
 
 
 
 
 
 
 

页脚

网易公司版权所有 ©1997-2017