Skip to content

Commit 0e26b17

Browse files
authored
Merge pull request #323 from watson-developer-cloud/pi-v3
Add Personality Insights v3
2 parents 3013f0b + a7ed099 commit 0e26b17

11 files changed

Lines changed: 398 additions & 14 deletions

.travis.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ script:
1010
after_success:
1111
- jsdoc/publish.sh
1212
before_install:
13-
- '[ "${TRAVIS_PULL_REQUEST}" = "false" ] && openssl aes-256-cbc -K $encrypted_cb4d3d070e32_key -iv $encrypted_cb4d3d070e32_iv -in auth.js.enc -out test/resources/auth.js -d || true'
13+
- '[ "${TRAVIS_PULL_REQUEST}" = "false" ] && openssl aes-256-cbc -K $encrypted_d4f181ef7c79_key -iv $encrypted_d4f181ef7c79_iv -in auth.js.enc -out test/resources/auth.js -d || true'
1414
env:
1515
global:
1616
secure: I50FnfubwiZlol5Qs9tXPVACGbs+SG2dJLjpbzeio0Hl666d/hDuvf7IFwqqaRjQkmK2TdIjaGLMOgHxh/GGAoYf7LfZqjDID/tAhgoFOYiZwiPj66Z82F9P9cega2nkNW+125NzPuU3rhSCByG8+K/PMH/5JxPwPMoAYNLQjWc=

README.md

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -377,16 +377,18 @@ Analyze text in english and get a personality profile by using the
377377
[Personality Insights][personality_insights] service.
378378

