11#Copyright (c) Microsoft Corporation. All rights reserved.
22#Licensed under the MIT License.
33
4- import requests
4+ import http . client
55import json
6+ import os
7+ from pprint import pprint
8+ import urllib .parse
69
7- api_key = "enter-your-key-here"
8- example_text = "Hollo, wrld"
10+ '''
11+ This sample uses the Bing Spell Check API to check the spelling of query words and then suggests corrections.
12+ Bing Spell Check API: https://dev.cognitive.microsoft.com/docs/services/5f7d486e04d2430193e1ca8f760cd7ed/operations/57855119bca1df1c647bc358
13+ '''
914
10- endpoint = "https://api.cognitive.microsoft.com/bing/v7.0/SpellCheck"
15+ text = 'Hollo, wrld!'
1116
12- data = {'text' : example_text }
17+ params = {'mkt' : 'en-US' , 'mode' : 'proof' , ' text' : text }
1318
14- params = {
15- 'mkt' :'en-us' ,
16- 'mode' :'proof'
17- }
19+ # Add your Bing Spell Check subscription key anf endpoint to your environment variables.
20+ key = os .environ ['BING_SPELL_CHECK_SUBSCRIPTION_KEY' ]
21+ host = os .environ ['BING_SPELL_CHECK_ENDPOINT' ]
22+ host = host .replace ('https://' , '' )
23+ path = '/bing/v7.0/spellcheck'
1824
19- headers = {
20- 'Ocp-Apim-Subscription-Key' : api_key ,
21- 'Content-Type' : 'application/x-www-form-urlencoded'
22- }
25+ headers = {'Ocp-Apim-Subscription-Key' : key ,
26+ 'Content-Type' : 'application/x-www-form-urlencoded' }
2327
24- response = requests .post (endpoint , headers = headers , params = params , data = data )
25- json_response = response .json ()
26- print (json .dumps (json_response , indent = 4 ))
28+ # The headers in the following example
29+ # are optional but should be considered as required:
30+ #
31+ # X-MSEdge-ClientIP: 999.999.999.999
32+ # X-Search-Location: lat: +90.0000000000000;long: 00.0000000000000;re:100.000000000000
33+ # X-MSEdge-ClientID: <Client ID from Previous Response Goes Here>
34+
35+ conn = http .client .HTTPSConnection (host )
36+ params = urllib .parse .urlencode (params )
37+ conn .request ("POST" , path , params , headers )
38+ response = conn .getresponse ()
39+ pprint (json .loads (response .read ()))
0 commit comments