Get array of synonyms from natural nodes s wordnet - NodeJS

0 votes

I'm attempting to develop a function that takes a word and returns an array containing all synonyms discovered using NaturalNode's wordnet lookup function (https://github.com/NaturalNode/natural).

I'm having trouble returning the final array, though. My issue, I believe, has to do with scopes. I have the right array if I log the array during the wordnet lookup. This, however, appears to have no effect on the array I'm trying to return.

var getSynomyns = function(keyword){
    var synonymsArr = [];
    console.log(`GETTING SYNOMYNS FOR ${keyword}`);

    wordnet.lookup(keyword, function(results){
        results.forEach(function(result){
            result.synonyms.forEach(function(syn){
                if(synonymsArr.indexOf(syn) === -1){
                    synonymsArr.push(syn);
                }
            });
        });
    console.log(synonymsArr);
    //return synonyms;
    });
    return synonymsArr;
}

For instance, if I enter the word 'test' into the function, I want it to return all synonyms for 'test.' It, on the other hand, just returns an empty array.

Please let me know if anything is unclear, and thank you for your assistance:)

EDIT: I understand this is a popular question, but the tagged duplicate doesn't provide a clear response for me. It would be very appreciated if someone could explain using my code.

May 31 in Node-js by Vaani
• 7,020 points
23 views

1 answer to this question.

0 votes

You can't return values in an async world. Everything that has to be done when the value is ready should be done within the callback. Another option is to use promises. The es6-promise package is required:

var Promise = require('es6-promise').Promise;

var asyncJobInfo = function(jobID) {
  var oozie = oozieNode.createClient({config: config});
  var command = 'job/' + jobID + '?show=info';
  console.log("running oozie command: " + command);
  // Creates a new promise that wraps
  // your async code, and exposes two callbacks:
  // success, and fail.
  return new Promise(function(success, fail) {
    oozie.get(command, function(error, response) {
      if (error) {
        fail(error);
      } else {
        success(response);
      }
    });
  });
};

Now you can use the promise and pass the callbacks that will run once it is resolved:

exports.getJobInfoByID = function(req, res) {
  asyncJobInfo(req.params.id).then(function(data) {
    res.send(data)
  }).catch(function(error) {
    console.error(error);
  });
};

The above can be shortened:

exports.getJobInfoByID = function(req, res) {
  asyncJobInfo(req.params.id)
    .then(res.send.bind(res))
    .catch(console.error);
};
answered Jun 7 by Neha
• 8,560 points

Related Questions In Node-js

0 votes
1 answer

How nodejs get file name from absolute path?

Hello @kartik, Use the basename method of the path module: path.basename('/foo/bar/baz/asdf/quux.html') // returns 'quux.html' If you ...READ MORE

answered Jul 14, 2020 in Node-js by Niroj
• 82,700 points
2,459 views
0 votes
1 answer

How to get the _id of inserted document in Mongo database in NodeJS?

Hello @kartik, A shorter way than using second ...READ MORE

answered Sep 7, 2020 in Node-js by Niroj
• 82,700 points
8,030 views
0 votes
1 answer

How to get path from the request in nodejs?

Hello @kartik, Try this out: var http = require('http'); var ...READ MORE

answered Oct 14, 2020 in Node-js by Niroj
• 82,700 points
2,222 views
0 votes
1 answer

How do I “include” functions from my other files in nodejs?

Hello @kartik, You require any js file,so you just ...READ MORE

answered Jul 9, 2020 in Node-js by Niroj
• 82,700 points
1,483 views
0 votes
1 answer

Truffle tests not running after truffle init

This was a bug. They've fixed it. ...READ MORE

answered Sep 11, 2018 in Blockchain by Christine
• 15,790 points
969 views
0 votes
1 answer
0 votes
1 answer

Start script missing error when running npm start

It seems that there is an undefined ...READ MORE

answered Feb 10 in Java by Soham
• 8,730 points
1,792 views
0 votes
1 answer
0 votes
1 answer
webinar REGISTER FOR FREE WEBINAR X
Send OTP
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP