-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
executable file
·141 lines (121 loc) · 4.11 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
/**
* 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,
watson = require('watson-developer-cloud'),
async = require('async');
// Bootstrap application settings
require('./config/express')(app);
// if bluemix credentials exists, then override local
var credentials = extend({
username: 'a425694b-f06a-4956-886d-e2e9e66d7c65',
password: 'FPhRNXvLnJ9m',
version: 'v2'
}); // VCAP_SERVICES
var alchemyLanguage = watson.alchemy_language({
api_key: process.env.API_KEY || '446dc444d593ed09eece2c66476c87c269c37896'
});
var corpus_id = process.env.CORPUS_ID || '/corpora/eve6tionsto1/devoxx_corpus1';
var graph_id = process.env.GRAPH_ID || '/graphs/wikipedia/en-20120601';
// Create the service wrapper
var conceptInsights = watson.concept_insights(credentials);
app.get('/api/labelSearch', function(req, res, next) {
var params = extend({
corpus: corpus_id,
prefix: true,
limit: 10,
concepts: true
}, req.query);
conceptInsights.corpora.searchByLabel(params, function(err, results) {
if (err)
return next(err);
else
res.json(results);
});
});
app.get('/api/conceptualSearch', function(req, res, next) {
var params = extend({ corpus: corpus_id, limit: 10 }, req.query);
conceptInsights.corpora.getRelatedDocuments(params, function(err, data) {
if (err)
return next(err);
else {
async.parallel(data.results.map(getPassagesAsync), function(err, documentsWithPassages) {
if (err)
return next(err);
else{
data.results = documentsWithPassages;
res.json(data);
}
});
}
});
});
app.post('/api/extractConceptMentions', function(req, res, next) {
var params = extend({ graph: graph_id }, req.body);
conceptInsights.graphs.annotateText(params, function(err, results) {
if (err)
return next(err);
else
res.json(results);
});
});
/**
* Builds an Async function that get a document and call crop the passages on it.
* @param {[type]} doc The document
* @return {[type]} The document with the passages
*/
var getPassagesAsync = function(doc) {
return function (callback) {
conceptInsights.corpora.getDocument(doc, function(err, fullDoc) {
if (err)
callback(err);
else {
doc = extend(doc, fullDoc);
doc.explanation_tags.forEach(crop.bind(this, doc));
delete doc.parts;
callback(null, doc);
}
});
};
};
/**
* Crop the document text where the tag is.
* @param {Object} doc The document.
* @param {Object} tag The explanation tag.
*/
var crop = function(doc, tag){
var textIndexes = tag.text_index;
var documentText = doc.parts[tag.parts_index].data;
var anchor = documentText.substring(textIndexes[0], textIndexes[1]);
var leftIndex = Math.max(textIndexes[0] - 100, 0);
var rightIndex = Math.min(textIndexes[1] + 100, documentText.length);
var prefix = documentText.substring(leftIndex, textIndexes[0]);
var suffix = documentText.substring(textIndexes[1], rightIndex);
var firstSpace = prefix.indexOf(' ');
if ((firstSpace !== -1) && (firstSpace + 1 < prefix.length))
prefix = prefix.substring(firstSpace + 1);
var lastSpace = suffix.lastIndexOf(' ');
if (lastSpace !== -1)
suffix = suffix.substring(0, lastSpace);
tag.passage = '...' + prefix + '<b>' + anchor + '</b>' + suffix + '...';
};
// error-handler settings
require('./config/error-handler')(app);
var port = process.env.VCAP_APP_PORT || 3000;
app.listen(port);
console.log('listening at:', port);