-
Notifications
You must be signed in to change notification settings - Fork 0
/
microsoftTextAnalysis.py
53 lines (36 loc) · 1.33 KB
/
microsoftTextAnalysis.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
import httplib, urllib, base64
import json
from json import JSONEncoder
text = "Bring people from outside amazon to amazon (Facebook ads!!!) Amazon says thank you and give you free traffic!"
def microsoftParse(what, text):
headers = {
'Content-Type': 'application/json',
'Ocp-Apim-Subscription-Key': '0c3a0dfd10294ff49b41284521a05d70',
}
params = urllib.urlencode({
})
rext = json.dumps(text)
jsonString = JSONEncoder().encode({
"documents": [{"language": "en", "id": "string", "text": rext}]
})
print jsonString
try:
conn = httplib.HTTPSConnection('westus.api.cognitive.microsoft.com')
if what == "sentiment":
conn.request("POST", "/text/analytics/v2.0/sentiment?%s" % params, jsonString, headers)
response = conn.getresponse()
data = response.read()
rs = json.load(data)
return rs
conn.close()
elif what == "phrases":
conn.request("POST", "/text/analytics/v2.0/keyPhrases?%s" % params, text, headers)
response = conn.getresponse()
data = response.read()
return data
conn.close()
else:
print("you forget something")
except Exception as e:
print("Error")
microsoftParse("sentiment", text)