From bd684304c20cd7deed41597819dc13991f26a828 Mon Sep 17 00:00:00 2001 From: chughts Date: Sat, 29 Oct 2016 20:07:12 -0400 Subject: [PATCH 1/6] Added V3 Personality Insights Node --- README.md | 4 + package.json | 5 +- services/language_translator_identify/v2.html | 2 +- services/personality_insights/v1.html | 12 +- services/personality_insights/v3.html | 140 ++++++++++++++++++ services/personality_insights/v3.js | 138 +++++++++++++++++ 6 files changed, 296 insertions(+), 5 deletions(-) create mode 100644 services/personality_insights/v3.html create mode 100644 services/personality_insights/v3.js diff --git a/README.md b/README.md index f54b968f..5a1313b5 100644 --- a/README.md +++ b/README.md @@ -7,6 +7,10 @@ Node-RED Watson Nodes for IBM Bluemix CLA assistant +### 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 diff --git a/package.json b/package.json index 76432d9c..78e49742 100644 --- a/package.json +++ b/package.json @@ -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", @@ -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" }, @@ -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", diff --git a/services/language_translator_identify/v2.html b/services/language_translator_identify/v2.html index dfff593b..4b537ba5 100644 --- a/services/language_translator_identify/v2.html +++ b/services/language_translator_identify/v2.html @@ -35,7 +35,7 @@ + + + + diff --git a/services/personality_insights/v3.js b/services/personality_insights/v3.js new file mode 100644 index 00000000..50dcf6ec --- /dev/null +++ b/services/personality_insights/v3.js @@ -0,0 +1,138 @@ +/** + * 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 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); + + this.on('input', function (msg) { + //var self = this; + + if (!msg.payload) { + var message = 'Missing property: msg.payload'; + node.status({fill:'red', shape:'ring', text:'missing payload'}); + node.error(message, msg) + return; + } + + if ('string' !== typeof(msg.payload)) { + var 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) { + var 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) { + var message = 'Missing Personality Insights service credentials'; + node.status({fill:'red', shape:'ring', text:'missing credentials'}); + node.error(message, msg); + return; + } + + var personality_insights = new PersonalityInsightsV3({ + username: username, + password: password, + version_date: '2016-10-20' + }); + + var inputlang = config.inputlang ? config.inputlang : 'en'; + var 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; + } + } + + var 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' + } + }; + + 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"} + } + }); +}; From 197fe59a1379406b107756f2c7e527eda8faf487 Mon Sep 17 00:00:00 2001 From: chughts Date: Sat, 29 Oct 2016 20:24:39 -0400 Subject: [PATCH 2/6] Added V3 Personality Insights Node --- services/personality_insights/v3.js | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/services/personality_insights/v3.js b/services/personality_insights/v3.js index 50dcf6ec..b38fd953 100644 --- a/services/personality_insights/v3.js +++ b/services/personality_insights/v3.js @@ -43,28 +43,29 @@ module.exports = function (RED) { function Node(config) { RED.nodes.createNode(this,config); var node = this, - wc = payloadutils.word_count(config.inputlang); + wc = payloadutils.word_count(config.inputlang), + message = ''; this.on('input', function (msg) { //var self = this; if (!msg.payload) { - var message = 'Missing property: msg.payload'; + message = 'Missing property: msg.payload'; node.status({fill:'red', shape:'ring', text:'missing payload'}); - node.error(message, msg) + node.error(message, msg); return; } if ('string' !== typeof(msg.payload)) { - var message = 'submitted msg.payload is not text'; + message = 'submitted msg.payload is not text'; node.status({fill:'red', shape:'ring', text:'payload is not text'}); - node.error(message, msg) + node.error(message, msg); return; } wc(msg.payload, function (length) { if (length < 100) { - var message = 'Personality insights requires a minimum of one hundred words.' + 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); @@ -75,7 +76,7 @@ module.exports = function (RED) { password = sPassword || node.credentials.password; if (!username || !password) { - var message = 'Missing Personality Insights service credentials'; + message = 'Missing Personality Insights service credentials'; node.status({fill:'red', shape:'ring', text:'missing credentials'}); node.error(message, msg); return; @@ -87,8 +88,8 @@ module.exports = function (RED) { version_date: '2016-10-20' }); - var inputlang = config.inputlang ? config.inputlang : 'en'; - var outputlang = config.outputlang ? config.outputlang : 'en'; + var inputlang = config.inputlang ? config.inputlang : 'en', + outputlang = config.outputlang ? config.outputlang : 'en'; if (msg.piparams) { if (msg.piparams.inputlanguage From e18d008e8783d782f5750e4d06a3e9e44d30a662 Mon Sep 17 00:00:00 2001 From: chughts Date: Sat, 29 Oct 2016 20:43:28 -0400 Subject: [PATCH 3/6] Added V3 Personality Insights Node --- services/personality_insights/v3.js | 31 ++++++++++++++--------------- 1 file changed, 15 insertions(+), 16 deletions(-) diff --git a/services/personality_insights/v3.js b/services/personality_insights/v3.js index b38fd953..84a5f7ec 100644 --- a/services/personality_insights/v3.js +++ b/services/personality_insights/v3.js @@ -82,23 +82,22 @@ module.exports = function (RED) { return; } - var personality_insights = new PersonalityInsightsV3({ + var inputlang = config.inputlang ? config.inputlang : 'en', + outputlang = config.outputlang ? config.outputlang : 'en', + personality_insights = new PersonalityInsightsV3({ username: username, password: password, version_date: '2016-10-20' }); - var 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.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; + if (msg.piparams.responselanguage && + -1 < VALID_RESPONSE_LANGUAGES.indexOf(msg.piparams.responselanguage)) { + outputlang = msg.piparams.responselanguage; } } @@ -113,9 +112,9 @@ module.exports = function (RED) { } }; - node.status({fill:"blue", shape:"dot", text:"requesting"}); + node.status({fill:'blue', shape:'dot', text:'requesting'}); personality_insights.profile(params, function(err, response){ - node.status({}) + node.status({}); if (err) { node.status({fill:'red', shape:'ring', text:'processing error'}); node.error(err, msg); @@ -130,10 +129,10 @@ module.exports = function (RED) { }); } - RED.nodes.registerType("watson-personality-insights-v3",Node,{ - credentials: { - username: {type:"text"}, - password: {type:"password"} + RED.nodes.registerType('watson-personality-insights-v3',Node,{ + credentials: { + username: {type:'text'}, + password: {type:'password'} } }); }; From 45b1bac4c4b175783cfb17ec8a289510a281389b Mon Sep 17 00:00:00 2001 From: chughts Date: Sat, 29 Oct 2016 21:14:33 -0400 Subject: [PATCH 4/6] Added V3 Personality Insights Node --- services/personality_insights/v3.js | 66 +++++++++++++++++------------ 1 file changed, 38 insertions(+), 28 deletions(-) diff --git a/services/personality_insights/v3.js b/services/personality_insights/v3.js index 84a5f7ec..bc71b74d 100644 --- a/services/personality_insights/v3.js +++ b/services/personality_insights/v3.js @@ -38,6 +38,39 @@ module.exports = function (RED) { 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 = {}; + + var 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) { @@ -82,35 +115,12 @@ module.exports = function (RED) { return; } - var inputlang = config.inputlang ? config.inputlang : 'en', - outputlang = config.outputlang ? config.outputlang : 'en', + var params = PrepareParams(msg, config), personality_insights = new PersonalityInsightsV3({ - username: username, - password: password, - version_date: '2016-10-20' - }); - - 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; - } - } - - var 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' - } - }; + username: username, + password: password, + version_date: '2016-10-20' + }); node.status({fill:'blue', shape:'dot', text:'requesting'}); personality_insights.profile(params, function(err, response){ From 0f1a2b916931e4c49deef6140a5373f0017a9458 Mon Sep 17 00:00:00 2001 From: chughts Date: Sat, 29 Oct 2016 21:26:24 -0400 Subject: [PATCH 5/6] Added V3 Personality Insights Node --- services/personality_insights/v3.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/services/personality_insights/v3.js b/services/personality_insights/v3.js index bc71b74d..e642a308 100644 --- a/services/personality_insights/v3.js +++ b/services/personality_insights/v3.js @@ -40,7 +40,7 @@ module.exports = function (RED) { // This function prepares the params object for the // call to Personality Insights - function PrepareParams(msg, config) { + function prepareParams(msg, config) { var params = {}; var inputlang = config.inputlang ? config.inputlang : 'en', @@ -98,8 +98,8 @@ module.exports = function (RED) { wc(msg.payload, function (length) { if (length < 100) { - message = 'Personality insights requires a minimum of one hundred words.' - + ' Only ' + length + ' submitted'; + 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; @@ -115,7 +115,7 @@ module.exports = function (RED) { return; } - var params = PrepareParams(msg, config), + var params = prepareParams(msg, config), personality_insights = new PersonalityInsightsV3({ username: username, password: password, From 4ef9e2205327e867cc1cb9dd429bcd3da24b017a Mon Sep 17 00:00:00 2001 From: chughts Date: Sat, 29 Oct 2016 21:38:31 -0400 Subject: [PATCH 6/6] Added V3 Personality Insights Node --- services/personality_insights/v3.js | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/services/personality_insights/v3.js b/services/personality_insights/v3.js index e642a308..5905aa77 100644 --- a/services/personality_insights/v3.js +++ b/services/personality_insights/v3.js @@ -41,9 +41,8 @@ module.exports = function (RED) { // This function prepares the params object for the // call to Personality Insights function prepareParams(msg, config) { - var params = {}; - - var inputlang = config.inputlang ? config.inputlang : 'en', + var params = {}, + inputlang = config.inputlang ? config.inputlang : 'en', outputlang = config.outputlang ? config.outputlang : 'en'; if (msg.piparams) {