Google Spell API — Ruby

Someone had asked if I could post the ruby code we use to interact with the google toolbar spell api.  Well, here it is.


require 'net/https'
require 'uri'
require 'rexml/document'

class GoogleSpell
   def GetWords(phrase)
     results = []
     x = 0
     i = 0

     phrase = phrase.downcase
     phrase = phrase.gsub("&", "&")
     phrase = phrase.gsub("<", "<")
     phrase = phrase.gsub(">", ">")
     word_frag = phrase.split(" ")
     word_frag.each do |lookup|
       words = "" + lookup + ""
       gword = Hash.new()
       gword["original"] = lookup;
       gword["data"] = ""
       http = Net::HTTP.new('www.google.com', 443)
       http.use_ssl = true
       http.verify_mode = OpenSSL::SSL::VERIFY_NONE
       response =  http.start {|net|
         net.request_post("/tbproxy/spell?lang=en", words) {|response|
           doc = REXML::Document.new response.body
           nodelist = doc.elements.to_a("//c")
           nodelist.each do |item|
             if item.text.downcase != gword["original"]
               gword["data"] = item.text.downcase
             else
               gword["data"] = ""
             end
           end
         }
       }
       results << gword
     end
     return results
   end
end

--TR


Posted

in

by

Tags: