Skip to content

Commit 32fd6c3

Browse files
committed
Merge branch 'master' into issue_66_dynamic_cluster_name
2 parents f67c744 + 0789f83 commit 32fd6c3

6 files changed

Lines changed: 334 additions & 1 deletion

File tree

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@ Node-RED Watson Nodes for IBM Bluemix
77
- The transcription returned for the Speech to Text node now returns full (untruncated) transcription. The
88
alternatives are returned in msg.fullresult. The dialog now loads available models dynamically. This will
99
allow new speech models to be identified without requiring a further code change.
10+
- The Retrieve and Rank node nows stores credentials in a configuration node, allowing the credentials to be
11+
shared acrosss a flow with multiple Retrieve and Rank nodes.
12+
- New Tone Analyzer V3 node to support the V3 GA API.
1013

1114
### New in version 0.4.1
1215
- AlchemyAPI Image Analysis and Language nodes migrated from old Alchemy SDK to current

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
"temp": "^0.8.3",
1212
"qs": "6.x",
1313
"image-type": "^2.0.2",
14-
"watson-developer-cloud": "^1.2.4"
14+
"watson-developer-cloud": "^1.9.2"
1515
},
1616
"repository": {
1717
"type": "git",
@@ -41,6 +41,7 @@
4141
"watson-text-to-speech-v1": "services/text_to_speech/v1.js",
4242
"watson-tradeoff-analytics-v1": "services/tradeoff_analytics/v1.js",
4343
"watson-tone-analyzer-v3-beta": "services/tone_analyzer/v3-beta.js",
44+
"watson-tone-analyzer-v3": "services/tone_analyzer/v3.js",
4445
"watson-visual-recognition-v1": "services/visual_recognition/v1.js"
4546
}
4647
}

services/tone_analyzer/v3-beta.html

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,11 +52,14 @@
5252
</script>
5353

5454
<script type="text/x-red" data-help-name="watson-tone-analyzer">
55+
<p><b>NB:</b> This node uses the old beta API and it is only being retained for backward compatibility for anyone that has old credentials. It will no longer work with new credentials.</p>
56+
<br/>
5557
<p>The Tone Analyzer service uses linguistic analysis to detect emotional tones, social propensities, and writing styles in written communication.</p>
5658
<p>The text to analyze should be passed in on <b>msg.payload</b>.</p>
5759
<p>The service response will be returned on <b>msg.response</b>.</p>
5860
<p>Usng the node editor dialog users can filter the results by tone (emotion, writing or social) and whether to include sentence-level analysis.</p>
5961
<p>For more information about the Tone Analyzer service, read the <a href="https://www.ibm.com/smarterplanet/us/en/ibmwatson/developercloud/tone-analyzer.html">documentation</a>.</p>
62+
<p><b>NB:</b> This is the old beta version and it is only being retained for backward compatibility for anyone that has old credentials. It will no longer work with new credentials.</p>
6063
</script>
6164

6265
<script type="text/javascript">

