The Web Language Model API is used in the Microsoft Cognitive Services, and allows for the analysis of text. In this example we generate five contenders for the phrase when broken up:
Web Language Model API |
Coding
The following gives an outline of the Python code:
import httplib, urllib, base64 import sys import json headers = { # Request headers 'Content-Type': 'application/json', 'Ocp-Apim-Subscription-Key': 'key goes HERE', } text="Thisisanimportantmessage" rtns=10 params = urllib.urlencode({ # Request parameters 'model': 'body', 'text': text, 'order': '2', 'maxNumOfCandidatesReturned': rtns, }) try: conn = httplib.HTTPSConnection('westus.api.cognitive.microsoft.com') conn.request("POST", "/text/weblm/v1.0/breakIntoWords?%s" % params, "{body}", headers) response = conn.getresponse() data = response.read() conn.close() d= json.loads(data) for x in range(0, rtns): print '%3s \"%-30s\" Prob: %10s' % (str(x+1),d['candidates'][x]['words'],str(d['candidates'][x]['probability'])) except Exception as e: print("[Errno {0}] {1}".format(e.errno, e.strerror))