经常要查
单词,所以利用qq dict api写了一个命令行词典工具:
#!/usr/bin/env ruby
require 'rubygems'
require 'net/http'
require 'json'
def trans(q)
url = "http://dict.qq.com/dict?q=#{URI.encode(q)}"
resp = Net::HTTP.get_response(URI.parse(url))
result = JSON.parse(resp.body)
tran_entries = result['local']
if tran_entries.nil? or tran_entries.empty? then
puts "没有找到"
return
end
base_s = tran_entries[0]
puts base_s['word']
puts "基本解释"
result = base_s['des'].collect do |entry|
if entry.is_a? Hash then
entry.values.join(',')
else
entry
end
end
puts result.join(',').gsub('"','"')
end
if ARGV.empty? then
puts "Usage: #$0 word"
exit(1)
end
trans ARGV[0]
把代码文件命名为t,放在~/bin下
sudo chmod +x t
在~/.bashrc下添加
export PATH=~/bin:$PATH
然后就可以直接查询单词了:
$t book
book
基本解释
书, 书籍, 帐簿, 名册, 工作簿,n.,登记, 预订,vt.,登记, 预订
$t 书
书
基本解释
book,letter,script,write
补充一个shell的:
t(){
if [ "$(echo "$1" | sed 's/[a-zA-Z]//g')" = "" ]; then
langpair="en|zh"
else
langpair="zh|en"
fi
wget -qO- "http://ajax.googleapis.com/ajax/services/language/translate?v=1.0&q=$1&langpair=$langpair" | sed 's/.*"translatedText":"\([^"]*\)".*}/\1\n/';
}
t $1