本帖最后由 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
- =info
- Code by : 523066680@163.com
- Date : 2015-06-14
- Version : 1.66
-
- 按 Tab 键切换剪切板访问开关
- 键入 quit/exit 或者 按 ESC 退出
- =cut
-
- use v5.16; # given
- use utf8;
- use Encode;
- use IO::Handle;
- use LWP::Simple;
- use Term::ReadKey;
- use Win32::Clipboard;
-
- use Time::HiRes 'sleep';
- ReadMode 4; #Turn off controls keys
-
- 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; } # ReadKey状态下按 Enter = \r
- when ( /\e/ ) { exit ; } # 按 ESC 退出
- 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 ) #非ANSII即GBK
- {
- $word .= ReadKey(-1); #读入后半字节
- if ( $word =~ /^[\xA1-\xA9]/ ) { break; } #GBK 符号区域
- $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}/ )
- {
- #if ($text =~/\W/) { return; }
- #最多匹配3个词
- 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;
- }
复制代码
|