This repository has been archived by the owner on May 10, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 63
/
app.js
86 lines (70 loc) · 2 KB
/
app.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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
'use strict';
const express = require('express');
const app = express();
const ToneAnalyzerV3 = require('watson-developer-cloud/tone-analyzer/v3');
const bodyParser = require('body-parser');
require('dotenv').config({silent: true});
// Create the service wrapper
const toneAnalyzer = new ToneAnalyzerV3({
version: '2017-09-21',
});
app.use(bodyParser.json());
app.use(express.static('public'));
function createToneRequest (request) {
let toneChatRequest;
if (request.texts) {
toneChatRequest = {utterances: []};
for (let i in request.texts) {
const utterance = {text: request.texts[i]};
toneChatRequest.utterances.push(utterance);
}
}
return toneChatRequest;
}
function happyOrUnhappy (response) {
const happyTones = ['satisfied', 'excited', 'polite', 'sympathetic'];
const unhappyTones = ['sad', 'frustrated', 'impolite'];
let happyValue = 0;
let unhappyValue = 0;
for (let i in response.utterances_tone) {
const utteranceTones = response.utterances_tone[i].tones;
for (let j in utteranceTones) {
if (happyTones.includes(utteranceTones[j].tone_id)) {
happyValue = happyValue + utteranceTones[j].score;
}
if (unhappyTones.includes(utteranceTones[j].tone_id)) {
unhappyValue = unhappyValue + utteranceTones[j].score;
}
}
}
if (happyValue >= unhappyValue) {
return 'happy';
}
else {
return 'unhappy';
}
}
/* Example
{
"texts": ["I do not like what I see", "I like very much what you have said."]
}
*/
app.post('/tone', (req, res, next) => {
const toneRequest = createToneRequest(req.body);
if (toneRequest) {
toneAnalyzer.toneChat(toneRequest, (err, response) => {
if (err) {
return next(err);
}
const answer = {mood: happyOrUnhappy(response)};
return res.json(answer);
});
}
else {
return res.status(400).send({error: 'Invalid Input'});
}
});
const port = process.env.PORT || 3000;
app.listen(port, () => {
console.log('Server running on port: %d', port);
});