-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdiscoveryQuery.js
136 lines (117 loc) · 3.64 KB
/
discoveryQuery.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
var request = require('request');
var queryUri = 'https://gateway.watsonplatform.net/discovery/api/v1/environments/'+process.env.DISCOVERY_ENVIRONMENT_ID+'/collections/'+process.env.DISCOVERY_COLLECTION_ID+'/query';
var queryObject = {
uri: queryUri,
method: 'GET',
auth: {
user: process.env.DISCOVERY_USERNAME,
pass: process.env.DISCOVERY_PASSWORD
}
};
/**
* Query Watson Discovery Service to get results
* @param {String} author - The author of the article
* @param {requestCallback} callback - Callback.
*/
function getTextsByAuthor(author, callback){
//Discovery Service query string
queryObject.qs = {
version: process.env.DISCOVERY_VERSION,
query: 'author:'+author,
sort: '-publication_date',
count: 50,
return: 'text'
};
request(queryObject, handleResponse);
// Callback from request to handle HTTP response
function handleResponse(err, httpResponse, body){
if(err){
console.log(err);
return callback(err);
}
else{
var results = [];
if(body){
var jsonBody = JSON.parse(body);
if(jsonBody.results){
results = jsonBody.results;
}
}
return callback(false, results);
}
};
}
/**
* Query Watson Discovery Service to get results
* @param {String} name - The names a player goes by ('Rafael Nadal').
* @param {requestCallback} callback - Callback.
*/
function getAuthorsByCategory(category, callback){
console.log('getting authors for category ' + category);
//Discovery Service query string
queryObject.qs = {
version: process.env.DISCOVERY_VERSION,
aggregation: 'filter(enriched_text.categories.label::'+category+').'
+ 'term(author, count:50)',
count:0
};
request(queryObject, handleResponse);
// Callback from request to handle HTTP response
function handleResponse(err, httpResponse, body){
if(err){
console.log(err);
return callback(err);
}
else{
var aggregations = [];
if(body){
var jsonBody = JSON.parse(body);
//console.dir(jsonBody,{depth:6});
if(jsonBody.aggregations && jsonBody.aggregations[0].aggregations && jsonBody.aggregations[0].aggregations[0].results){
aggregations = jsonBody.aggregations[0].aggregations[0].results;
}
}
return callback(false, aggregations);
}
};
}
/**
* Query Watson Discovery Service to get results
* @param {String} name - The names a player goes by ('Rafael Nadal').
* @param {String} author - The author of the article
* @param {requestCallback} callback - Callback.
*/
function getSentimentByAuthor(author, callback){
console.log('analyse wds sentiment by author:' + author);
//Discovery Service query string
queryObject.qs = {
version: process.env.DISCOVERY_VERSION,
aggregation: 'filter(author::'+author+')'
+ '.term(enriched_text.sentiment.document.label)',
count:0
};
request(queryObject, handleResponse);
// Callback from request to handle HTTP response
function handleResponse(err, httpResponse, body){
if(err){
console.log(err);
return callback(err);
}
else{
//console.dir(body);
var aggregations = [];
if(body){
var jsonBody = JSON.parse(body);
if(jsonBody.aggregations && jsonBody.aggregations[0].aggregations && jsonBody.aggregations[0].aggregations[0].results){
aggregations = jsonBody.aggregations[0].aggregations[0].results;
}
}
return callback(false, aggregations);
}
};
}
module.exports = {
getAuthorsByCategory: getAuthorsByCategory,
getSentimentByAuthor: getSentimentByAuthor,
getTextsByAuthor: getTextsByAuthor
};