-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhelpers.py
127 lines (108 loc) · 3.6 KB
/
helpers.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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
import os, requests, uuid, json
# Don't forget to replace with your Cog Services subscription key!
subscription_key = 'a0ed5683ab204deeb4e7f5696e37a3ef'
# 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_languages(input_text, output_text):
base_url = 'https://mytextservice.cognitiveservices.azure.com/text/analytics'
path = '/v2.1/languages'
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': [
{
'id': '1',
'text': input_text
},
{
'id': '2',
'text': output_text
}
]
}
response = requests.post(constructed_url, headers=headers, json=body)
return response.json()
def get_sentiment(input_text, input_language, output_text, output_language):
base_url = 'https://mytextservice.cognitiveservices.azure.com/text/analytics'
path = '/v2.1/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()
def get_entities(input_text, output_text):
base_url = 'https://mytextservice.cognitiveservices.azure.com/text/analytics'
path = '/v2.1/entities'
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': [
{
'id': '1',
'text': input_text
},
{
'id': '2',
'text': output_text
}
]
}
response = requests.post(constructed_url, headers=headers, json=body)
return response.json()
def get_keyphrases(input_text, input_language, output_text, output_language):
base_url = 'https://mytextservice.cognitiveservices.azure.com/text/analytics'
path = '/v2.1/keyphrases'
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()