services/tone_analyzer/v3.html

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
<!--
2+
Copyright 2013,2015 IBM Corp.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
-->
16+
17+
<script type="text/x-red" data-template-name="watson-tone-analyzer-v3">
18+
<div id="credentials-check" class="form-row">
19+
<div class="form-tips">
20+
<i class="fa fa-question-circle"></i><b> Please wait: </b> Checking for bound service credentials...
21+
</div>
22+
</div>
23+
<div class="form-row">
24+
<label for="node-input-name"><i class="fa fa-tag"></i> Name</label>
25+
<input type="text" id="node-input-name" placeholder="Name">
26+
</div>
27+
<div class="form-row credentials" style="display: none;">
28+
<label for="node-input-username"><i class="fa fa-user"></i> Username</label>
29+
<input type="text" id="node-input-username" placeholder="Username">
30+
</div>
31+
<div class="form-row credentials" style="display: none;">
32+
<label for="node-input-password"><i class="fa fa-key"></i> Password</label>
33+
<input type="password" id="node-input-password" placeholder="Password">
34+
</div>
35+
<div class="form-row">
36+
<label for="node-input-tones"><i class="fa fa-comments-o"></i> Tones</label>
37+
<select type="text" id="node-input-tones" style="display: inline-block; width: 70%;" >
38+
<option value="all">All</option>
39+
<option value="emotion">Emotion</option>
40+
<option value="language">Language</option>
41+
<option value="social">Social</option>
42+
</select>
43+
</div>
44+
<div class="form-row">
45+
<label for="node-input-sentences"><i class="fa fa-language"></i> Sentences</label>
46+
<select type="text" id="node-input-sentences" style="display: inline-block; width: 70%;" >
47+
<option value="true">True</option>
48+
<option value="false">False</option>
49+
</select>
50+
</div>
51+
<div class="form-row">
52+
<label for="node-input-contentType"><i class="fa fa-comments-o"></i> Content type</label>
53+
<select type="text" id="node-input-contentType" style="display: inline-block; width: 70%;" >
54+
<option value="false">Text</option>
55+
<option value="true">HTML</option>
56+
</select>
57+
</div>
58+
59+
</script>
60+
61+
<script type="text/x-red" data-help-name="watson-tone-analyzer-v3">
62+
<p>The Tone Analyzer service uses linguistic analysis to detect emotional tones, social propensities,
63+
and writing styles in written communication.</p>
64+
<p>The text to analyze should be passed in on <b>msg.payload</b>.</p>
65+
<p>The service response will be returned on <b>msg.response</b>.</p>
66+
<p>The tone and sentances can be programmaticaly set in <code>msg.tones</code>
67+
and <code>msg.sentences</code></p>
68+
<p>Usng the node editor dialog users can filter the results by tone (emotion, language or social) and
69+
whether to include sentence-level analysis.</p>
70+
<p>For more information about the Tone Analyzer service, read the <a href="https://www.ibm.com/smarterplanet/us/en/ibmwatson/developercloud/tone-analyzer.html">documentation</a>.</p>
71+
</script>
72+
73+
<script type="text/javascript">
74+
(function() {
75+
RED.nodes.registerType('watson-tone-analyzer-v3', {
76+
category: 'IBM Watson',
77+
defaults: {
78+
name: {value: ""},
79+
tones: {value: "all"},
80+
sentences: {value: "true"},
81+
contentType: {value: "false"}
82+
},
83+
credentials: {
84+
username: {type:"text"},
85+
password: {type:"password"}
86+
},
87+
color: "rgb(84,149,230)",
88+
inputs: 1,
89+
outputs: 1,
90+
icon: "tone_analyzer.png",
91+
paletteLabel: "tone analyzer v3",
92+
label: function() {
93+
return this.name || "tone analyzer v3";
94+
},
95+
labelStyle: function() {
96+
return this.name ? "node_label_italic" : "";
97+
},
98+
oneditprepare: function() {
99+
$.getJSON('watson-tone-analyzer/vcap/')
100+
.done(function (service) {
101+
$('.credentials').toggle(!service);
102+
})
103+
.fail(function () {
104+
$('.credentials').show();
105+
}).always(function () {
106+
$('#credentials-check').hide();
107+
})
108+
}
109+
});
110+
})();
111+
</script>

