Quickstart: Check spelling with Bing Spell Check REST API and Ruby

Use this quickstart to make your first call to Bing Spell Check REST API using Ruby. This simple application sends a request to the API and returns a list of suggested corrections.

Although this application is written in Ruby, the API is a RESTful Web service compatible with most programming languages. The source code for this application is available on GitHub.

Prerequisites

Create and initialize the application

  1. Create a new Ruby file in your favorite editor or IDE, and add the following requirements:

    require 'net/http'
    require 'uri'
    require 'json'
    
  2. Create variables for your subscription key, endpoint URI, and path. Create your request parameters:

    1. Assign your market code to the mkt parameter with the = operator. The market code is the code of the country/region you make the request from.

    2. Add the mode parameter with the & operator, and then assign the spell-check mode. The mode can be either proof (catches most spelling/grammar errors) or spell (catches most spelling errors, but not as many grammar errors).

    key = 'ENTER YOUR KEY HERE'
    uri = 'https://api.bing.microsoft.com'
    path = '/v7.0/spellcheck?'
    params = 'mkt=en-us&mode=proof'
    

Send a spell check request

  1. Create a URI from your host uri, path, and parameters string. Set its query to contain the text you want to spell check.

    uri = URI(uri + path + params)
    uri.query = URI.encode_www_form({
       # Request parameters
    'text' => 'Hollo, wrld!'
    })
    
  2. Create a request using the URI constructed previously. Add your key to the Ocp-Apim-Subscription-Key header.

    request = Net::HTTP::Post.new(uri)
    request['Content-Type'] = "application/x-www-form-urlencoded"
    request['Ocp-Apim-Subscription-Key'] = key
    
  3. Send the request.

    response = Net::HTTP.start(uri.host, uri.port, :use_ssl => uri.scheme == 'https') do |http|
        http.request(request)
    end
    
  4. Get the JSON response, and print it to the console.

    result = JSON.pretty_generate(JSON.parse(response.body))
    puts result
    

Run the application

Build and run your project. If you're using the command line, use the following command to run the application:

ruby <FILE_NAME>.rb

Example JSON response

A successful response is returned in JSON, as shown in the following example:

{
   "_type": "SpellCheck",
   "flaggedTokens": [
      {
         "offset": 0,
         "token": "Hollo",
         "type": "UnknownToken",
         "suggestions": [
            {
               "suggestion": "Hello",
               "score": 0.9115257530801
            },
            {
               "suggestion": "Hollow",
               "score": 0.858039839213461
            },
            {
               "suggestion": "Hallo",
               "score": 0.597385084464481
            }
         ]
      },
      {
         "offset": 7,
         "token": "wrld",
         "type": "UnknownToken",
         "suggestions": [
            {
               "suggestion": "world",
               "score": 0.9115257530801
            }
         ]
      }
   ]
}

Next steps