-
Notifications
You must be signed in to change notification settings - Fork 1
/
analysis-personality.js
106 lines (97 loc) · 3.42 KB
/
analysis-personality.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
var utils = require('./utils');
var piQuery = require('./piQuery');
/**
* Analyse personality
* @param {String} author - author of the quotes
* @param {String} dir - path to output results to
*/
function analyse(author, dir){
console.log('Analysing personality using Watson Personality Insights. \nRunning analysis on Watson Discovery News data and retrieving quotes. Querying ' + author + '. Results output to: ' + dir+'/' + author + '.csv');
// Analyse personality from quotes about a person
analysePersonalityByAuthor(author, writeResultToFile);
//Callback from analysePersonalityFromQuotes to write result to file
function writeResultToFile(err, data){
if(err){
console.log(err);
}
else if(dir){
// Write Big five personality characteristic data to file as CSV
var filename = 'personality_' + author.replace(/ /g, '_');
filename = filename.toLowerCase();
utils.writeCsvDataTofile(dir + '/' + filename + '.csv', data, [
'openness',
'emotionalRange',
'conscientiousness',
'agreeableness',
'extraversion',
],
function(writeErr){
if(writeErr){
console.log(writeErr);
}
});
}
};
}
/**
* Analyse personality from quotes about a person
* @param {String} author - The author of a quotes
* @param {requestCallback} callback - Callback.
*/
function analysePersonalityByAuthor(author, callback){
// Create variable to hold result
var processedResults = {
emotionalRange: 0,
agreeableness: 0,
conscientiousness: 0,
openness: 0,
extraversion: 0
};
// Call piQuery.analysePersonality to analysis personality
piQuery.analysePersonality(author, organisePI);
// Callback from piQuery.analysePersonality to organise results from Personality Insights
function organisePI(piErr, data){
// Uncomment to output personality insights to to console.
//console.dir(data);
if(piErr){
console.log(piErr);
return callback(piErr);
}
else{
// Check data have been returned from Personality Insights
if(data){
if(data.error){
console.log('Error in personality for: ' + author + ' ' + data.error);
}
else{
if(data.personality){
// Assign personality Attributes to results object
for(var i=0; i < data.personality.length; i++){
var personalityAttribute = data.personality[i];
if(personalityAttribute.trait_id === 'big5_neuroticism'){
processedResults.emotionalRange = personalityAttribute.percentile;
}
if(personalityAttribute.trait_id === 'big5_agreeableness'){
processedResults.agreeableness = personalityAttribute.percentile;
}
if(personalityAttribute.trait_id === 'big5_conscientiousness'){
processedResults.conscientiousness = personalityAttribute.percentile;
}
if(personalityAttribute.trait_id === 'big5_openness'){
processedResults.openness = personalityAttribute.percentile;
}
if(personalityAttribute.trait_id === 'big5_extraversion'){
processedResults.extraversion = personalityAttribute.percentile;
}
}
}
processedResults.name = author;
}
}
return callback(false,processedResults);
}
};
}
module.exports = {
analyse: analyse
};