Skip to content

Commit 501e39c

Browse files
authored
Updated libraries
1 parent 59f0417 commit 501e39c

File tree

1 file changed

+27
-21
lines changed

1 file changed

+27
-21
lines changed

python/Search/BingSpellCheckv7.py

Lines changed: 27 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,45 @@
1-
#Copyright (c) Microsoft Corporation. All rights reserved.
2-
#Licensed under the MIT License.
1+
# Copyright (c) Microsoft Corporation. All rights reserved.
2+
# Licensed under the MIT License.
33

4-
import http.client
54
import json
65
import os
76
from pprint import pprint
7+
import requests
88
import urllib.parse
99

1010
'''
11-
This sample uses the Bing Spell Check API to check the spelling of query words and then suggests corrections.
11+
This sample uses the Bing Spell Check API to check the spelling of query words
12+
and then suggests corrections with a scored confidence.
1213
Bing Spell Check API: https://dev.cognitive.microsoft.com/docs/services/5f7d486e04d2430193e1ca8f760cd7ed/operations/57855119bca1df1c647bc358
1314
'''
1415

15-
text = 'Hollo, wrld!'
16-
17-
params = {'mkt': 'en-US', 'mode': 'proof', 'text': text}
18-
19-
# Add your Bing Spell Check subscription key anf endpoint to your environment variables.
16+
# Add your Bing Spell Check subscription key and endpoint to your environment variables.
2017
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'
18+
endpoint = os.environ['BING_SPELL_CHECK_ENDPOINT'] + '/bing/v7.0/spellcheck'
19+
20+
# Query you want spell-checked.
21+
query = 'Hollo, wrld!'
2422

25-
headers = {'Ocp-Apim-Subscription-Key': key,
26-
'Content-Type': 'application/x-www-form-urlencoded'}
23+
# Construct request
24+
params = urllib.parse.urlencode( { 'mkt': 'en-US', 'mode': 'proof', 'text': query } )
25+
headers = { 'Ocp-Apim-Subscription-Key': key,
26+
'Content-Type': 'application/x-www-form-urlencoded' }
2727

28-
# The headers in the following example
29-
# are optional but should be considered as required:
28+
# Optional headers
3029
#
3130
# X-MSEdge-ClientIP: 999.999.999.999
3231
# X-Search-Location: lat: +90.0000000000000;long: 00.0000000000000;re:100.000000000000
3332
# X-MSEdge-ClientID: <Client ID from Previous Response Goes Here>
3433

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()))
34+
# Call the API
35+
try:
36+
response = requests.get(endpoint, headers=headers, params=params)
37+
response.raise_for_status()
38+
39+
print("\nHeaders:\n")
40+
print(response.headers)
41+
42+
print("\nJSON Response:\n")
43+
pprint(response.json())
44+
except Exception as ex:
45+
raise ex

0 commit comments

Comments
 (0)