forked from MiumiuCodelicious/dialog-nodejs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
executable file
·173 lines (147 loc) · 5.05 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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
/**
* Copyright 2015 IBM Corp. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/*eslint-env node */
'use strict';
var express = require('express'),
app = express(),
bluemix = require('./config/bluemix'),
extend = require('util')._extend,
watson = require('watson-developer-cloud');
// Bootstrap application settings
require('./config/express')(app);
// if bluemix credentials exists, then override local
var credentialsNLC = extend({
version: 'v1',
url : 'https://gateway.watsonplatform.net/natural-language-classifier/api',
username : 'abc2706e-43c4-440e-8e01-5428aeb43666',
password : 'QIFQWsr7PsMX',
}, bluemix.getServiceCreds('natural_language_classifier')); // VCAP_SERVICES
// if bluemix credentials exists, then override local
var credentialsDialog = extend({
url: 'https://gateway.watsonplatform.net/dialog/api',
username: 'c0820949-c9c3-4f4b-9932-26278ef3cad7',
password: '7DKt5aDAcnpx',
version: 'v1'
}, bluemix.getServiceCreds('dialog')); // VCAP_SERVICES
// Create the service wrapper
var nlClassifier = watson.natural_language_classifier(credentialsNLC);
// Create the service wrapper
var dialog = watson.dialog(credentialsDialog);
// render index page
app.get('/', function(req, res) {
res.render('index');
});
function setDialogID(classification) {
switch (classification) {
case "empadronamiento":
return process.env.DIALOG_ID || "8e6e51ac-3702-4515-86ff-90c48890ab5f"; //"e322d14a-7df3-40b3-bed8-f39311e628f6"; //process.env.PADRON_ID;
default:
return process.env.DIALOG_ID || "8e6e51ac-3702-4515-86ff-90c48890ab5f"; //return process.env.DEFAULT_ID;
}
}
function containsString(array, arrayStrings) {
for (var i = 0; i < arrayStrings.length; i++) {
if(array.indexOf(arrayStrings[i]) > -1 )
{
return true;
}
}
return false;
}
app.post('/classification', function(req, res, next) {
console.log ("Estoy en /classification");
var params = {
classifier: "563C46x20-nlc-4274", // CLASSIFIER_ID general para especificar el dialogo
text: req.body.text
};
nlClassifier.classify(params, function(err, results) {
if (err)
return next(err);
else
res.json({dialog: setDialogID(results.top_class), result: results.top_class, confidence: results.classes[0].confidence});
});
});
app.post('/conversation', function(req, res, next) {
console.log ("Estoy en /conversation");
var params = req.body;
//console.log(params);
dialog.conversation(params, function(err, results) {
if (err){
return next(err);
}
// Estos results.response hay que cambiarlos por códigos de error
var badResponseStrings = ["Lo siento, no le he entendido, por favor, reformule su consulta con otras palabras",
"Aún estoy en fase de aprendizaje sobre los trámites del padrón, ¿podría decirlo de otra forma, por favor?",
"Lo siento, no le he entendido, ¿podría repetírmelo?"];
if (containsString(results.response, badResponseStrings) || results.confidence === 0 || results.response.length === 0 )
{
console.log('rellamada');
var paramsClassifier = {
classifier: process.env.CLASSIFIER_SPECIFIC_ID || "c7fa4ax22-nlc-1977", // CLASSIFIER_ID especifico para saber la pregunta concreta a la que nos referimos
text: req.body.input
};
nlClassifier.classify(paramsClassifier, function(err, resultsClassifier) {
if (err){
return next(err);
}
if(resultsClassifier.classes[0].confidence > 0.4){
params.input = resultsClassifier.top_class;
}
dialog.conversation(params, function(err, resultsSegun) {
if (err){
return next(err);
}
res.json({ dialog_id: req.body.dialog_id,
conversationOrigin : results,
nlcParameter : paramsClassifier ,
nlc: resultsClassifier,
conversationParameters : params ,
conversation: resultsSegun
});
});
});
} else
res.json({ dialog_id: req.body.dialog_id, conversation: results, nlc: undefined});
});
});
app.post('/profile', function(req, res, next) {
var params = req.body;
dialog.getProfile(params, function(err, results) {
if (err)
return next(err);
else
res.json(results);
});
});
// catch 404 and forward to error handler
app.use(function(req, res, next) {
var err = new Error('Not Found');
err.code = 404;
err.message = 'Not Found';
next(err);
});
// error handler
app.use(function(err, req, res, next) {
var error = {
code: err.code || 500,
error: err.message || err.error
};
console.log('error:', error);
res.status(error.code).json(error);
});
var port = process.env.VCAP_APP_PORT || 3000;
app.listen(port);
console.log('listening at:', port);