The Web Language Model API is used in the Microsoft Cognitive Services, and allows for the analysis of text. In this example we determine spelling errors:
Web Language Model API (Spell checking) |
Coding
The following gives an outline of the Python code:
import httplib, urllib, base64 import sys import json text='the kinh is not the rulr' if (len(sys.argv)>1): text=str(sys.argv[1]) headers = { # Request headers 'Content-Type': 'application/x-www-form-urlencoded', 'Ocp-Apim-Subscription-Key': 'YOUR KEY GOES HERE', } params = urllib.urlencode({ # Request parameters 'mode': 'spell', }) body = urllib.urlencode({ # Request parameters 'Text': text, }) try: conn = httplib.HTTPSConnection('api.cognitive.microsoft.com') conn.request("POST", "/bing/v5.0/spellcheck?%s" % params, body, headers) response = conn.getresponse() data = response.read() conn.close() print data d= json.loads(data) z=d['flaggedTokens'] ctn=len(z) for x in range(0, ctn): print '%3s Offset:%3s Token: \"%s\"' % (str(x+1),z[x]['offset'],z[x]['token']) w=z[x]['suggestions'] ctn2=len(w) for y in range(0, ctn2): print ' --->Suggestion: %s Score:%3s' % (w[y]['suggestion'],w[y]['score']) except Exception as e: print("[Errno {0}] {1}".format(e.errno, e.strerror))