Skip to content

Instantly share code, notes, and snippets.

@msimpson
Created July 21, 2011 10:55
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save msimpson/1096958 to your computer and use it in GitHub Desktop.
Save msimpson/1096958 to your computer and use it in GitHub Desktop.
Google definition (this method is frowned upon by Google, use for educational purposes only).
#!/usr/bin/env ruby
# Define: a Google Dictionary API tool
require "rubygems"
require "net/http"
require "json"
if not ARGV[0]; exit; end
query = ARGV.join("+").gsub(" ","+")
response = Net::HTTP.start("www.google.com",80) do |http|
http.get("/dictionary/json?callback=r&sl=en&tl=en&q="+query); end
begin
data = JSON.parse(/r\((:?.*?),200,null\)/.match(response.body)[1])
phonetic = data["primaries"][0]["terms"][1]["text"]
label = data["primaries"][0]["terms"][0]["labels"]
type = (label)?label[0]["text"]:"person"
puts "\n%s %s (%s)\n" % [data["query"].capitalize, phonetic, type.downcase]
for i in (0..5)
entry = data["primaries"][0]["entries"][i]["terms"][0]["text"].
gsub(/(x3cemx3e|x3c\/emx3e)/,"").scan(/.{1,80}(?:\s|\Z)/)
puts "\u2022 "+entry[0].capitalize
entry[1..-1].each do |line|; puts " "+line; end
end
puts
rescue
puts
exit
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment