-
Notifications
You must be signed in to change notification settings - Fork 0
/
sentiment.py
39 lines (34 loc) · 1.32 KB
/
sentiment.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
import os, requests, uuid, json
# Don't forget to replace with your Cog Services subscription key!
subscription_key = '59637cdaa7fd4d6b8b0a9dc8dae465e1'
# Our Flask route will supply four arguments: input_text, input_language,
# output_text, output_language.
# When the run sentiment analysis button is pressed in our Flask app,
# the Ajax request will grab these values from our web app, and use them
# in the request. See main.js for Ajax calls.
def get_sentiment(input_text, input_language, output_text, output_language):
base_url = 'https://westus.api.cognitive.microsoft.com/text/analytics'
path = '/v2.0/sentiment'
constructed_url = base_url + path
headers = {
'Ocp-Apim-Subscription-Key': subscription_key,
'Content-type': 'application/json',
'X-ClientTraceId': str(uuid.uuid4())
}
# You can pass more than one object in body.
body = {
'documents': [
{
'language': input_language,
'id': '1',
'text': input_text
},
{
'language': output_language,
'id': '2',
'text': output_text
}
]
}
response = requests.post(constructed_url, headers=headers, json=body)
return response.json()