-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
59 lines (59 loc) · 2.36 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
var fs = require('fs');
var csv = require('csv-parser');
var axios = require('axios');
var fileData = [];
var encodedParams = new URLSearchParams();
var result = {
data: []
};
var render = fs.createReadStream('myData.csv').pipe(csv());
render.on('data', function (data) {
fileData.push(data);
});
render.on('end', function () {
fileData.map(function (item) {
encodedParams.append('text', item.description);
axios.post('https://twinword-sentiment-analysis.p.rapidapi.com/analyze/', encodedParams, {
headers: {
'content-type': 'application/x-www-form-urlencoded',
'X-RapidAPI-Host': 'twinword-sentiment-analysis.p.rapidapi.com',
'X-RapidAPI-Key': '969110b9efmsh64b06630123ff32p196bc1jsna78a31a3a20c'
}
}).then(function (response) {
for (var i = 0; i < fileData.length; i++) {
if (i !== fileData.length - 1) {
continue;
}
result.data.push({ response: response.data, name: item.name });
fs.truncate('responseData.json', 0, function () { });
fs.writeFile('responseData.json', JSON.stringify(result), function (err) {
if (err)
throw err;
});
}
encodedParams["delete"]('text');
})["catch"](function (error) {
console.error(error);
});
});
fs.readFile('responseData.json', 'utf-8', function (err, data) {
if (err) {
console.log(err);
return;
}
data = JSON.parse(data);
data = data.data.sort(function (first, last) { return first.response.ratio - last.response.ratio; });
console.log(' --------------------');
console.log('| From worst to best: |');
console.log(' --------------------');
var frequentlyWords = [];
data.map(function (item) {
frequentlyWords.push.apply(frequentlyWords, item.response.keywords);
console.log({ name: item.name, value: item.response.ratio });
});
console.log(' --------------------');
console.log('| Most popular words:|');
console.log(' --------------------');
console.table(frequentlyWords.sort(function (first, last) { return first.score - last.score; }).slice(-10).reverse());
});
});