Skip to content

Commit 721993c

Browse files
authored
Merge pull request #143 from watson-developer-cloud/master
Rebasing
2 parents e98a06d + cc7dbb3 commit 721993c

File tree

4 files changed

+78
-10
lines changed

4 files changed

+78
-10
lines changed

README.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ Node-RED Watson Nodes for IBM Cloud
77

88
<a href="https://cla-assistant.io/watson-developer-cloud/node-red-node-watson"><img src="https://cla-assistant.io/readme/badge/watson-developer-cloud/node-red-node-watson" alt="CLA assistant" /></a>
99

10+
### New in version 0.6.9
11+
- Implemented classify collection on Natural Language Classifier node. The collection can be
12+
in the form of multiple sentences, an array of strings, or an array of objects.
13+
1014
### New in version 0.6.8
1115
- Move all Discovery calls to latest API version - 2017-11-07
1216
- Updated calls to deprecated discovery methods addJsonDocument and getCollections
@@ -61,7 +65,7 @@ out for sample flows and templates showing how to use this feature.
6165
- Text Recognition option removed from Visual Recognition Node
6266
- Move Retrieve and Rank nodes to deprecated list
6367
- Removed Document Conversion, Alchemy, Concept Insights, Dialog, Tradeoff Analytics and Similarity Search Nodes
64-
- Nodes deprecated in 0.5.x will be removed in 0.5.x releases
68+
- Nodes deprecated in 0.5.x will be removed in 0.6.x releases
6569

6670

6771
### Watson Nodes for Node-RED

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "node-red-node-watson",
3-
"version": "0.6.8",
3+
"version": "0.6.9",
44
"description": "A collection of Node-RED nodes for IBM Watson services",
55
"dependencies": {
66
"async": "^1.5.2",

services/natural_language_classifier/v1.html

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,17 @@
8181
<p>Set the classifier id in the node configuration window.</p>
8282
<p>The classifier can be overriden by setting msg.nlcparams.classifier_id</p>
8383
<p>The text to classify should be passed in on <code>msg.payload</code>.</p>
84+
85+
<p> If <code>msg.payload</code> contains multiple sentences then the sentences will
86+
be split and the set will be classified as a collection. If there are no sentence
87+
terminators then it will not be recognised as a sentence.
88+
</p>
89+
<p> If <code>msg.payload</code> contains an array of strings, then each string will be
90+
treated as a separate sentence and the set treated as a collection. </p>
91+
<p>If <code>msg.payload</code> contains an array of objects, then there is a
92+
search for the <code>text</code> key in each object. Each text object will be
93+
treated as part of the collection.</p>
94+
8495
<p>The returned classification and confidence values will be returned on <code>msg.payload</code>.</p>
8596
<p></p>
8697
<p><b>Training Mode</b>.</p>

services/natural_language_classifier/v1.js

Lines changed: 61 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,33 @@ module.exports = function(RED) {
7979
}
8080
};
8181

82+
83+
node.payloadCollectionCheck = function(msg, config, payloadData) {
84+
if ('classify' === config.mode) {
85+
if ('string' === typeof msg.payload) {
86+
let collection = msg.payload.match( /\(?[^\.\?\!]+[\.!\?$]\)?/g );
87+
if (collection && collection.length > 1) {
88+
payloadData.collection = [];
89+
collection.forEach((s) => {
90+
let textObject = { text : s };
91+
payloadData.collection.push(textObject);
92+
});
93+
}
94+
} else if (Array.isArray(msg.payload)){
95+
payloadData.collection = [];
96+
msg.payload.forEach((p) => {
97+
if ('string' === typeof p) {
98+
let textObject = { text : p };
99+
payloadData.collection.push(textObject);
100+
} else if ('object' === typeof p) {
101+
payloadData.collection.push(p);
102+
}
103+
});
104+
}
105+
}
106+
return Promise.resolve();
107+
};
108+
82109
// Standard temp file open
83110
node.openTemp = function() {
84111
var p = new Promise(function resolver(resolve, reject) {
@@ -118,13 +145,18 @@ module.exports = function(RED) {
118145
}
119146
};
120147

121-
node.buildParams = function(msg, config, info) {
148+
node.buildParams = function(msg, config, info, payloadData) {
122149
var params = {},
123150
message = '';
124151

125152
switch (config.mode) {
126153
case 'classify':
127-
params.text = msg.payload;
154+
if (payloadData && payloadData.collection) {
155+
params.collection = payloadData.collection;
156+
} else {
157+
params.text = msg.payload;
158+
}
159+
128160
params.classifier_id = config.classifier;
129161
if (msg.nlcparams && msg.nlcparams.classifier_id) {
130162
params.classifier_id = msg.nlcparams.classifier_id;
@@ -170,14 +202,31 @@ module.exports = function(RED) {
170202

171203
natural_language_classifier = new NaturalLanguageClassifierV1(serviceSettings);
172204

173-
natural_language_classifier[config.mode](params, function(err, response) {
205+
let mode = config.mode;
206+
if (params.collection) {
207+
mode = 'classifyCollection';
208+
}
209+
210+
natural_language_classifier[mode](params, function(err, response) {
174211
if (err) {
175212
reject(err);
176213
} else {
177-
msg.payload = (config.mode === 'classify') ? {
178-
classes: response.classes,
179-
top_class: response.top_class
180-
} : response;
214+
console.log(response);
215+
switch (mode) {
216+
case 'classify':
217+
msg.payload = {
218+
classes: response.classes,
219+
top_class: response.top_class
220+
};
221+
break;
222+
case 'classifyCollection':
223+
msg.payload = {
224+
collection: response.collection
225+
};
226+
break;
227+
default:
228+
msg.payload = response;
229+
}
181230
resolve();
182231
}
183232
});
@@ -189,15 +238,19 @@ module.exports = function(RED) {
189238

190239
this.on('input', function(msg) {
191240
//var params = {}
241+
let payloadData = {};
192242
node.verifyCredentials(msg)
193243
.then(function() {
194244
return node.payloadCheck(msg);
195245
})
246+
.then(function() {
247+
return node.payloadCollectionCheck(msg, config, payloadData);
248+
})
196249
.then(function() {
197250
return node.checkForCreate(msg, config);
198251
})
199252
.then(function(info) {
200-
return node.buildParams(msg, config, info);
253+
return node.buildParams(msg, config, info, payloadData);
201254
})
202255
.then(function(params) {
203256
node.status({

0 commit comments

Comments
 (0)