-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathSentiment Analysis.gs
More file actions
79 lines (62 loc) · 2.14 KB
/
Sentiment Analysis.gs
File metadata and controls
79 lines (62 loc) · 2.14 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
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
function onOpen() {
var ui = SpreadsheetApp.getUi();
ui.createMenu('Review Analysis Tools')
.addItem('Analyze Sentiment', 'analyzeSentiment')
.addToUi();
}
function analyzeSentiment() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getActiveSheet();
var startRow = 2;
var startColumn = 1;
var numColumns = sheet.getLastColumn();
var numRows = sheet.getLastRow();
//Get the number of rows
var dataRange = sheet.getRange(startRow,startColumn,numRows-1,numColumns);
var data = dataRange.getValues();
for (var i = 0; i < data.length; ++i) {
var row = data[i];
var review = row[3]; //review column
//Invoke the retrieveSentiment method
var score = retrieveSentiment(review);
//Find the cell in the row (5th column) to insert the Sentiment score.
var range = sheet.getRange(startRow+i,numColumns);
//range.setValue(score);
if (score < 0 && score > -0.25) {
range.setBackground("#fcaeb4");
} else if (score < -0.25) {
range.setBackground("#f7021f");
} else if (score > 0 && score < 0.25) {
range.setBackground("#affcae");
} else if (score > 0.25) {
range.setBackground("#01f774");
} else {
range.setBackground("#f2f701");
}
}
}
function retrieveSentiment (line) {
var apiKey = "AIzaSyB_Tkq7mq4sQQWxz7h--H7YyQmYy6mR4_c";
var apiEndpoint = 'https://language.googleapis.com/v1/documents:analyzeSentiment?key=' + apiKey;
var reviewData = {
language: 'en-us',
type: 'PLAIN_TEXT',
content: line
};
var nlAPIData = {
document: reviewData,
encodingType: 'UTF8'
};
var nlCallOptions = {
method : 'post',
contentType: 'application/json',
payload : JSON.stringify(nlAPIData)
}
var response = UrlFetchApp.fetch(apiEndpoint, nlCallOptions);
var data = JSON.parse(response);
var sentiment = 0.0;
if (data && data.documentSentiment && data.documentSentiment.score){
sentiment = data.documentSentiment.score;
}
return sentiment;
}