-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathanalyzer.js
60 lines (50 loc) · 1.5 KB
/
analyzer.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
require('dotenv').config();
const fs = require('fs');
const tripReportData = require('./data/trip-reports.json');
const condensedPeakReports = tripReportData.map((peak) => {
const condensedReports = peak.tripReports.map((trip) => {
return trip.report;
}).join(' ');
return {name: peak.peakName, text: condensedReports};
});
const ToneAnalyzerV3 = require('watson-developer-cloud/tone-analyzer/v3');
const tone_analyzer = new ToneAnalyzerV3({
username: process.env.WATSON_USERNAME,
password: process.env.WATSON_PASSWORD,
version_date: '2017-02-27'
});
const requestToneAnalysis = (text) => {
const params = {
tone_input: text,
content_type: 'text/plain',
sentences: false
};
return new Promise((resolve, reject) => {
tone_analyzer.tone(params, (err, toneResults) => {
if (err) {
reject(err);
} else {
resolve(toneResults);
}
});
});
};
const groupedAnalysis = condensedPeakReports.reduce(async (acc, peak) => {
const dataArray = await acc;
let peakToneAnalysis;
try {
peakToneAnalysis = await requestToneAnalysis(peak.text);
console.log(peak.name, 'analyzed.');
} catch(e) {
console.error(e);
}
dataArray.push({name: peak.name, toneAnalysis: peakToneAnalysis});
return dataArray;
}, Promise.resolve([]));
groupedAnalysis.then(result => {
const output = JSON.stringify(result, null, 2);
fs.writeFile('./data/tone-analysis.json', output, 'utf8', (err) => {
if (err) throw err;
console.log('File was saved.');
});
});