-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathanalyzers.go
196 lines (161 loc) · 8.51 KB
/
analyzers.go
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
package azuretextanalysis
import (
"encoding/json"
"io/ioutil"
"log"
"net/http"
"os"
"strings"
"time"
)
// Request struct for building clean interaction between analysis and request func
type Request struct {
Key string
Endpoint string
Type string
Text []map[string]string
}
// Entities makes a request to the Entities API using the supplied:
//
// - API Key (Dashboard > Resources > {COGNATIVE SERVICES RESOURCE NAME} > Resource Management > Keys)
//
// - Resource Name ('http://{COGNATIVE SERVICES RESOURCE NAME}.cognitiveservices.azure.com')
//
// - Text to be analyzed matching expected format of `{"language": "LANGUAGE", "id": "ID NUMBER", "text": "TEXT"}`.
//
// The API returns a list of known entities and general named entities ("Person", "Location", "Organization" etc) in a given document. Known entities are returned with Wikipedia Id and Wikipedia link, and also Bing Id which can be used in Bing Entity Search API. General named entities are returned with entity types. If a general named entity is also a known entity, then all information regarding it (Wikipedia Id, Bing Id, entity type etc) will be returned.
//
// For more information see the Azure Cognative Service for Text Analytics [Entities documentation](https://westus.dev.cognitive.microsoft.com/docs/services/TextAnalytics-V2-1/operations/5ac4251d5b4ccd1554da7634). See the [Supported Entity Types in Text Analytics API](https://docs.microsoft.com/en-us/azure/cognitive-services/text-analytics/how-tos/text-analytics-how-to-entity-linking#supported-types-for-named-entity-recognition) for the list of supported Entity Types. See the [Supported languages in Text Analytics API](https://docs.microsoft.com/en-us/azure/cognitive-services/text-analytics/text-analytics-supported-languages) for the list of enabled languages.
func Entities(apiKey string, resourceName string, document []map[string]string) string {
// Define the API to make a call to
apiType := "entities"
// Build a new Request struct with the inputs to pass into
request := Request{apiKey, resourceName, apiType, document}
output := apiRequest(request)
return string(output)
}
// Phrases makes a request to the Key Phrase Extration API using the supplied:
//
// - API Key (Dashboard > Resources > {COGNATIVE SERVICES RESOURCE NAME} > Resource Management > Keys)
//
// - Resource Name ('http://{COGNATIVE SERVICES RESOURCE NAME}.cognitiveservices.azure.com')
//
// - Text to be analyzed matching expected format of `{"language": "LANGUAGE", "id": "ID NUMBER", "text": "TEXT"}`.
//
// The API returns a list of strings denoting the key talking points in the input text.
//
// For more information see Azure Cognative Service for Text Analytics [Key Phrases documentation](https://westcentralus.dev.cognitive.microsoft.com/docs/services/TextAnalytics-v2-1/operations/56f30ceeeda5650db055a3c6). See the [Supported languages in Text Analytics API](https://docs.microsoft.com/en-us/azure/cognitive-services/text-analytics/text-analytics-supported-languages) for the list of enabled languages.
func Phrases(apiKey string, resourceName string, document []map[string]string) string {
// Define the API to make a call to
apiType := "keyPhrases"
// Build a new Request struct with the inputs to pass into
request := Request{apiKey, resourceName, apiType, document}
output := apiRequest(request)
return string(output)
}
// Language makes a request to the Language Detection API using the supplied:
//
// - API Key (Dashboard > Resources > {COGNATIVE SERVICES RESOURCE NAME} > Resource Management > Keys)
//
// - Resource Name ('http://{COGNATIVE SERVICES RESOURCE NAME}.cognitiveservices.azure.com')
//
// - Text to be analyzed matching expected format of `{"countryHint": "OPTIONAL", "id": "ID NUMBER", "text": "TEXT"}`.
//
// The API returns the detected language and a numeric score between 0 and 1. Scores close to 1 indicate 100% certainty that the identified language is true. A total of 120 languages are supported.
//
// For more information see Azure Cognative Service for Text Analytics [Languages Detection documentation](https://westcentralus.dev.cognitive.microsoft.com/docs/services/TextAnalytics-v2-1/operations/56f30ceeeda5650db055a3c7).
func Language(apiKey string, resourceName string, document []map[string]string) string {
// Define the API to make a call to
apiType := "languages"
// Build a new Request struct with the inputs to pass into
request := Request{apiKey, resourceName, apiType, document}
output := apiRequest(request)
return string(output)
}
// Sentiment makes a request to the Sentiment Analysis API of the Azure Cognative Service for Text Analytics using the supplied:
//
// - API Key (Dashboard > Resources > {COGNATIVE SERVICES RESOURCE NAME} > Resource Management > Keys)
//
// - Resource Name ('http://{COGNATIVE SERVICES RESOURCE NAME}.cognitiveservices.azure.com')
//
// - Text to be analyzed matching expected format of `{"language": "LANGUAGE", "id": "ID NUMBER", "text": "TEXT"}`.
//
// The API returns a numeric score between 0 and 1. Scores close to 1 indicate positive sentiment, while scores close to 0 indicate negative sentiment. A score of 0.5 indicates the lack of sentiment (e.g. a factoid statement).
//
// For more information see Azure Cognative Service for Text Analytics [Sentiment Analysis documentation](https://westcentralus.dev.cognitive.microsoft.com/docs/services/TextAnalytics-v2-1/operations/56f30ceeeda5650db055a3c9). See the [Supported languages in Text Analytics API](https://docs.microsoft.com/en-us/azure/cognitive-services/text-analytics/text-analytics-supported-languages) for the list of enabled languages.
func Sentiment(apiKey string, resourceName string, document []map[string]string) string {
// Define the API to make a call to
apiType := "sentiment"
// Build a new Request struct with the inputs to pass into
request := Request{apiKey, resourceName, apiType, document}
output := apiRequest(request)
return string(output)
}
func apiRequest(apiRequest Request) []byte {
// If API Key input is blank
if apiRequest.Key == "" {
// Lookup API Key as environment variable 'TEXT_ANALYTICS_SUBSCRIPTION_KEY'
key, ok := os.LookupEnv("TEXT_ANALYTICS_SUBSCRIPTION_KEY")
// If environment variable 'TEXT_ANALYTICS_SUBSCRIPTION_KEY' is false (does not exist)
if !ok {
// No dice
log.Fatal("API Key must be provided as an input or set as environment variable 'TEXT_ANALYTICS_SUBSCRIPTION_KEY'.")
} else {
// Otherwise use environment variable as API Key
apiRequest.Key = key
}
}
// If Resource Name input is blank
if apiRequest.Endpoint == "" {
// Lookup Resource Name as environment variable 'TEXT_ANALYTICS_ENDPOINT'
endpoint, ok := os.LookupEnv("TEXT_ANALYTICS_ENDPOINT")
// If environment variable 'TEXT_ANALYTICS_ENDPOINT' is false (does not exist)
if !ok {
// No dice
log.Fatal("Resource Name must be provided as an input or set as environment variable 'TEXT_ANALYTICS_ENDPOINT'.")
} else {
apiRequest.Endpoint = endpoint
}
}
// Complete the definition of the API Endpoint for sentiment analysis
apiEndpoint := "https://" + apiRequest.Endpoint + ".cognitiveservices.azure.com/text/analytics/v2.1/" + apiRequest.Type
// Ensuring input text to be analyzed encoded in JSON. Address pointer probably unnecessary
documents, err := json.Marshal(&apiRequest.Text)
if err != nil {
log.Fatal("Error marshaling provided text into data\n", err)
}
// Serialize text into Reader for POST request
data := strings.NewReader("{\"documents\": " + string(documents) + "}")
// Define HTTP request client within HTTP and timeout parameters
client := &http.Client{
Timeout: time.Second * 2,
}
// Define HTTP request as POST with API Endpoint and Text for transmission
request, err := http.NewRequest("POST", apiEndpoint, data)
if err != nil {
log.Fatal("Error creating POST request\n", err)
}
// Add Headers to the defined HTTP request
request.Header.Add("Content-Type", "application/json")
request.Header.Add("Ocp-Apim-Subscription-Key", apiRequest.Key)
response, err := client.Do(request)
if err != nil {
log.Fatal("Error on request\n", err)
}
defer response.Body.Close()
body, err := ioutil.ReadAll(response.Body)
if err != nil {
log.Fatal("Error reading response body\n", err)
}
// Define throw-away interface to store the soon-to-be uncoded JSON
var f interface{}
// Uncoding the JSON-encoded response into our throw-away interface
json.Unmarshal(body, &f)
// Format the uncoded JSON into readable JSON
jsonFormatted, err := json.MarshalIndent(f, "", " ")
if err != nil {
log.Fatal("Error producing JSON\n", err)
}
// BOOM
return jsonFormatted
}