-
Notifications
You must be signed in to change notification settings - Fork 86
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #79 from watson-developer-cloud/master
Rebasing
- Loading branch information
Showing
6 changed files
with
305 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> </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> </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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'} | ||
} | ||
}); | ||
}; |