本帖最后由 523066680 于 2015-6-14 21:34 编辑
测试环境:WIN XP or WIN7 32/64 , Perl v5.16 (ActiveState)
代码保存为UTF8编码格式,查询dict.cn网站的翻译结果
(一直想要一个终端版的翻译工具,挂着其他翻译工具或者开浏览器总感觉耗内存)
V1.3 更新内容
支持剪切板获取,同时可以在终端进行输入(在输入单个字符后自动切换到<STDIN>,节约循环开销)
按 / 或者 ESC 或者 输入exit 退出
V1.60 更新内容
添加了中文输入的判断处理(毕竟Term::ReadKey是逐字节读取,不过还好,Read两个字节后转<STDIN>小事化无)
如果Ctrl+C中文文本,需要确保系统默认输入法为中文,否则可能提取为乱码
V1.65 增加剪切板访问开关(按Tab键切换),修正部分文字、符号判断的BUG
 | | | | | | | | | | | | | | | | | | | use v5.16; | | use utf8; | | use Encode; | | use IO::Handle; | | use LWP::Simple; | | use Term::ReadKey; | | use Win32::Clipboard; | | | | use Time::HiRes 'sleep'; | | ReadMode 4; | | | | system(""); | | STDOUT->autoflush(1); | | binmode(STDOUT, ":encoding(gbk)"); | | | | our $clip = Win32::Clipboard->new(); | | our $main = "http:\/\/dict.cn"; | | our $Clip_Access = 1; | | | | my $text; | | my $word; | | my $key; | | my $i = 0; | | $clip->Empty(); | | | | notice(); | | | | MAIN: while (1) | | { | | clipboardEvent() if ( $Clip_Access == 1 ); | | keyboardEvent(); | | | | print $i == 0 ? "_\b" : " \b"; | | $i = 1 - $i; | | sleep 0.2; | | } | | | | sub keyboardEvent | | { | | my $word; | | | | $word = ReadKey(-1); | | given ($word) | | { | | when ( /\r/ ) { break; } | | when ( /\e/ ) { exit ; } | | when ( /\t/ ) { switch_CA(); } | | when ( /.{1}/ ) { readContinue($word); } | | } | | } | | | | sub switch_CA | | { | | our $Clip_Access; | | $Clip_Access = 1 - $Clip_Access; | | notice(); | | } | | | | sub readContinue | | { | | my $word = shift; | | my $LANG; | | | | if ( ord($word) > 128 ) | | { | | $word .= ReadKey(-1); | | if ( $word =~ /^[\xA1-\xA9]/ ) { break; } | | $LANG = "CHN"; | | } | | else | | { | | if ( $word =~ /[^a-zA-Z]/ ) { break; } | | $LANG = "ENG"; | | } | | | | print " ", decode('gbk', $word); | | $word .= <STDIN>; | | chomp $word; | | $word = decode('gbk', $word); | | | | if ( $word =~ /^(quit|exit)$/i ) { exit; } | | trans($word, $LANG); | | } | | | | sub clipboardEvent | | { | | our $clip; | | my $text; | | | | $text = $clip->GetText(); | | $text = decode('gbk', $text); | | | | if ( $text =~ /\p{IsWord}/ ) | | { | | | | | | unless ( $text =~ /^( ?\p{IsWord}+ ?){1,3}$/ ) | | { | | return; | | } | | | | print " $text {Clipboard}\n"; | | given ($text) | | { | | when (/\p{han}/) { trans($text, "CHN"); } | | when (/[\w ]+/) { trans($text, "ENG"); } | | } | | $clip->Empty(); | | } | | } | | | | sub trans | | { | | our $main; | | my $word = shift; | | my $type = shift; | | my $all; | | my $chk = 0; | | | | $word = encode('utf8', $word); | | $all = get("$main/$word") or warn "$!"; | | | | $chk = | | $type eq 'CHN' ? | | Chn2Eng(\$all) : Eng2Chn(\$all); | | | | if ($chk == 0) | | { | | print "找不到该单词的翻译信息。\n"; | | } | | | | print "\n"; | | notice(); | | } | | | | sub notice { | | our $enable; | | state $tip = | | { | | 0 => "关", | | 1 => "开", | | }; | | print "\b \b"x80; | | print "(剪切板访问:". $tip->{ $Clip_Access }; | | print ") 请输入单词:"; | | } | | | | | | sub Chn2Eng | | { | | my $ref = shift; | | my $start; | | my $chk = 0; | | for (split("\n", ${$ref})) | | { | | if ( /<div class="layout cn">/i ) { $start = 1; } | | if ( $start == 1 ) | | { | | if ( /<li><a href.*>(.*)<\/a><\/li>/i ) | | { | | print $1,"\n"; | | $chk++; | | } | | elsif ( /<\/div>/i ) | | { | | last; | | } | | } | | } | | return $chk; | | } | | | | sub Eng2Chn | | { | | my $ref = shift; | | my $chk = 0; | | for (split("\n", ${$ref})) | | { | | if (/<li><span>(.*)<\/span> | | <strong>(.*)<\/strong> | | /ix | | ) { | | print $1, $2,"\n"; | | $chk++; | | } | | } | | return $chk; | | }COPY |
|