services/tone_analyzer/v3.js

Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
1+
/**
2+
* Copyright 2013,2016 IBM Corp.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
**/
16+
17+
module.exports = function (RED) {
18+
var watson = require('watson-developer-cloud');
19+
var cfenv = require('cfenv');
20+
var toneutils = require('../../utilities/tone-utils');
21+
22+
// Require the Cloud Foundry Module to pull credentials from bound service
23+
// If they are found then they are stored in sUsername and sPassword, as the
24+
// service credentials. This separation from sUsername and username to allow
25+
// the end user to modify the node credentials when the service is not bound.
26+
// Otherwise, once set username would never get reset, resulting in a frustrated
27+
// user who, when he errenously enters bad credentials, can't figure out why
28+
// the edited ones are not being taken.
29+
30+
// Not ever used, and codeacy complains about it.
31+
32+
var username, password, sUsername, sPassword;
33+
34+
var service = cfenv.getAppEnv().getServiceCreds(/tone analyzer/i)
35+
36+
if (service) {
37+
sUsername = service.username;
38+
sPassword = service.password;
39+
}
40+
41+
42+
// Node RED Admin - fetch and set vcap services
43+
RED.httpAdmin.get('/watson-tone-analyzer/vcap', function (req, res) {
44+
res.json(service ? {bound_service: true} : null);
45+
});
46+
47+
48+
// Check that the credentials have been provided
49+
// Credentials are needed for each the service.
50+
var checkCreds = function(credentials) {
51+
var taSettings = null;
52+
53+
username = sUsername || credentials.username;
54+
password = sPassword || credentials.password;
55+
56+
if (username && password) {
57+
taSettings = {};
58+
taSettings.username = username;
59+
taSettings.password = password;
60+
}
61+
62+
return taSettings;
63+
}
64+
65+
66+
// Function that checks the configuration to make sure that credentials,
67+
// payload and options have been provied in the correct format.
68+
var checkConfiguration = function(msg, node, cb) {
69+
var message = null;
70+
var taSettings = null;
71+
72+
taSettings = checkCreds(node.credentials);
73+
74+
if (!taSettings) {
75+
message = 'Missing Tone Analyzer service credentials';
76+
} else if (msg.payload) {
77+
message = toneutils.checkPayload(msg.payload);
78+
} else {
79+
message = 'Missing property: msg.payload';
80+
}
81+
82+
if (cb) {
83+
cb(message, taSettings);
84+
}
85+
};
86+
87+
88+
// function when the node recieves input inside a flow.
89+
// Configuration is first checked before the service is invoked.
90+
var processOnInput = function(msg, config, node) {
91+
checkConfiguration (msg, node, function(err, settings){
92+
if (err) {
93+
node.status({fill:'red', shape:'dot', text:err});
94+
node.error(err, msg);
95+
return;
96+
} else {
97+
var tone_analyzer = watson.tone_analyzer({
98+
'username': settings.username,
99+
'password': settings.password,
100+
'version': 'v3',
101+
'version_date': '2016-05-19'
102+
});
103+
104+
var options = toneutils.parseOptions(msg, config);
105+
106+
node.status({fill:'blue', shape:'dot', text:'requesting'});
107+
tone_analyzer.tone(options, function (err, response) {
108+
node.status({})
109+
if (err) {
110+
node.error(err, msg);
111+
} else {
112+
msg.response = response;
113+
}
114+
node.send(msg);
115+
});
116+
117+
}
118+
});
119+
120+
};
121+
122+
123+
// This is the Tone Analyzer Node.
124+
function Node (config) {
125+
RED.nodes.createNode(this, config);
126+
var node = this;
127+
128+
// Invoked whenb the node has received an input as part of a flow.
129+
this.on('input', function (msg) {
130+
processOnInput(msg, config, node);
131+
});
132+
}
133+
134+
135+
RED.nodes.registerType('watson-tone-analyzer-v3', Node, {
136+
credentials: {
137+
username: {type:'text'},
138+
password: {type:'password'}
139+
}
140+
});
141+
};

utilities/tone-utils.js

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
/**
2+
* Copyright 2016 IBM Corp.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
**/
16+
17+
function ToneUtils () {
18+
}
19+
20+
21+
ToneUtils.prototype = {
22+
check: function () {
23+
return '"IBM Watson Node-RED Utilities for Tone Analyser';
24+
},
25+
26+
// Function that checks the payload and determines
27+
// whether it is JSON or a Buffer
28+
checkPayload: function(payload) {
29+
var message = null;
30+
var isBuffer = false;
31+
32+
var hasJSONmethod = (typeof payload.toJSON === 'function') ;
33+
34+
if (hasJSONmethod === true) {
35+
if (payload.toJSON().type === 'Buffer') {
36+
isBuffer = true;
37+
}
38+
}
39+
// Payload (text to be analysed) must be a string (content is either raw string or Buffer)
40+
if (typeof payload !== 'string' && isBuffer !== true) {
41+
message = 'The payload must be either a string or a Buffer';
42+
}
43+
44+
return message;
45+
},
46+
47+
// Function to parse and determine tone setting
48+
// 'all' is the setting which needs be be blanked
49+
// if not the service will throw an error
50+
parseToneOption: function(msg, config) {
51+
var tones = msg.tones || config.tones;
52+
53+
return (tones === 'all' ? '' : tones);
54+
},
55+
56+
// function to parse through the options in preparation
57+
// for the sevice call.
58+
parseOptions: function(msg, config) {
59+
var options = {
60+
'text': msg.payload,
61+
'sentences': msg.sentences || config.sentences,
62+
'isHTML': msg.contentType || config.contentType
63+
};
64+
65+
options.tones = this.parseToneOption(msg, config);
66+
return options;
67+
}
68+
69+
70+
};
71+
72+
var toneutils = new ToneUtils();
73+
74+
module.exports = toneutils;

0 commit comments

Comments
 (0)