google translator 0.2

    过去写的那个利用google在线翻译的 小脚本工具一直在用,今天用的时候,突然想,我今年不是想加强下英语学习吗?那么把每天查过的单词保存下来,每天早上或者上班空闲期间花那么几分钟记忆复习下这些单词不是很好,毕竟技术性文章翻来覆去运用的单词就那么多,过去没有注意积累,导致常常还得重新查,所谓提高也就放在口头上了。说改就改,脚本语言改起来就是容易:

# 利用google在线翻译,翻译中文<->英文
#
author dennis
#
version 0.2
require  ' net/http '
$contents
= Hash.new
$dir
= " F:/English/ "
now
= Time.now
$today
= " #{now.year}#{now.month.to_s.rjust(2,'0')}#{now.day.to_s.rjust(2,'0')} "
def  translate
  txt
= STDIN.gets
  
if  txt.strip == ' e '   or  txt.strip == ' exit '
    
# 退出前保存
     if  $contents.size > 0 then
      File.open(
" #{$dir}#{$today}.txt " , " a+ " ) do  | file |
        $contents.each {
| key,value |  file.write(key.ljust( 20 ) + value.ljust( 20 ) + " \n " )}
      end
    end
    exit
  end
  temp
= txt.split( '   ' )
  
if  temp[ 1 ] == ' 1 '   or  temp.size == 1
    langpair
= ' en|zh-CN '
  
else
    langpair
= ' zh-CN|en '
  end
  begin 
    
# 使用代理  
     # $proxy_addr = '192.168.9.25'
    $proxy_port  =   8081
    $proxy_user
= ' test '
    $proxy_passwd
= ' test '
    
if  $proxy_addr
     response 
=  Net::HTTP.Proxy($proxy_addr,$proxy_port,$proxy_user,$proxy_passwd).post_form(URI.parse( " http://translate.google.com/translate_t " ),{ ' text ' => temp[0], ' langpair ' => langpair}) 
    
else
      response 
=  Net::HTTP.post_form(URI.parse( " http://translate.google.com/translate_t " ),{ ' text ' => temp[0], ' langpair ' => langpair}) 
    end
    response.body 
=~   /< textarea. * ?id = suggestion > (. * ?) < \ / textarea >/
  rescue  StandardError 
=> e
    $stderr.
print   " 错误: " + e
  
else
    result 
=  $ 1  
    puts 
' 翻译内容: ' + temp[0]
    puts 
' google返回: ' + result  if  result
    $contents[temp[0]]
= result
    puts 
' -------------------退出请打e或者exit--------------- '
    translate
  end
end
translate


你可能感兴趣的:(google translator 0.2)