Skip to content

Commit

Permalink
Merge pull request #79 from watson-developer-cloud/master
Browse files Browse the repository at this point in the history
Rebasing
  • Loading branch information
chughts authored Oct 30, 2016
2 parents 7cfc07a + b834902 commit 92024ab
Show file tree
Hide file tree
Showing 6 changed files with 305 additions and 5 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ Node-RED Watson Nodes for IBM Bluemix

<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>

### New in version 0.4.26
- New V3 Personality Insights Node
- Deprecated V2 Personality Insights Node

### New in version 0.4.25
- Fix to Language Translation Node to allow it work with File Inject

Expand Down
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "node-red-node-watson",
"version": "0.4.25",
"version": "0.4.26",
"description": "A collection of Node-RED nodes for IBM Watson services",
"dependencies": {
"alchemy-api": "^1.3.0",
Expand All @@ -11,7 +11,7 @@
"temp": "^0.8.3",
"qs": "6.x",
"image-type": "^2.0.2",
"watson-developer-cloud": "^2.4.6",
"watson-developer-cloud": "^2.7.1",
"kuromoji": "^0.0.5",
"is-docx": "^0.0.3"
},
Expand Down Expand Up @@ -48,6 +48,7 @@
"watson-natural-language-classifier-v1": "services/natural_language_classifier/v1.js",
"alchemy-news-v1": "services/alchemy_data_news/v1.js",
"watson-personality-insights-v1": "services/personality_insights/v1.js",
"watson-personality-insights-v3": "services/personality_insights/v3.js",
"watson-relationship-extraction-v1": "services/relationship_extraction/v1.js",
"watson-retrieve-and-rank-v1": "services/retrieve_and_rank/v1.js",
"watson-speech-to-text-v1": "services/speech_to_text/v1.js",
Expand Down
2 changes: 1 addition & 1 deletion services/language_translator_identify/v2.html
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
</script>

<script type="text/x-red" data-help-name="watson-language-translator-identify">
<p>Packaged in with release 0.4.25 of node-red-node-watson</p>
<p>Packaged in with release 0.4.26 of node-red-node-watson</p>
<p>The Watson Language Translator service can be used to identify languages used in a text input. <p>
<p>Node input : </p>
<ul>
Expand Down
12 changes: 10 additions & 2 deletions services/personality_insights/v1.html
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,15 @@
</script>

<script type="text/x-red" data-help-name="watson-personality-insights">
<p>The Personality Insights service uses linguistic analytics to infer personality and social characteristics, including Big Five, Needs, and Values, from text.</p>
<p><b>NB:</b> This Node makes use of V2 of the Watson Personality Insights service has been
<b>deprecated</b> in favor
of V3 of the service. Once the V2 service
has been switched off this node will no longer work. This node will be removed from the palette in a
future release. Please use the Personality Insights node that supports V3 of the service.
</p>
<br/>

