Skip to content

Commit 58f1261

Browse files
committed
added new v3 tone analyzer
1 parent 001821b commit 58f1261

2 files changed

Lines changed: 224 additions & 0 deletions

File tree

services/tone_analyzer/v3.html

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
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">
62+
<p>The Tone Analyzer service uses linguistic analysis to detect emotional tones, social propensities, and writing styles in written communication.</p>
63+
<p>The text to analyze should be passed in on <b>msg.payload</b>.</p>
64+
<p>The service response will be returned on <b>msg.response</b>.</p>
65+
<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>
66+
<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>
67+
</script>
68+
69+
<script type="text/javascript">
70+
(function() {
71+
RED.nodes.registerType('watson-tone-analyzer-v3', {
72+
category: 'IBM Watson',
73+
defaults: {
74+
name: {value: ""},
75+
tones: {value: "all"},
76+
sentences: {value: "true"},
77+
contentType: {value: "false"}
78+
},
79+
credentials: {
80+
username: {type:"text"},
81+
password: {type:"password"}
82+
},
83+
color: "rgb(84,149,230)",
84+
inputs: 1,
85+
outputs: 1,
86+
icon: "tone_analyzer.png",
87+
paletteLabel: "tone analyzer",
88+
label: function() {
89+
return this.name || "tone analyzer";
90+
},
91+
labelStyle: function() {
92+
return this.name ? "node_label_italic" : "";
93+
},
94+
oneditprepare: function() {
95+
$.getJSON('watson-tone-analyzer/vcap/')
96+
.done(function (service) {
97+
$('.credentials').toggle(!service);
98+
})
99+
.fail(function () {
100+
$('.credentials').show();
101+
}).always(function () {
102+
$('#credentials-check').hide();
103+
})
104+
}
105+
});
106+
})();
107+
</script>

services/tone_analyzer/v3.js

Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
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 cfenv = require('cfenv');
19+
20+
var services = cfenv.getAppEnv().services,
21+
service;
22+
23+
var username, password;
24+
25+
var service = cfenv.getAppEnv().getServiceCreds(/tone analyzer/i)
26+
27+
if (service) {
28+
username = service.username;
29+
password = service.password;
30+
}
31+
32+
RED.httpAdmin.get('/watson-tone-analyzer/vcap', function (req, res) {
33+
res.json(service ? {bound_service: true} : null);
34+
});
35+
36+
function Node (config) {
37+
RED.nodes.createNode(this, config);
38+
var node = this;
39+
40+
this.on('input', function (msg) {
41+
if (!msg.payload) {
42+
var message = 'Missing property: msg.payload';
43+
node.error(message, msg);
44+
return;
45+
}
46+
47+
username = username || this.credentials.username;
48+
password = password || this.credentials.password;
49+
50+
if (!username || !password) {
51+
var message = 'Missing Tone Analyzer service credentials';
52+
node.error(message, msg);
53+
return;
54+
}
55+
56+
var tones = msg.tones || config.tones;
57+
var sentences = msg.sentences || config.sentences;
58+
var contentType = msg.contentType || config.contentType
59+
60+
var watson = require('watson-developer-cloud');
61+
62+
var tone_analyzer = watson.tone_analyzer({
63+
username: username,
64+
password: password,
65+
version: 'v3',
66+
version_date: '2016-05-19'
67+
});
68+
69+
var hasJSONmethod = (typeof msg.payload.toJSON === 'function') ;
70+
var isBuffer = false;
71+
if (hasJSONmethod==true)
72+
{
73+
if (msg.payload.toJSON().type == 'Buffer')
74+
isBuffer=true;
75+
}
76+
77+
// Payload (text to be analysed) must be a string (content is either raw string or Buffer)
78+
if (typeof msg.payload == 'string' || isBuffer == true )
79+
{
80+
var options = {
81+
text: msg.payload,
82+
sentences: config.sentences,
83+
isHTML: contentType
84+
};
85+
86+
if (tones !== 'all') {
87+
options.tones = tones;
88+
}
89+
90+
console.log(options);
91+
92+
node.status({fill:'blue', shape:'dot', text:'requesting'});
93+
tone_analyzer.tone(options, function (err, response) {
94+
node.status({})
95+
if (err) {
96+
node.error(err, msg);
97+
} else {
98+
msg.response = response;
99+
}
100+
101+
node.send(msg);
102+
});
103+
}
104+
else {
105+
var message = 'The payload must be either a string or a Buffer';
106+
node.status({fill:'red', shape:'dot', text:message});
107+
node.error(message, msg);
108+
}
109+
});
110+
}
111+
RED.nodes.registerType('watson-tone-analyzer-v3', Node, {
112+
credentials: {
113+
username: {type:'text'},
114+
password: {type:'password'}
115+
}
116+
});
117+
};

0 commit comments

Comments
 (0)