Skip to content

Commit b834902

Browse files
authored
Merge pull request #214 from chughts/piv3
Personality Insights V3
2 parents cdbc8d9 + 4ef9e22 commit b834902

File tree

6 files changed

+305
-5
lines changed

6 files changed

+305
-5
lines changed

README.md

+4
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ 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.4.26
11+
- New V3 Personality Insights Node
12+
- Deprecated V2 Personality Insights Node
13+
1014
### New in version 0.4.25
1115
- Fix to Language Translation Node to allow it work with File Inject
1216

package.json

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "node-red-node-watson",
3-
"version": "0.4.25",
3+
"version": "0.4.26",
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.4.6",
14+
"watson-developer-cloud": "^2.7.1",
1515
"kuromoji": "^0.0.5",
1616
"is-docx": "^0.0.3"
1717
},
@@ -48,6 +48,7 @@
4848
"watson-natural-language-classifier-v1": "services/natural_language_classifier/v1.js",
4949
"alchemy-news-v1": "services/alchemy_data_news/v1.js",
5050
"watson-personality-insights-v1": "services/personality_insights/v1.js",
51+
"watson-personality-insights-v3": "services/personality_insights/v3.js",
5152
"watson-relationship-extraction-v1": "services/relationship_extraction/v1.js",
5253
"watson-retrieve-and-rank-v1": "services/retrieve_and_rank/v1.js",
5354
"watson-speech-to-text-v1": "services/speech_to_text/v1.js",

services/language_translator_identify/v2.html

+1-1
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535
</script>
3636

3737
<script type="text/x-red" data-help-name="watson-language-translator-identify">
38-
<p>Packaged in with release 0.4.25 of node-red-node-watson</p>
38+
<p>Packaged in with release 0.4.26 of node-red-node-watson</p>
3939
<p>The Watson Language Translator service can be used to identify languages used in a text input. <p>
4040
<p>Node input : </p>
4141
<ul>

services/personality_insights/v1.html

+10-2
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,15 @@
4444
</script>
4545

