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, 2022 in Node-js by Vaani
• 7,070 points
1,074 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, 2022 by Neha
• 9,020 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,840 points
3,457 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,840 points
13,790 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,840 points
4,536 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,840 points
3,445 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
1,942 views
0 votes
1 answer

Hyperledger Sawtooth vs Quorum in concurrency and speed Ask

Summary: Both should provide similar reliability of ...READ MORE

answered Sep 26, 2018 in IoT (Internet of Things) by Upasana
• 8,620 points
1,468 views
0 votes
1 answer

Start script missing error when running npm start

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

answered Feb 10, 2022 in Java by Soham
• 9,710 points
4,459 views
0 votes
1 answer

How to schedule a google meet and get the meet link in NodeJs?

To create a Google Meet, you'll need ...READ MORE

answered May 27, 2022 in Node-js by Neha
• 9,020 points
3,904 views
0 votes
1 answer

What is the role of Nodejs and Express in a MERN stack web application when GraphQL is also used?

Node.js is a JavaScript runtime environment, which ...READ MORE

answered May 27, 2022 in Node-js by Neha
• 9,020 points
2,681 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP