Increasingly we need to mine on-line content. This page uses the Microsoft Cognitive Services Text Analysis method to mine the most significant words. A sentiment score close to 1 defines a positive sentiment, while one near zero indicates a negative sentiment.
Text Analysis |
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', } params = urllib.urlencode({ # Request parameters 'numberOfLanguagesToDetect': '1', }) text="This is an important message" body = "{'documents': [{'id': 'test001',\'text':\'"+text+"\'}]}" print try: conn = httplib.HTTPSConnection('westus.api.cognitive.microsoft.com') conn.request("POST", "/text/analytics/v2.0/sentiment?%s" % params, body, headers) response = conn.getresponse() data = response.read() conn.close() d= json.loads(data) s1 = d['documents'][0] ll = s1['score'] print "Sentiment score: ",ll conn.request("POST", "/text/analytics/v2.0/keyPhrases?%s" % params, body, headers) response = conn.getresponse() data = response.read() conn.close() d= json.loads(data) s1 = d['documents'][0] ll = s1['keyPhrases'] print 'Most significant phrases:' for member in ll: print ' '+member