Skip to content

Commit d75431a

Browse files
authored
Merge pull request #336 from chughts/langfix
Language updates for Tone Analyzer and Discovery
2 parents 4773fce + 6de4bb3 commit d75431a

File tree

9 files changed

+151
-10
lines changed

9 files changed

+151
-10
lines changed

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,12 @@ Node-RED Watson Nodes for IBM Bluemix
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.5.15
11+
- Allow language code to be specified on create collection in Discovery Node.
12+
- Implement delete collection, delete environment in Discovery Node.
13+
- Allow multiple interface versions for Tone Analyzer Node.
14+
- Enable Content, en or fr, in Tone Analyzer Node.
15+
1016
### New in version 0.5.14
1117
- Bump to latest version of watson-developer-cloud node.js sdk
1218
- Allow empty input into converse for Conversation Node

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "node-red-node-watson",
3-
"version": "0.5.14",
3+
"version": "0.5.15",
44
"description": "A collection of Node-RED nodes for IBM Watson services",
55
"dependencies": {
66
"alchemy-api": "^1.3.0",
@@ -11,7 +11,7 @@
1111
"temp": "^0.8.3",
1212
"qs": "6.x",
1313
"image-type": "^2.0.2",
14-
"watson-developer-cloud": "^2.39.2",
14+
"watson-developer-cloud": "^2.40.0",
1515
"kuromoji": "^0.1.1",
1616
"is-docx": "^0.0.3",
1717
"stream-to-array" : "^2.3.0"

services/discovery/discovery-utils.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ DiscoveryUtils.prototype = {
8686
params = me.buildParamsForQuery(msg, config, params);
8787

8888
['environment_id', 'collection_id', 'configuration_id',
89-
'collection_name',
89+
'collection_name', 'language_code',
9090
'passages', 'description', 'size'
9191
].forEach(function(f) {
9292
params = me.buildParamsFor(msg, config, params, f);

services/discovery/v1-query-builder.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ module.exports = function(RED) {
4343
username: sUsername ? sUsername : req.query.un,
4444
password: sPassword ? sPassword : req.query.pwd,
4545
url: req.query.endpoint ? req.query.endpoint : sEndpoint,
46-
version_date: '2017-08-01',
46+
version_date: '2017-09-01',
4747
headers: {
4848
'User-Agent': pkg.name + '-' + pkg.version
4949
}
@@ -67,7 +67,7 @@ module.exports = function(RED) {
6767
username: sUsername ? sUsername : req.query.un,
6868
password: sPassword ? sPassword : req.query.pwd,
6969
url: req.query.endpoint ? req.query.endpoint : sEndpoint,
70-
version_date: '2017-08-01',
70+
version_date: '2017-09-01',
7171
headers: {
7272
'User-Agent': pkg.name + '-' + pkg.version
7373
}
@@ -91,8 +91,8 @@ module.exports = function(RED) {
9191
var discovery = new DiscoveryV1({
9292
username: sUsername ? sUsername : req.query.un,
9393
password: sPassword ? sPassword : req.query.pwd,
94-
url: req.query.endpoint ? req.query.endpoint : sEndpoint,
95-
version_date: '2017-08-01',
94+
url: req.query.endpoint ? req.query.endpoint : sEndpoint,
95+
version_date: '2017-09-01',
9696
headers: {
9797
'User-Agent': pkg.name + '-' + pkg.version
9898
}

services/discovery/v1.html

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,11 @@
5050
<option value="createEnvrionment">Create new environment</option>
5151
<option value="listEnvrionments">List existing environments</option>
5252
<option value="getEnvironmentDetails">Retrieve environment details</option>
53+
<option value="deleteEnvironment">Delete environment</option>
5354
<option value="createCollection">Create new collection</option>
5455
<option value="listCollections">List existing collections</option>
5556
<option value="getCollectionDetails">Retrieve collection details</option>
57+
<option value="deleteCollection">Delete collection</option>
5658
<option value="createConfiguration">Create new configuration</option>
5759
<option value="listConfigurations">List existing configurations</option>
5860
<option value="getConfigurationDetails">Retrieve configuration details</option>
@@ -83,6 +85,21 @@
8385
<label for="node-input-configuration_id"><i class="fa fa-tag"></i> Configuration ID</label>
8486
<input type="text" id="node-input-configuration_id" placeholder="">
8587
</div>
88+
89+
<div class="form-row">
90+
<label for="node-input-language_code"><i class="fa fa-language"></i> Language: </label>
91+
<select type="text" id="node-input-language_code" style="display: inline-block; width: 70%;">
92+
<option value="en">English</option>
93+
<option value="de">German</option>
94+
<option value="es">Spanish</option>
95+
<option value="ar">Arabic</option>
96+
<option value="fr">French</option>
97+
<option value="it">Italian</option>
98+
<option value="ja">Japanese</option>
99+
<option value="pt-br">Brazilian Portuguese</option>
100+
</select>
101+
</div>
102+
86103
<div class="form-row">
87104
<label for="node-input-count"><i class="fa fa-tag"></i> Number of documents</label>
88105
<input type="text" id="node-input-count" placeholder="">
@@ -180,6 +197,25 @@
180197
<li><code>msg.environment_details</code> : Details of the requested environment.</li>
181198
</ul>
182199
</li>
200+
<li>
201+
<p><b>Create New Collection</b><p>
202+
<p>For this method the node needs an Environment ID, Collection Name,
203+
Description, Configuration ID and Language Code as input.
204+
</p>
205+
<p>The fields can be overridden by specifying
206+
in <code>msg.discoveryparams.environment_id</code>
207+
, <code>msg.discoveryparams.collection_name</code>
208+
, <code>msg.discoveryparams.description</code>
209+
, <code>msg.discoveryparams.configuration_id</code>
210+
and <code>msg.discoveryparams.language_code</code>
211+
</p>
212+
213+
<p>Node output : </p>
214+
<ul>
215+
<li><code>msg.collection</code> : Information about the
216+
newly created collection.</li>
217+
</ul>
218+
</li>
183219
<br/>
184220
<li>
185221
<p><b>List Existing Collections</b><p>
@@ -295,6 +331,7 @@
295331
$('#node-input-collection_id').parent().hide();
296332
$('#node-input-configurationname').parent().hide();
297333
$('#node-input-configuration_id').parent().hide();
334+
$('#node-input-language_code').parent().hide();
298335
$('#node-input-count').parent().hide();
299336
$('#node-input-passages').parent().hide();
300337
$('#node-input-nlp_query').parent().hide();
@@ -327,15 +364,18 @@
327364
case 'createCollection':
328365
fields.push('#node-input-environment_id'
329366
+ ', #node-input-configuration_id'
367+
+ ', #node-input-language_code'
330368
+ ', #node-input-collection_name'
331369
+ ', #node-input-description');
332370
break;
333371
case 'createConfiguration':
334372
case 'getEnvironmentDetails':
335373
case 'listCollections':
374+
case 'deleteEnvironment':
336375
fields.push('#node-input-environment_id');
337376
break;
338377
case 'getCollectionDetails':
378+
case 'deleteCollection':
339379
fields.push('#node-input-environment_id'
340380
+ ', #node-input-collection_id');
341381
break;
@@ -407,6 +447,7 @@
407447
collection_id: {value: ''},
408448
configurationname: {value: ''},
409449
configuration_id: {value: ''},
450+
language_code: {value: 'en'},
410451
collection_name: {value: ''},
411452
count: {value: '1'},
412453
passages: {value: 'false'},

services/discovery/v1.js

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,6 @@ module.exports = function (RED) {
118118
// //delete params[f];
119119
//});
120120

121-
//console.log('about to create collection with params : ', params);
122121
discovery.createCollection(params, function (err, response) {
123122
if (err) {
124123
reject(err);
@@ -131,6 +130,33 @@ module.exports = function (RED) {
131130
return p;
132131
}
133132

133+
function executeDeleteCollection(node, discovery, params, msg) {
134+
var p = new Promise(function resolver(resolve, reject){
135+
discovery.deleteCollection(params, function (err, response) {
136+
if (err) {
137+
reject(err);
138+
} else {
139+
msg.collection = response.collection ? response.collection : response;
140+
resolve();
141+
}
142+
});
143+
});
144+
return p;
145+
}
146+
147+
function executeDeleteEnvironment(node, discovery, params, msg) {
148+
var p = new Promise(function resolver(resolve, reject){
149+
discovery.deleteEnvironment(params, function (err, response) {
150+
if (err) {
151+
reject(err);
152+
} else {
153+
msg.environment = response.environment ? response.environment : response;
154+
resolve();
155+
}
156+
});
157+
});
158+
return p;
159+
}
134160

135161
function executeListCollections(node, discovery, params, msg) {
136162
var p = new Promise(function resolver(resolve, reject){
@@ -225,7 +251,7 @@ module.exports = function (RED) {
225251
serviceSettings = {
226252
username: username,
227253
password: password,
228-
version_date: '2017-08-01',
254+
version_date: '2017-09-01',
229255
headers: {
230256
'User-Agent': pkg.name + '-' + pkg.version
231257
}
@@ -256,6 +282,9 @@ module.exports = function (RED) {
256282
case 'getCollectionDetails':
257283
p = executeGetCollectionDetails(node, discovery, params, msg);
258284
break;
285+
case 'deleteCollection':
286+
p = executeDeleteCollection(node, discovery, params, msg);
287+
break;
259288
case 'createConfiguration':
260289
p = executeCreateConfiguration(node, discovery, params, msg);
261290
break;
@@ -265,6 +294,9 @@ module.exports = function (RED) {
265294
case 'getConfigurationDetails':
266295
p = executeGetConfigurationDetails(node, discovery, params, msg);
267296
break;
297+
case 'deleteEnvironment':
298+
p = executeDeleteEnvironment(node, discovery, params, msg);
299+
break;
268300
case 'query':
269301
p = executeQuery(node, discovery, params, msg);
270302
break;

services/tone_analyzer/v3.html

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,14 @@
5151
</select>
5252
</div>
5353

54+
<div class="form-row">
55+
<label for="node-input-interface-version"><i class="fa fa-history"></i> version_date: </label>
56+
<select type="text" id="node-input-interface-version" style="display: inline-block; width: 70%;">
57+
<option value="2016-05-19">Multiple Tones</option>
58+
<option value="2017-09-21">Dominant Tone</option>
59+
</select>
60+
</div>
61+
5462
<div class="form-row">
5563
<label for="node-input-tones"><i class="fa fa-comments-o"></i> Tones</label>
5664
<select type="text" id="node-input-tones" style="display: inline-block; width: 70%;" >
@@ -75,6 +83,14 @@
7583
</select>
7684
</div>
7785

86+
<div class="form-row">
87+
<label for="node-input-inputlang"><i class="fa fa-language"></i> Input Text Language</label>
88+
<select type="text" id="node-input-inputlang" style="display: inline-block; width: 70%;" >
89+
<option value="en">English</option>
90+
<option value="fr">French</option>
91+
</select>
92+
</div>
93+
7894
</script>
7995

8096
<script type="text/x-red" data-help-name="watson-tone-analyzer-v3">
@@ -115,6 +131,7 @@
115131
fields.push('#node-input-tones');
116132
fields.push('#node-input-sentences');
117133
fields.push('#node-input-contentType');
134+
fields.push('#node-input-inputlang');
118135
ToneV3.hideSelectedFields(fields);
119136
};
120137

@@ -126,18 +143,37 @@
126143
fields.push('#node-input-tones');
127144
fields.push('#node-input-sentences');
128145
fields.push('#node-input-contentType');
146+
fields.push('#node-input-inputlang');
129147
break;
130148
}
131149
ToneV3.showSelectedFields(fields);
132150
}
133151

152+
ToneV3.processSelectedVersion = function(method, version) {
153+
if ('generalTone' == method)
154+
{
155+
fields = [];
156+
fields.push('#node-input-tones');
157+
if ('2016-05-19' == version) {
158+
ToneV3.showSelectedFields(fields);
159+
} else {
160+
ToneV3.hideSelectedFields(fields);
161+
}
162+
}
163+
}
134164

135165
ToneV3.UIListeners = function () {
136166
$('#node-input-tone-method').change(function(val){
137167
var method = $('#node-input-tone-method').val();
138168
ToneV3.processSelectedMethod(method);
139169
});
140170

171+
$('#node-input-interface-version').change(function(val){
172+
var version = $('#node-input-interface-version').val();
173+
var method = $('#node-input-tone-method').val();
174+
ToneV3.processSelectedVersion(method, version);
175+
});
176+
141177
$('#node-input-default-endpoint').change(function () {
142178
var checked = $('#node-input-default-endpoint').prop('checked')
143179
if (checked) {
@@ -176,6 +212,9 @@
176212
sentences: {value: "true"},
177213
contentType: {value: "false"},
178214
"tone-method": {value:"generalTone"},
215+
"interface-version": {value:"generalTone"},
216+
name: {value: ""},
217+
inputlang: {value: "en"},
179218
'default-endpoint' :{value: true},
180219
'service-endpoint' :{value: 'https://gateway.watsonplatform.net/tone-analyzer/api'}
181220
},

services/tone_analyzer/v3.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ module.exports = function (RED) {
9494
var serviceSettings = {
9595
'username': settings.username,
9696
'password': settings.password,
97-
version_date: '2016-05-19',
97+
version_date: '2017-09-21',
9898
headers: {
9999
'User-Agent': pkg.name + '-' + pkg.version
100100
}
@@ -109,6 +109,10 @@ module.exports = function (RED) {
109109
serviceSettings.url = endpoint;
110110
}
111111

112+
if (config['interface-version']) {
113+
serviceSettings.version_date = config['interface-version'];
114+
}
115+
112116
const tone_analyzer = new ToneAnalyzerV3(serviceSettings);
113117

114118
var p = new Promise(function resolver(resolve, reject){
@@ -120,6 +124,7 @@ module.exports = function (RED) {
120124
m = 'tone_chat';
121125
break;
122126
}
127+
123128
tone_analyzer[m](options, function (err, response) {
124129
if (err) {
125130
reject(err);
@@ -138,6 +143,7 @@ module.exports = function (RED) {
138143
checkConfiguration(msg, node)
139144
.then(function(settings) {
140145
var options = toneutils.parseOptions(msg, config);
146+
options = toneutils.parseLanguage(msg, config, options);
141147
node.status({fill:'blue', shape:'dot', text:'requesting'});
142148
return invokeService(config, options, settings);
143149
})

utilities/tone-utils.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,23 @@ ToneUtils.prototype = {
8686
break;
8787
}
8888

89+
return options;
90+
},
91+
92+
// function to splice in language options into header
93+
parseLanguage: function(msg, config, options) {
94+
var inputlang = config.inputlang ? config.inputlang : 'en';
95+
//outputlang = config.outputlang ? config.outputlang : 'en';
96+
97+
// The SDK is currently ignoring this, but this is how it should be done.
98+
// If you need it, then Personality Insights as full set of accept-language
99+
//options.headers = {
100+
// 'content-language': inputlang,
101+
// 'accept-language': outputlang
102+
//}
103+
104+
// This is how it is currently working.
105+
options.language = inputlang;
89106
return options;
90107
}
91108

0 commit comments

Comments
 (0)