Posted on 2008-02-14 11:15
dennis 閱讀(531)
評(píng)論(0) 編輯 收藏 所屬分類:
動(dòng)態(tài)語(yǔ)言
過(guò)去寫(xiě)的那個(gè)利用google在線翻譯的
小腳本工具一直在用,今天用的時(shí)候,突然想,我今年不是想加強(qiáng)下英語(yǔ)學(xué)習(xí)嗎?那么把每天查過(guò)的單詞保存下來(lái),每天早上或者上班空閑期間花那么幾分鐘記憶復(fù)習(xí)下這些單詞不是很好,畢竟技術(shù)性文章翻來(lái)覆去運(yùn)用的單詞就那么多,過(guò)去沒(méi)有注意積累,導(dǎo)致常常還得重新查,所謂提高也就放在口頭上了。說(shuō)改就改,腳本語(yǔ)言改起來(lái)就是容易:
#利用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 "錯(cuò)誤:"+e
else
result = $1
puts '翻譯內(nèi)容:'+temp[0]
puts 'google返回:'+result if result
$contents[temp[0]]=result
puts '-------------------退出請(qǐng)打e或者exit---------------'
translate
end
end
translate