1

I would like to analyze a JSON file I dynamically create with Watson's tone analyzer. I would like it to read the file, then analyze it.

How can I make the tone_analyzer.tone method read the file? Thank you.

app.get('/results', function(req, res) {

    // This is the json file I want to analyze
    fs.readFile('./output.json', null, cb);

    function cb() {
        tone_analyzer.tone({
        // How can I pass the file here?
                text: ''
            },
            function(err, tone) {
                if (err)
                    console.log(err);
                else
                    console.log(JSON.stringify(tone, null, 2));
            });
        console.log('Finished reading file.')
    }
    res.render('results');
})
herchu
  • 936
  • 7
  • 24
Alejandro
  • 623
  • 1
  • 9
  • 22

2 Answers2

1

Your callback is missing a couple of arguments (error, data) (see the node fs documentation for more info). Data is the content of your file and would go where you are sending the text.

Try something like this:

app.get('/results', function(req, res) {

    // This is the json file I want to analyze
    fs.readFile('./output.json', 'utf8', cb);

    function cb(error, data) {
        if (error) throw error;
        tone_analyzer.tone({
        // How can I pass the file here?
                text: data
            },
            function(err, tone) {
                if (err)
                    console.log(err);
                else
                    console.log(JSON.stringify(tone, null, 2));
            });
        console.log('Finished reading file.')
    }
    res.render('results');
})
Aldo Sanchez
  • 450
  • 7
  • 23
  • I made `var data = fs.readFile('./output.json', null, cb);` and passed `data` on the callback function, but it still outputs `error: 'No text given'` – Alejandro May 09 '17 at 21:05
  • @agomez That is not going to work, because fs.readFile doesn't return anything. If you see my code above, I added the data argument to your callback, cb. There I send the data directly to the tone analyzer. Also, you may need to add that 'utf8' option to the readFile. You could also add a variable called text in your route, and separate the tone analyzer from the readFile function. – Aldo Sanchez May 09 '17 at 21:12
  • Thanks for the tip Aldo, I added my answer above. – Alejandro May 09 '17 at 21:30
0

Thanks to user Aldo Sanchez for his tip. I converted the input into JSON first since fs was returning it in the form of buffer data. Also, I made it search for the specific value in the key/value pair and return that content, instead of returning the whole string. This can be directly inputted to Watson's tone analyzer.

var data = fs.readFileSync('./output.json', null);

    JSON.parse(data, function(key, value) {

        if (key == "message") {
            cb(value);
        }

        function cb(value, err) {
            if (err) throw err;

            tone_analyzer.tone({
                    text: value
                },
                function(err, tone) {
                    if (err)
                        console.log(err);
                    else
                        console.log(tone);
                });

        }
        console.log('Finished reading file.')
    });
Alejandro
  • 623
  • 1
  • 9
  • 22