-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsentiment.gs
More file actions
42 lines (30 loc) · 1.01 KB
/
sentiment.gs
File metadata and controls
42 lines (30 loc) · 1.01 KB
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
// Sets API key for accessing Cloud Natural Language API.
var myApiKey = "AIzaSyCidrhoYLfZ_ZyCn5FcsWByunUYBY6oUFM";
function getSentiment(feedback){
var resp = retrieveEntitySentiment(feedback);
var sentiment = (resp.documentSentiment.score *2)-1;
return sentiment;
}
function retrieveEntitySentiment(line) {
var apiKey = myApiKey;
var apiEndpoint = 'https://language.googleapis.com/v1/documents:analyzeSentiment?key=' + apiKey;
// Creates a JSON request, with text string, language, type and encoding
var nlData = {
document: {
language: 'en-us',
type: 'PLAIN_TEXT',
content: line
},
encodingType: 'UTF8'
};
// Packages all of the options and the data together for the API call.
var nlOptions = {
method : 'post',
contentType: 'application/json',
payload : JSON.stringify(nlData)
};
// Makes the API call.
var response = UrlFetchApp.fetch(apiEndpoint, nlOptions);
console.log(JSON.parse(response));
return JSON.parse(response);
};