-
Notifications
You must be signed in to change notification settings - Fork 654
Expand file tree
/
Copy pathclient.js
More file actions
49 lines (44 loc) · 1.35 KB
/
client.js
File metadata and controls
49 lines (44 loc) · 1.35 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
'use strict';
// notes:
//
// * This file is bundled by exprss-browserify into bundle.js
//
// * The require('ibm-watson/language_translator/v3') could also be written as require('ibm-watson').LanguageTranslatorV3,
// but that version results in a much larger bundle size.
//
// * Tokens expire after 1 hour. This demo simply fetches a new one for each translation rather than keeping a fresh one.
//
// * fetch() is a modern version of XMLHttpRequest. A pollyfill is available for older browsers: https://github.com/github/fetch
var ToneAnalyzerV3 = require('ibm-watson/tone-analyzer/v3');
var btn = document.getElementById('analyze-btn');
var input = document.getElementById('input');
var output = document.getElementById('output');
/**
* @return {Promise<String>} returns a promise that resolves to a string token
*/
function getToken() {
return fetch('/api/token/tone_analyzer').then(function (response) {
return response.text();
});
}
function analyze(token) {
var toneAnalyzer = new ToneAnalyzerV3({
token: token,
version: '2016-05-19',
});
toneAnalyzer.tone(
{
text: input.value,
},
function (err, result) {
if (err) {
output.innerHTML = err;
return console.log(err);
}
output.innerHTML = JSON.stringify(result, null, 2);
}
);
}
btn.onclick = function () {
getToken().then(analyze);
};