Ruby client for search and spelling correction using Bing's APIs
I noticed that Microsoft allows free use of their search and spelling correction APIs. I just played with the APIs for a few minutes. Here is a Ruby code snippet that I just wrote:
API_KEY = ENV['BING_API_KEY']You need a free Bing API key - notice that I set the key value in my environment. If you get a key, then try:
require 'rubygems' # needed for Ruby 1.8.x
require 'simple_http'
require 'json'
def search query
uri = "http://api.search.live.net/json.aspx?AppId=#{API_KEY}&Market=en-US&Query=#{CGI.escape(query)}&Sources=web+spell&Web.Count=4"
JSON.parse(SimpleHttp.get(uri))["SearchResponse"]["Web"]["Results"]
end
def correct_spelling text
uri = "http://api.search.live.net/json.aspx?AppId=#{API_KEY}&Market=en-US&Query=#{CGI.escape(text)}&Sources=web+spell&Web.Count=1"
JSON.parse(SimpleHttp.get(uri))["SearchResponse"]["Spell"]["Results"][0]["Value"]
end
search "semantic web java ruby lisp"The first method does spelling correction before search.
correct_spelling "semaantic web jaava ruby lisp"
Comments
Post a Comment