4646
<script type="text/x-red" data-help-name="watson-personality-insights">
47-
<p>The Personality Insights service uses linguistic analytics to infer personality and social characteristics, including Big Five, Needs, and Values, from text.</p>
47+
<p><b>NB:</b> This Node makes use of V2 of the Watson Personality Insights service has been
48+
<b>deprecated</b> in favor
49+
of V3 of the service. Once the V2 service
50+
has been switched off this node will no longer work. This node will be removed from the palette in a
51+
future release. Please use the Personality Insights node that supports V3 of the service.
52+
</p>
53+
<br/>
54+
55+
<p>The Personality Insights V2 service uses linguistic analytics to infer personality and social characteristics, including Big Five, Needs, and Values, from text.</p>
4856
<p>The text (minimum of a hundred words) to analyse should be passed in on <code>msg.payload</code>.</p>
4957
<p>You can set the source text language to be either English or Spanish.</p>
5058
<p>The insights will be returned as a tree on <code>msg.insights</code>.</p>
@@ -54,7 +62,7 @@
5462
<script type="text/javascript">
5563
(function() {
5664
RED.nodes.registerType('watson-personality-insights', {
57-
category: 'IBM Watson',
65+
category: 'Watson Deprecated',
5866
defaults: {
5967
name: {value: ""},
6068
lang: {value: "en"}

services/personality_insights/v3.html

+140
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
1+
<!--
2+
Copyright 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-personality-insights-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-inputlang"><i class="fa fa-language"></i> Input Text Language</label>
37+
<select type="text" id="node-input-inputlang" style="display: inline-block; width: 70%;" >
38+
<option value="en">English</option>
39+
<option value="es">Spanish</option>
40+
<option value="ar">Arabic</option>
41+
<option value="ja">Japanese</option>
42+
</select>
43+
</div>
44+
<div class="form-row">
45+
<label for="node-input-outputlang"><i class="fa fa-language"></i> Language For Response</label>
46+
<select type="text" id="node-input-outputlang" style="display: inline-block; width: 70%;" >
47+
<option value="en">English</option>
48+
<option value="fr">French</option>
49+
<option value="it">Italian</option>
50+
<option value="de">German</option>
51+
<option value="pt-br">Braillian Portuguese</option>
52+
<option value="es">Spanish</option>
53+
<option value="ar">Arabic</option>
54+
<option value="ja">Japanese</option>
55+
<option value="ko">Korean</option>
56+
<option value="zh-cn">Simplified Chinese</option>
57+
<option value="zh-tw">Traditional Chinese</option>
58+
</select>
59+
</div>
60+
<div class="form-row">
61+
<label>&nbsp;</label>
62+
<input type="checkbox" id="node-input-rawscores" style="display: inline-block; width: auto; vertical-align: top;">
63+
<label for="node-input-rawscores" style="width: 70%;"> Raw Scores</label>
64+
</div>
65+
<div class="form-row">
66+
<label>&nbsp;</label>
67+
<input type="checkbox" id="node-input-consumption" style="display: inline-block; width: auto; vertical-align: top;">
68+
<label for="node-input-consumption" style="width: 70%;"> Consumption Preferences</label>
69+
</div>
70+
</script>
71+
72+
<script type="text/x-red" data-help-name="watson-personality-insights-v3">
73+
<p>The Personality Insights V3 service uses
74+
linguistic analytics to infer personality characteristics from text.
75+
The service can infer consumption preferences based on the results of its analysis.
76+
</p>
77+
<p>The text (minimum of a hundred words) to analyse should be passed in on <code>msg.payload</code>.</p>
78+
<p>Input Text Language is the language of the input text.
79+
This setting can be overridden by setting <code>msg.piparams.inputlanguage</code> to a valid
80+
supported language iso code eg. 'es'.
81+
</p>
82+
<p>Language for Response is the language that the response will be returned in.
83+
<p>Input Text Language is the language of the input text.
84+
This setting can be overridden by setting <code>msg.piparams.responselanguage</code> to a valid
85+
supported language iso code eg. 'pt-br'.
86+
</p>
87+
</p>
88+
<p>Raw Scores indicates whether a raw score in addition to a normalized percentile is to be returned
89+
for each characteristic; raw scores are not compared with a sample population.
90+
</p>
91+
<p>Consumption Preferences indicates whether consumption preferences are to be returned
92+
with the results.
93+
</p>
94+
<p>The insights will be returned as a tree on <code>msg.insights</code>.</p>
95+
<p>For more information about the Personality Insights service,
96+
read the <a href="https://www.ibm.com/smarterplanet/us/en/ibmwatson/developercloud/personality-insights.html">
97+
documentation</a>.
98+
</p>
99+
</script>
100+
101+
<script type="text/javascript">
102+
(function() {
103+
RED.nodes.registerType('watson-personality-insights-v3', {
104+
category: 'IBM Watson',
105+
defaults: {
106+
name: {value: ""},
107+
inputlang: {value: "en"},
108+
outputlang: {value: "en"},
109+
rawscores: {value: false},
110+
consumption: {value: false}
111+
},
112+
credentials: {
113+
username: {type:"text"},
114+
password: {type:"password"}
115+
},
116+
color: "rgb(140, 198, 63)",
117+
inputs: 1,
118+
outputs: 1,
119+
icon: "personality_insights.png",
120+
paletteLabel: "personality insights",
121+
label: function() {
122+
return this.name || "personality insights";
123+
},
124+
labelStyle: function() {
125+
return this.name ? "node_label_italic" : "";
126+
},
127+
oneditprepare: function() {
128+
$.getJSON('watson-personality-insights-v3/vcap/')
129+
.done(function (service) {
130+
$('.credentials').toggle(!service);
131+
})
132+
.fail(function () {
133+
$('.credentials').show();
134+
}).always(function () {
135+
$('#credentials-check').hide();
136+
})
137+
}
138+
});
139+
})();
140+
</script>

services/personality_insights/v3.js

+147
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,147 @@
1+
/**
2+
* Copyright 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+
module.exports = function (RED) {
18+
var PersonalityInsightsV3 = require('watson-developer-cloud/personality-insights/v3'),
19+
cfenv = require('cfenv'),
20+
payloadutils = require('../../utilities/payload-utils'),
21+
service = cfenv.getAppEnv().getServiceCreds(/personality insights/i),
22+
username = null,
23+
password = null,
24+
sUsername = null,
25+
sPassword = null,
26+
27+
VALID_INPUT_LANGUAGES = ['ar','en','es','ja'],
28+
VALID_RESPONSE_LANGUAGES = ['ar','de','en','es','fr','it','ja','ko','pt-br','zh-cn','zh-tw'];
29+
30+
if (service) {
31+
sUsername = service.username;
32+
sPassword = service.password;
33+
}
34+
35+
// This HTTP GET REST request is used by the browser side of the node to
36+
// determine if credentials are found.
37+
RED.httpAdmin.get('/watson-personality-insights-v3/vcap', function (req, res) {
38+
res.json(service ? {bound_service: true} : null);
39+
});
40+
41+
// This function prepares the params object for the
42+
// call to Personality Insights
43+
function prepareParams(msg, config) {
44+
var params = {},
45+
inputlang = config.inputlang ? config.inputlang : 'en',
46+
outputlang = config.outputlang ? config.outputlang : 'en';
47+
48+
if (msg.piparams) {
49+
if (msg.piparams.inputlanguage &&
50+
-1 < VALID_INPUT_LANGUAGES.indexOf(msg.piparams.inputlanguage)) {
51+
inputlang = msg.piparams.inputlanguage;
52+
}
53+
if (msg.piparams.responselanguage &&
54+
-1 < VALID_RESPONSE_LANGUAGES.indexOf(msg.piparams.responselanguage)) {
55+
outputlang = msg.piparams.responselanguage;
56+
}
57+
}
58+
59+
params = {
60+
text: msg.payload,
61+
consumption_preferences: config.consumption ? config.consumption : false,
62+
raw_scores: config.rawscores ? config.rawscores : false,
63+
headers: {
64+
'content-language': inputlang,
65+
'accept-language': outputlang,
66+
'accept': 'application/json'
67+
}
68+
};
69+
70+
return params;
71+
}
72+
73+
// This is the start of the Node Code. In this case only on input
74+
// is being processed.
75+
function Node(config) {
76+
RED.nodes.createNode(this,config);
77+
var node = this,
78+
wc = payloadutils.word_count(config.inputlang),
79+
message = '';
80+
81+
this.on('input', function (msg) {
82+
//var self = this;
83+
84+
if (!msg.payload) {
85+
message = 'Missing property: msg.payload';
86+
node.status({fill:'red', shape:'ring', text:'missing payload'});
87+
node.error(message, msg);
88+
return;
89+
}
90+
91+
if ('string' !== typeof(msg.payload)) {
92+
message = 'submitted msg.payload is not text';
93+
node.status({fill:'red', shape:'ring', text:'payload is not text'});
94+
node.error(message, msg);
95+
return;
96+
}
97+
98+
wc(msg.payload, function (length) {
99+
if (length < 100) {
100+
message = 'Personality insights requires a minimum of one hundred words.' +
101+
' Only ' + length + ' submitted';
102+
node.status({fill:'red', shape:'ring', text:'insufficient words submitted'});
103+
node.error(message, msg);
104+
return;
105+
}
106+
107+
username = sUsername || node.credentials.username;
108+
password = sPassword || node.credentials.password;
109+
110+
if (!username || !password) {
111+
message = 'Missing Personality Insights service credentials';
112+
node.status({fill:'red', shape:'ring', text:'missing credentials'});
113+
node.error(message, msg);
114+
return;
115+
}
116+
117+
var params = prepareParams(msg, config),
118+
personality_insights = new PersonalityInsightsV3({
119+
username: username,
120+
password: password,
121+
version_date: '2016-10-20'
122+
});
123+
124+
node.status({fill:'blue', shape:'dot', text:'requesting'});
125+
personality_insights.profile(params, function(err, response){
126+
node.status({});
127+
if (err) {
128+
node.status({fill:'red', shape:'ring', text:'processing error'});
129+
node.error(err, msg);
130+
} else{
131+
msg.insights = response;
132+
}
133+
134+
node.send(msg);
135+
});
136+
137+
});
138+
});
139+
}
140+
141+
RED.nodes.registerType('watson-personality-insights-v3',Node,{
142+
credentials: {
143+
username: {type:'text'},
144+
password: {type:'password'}
145+
}
146+
});
147+
};

0 commit comments

Comments
 (0)