How can i use spell check feature of bing spell check API on the text mentioned in the code?

50 Views Asked by At

I am using this code to check for any grammar or spell check but seems like code is not working. Please provide me some changes in this code below to get expected output

import http.client, json, urllib, urllib.request, urllib.error, urllib.parse

key = '82255asf52f45f499eb23509d00789a'
header = {
    'Content-Type': 'application/json',
    'Ocp-Apim-Subscription-Key': key
}
baseUrl = "api.bing.microsoft.com"

**text = 'my name are sam'**

params = urllib.parse.urlencode({
    'text': text,
    'mkt': 'en-US',
    'setLang': 'EN',
    'postContextText': '',
    'preContextText': ''
})
endpoint = '/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)

current output: No Suggestion

Expected Output: my name is sam

0

There are 0 best solutions below