<p>The Personality Insights V2 service uses linguistic analytics to infer personality and social characteristics, including Big Five, Needs, and Values, from text.</p>
<p>The text (minimum of a hundred words) to analyse should be passed in on <code>msg.payload</code>.</p>
<p>You can set the source text language to be either English or Spanish.</p>
<p>The insights will be returned as a tree on <code>msg.insights</code>.</p>
Expand All @@ -54,7 +62,7 @@
<script type="text/javascript">
(function() {
RED.nodes.registerType('watson-personality-insights', {
category: 'IBM Watson',
category: 'Watson Deprecated',
defaults: {
name: {value: ""},
lang: {value: "en"}
Expand Down
140 changes: 140 additions & 0 deletions services/personality_insights/v3.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
<!--
Copyright 2015 IBM Corp.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->

<script type="text/x-red" data-template-name="watson-personality-insights-v3">
<div id="credentials-check" class="form-row">
<div class="form-tips">
<i class="fa fa-question-circle"></i><b> Please wait: </b> Checking for bound service credentials...
</div>
</div>
<div class="form-row">
<label for="node-input-name"><i class="fa fa-tag"></i> Name</label>
<input type="text" id="node-input-name" placeholder="Name">
</div>
<div class="form-row credentials" style="display: none;">
<label for="node-input-username"><i class="fa fa-user"></i> Username</label>
<input type="text" id="node-input-username" placeholder="Username">
</div>
<div class="form-row credentials" style="display: none;">
<label for="node-input-password"><i class="fa fa-key"></i> Password</label>
<input type="password" id="node-input-password" placeholder="Password">
</div>
<div class="form-row">
<label for="node-input-inputlang"><i class="fa fa-language"></i> Input Text Language</label>
<select type="text" id="node-input-inputlang" style="display: inline-block; width: 70%;" >
<option value="en">English</option>
<option value="es">Spanish</option>
<option value="ar">Arabic</option>
<option value="ja">Japanese</option>
</select>
</div>
<div class="form-row">
<label for="node-input-outputlang"><i class="fa fa-language"></i> Language For Response</label>
<select type="text" id="node-input-outputlang" style="display: inline-block; width: 70%;" >
<option value="en">English</option>
<option value="fr">French</option>
<option value="it">Italian</option>
<option value="de">German</option>
<option value="pt-br">Braillian Portuguese</option>
<option value="es">Spanish</option>
<option value="ar">Arabic</option>
<option value="ja">Japanese</option>
<option value="ko">Korean</option>
<option value="zh-cn">Simplified Chinese</option>
<option value="zh-tw">Traditional Chinese</option>
</select>
</div>
<div class="form-row">
<label>&nbsp;</label>
<input type="checkbox" id="node-input-rawscores" style="display: inline-block; width: auto; vertical-align: top;">
<label for="node-input-rawscores" style="width: 70%;"> Raw Scores</label>
</div>
<div class="form-row">
<label>&nbsp;</label>
<input type="checkbox" id="node-input-consumption" style="display: inline-block; width: auto; vertical-align: top;">
<label for="node-input-consumption" style="width: 70%;"> Consumption Preferences</label>
</div>
</script>

<script type="text/x-red" data-help-name="watson-personality-insights-v3">
<p>The Personality Insights V3 service uses
linguistic analytics to infer personality characteristics from text.
The service can infer consumption preferences based on the results of its analysis.
</p>
<p>The text (minimum of a hundred words) to analyse should be passed in on <code>msg.payload</code>.</p>
<p>Input Text Language is the language of the input text.
This setting can be overridden by setting <code>msg.piparams.inputlanguage</code> to a valid
supported language iso code eg. 'es'.
</p>
<p>Language for Response is the language that the response will be returned in.
<p>Input Text Language is the language of the input text.
This setting can be overridden by setting <code>msg.piparams.responselanguage</code> to a valid
supported language iso code eg. 'pt-br'.
</p>
</p>
<p>Raw Scores indicates whether a raw score in addition to a normalized percentile is to be returned
for each characteristic; raw scores are not compared with a sample population.
</p>
<p>Consumption Preferences indicates whether consumption preferences are to be returned
with the results.
</p>
<p>The insights will be returned as a tree on <code>msg.insights</code>.</p>
<p>For more information about the Personality Insights service,
read the <a href="https://www.ibm.com/smarterplanet/us/en/ibmwatson/developercloud/personality-insights.html">
documentation</a>.
</p>
</script>

<script type="text/javascript">
(function() {
RED.nodes.registerType('watson-personality-insights-v3', {
category: 'IBM Watson',
defaults: {
name: {value: ""},
inputlang: {value: "en"},
outputlang: {value: "en"},
rawscores: {value: false},
consumption: {value: false}
},
credentials: {
username: {type:"text"},
password: {type:"password"}
},
color: "rgb(140, 198, 63)",
inputs: 1,
outputs: 1,
icon: "personality_insights.png",
paletteLabel: "personality insights",
label: function() {
return this.name || "personality insights";
},
labelStyle: function() {
return this.name ? "node_label_italic" : "";
},
oneditprepare: function() {
$.getJSON('watson-personality-insights-v3/vcap/')
.done(function (service) {
$('.credentials').toggle(!service);
})
.fail(function () {
$('.credentials').show();
}).always(function () {
$('#credentials-check').hide();
})
}
});
})();
</script>
147 changes: 147 additions & 0 deletions services/personality_insights/v3.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
/**
* Copyright 2015 IBM Corp.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
**/

module.exports = function (RED) {
var PersonalityInsightsV3 = require('watson-developer-cloud/personality-insights/v3'),
cfenv = require('cfenv'),
payloadutils = require('../../utilities/payload-utils'),
service = cfenv.getAppEnv().getServiceCreds(/personality insights/i),
username = null,
password = null,
sUsername = null,
sPassword = null,

VALID_INPUT_LANGUAGES = ['ar','en','es','ja'],
VALID_RESPONSE_LANGUAGES = ['ar','de','en','es','fr','it','ja','ko','pt-br','zh-cn','zh-tw'];

if (service) {
sUsername = service.username;
sPassword = service.password;
}

// This HTTP GET REST request is used by the browser side of the node to
// determine if credentials are found.
RED.httpAdmin.get('/watson-personality-insights-v3/vcap', function (req, res) {
res.json(service ? {bound_service: true} : null);
});

// This function prepares the params object for the
// call to Personality Insights
function prepareParams(msg, config) {
var params = {},
inputlang = config.inputlang ? config.inputlang : 'en',
outputlang = config.outputlang ? config.outputlang : 'en';

if (msg.piparams) {
if (msg.piparams.inputlanguage &&
-1 < VALID_INPUT_LANGUAGES.indexOf(msg.piparams.inputlanguage)) {
inputlang = msg.piparams.inputlanguage;
}
if (msg.piparams.responselanguage &&
-1 < VALID_RESPONSE_LANGUAGES.indexOf(msg.piparams.responselanguage)) {
outputlang = msg.piparams.responselanguage;
}
}

params = {
text: msg.payload,
consumption_preferences: config.consumption ? config.consumption : false,
raw_scores: config.rawscores ? config.rawscores : false,
headers: {
'content-language': inputlang,
'accept-language': outputlang,
'accept': 'application/json'
}
};

return params;
}

// This is the start of the Node Code. In this case only on input
// is being processed.
function Node(config) {
RED.nodes.createNode(this,config);
var node = this,
wc = payloadutils.word_count(config.inputlang),
message = '';

this.on('input', function (msg) {
//var self = this;

if (!msg.payload) {
message = 'Missing property: msg.payload';
node.status({fill:'red', shape:'ring', text:'missing payload'});
node.error(message, msg);
return;
}

if ('string' !== typeof(msg.payload)) {
message = 'submitted msg.payload is not text';
node.status({fill:'red', shape:'ring', text:'payload is not text'});
node.error(message, msg);
return;
}

wc(msg.payload, function (length) {
if (length < 100) {
message = 'Personality insights requires a minimum of one hundred words.' +
' Only ' + length + ' submitted';
node.status({fill:'red', shape:'ring', text:'insufficient words submitted'});
node.error(message, msg);
return;
}

username = sUsername || node.credentials.username;
password = sPassword || node.credentials.password;

if (!username || !password) {
message = 'Missing Personality Insights service credentials';
node.status({fill:'red', shape:'ring', text:'missing credentials'});
node.error(message, msg);
return;
}

var params = prepareParams(msg, config),
personality_insights = new PersonalityInsightsV3({
username: username,
password: password,
version_date: '2016-10-20'
});

node.status({fill:'blue', shape:'dot', text:'requesting'});
personality_insights.profile(params, function(err, response){
node.status({});
if (err) {
node.status({fill:'red', shape:'ring', text:'processing error'});
node.error(err, msg);
} else{
msg.insights = response;
}

node.send(msg);
});

});
});
}

RED.nodes.registerType('watson-personality-insights-v3',Node,{
credentials: {
username: {type:'text'},
password: {type:'password'}
}
});
};

0 comments on commit 92024ab

Please sign in to comment.