0
我在python中使用了bing API進行拼寫糾正。儘管我得到了帶有建議的正確的Json格式,但它並未替換原始字符串。我試着用data.replace,但它不起作用。有沒有其他簡單的方法可以用建議的詞語替換原始字符串。如何用python bing替換單詞拼寫更正建議
import httplib,urllib,base64
headers = {
# Request headers
'Ocp-Apim-Subscription-Key': '7fdf55a1a7e42d0a7890bab142343f8'
}
params = urllib.urlencode({
# Request parameters
'text': 'Lectures were really good. There were lot of people who came their without any Java knowledge and yet you were very suppor.',
'mode': 'proof',
'preContextText': '{string}',
'postContextText': '{string}',
'mkt': '{string}',
})
try:
conn = httplib.HTTPSConnection('api.cognitive.microsoft.com')
conn.request("GET", "/bing/v5.0/spellcheck/?%s" % params, "{body}", headers)
response = conn.getresponse()
data = response.read()
print(data)
conn.close()
except Exception as e:
print("[Errno {0}] {1}".format(e.errno, e.strerror))
輸出(打印漂亮):
{'_type': 'SpellCheck',
'flaggedTokens': [{'offset': 61,
'suggestions': [{'score': 0.854956767552189,
'suggestion': 'there'}],
'token': 'their',
'type': 'UnknownToken'},
{'offset': 116,
'suggestions': [{'score': 0.871971469417366,
'suggestion': 'support'}],
'token': 'suppor',
'type': 'UnknownToken'}]}