Hello and Welcome to app.py
Today i will blog on how to connect to Azure Cognitive Service and leverage Azure Bing Spell Check API to get correct suggest for wrong spellings.
Prerequisite –
1. Basic knowledge of Python programming.
2. Azure Subscription (Trial or Paid).
First we will create Bing Spell Check Cognitive Service in Azure and obtain the Endpoint and Subscription Key.
Second We will use Bing Spell Check Endpoint and Subscription Key in Python to get suggestion of correct words based on wrong word.
- Step 1 : Go to Portal.azure.com and Search Bing Spell Check and click Bing Spell Check to create new

- Step 2 : On create Screen,

- Provide Name for your Bing Spell Check.
Subscription, you can select free one for demo.
Location – This is were your server is hosted, Azure server is replicated to multiple environment, but i would suggest to select the nearest to you, As i am INDIA, i selected Central INDIA.
Pricing Tier – This is where your charges are considered for your Cognitive Service, Azure charges based on selected pricing tier.
for this Demo, please select Free F0 – 1K calls per month.
Resource Group – Select existing or create new if you dont want to link any existing one.
- Step 3 – It will take some seconds to be deployed, once deployed
Go to Key and Endpoints Section and you need to get following details from Key and Endpoints.

- Endpoint – https://{BingSpellSearchName}.cognitiveservices.azure.com/
Key1 – This is alphanumeric Key, used in API call.
Now we have everything we need to make call from Python.
Lets move to Coding part.
Let me first paste the code and then we will go each line by line explanation
#Python 3.6 | Azure NoteBook | Azure Cognitive Services import http.client, json, urllib, urllib.request, urllib.error, urllib.parse key='9c5bfdd7445448f898f19379eb6e0f58' header = { 'Content-Type' : 'application/json', 'Ocp-Apim-Subscription-Key' : key } baseUrl = "aml-spellcheck.cognitiveservices.azure.com" text = 'barke' params = urllib.parse.urlencode({ 'text' : text, 'mkt' : 'en-US', 'setLang' : 'EN', 'postContextText' : 'dogs', 'preContextText' : 'animal' }) endpoint = '/bing/v7.0/spellcheck?%s'%params body = {} try : conn = http.client.HTTPSConnection(baseUrl) conn.request('POST', endpoint,body,header) response = conn.getresponse() jsonData = response.read().decode('UTF-8') data = json.loads(jsonData) if data['flaggedTokens'] == []: print('No Suggestion ') for token in data['flaggedTokens']: print('You can replace '+ token['token'] +' with folowing words') for suggestion in token['suggestions']: print(suggestion['suggestion'] + " Score is "+ str(suggestion['score'])) conn.close() except Exception as ex: print(ex)
Code Explanation :
–> Line 1 : import http.client, json, urllib, urllib.request, urllib.error, urllib.parse
…… Importing modules which will be used in code through out.
–> Line 2 :
key=’9c5bfdd7445448f898f19379eb6e0f58′
This is the key we copied from Azure bing spell check Cognitive Service.[Step 3]
–> Line 3 – 6 :
header = {
‘Content-Type’ : ‘application/json’,
‘Ocp-Apim-Subscription-Key’ : key
}
Setting some Content Type and Subscription key for API call.
–> Line 7 : baseUrl = “aml-spellcheck.cognitiveservices.azure.com”
Endpoint we copied in step 3 when creating bing spell check cognitive Service.
IMP Note : Make sure you remove “https://” from Endpoint and remove “/bing/v7.0/spellcheck” as we will set this in another variable.
–> Line 8 : text = ‘barke’ // This is the text for which we will do spelling check.
–> Line 9 – 15 :
params = urllib.parse.urlencode({
‘text’ : text,
‘mkt’ : ‘en-US’,
‘setLang’ : ‘EN’,
‘postContextText’ : ‘vehical’,
‘preContextText’ : ‘animal’
})
This is parameter strings which we will send as a part of Azure Endpoint in form of query string, so url encoding is used.
text | This is the word for which we want to do a spell check. |
mkt | This specify which words list we have search in, so en-US |
setLang | This is language of current text we are sending |
postContextText | The Context helps bing spell check to check word spelling in specific context. example : if i miss types word barke, and when i specify context as car or vehicle, Bing spell will check spelling in context of car or vehicle and return me “Brake“ if i specify context as animal or dog, bing spell will check spelling in context of animal or dog and it will return me “Bark”. |
preContextText | The Context helps bing spell check to check word spelling in specific context. example : if i miss types word barke, and when i specify context as car or vehicle, Bing spell will check spelling in context of car or vehicle and return me “Brake“ if i specify context as animal or dog, bing spell will check spelling in context of animal or dog and it will return me “Bark”. |
–> Line 16 -17 : endpoint = ‘/bing/v7.0/spellcheck?%s’%params
body = {}
setting up variables of endpoint and empty body.
body is empty because this service has no requirement of body parameter.
–> Line 18 : try except block
–> Line 19 : conn = http.client.HTTPSConnection(baseUrl)
Creating Connection object using HTTPSConnection method.
–> Line 20 : conn.request(‘POST’, endpoint,body,header)
setting up the parameters as we defined in above list to send to Azure API.
Sending Request to Azure bing spell check API with header, body. and endpoint [with query string].
–> Line 21 -24 :
response = conn.getresponse()
jsonData = response.read().decode(‘UTF-8’)
data = json.loads(jsonData)
Reading response and converting it into JSON.
–> Line 25-30 :
if data[‘flaggedTokens’] == []:
print(‘No Suggestion ‘)
for token in data[‘flaggedTokens’]:
print(‘You can replace ‘+ token[‘token’] +’ with following words’)
for suggestion in token[‘suggestions’]:
print(suggestion[‘suggestion’] + ” Score is “+ str(suggestion[‘score’]))
— Displaying the response
–> Line 31 :
conn.close()
Finally closing the connection.
Microsoft Document Reference
Hope it helps.
Please post question/queries if any.
*This post is locked for comments