-
Notifications
You must be signed in to change notification settings - Fork 1
/
watson-test.js
46 lines (42 loc) · 1.77 KB
/
watson-test.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
"use strict";
let NLU = require('watson-developer-cloud/natural-language-understanding/v1.js');
let tokenizer = require('string-tokenizer');
module.exports = {
analyse: function () {
let nlu = new NLU({
'username': '3f9b8595-b15b-4432-a413-43b72d93fbf8',
'password': 'HvFtLfWWbccR',
'version': '2018-03-16'
});
return function(text) {
return new Promise(function(resolve, reject) {
let tokens_result = tokenizer().input(text).token('word', /[\w]+/).resolve();
let tokens = tokens_result.word;
let word_lengths = tokens.map(s => s.length);
let parameters = {
'text': text,
'features': {
'emotion': {},
'sentiment': {},
}
};
nlu.analyze(parameters, function (err, response) {
if (err) {
console.log('error:', err);
reject(err);
} else {
var emotions = response.emotion.document.emotion;
var sentiment = response.sentiment.document.score;
// console.log('emotions');
// console.log(JSON.stringify(emotions, null, 2));
// console.log('sentiment');
// console.log(JSON.stringify(sentiment));
// console.log('word counts');
// console.log(word_lengths);
resolve({ 'emotions': emotions, 'sentiment': sentiment, 'word_lengths': word_lengths });
}
});
})
}
}
}