This repository has been archived by the owner on Jun 23, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
64 lines (57 loc) · 1.53 KB
/
server.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
var Hapi = require('hapi');
var server = Hapi.createServer(~~process.env.PORT || 3000, '0.0.0.0');
var db = require('./lib/db');
var natural = require('natural');
var BPromise = require('bluebird');
var _ = require('lodash');
var analyze = require('./analyze');
var topics = db.topics;
var tfidf = db.tfidf;
var getTfidf = BPromise.promisify(tfidf.get, tfidf);
var getTopic = BPromise.promisify(topics.get, topics);
server.pack.register({
plugin: require('good'),
options: {
subscribers: {
console: ['ops', 'error', 'request', 'log']
}
}
}, function(err) { if(err) throw err; });
server.route({
path: '/',
method: 'POST',
config: {
payload: {
output: 'data',
parse: true
}
},
handler: function(req, reply) {
var message = req.payload.message,
bioid = req.payload.bioid;
getTfidf(bioid).
then(function(tfidfJson) {
req.log(['debug', 'database', 'tfidf'],
_.extend({ bioid: bioid }, tfidfJson));
var tfidf = new natural.TfIdf(JSON.parse(tfidfJson));
return tfidf.tfidfs(analyze(message));
}).
then(function(scores) {
var index = _.indexOf(scores, _.max(scores));
if(index === -1) {
return Hapi.error.notFound(bioid+' does not use topics.');
}
else {
return getTopic(bioid+'-'+index).
then(function(topic) {
return { topic: topic };
});
}
}).
catch(function(reason) {
return Hapi.error.internal('uh oh', reason);
}).
then(reply);
}
});
module.exports = server;