-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
55 lines (48 loc) · 1.79 KB
/
index.js
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
/* Javascript for index.html
* Author: Sungjoon Steve Won / swon331@gmail.com / www.damkee.com
* Date: August 8, 2016
* Javascript template for using Microsoft Cognitive Services - Text Analytics
*/
function calculateSentiment(sentenceIn) {
var documentsVal = [{
"language": "en",
"id":1,
"text": sentenceIn
}];
var documentsKey = {
"documents": documentsVal
};
console.log("Stringify: " + JSON.stringify(documentsKey, null, 2));
//For Ocp-Apim-Subscription-Key, look for key (not subscription ID) from Azure
$.ajax({
type: 'POST',
url: "https://westus.api.cognitive.microsoft.com/text/analytics/v2.0/sentiment",
headers: {
"Ocp-Apim-Subscription-Key": "yourownkey",
"Content-Type": "application/json",
"Accept": "application/json"},
data: JSON.stringify(documentsKey, null, 2),
dataType: "json",
success: function (result) {
switch (result) {
case true:
console.log("success 1: " + result.toString());
break;
default:
console.log("success 2: " + JSON.stringify(result));
var documents = result.documents;
for (var i = 0; i < documents.length; i++) {
var iterResult = documents[i];
var iterScore = iterResult.score;
var iterId = iterResult.id;
}
}
},
error: function (xhr, ajaxOptions, thrownError) {
console.log("Error statue: " +
xhr.status);
console.log("Thrown error: " + thrownError);
}
});
}
calculateSentiment('I love burgers');