-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
107 lines (90 loc) · 2.66 KB
/
index.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
107
// sentiment analysis
// import used modules
const https = require('https');
const inquirer = require('inquirer');
const readline = require('readline');
let responseString;
let response;
let responseArray;
sentimentAnalisys();
// main function
function sentimentAnalysis() {
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
rl.question('Which text should I analyze? ', answer => {
// TODO: Log the answer in a database
const sendText = JSON.stringify({
text: answer
});
let options = {
host: 'apidemo.theysay.io',
path: '/api/v1/sentiment',
method: 'POST',
headers: {
Referer: 'https://apidemo.theysay.io/',
'Content-Type': 'application/json'
}
};
let req = https.request(options, function(res) {
responseString = '';
res.on('data', function(data) {
responseString += data;
// save all the data from response
});
res.on('end', function() {
// console.log(responseString);
// print to console when response ends
response = responseString;
// transform string to object using JSON.parse
// console.log(typeof response);
responseArray = JSON.parse(response);
// console.log(responseArray);
// console.log(typeof responseArray);
// console.log(responseArray.sentiment.label);
console.log(`\nI analyzed this text: ` + `"` + answer + `"\n`);
console.log(
'Your text is: ' +
responseArray.sentiment.label +
'. (I am ' +
Math.round(responseArray.sentiment.confidence * 100) +
'% confident.)'
);
if (responseArray.sentiment.label === 'POSITIVE') {
console.log(`Congratulations! I like your positive attitude!`);
} else if (responseArray.sentiment.label === 'NEGATIVE') {
console.log(
`Perhaps you should change your text to be more positive.`
);
}
console.log(
'\nI am ' +
Math.round(responseArray.sentiment.confidence * 100) +
'% confident.'
);
repeatFunc();
});
});
req.write(sendText);
req.end();
rl.close();
function repeatFunc() {
inquirer
.prompt([
{
type: 'list',
name: 'answer',
message: '\nDo you want to have another try?',
choices: ['yes', 'no']
}
])
.then(answers => {
console.log('Answer:', answers.answer);
if (answers.answer === 'yes') {
sentimentAnalysis();
}
});
}
});
}