379379
```javascript
380-
var PersonalityInsightsV2 = require('watson-developer-cloud/personality-insights/v2');
380+
var PersonalityInsightsV3 = require('watson-developer-cloud/personality-insights/v3');
381381

382382
var personality_insights = new PersonalityInsightsV2({
383383
username: '<username>',
384-
password: '<password>'
384+
password: '<password>',
385+
version_date: '2016-10-19'
385386
});
386387

387388
personality_insights.profile({
388389
text: 'Enter more than 100 unique words here...',
389-
language: 'en' },
390+
consumption_preferences: true
391+
},
390392
function (err, response) {
391393
if (err)
392394
console.log('error:', err);

auth.js.enc

368 Bytes
Binary file not shown.
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
'use strict';
2+
3+
var PersonalityInsightsV3 = require('watson-developer-cloud/personality-insights/v3');
4+
var fs = require('fs');
5+
6+
var personality_insights = new PersonalityInsightsV3({
7+
username: 'INSERT YOUR USERNAME FOR THE SERVICE HERE',
8+
password: 'INSERT YOUR PASSWORD FOR THE SERVICE HERE',
9+
version_date: '2016-10-19'
10+
});
11+
12+
13+
/*
14+
* English example:
15+
* 'text' parameter contains the input text.
16+
*/
17+
personality_insights.profile({
18+
text: 'Enter more than 100 unique words here...',
19+
consumption_preferences: true
20+
},
21+
function (err, response) {
22+
if (err)
23+
console.log('error:', err);
24+
else
25+
console.log(JSON.stringify(response, null, 2));
26+
});
27+
28+
/*
29+
* Spanish example:
30+
* 'language' parameter is needed in 'es' since our
31+
* text content is in Spanish.
32+
*/
33+
personality_insights.profile({
34+
text: 'Ingrese un texto de más de 100 palabras aquí...',
35+
headers: { 'Content-Language': 'es' }
36+
},
37+
function (err, response) {
38+
if (err)
39+
console.log('error:', err);
40+
else
41+
console.log(JSON.stringify(response, null, 2));
42+
});
43+
44+
/*
45+
* Requesting output in an specific language:
46+
* Following the Spanish Example, now we would like to
47+
* obtain the output in Spanish, i.e. all the trait
48+
* names and output messages in Spanish. You can specify
49+
* the expected language by passing 'accept_language'
50+
* parameter with the locale.
51+
*/
52+
personality_insights.profile({
53+
text: 'Ingrese un texto de más de 100 palabras aquí...',
54+
headers: { 'Content-Language': 'es', 'Accept-Language': 'es' }
55+
},
56+
function (err, response) {
57+
if (err)
58+
console.log('error:', err);
59+
else
60+
console.log(JSON.stringify(response, null, 2));
61+
});
62+
63+
64+
/*
65+
* CSV output example:
66+
* https://www.ibm.com/watson/developercloud/doc/personality-insights/output.shtml#outputCSV
67+
*/
68+
personality_insights.profile({
69+
text: 'Enter more than 100 unique words here...',
70+
csv_headers: true,
71+
headers: {'Accept': 'text/csv'}
72+
}).pipe(fs.createWriteStream('./output.csv'));

index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ exports.LanguageTranslatorV2 = require('./language-translator/v2');
3838
exports.NaturalLanguageClassifierV1 = require('./natural-language-classifier/v1');
3939

4040
exports.PersonalityInsightsV2 = require('./personality-insights/v2');
41+
exports.PersonalityInsightsV3 = require('./personality-insights/v3');
4142

4243
exports.RetrieveAndRankV1 = require('./retrieve-and-rank/v1');
4344

personality-insights/v2.js

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,13 @@ var BaseService = require('../lib/base_service');
2727
* @param options
2828
* @constructor
2929
*/
30-
function PersonalityInsightsV1(options) {
30+
function PersonalityInsightsV2(options) {
3131
BaseService.call(this, options);
3232
}
33-
util.inherits(PersonalityInsightsV1, BaseService);
34-
PersonalityInsightsV1.prototype.name = 'personality_insights';
35-
PersonalityInsightsV1.prototype.version = 'v2';
36-
PersonalityInsightsV1.URL = 'https://gateway.watsonplatform.net/personality-insights/api';
33+
util.inherits(PersonalityInsightsV2, BaseService);
34+
PersonalityInsightsV2.prototype.name = 'personality_insights';
35+
PersonalityInsightsV2.prototype.version = 'v2';
36+
PersonalityInsightsV2.URL = 'https://gateway.watsonplatform.net/personality-insights/api';
3737

3838
/**
3939
* @param params {Object} The parameters to call the service
@@ -46,7 +46,7 @@ PersonalityInsightsV1.URL = 'https://gateway.watsonplatform.net/personality-insi
4646
*
4747
* @param callback The callback.
4848
*/
49-
PersonalityInsightsV1.prototype.profile = function(params, callback) { // eslint-disable-line complexity
49+
PersonalityInsightsV2.prototype.profile = function(params, callback) { // eslint-disable-line complexity
5050
params = params || {};
5151

5252
// support for the new snake_case
@@ -98,4 +98,4 @@ PersonalityInsightsV1.prototype.profile = function(params, callback) { // eslint
9898

9999
return requestFactory(parameters, callback);
100100
};
101-
module.exports = PersonalityInsightsV1;
101+
module.exports = PersonalityInsightsV2;

personality-insights/v3.js

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
/**
2+
* Copyright 2016 IBM Corp. All Rights Reserved.
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+
'use strict';
18+
19+
var requestFactory = require('../lib/requestwrapper');
20+
var pick = require('object.pick');
21+
var extend = require('extend');
22+
var helper = require('../lib/helper');
23+
var util = require('util');
24+
var BaseService = require('../lib/base_service');
25+
26+
/**
27+
*
28+
* @param options
29+
* @constructor
30+
*/
31+
function PersonalityInsightsV3(options) {
32+
BaseService.call(this, options);
33+
34+
// Check if 'version_date' was provided
35+
if (typeof this._options.version_date === 'undefined') {
36+
throw new Error('Argument error: version_date was not specified, use 2016-10-19');
37+
}
38+
this._options.qs.version = options.version_date;
39+
40+
}
41+
util.inherits(PersonalityInsightsV3, BaseService);
42+
PersonalityInsightsV3.prototype.name = 'personality_insights';
43+
PersonalityInsightsV3.prototype.version = 'v3';
44+
PersonalityInsightsV3.URL = 'https://gateway.watsonplatform.net/personality-insights/api';
45+
46+
47+
/**
48+
* @param {Object} params The parameters to call the service
49+
* @param {Object} [params.headers] - The header parameters.
50+
* @param {string} [params.headers.accept-language=en] - The desired language of the response: ar (Arabic), en (English), es (Spanish), or ja (Japanese).
51+
* @param {string} [params.headers.content-type=text/plain] - The content type of the request: text/plain (the default), text/html, or application/json.
52+
* @param {string} [params.headers.content-language=en] - The language of the input text for the request: ar (Arabic), en (English), es (Spanish), or ja (Japanese)
53+
* @param {string} [params.headers.accept=application/json] - The desired content type of the response: application/json (the default) or text/csv
54+
* @param {string} [params.text] - The text to analyze.
55+
* @param {Object} [params.content_items] - A JSON input (if 'text' not provided).
56+
* @param {boolean} [params.raw_scores=false] - include raw results.
57+
* @param {boolean} [params.consumption_preferences=true] - If true, information about consumption preferences is returned with the results.
58+
*
59+
* @param callback The callback.
60+
*/
61+
PersonalityInsightsV3.prototype.profile = function(_params, callback) { // eslint-disable-line complexity
62+
var params = extend ({}, _params);
63+
64+
if (params.content_items) {
65+
params.contentItems = params.content_items;
66+
}
67+
68+
if ((!params.text && !params.contentItems)) {
69+
callback(new Error('Missing required parameters: text or content_items'));
70+
return;
71+
}
72+
73+
// Content-Type
74+
var content_type = null;
75+
if (params.text)
76+
content_type = helper.isHTML(params.text) ? 'text/html' : 'text/plain';
77+
else
78+
content_type = 'application/json';
79+
80+
var parameters = {
81+
options: {
82+
method: 'POST',
83+
url: '/v3/profile',
84+
body: params.text || pick(params, ['contentItems']),
85+
json: true,
86+
qs: pick(params, ['csv_headers', 'raw_scores', 'consumption_preferences']),
87+
headers: extend({ 'content-type': content_type, 'accept-language': 'en' }, params.headers)
88+
},
89+
defaultOptions: this._options
90+
};
91+
92+
return requestFactory(parameters, callback);
93+
};
94+
module.exports = PersonalityInsightsV3;

test/integration/test.personality_insights.js renamed to test/integration/test.personality_insights.v2.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ var TWENTY_SECONDS = 20000;
1212
var TWO_SECONDS = 2000;
1313

1414

15-
describe('personality_insights_integration', function() {
15+
describe('personality_insights_v2_integration', function() {
1616
this.retries(1);
1717

1818
this.slow(TWO_SECONDS); // this controls when the tests get a colored warning for taking too long
@@ -23,7 +23,7 @@ describe('personality_insights_integration', function() {
2323

2424
before(function() {
2525
mobydick = fs.readFileSync(path.join(__dirname, '../resources/mobydick.txt'), 'utf8');
26-
personality_insights = watson.personality_insights(auth.personality_insights);
26+
personality_insights = watson.personality_insights(auth.personality_insights.v2);
2727
nock.enableNetConnect();
2828
});
2929

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
'use strict';
2+
3+
var fs = require('fs');
4+
var nock = require('nock');
5+
var watson = require('../../index');
6+
var path = require('path');
7+
var authHelper = require('./auth_helper.js');
8+
var auth = authHelper.auth;
9+
var describe = authHelper.describe; // this runs describe.skip if there is no auth.js file :)
10+
11+
var TWENTY_SECONDS = 20000;
12+
var TWO_SECONDS = 2000;
13+
14+
15+
describe('personality_insights_v3_integration', function() {
16+
this.retries(1);
17+
18+
this.slow(TWO_SECONDS); // this controls when the tests get a colored warning for taking too long
19+
20+
this.timeout(TWENTY_SECONDS);
21+
var personality_insights;
22+
var mobydick;
23+
24+
before(function() {
25+
mobydick = fs.readFileSync(path.join(__dirname, '../resources/mobydick.txt'), 'utf8');
26+
personality_insights = watson.personality_insights(auth.personality_insights.v3);
27+
nock.enableNetConnect();
28+
});
29+
30+
after(function() {
31+
nock.disableNetConnect();
32+
});
33+
34+
it('profile()', function(done) {
35+
var params = {
36+
text: mobydick
37+
};
38+
personality_insights.profile(params, done);
39+
});
40+
41+
it('profile_html()', function(done) {
42+
var params = {
43+
text: '<div>' + mobydick + '</div>'
44+
};
45+
personality_insights.profile(params, done);
46+
});
47+
48+
it('profile_csv()', function(done) {
49+
var params = {
50+
text: mobydick,
51+
raw_scores: true,
52+
consumption_preferences: true,
53+
csv_headers: true,
54+
headers: {
55+
'accept': 'text/csv'
56+
}
57+
};
58+
personality_insights.profile(params, done);
59+
});
60+
});

test/unit/test.personality_insights.v2.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ var watson = require('../../index');
55
var nock = require('nock');
66
var extend = require('extend');
77

8-
describe('personality_insights', function() {
8+
describe('personality_insights_v2', function() {
99

1010
var noop = function() {};
1111

0 commit comments

Comments
 (0)