-
Notifications
You must be signed in to change notification settings - Fork 136
/
Copy pathapp.js
executable file
·183 lines (161 loc) · 5.77 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
174
175
176
177
178
179
180
181
182
183
/**
* 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.
*/
'use strict';
var express = require('express'),
app = express(),
extend = require('util')._extend,
pkg = require('./package.json'),
fs = require('fs'),
trim = require('trim'),
Q = require('q');
// Bootstrap application settings
require('./config/express')(app);
var PROMPT_MOVIE_SELECTED = 'USER CLICKS BOX';
var PROMPT_MOVIES_RETURNED = 'UPDATE NUM_MOVIES';
var PROMPT_CURRENT_INDEX = 'UPDATE CURRENT_INDEX';
var apis = require('./api/services');
var converse = Q.nfbind(apis.dialog.conversation.bind(apis.dialog));
var updateProfile = Q.nfbind(apis.dialog.updateProfile.bind(apis.dialog));
var getIntent = Q.nfbind(apis.classifier.classify.bind(apis.classifier));
var searchMovies = Q.nfbind(apis.movieDB.searchMovies.bind(apis.movieDB));
var getMovieInformation = Q.nfbind(apis.movieDB.getMovieInformation.bind(apis.movieDB));
var log = console.log.bind(null,' ');
// create the conversation
app.post('/api/create_conversation', function(req, res, next) {
converse(req.body)
.then(function(result){
res.json(result[0]);
})
.catch(next);
});
// converse
app.post('/api/conversation', function(req, res, next) {
log('1. classifying user intent');
getIntent({ text: req.body.input })
.then(function(result) {
log('2. updating the dialog profile with the user intent');
var classes = result[0].classes;
var profile = {
client_id: req.body.client_id,
name_values: [
{ name:'Class1', value: classes[0].class_name },
{ name:'Class1_Confidence', value: classes[0].confidence },
{ name:'Class2', value: classes[1].class_name },
{ name:'Class2_Confidence', value: classes[1].confidence }
]
};
return updateProfile(profile);
})
.catch(function(error ){
log('2.', error.description || error);
})
.then(function() {
log('3. calling dialog.conversation()');
return converse(req.body)
.then(function(result) {
var conversation = result[0];
if (searchNow(conversation.response.join(' '))) {
log('4. dialog thinks we have information enough to search for movies');
var searchParameters = parseSearchParameters(conversation);
conversation.response = conversation.response.slice(0, 1);
log('5. searching for movies in themoviedb.com');
return searchMovies(searchParameters)
.then(function(searchResult) {
log('6. updating the dialog profile with the result from themoviedb.com');
var profile = {
client_id: req.body.client_id,
name_values: [
{ name:'Current_Index', value: searchResult.curent_index },
{ name:'Total_Pages', value: searchResult.total_pages },
{ name:'Num_Movies', value: searchResult.total_movies }
]
};
return updateProfile(profile)
.then(function() {
log('7. calling dialog.conversation()');
var params = extend({}, req.body);
if (['new','repeat'].indexOf(searchParameters.page) !== -1)
params.input = PROMPT_MOVIES_RETURNED;
else
params.input = PROMPT_CURRENT_INDEX;
return converse(params)
.then(function(result) {
res.json(extend(result[0], searchResult));
});
});
});
} else {
log('4. not enough information to search for movies, continue the conversation');
res.json(conversation);
}
});
})
.catch(next);
});
function searchNow(message) {
return message.toLowerCase().indexOf('search_now') !== -1;
}
function parseSearchParameters(conversation) {
var params = conversation.response[1].toLowerCase().slice(1, -1);
params = params.replace(/(['"])?([a-zA-Z0-9_]+)(['"])?:/g, '"$2": ');
return JSON.parse(params);
}
app.get('/api/movies', function(req, res, next) {
getMovieInformation(req.query)
.then(function(movie){
var profile = {
client_id: req.body.client_id,
name_values: [
{ name:'Selected_Movie', value: movie.movie_name },
{ name:'Popularity_Score', value: movie.popularity*10 }
]
};
return updateProfile(profile)
.then(function() {
var params = {
client_id: req.query.client_id,
conversation_id: req.query.conversation_id,
input: PROMPT_MOVIE_SELECTED
};
return converse(params)
.then(function(result) {
res.json(extend(result[0], { movies: [movie]}));
});
});
})
.catch(next);
});
/**
* Returns the classifier_id and dialog_id to the user.
*/
app.get('/api/services', function(req, res) {
var services = {};
fs.readFile(__dirname +'/training/dialog_id', 'utf8', function(error, id){
if (!error)
services.dialog_id = trim(id);
fs.readFile(__dirname +'/training/classifier_id', 'utf8', function(error, id){
if (!error)
services.classifier_id = trim(id);
res.json(services);
});
});
});
// error-handler application settings
require('./config/error-handler')(app);
var port = process.env.VCAP_APP_PORT || 3000;
var host = process.env.VCAP_APP_HOST || 'localhost';
app.listen(port);
console.log(pkg.name + ':' + pkg.version, host + ':' + port);