-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
executable file
·340 lines (271 loc) · 8.87 KB
/
index.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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
// BASE SETUP
var express = require('express')
var session = require('express-session');
const client = require('./controller/connection.js');
var app = express()
var path = require('path');
var bodyparser = require('body-parser');
const functions = require(__dirname+"/public/js/ext_functions.js");
var port = process.env.PORT || 3000;
var nlp = require(__dirname+"/public/js/natural.js");
//Routes
const user = require('./routes/user');
const historical_learning = require('./routes/historical_learning');
const router = express.Router();
// There is a special routing method which is not derived from any HTTP method.
// This method is used for loading middleware functions at a path for all request methods.
// app.all()
//set our default template engine to "ejs"
// which prevents the need for using file extensions. (pug, ejs...)
app.set('view engine', 'ejs');
//set views for 505, 404 and other view pages
app.set('views', path.join(__dirname, 'views'));
//set path for static files (css, images...)
app.use(express.static(path.join(__dirname, 'public')));
//parse request bodies (req.body / POST )
//app.use(bodyparser());
app.use(bodyparser.urlencoded({ extended: true}));
app.use(bodyparser.json());
app.use(session({
secret: 'chatbot',
resave: false,
saveUninitialized: false,
rolling: true
//obs cookie time is set at signin and signup route.user function
//cookie: { expires: new Date(253402300000000) }
//cookie: { expires: new Date(Date.now() + 6000) }
//cookie: { maxAge: 5000 }
}));
app.use(function(req, res, next){
// if there's a flash message in the session request, make it available in the response, then delete it
res.locals.sessionFlash = req.session.sessionFlash;
res.locals.user = req.session.user;
delete req.session.sessionFlash;
next();
});
//load file of route configs | also called as controllers
//require('./controller/routing.js')(app, { verbose: !module.parent, express: express });
router.get('/', function (req, res){
let adm = false;
let exp = false;
if(req.query){
if(req.query.adm){
adm = true;
}
if(req.query.exp){
exp = true;
res.render('index', {adm: adm, exp: exp});
}else{
res.redirect('/experimento');
}
}else{
res.render('index', {adm: adm, exp: exp});
}
});
router.post('/', function (req, res){
let adm = false;
let exp = false;
let termName = "";
if(req.body){
if(req.body.termName){
exp = true;
termName = req.body.termName;
}
}
res.render('index', {adm: adm, exp: exp, termName: req.body.termName});
});
router.get('/experimento/', function (req, res){
res.render('preexperimento');
});
router.get('/view/', function (req, res){
res.redirect('../');
});
router.post('/signin', user.signin);
router.post('/signup', user.signup);
router.get('/signout', user.signout);
router.get('/login', user.login);
const msgSessionTimeout = 'Tempo de Sessão expirou. Efetue login novamente.';
router.get('/dashboard/', function(req, res){
if(!req.session.user){
req.session.sessionFlash = {
type: "danger",
message: msgSessionTimeout
}
req.session.save(function(err) {
res.redirect('/login');
})
}else{
user.dashboard(req, res);
}
});
router.get('/edit/', function(req, res){
if(!req.session.user){
req.session.sessionFlash = {
type: "danger",
message: msgSessionTimeout
}
req.session.save(function(err) {
res.redirect('/login');
})
}else{
user.edit(req, res);
}
});
router.get('/mural_queixas/', function(req, res){
if(!req.session.user){
req.session.sessionFlash = {
type: "danger",
message: msgSessionTimeout
}
req.session.save(function(err) {
res.redirect('/login');
})
}else{
user.complaints(req, res);
}
});
router.get('/user/getUserInfoById', function(req, res){
if(!req.session.user){
req.session.sessionFlash = {
type: "danger",
message: msgSessionTimeout
}
req.session.save(function(err) {
res.redirect('/login');
})
}else{
user.getUserInfoById(req, res);
}
});
router.post('/update/', function(req, res){
console.log(req.session);
if(!req.session.user){
req.session.sessionFlash = {
type: "danger",
message: msgSessionTimeout
}
req.session.save(function(err) {
res.redirect('/login');
})
}else{
user.update(req, res);
}
});
router.post('/user/getUserInfoById', user.getUserInfoById)
router.post('/user/registerExperiment', user.registerExperiment)
router.post('/historical_learning/create', historical_learning.create);
router.post('/historical_learning/voteclaim', historical_learning.voteClaim);
router.post('/historical_learning/searchMostSimilarClaim', historical_learning.searchMostSimilarClaim);
router.post('/historical_learning/selectClaimById', historical_learning.selectClaimById);
router.post('/historical_learning/searchSimilarClaims', historical_learning.searchSimilarClaims);
//route that handle ajax requests
//in case of post request, all "variable" are passed in req.body
router.post('/ajax/:function', function(req, res){
if(req.params.function === "stopwordsremovalPT"){
//pos = part of speech flag/boolean
var posBool = req.body.posBool;
var claimTagged = "";
let keywords = [];
if(!posBool){
//apply stopword removal to the raw claim. return a object with the claim processed and keywords extracted
let result = functions.stopWordsRemovalPT((req.body.claim).toLowerCase());
//must be 'var' here instead of 'let'
var claim = result.claim;
keywords = result.keywords; //array containing keywords
keywords = functions.removeRepeatedWords(keywords);
}else{
var words = req.body.claim.split(" ");
claimTagged = nlp.posTagger(words);
var claim = "";
//just add as a claim, words that represents a NOUN (NNP,NN or NNPS);
// NN = noun, sigular or mass / NNS = noun, plural
// NNP = proper noun, singular / NNPS = proper noun, plural
var NounTags = ["NNP", "NNPS", "NN", "NNS"];
for(var i=0; i < claimTagged.length; i++){
NounTags.forEach(function(item, index){
if(item == claimTagged[i][1]){
if(claim != "") claim += " ";
claim += claimTagged[i][0];
}
});
}
//separate the keywords based on the new claim
keywords = claim.split(" ");
//detecting and removing empty ''
for(var i=0; i < keywords.length; i++){
if (keywords[i] == ''){
keywords.splice(i, 1);
}
//Removing some error judgment for DT as a NN
if(keywords[i] == "I"){
keywords.splice(i, 1);
}
}
}
//claim is the var that will be sent to the elasticsearch.
console.log("searching for keywords: "+keywords);
claim = { "claim" : claim, "keywords": keywords, "claimTagged": claimTagged}
obj = JSON.stringify(claim);
res.send(obj);
}
});
// route with parameters ex: localhost:8081/elastic/:id
router.get('/elastic/', function(req, res){
/*
var routePath = "Route path: "+req.originalUrl;
var fullUrl = "Request URL: "+ "http://"+req.hostname+":"+port+req.originalUrl;
// body content
var body = "<p>"+routePath+"</p>";
body+= "<p>"+fullUrl+"</p>";
body+= "<p>Params: "+JSON.stringify(req.params)+"</p>";
res.send(body);
*/
let q = req.query.q;
q.replace(/[\\$'"]/g, "\\$&");
//API of elasticsearch.js used to connect the elastic search engine (lucene)
let esParams = {
index: 'cdc',
q: q
}
client.search(esParams).then(function(response){
res.send(response);
});
/*
http.get('http://localhost:9200/cdc/_search?q=\''+q+'\'', function(response) {
//console.log("Got response: " + response.statusCode);
var str = '';
response.on('data', function (chunk) {
str += chunk;
});
response.on('end', function () {
res.send(str);
});
});
*/
//other alternatives at sending content to client
//res.write();
//res.end
//or
//res.set('Content-Type', 'text/html');
//res.send(...);
});
//router contain functions that not allow the request to go beyound it (res.send)
app.use('/', router);
app.use('/public', express.static(__dirname + '/public'));
//ERRORS:
//mount the app requests with the functions
app.use(function (err, req, res, next){
//log it
if (!module.parent) console.error(err.stack);
//error page
res.status(500).render('5xx');
});
//last middleware response: 404 page error
app.use(function(req, res, next){
res.status(404).render('404', { url: req.originalUrl });
});
// START THE SERVER
if(!module.parent) {
app.listen(port);
console.log("Website Server started at the port: "+port);
}