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,020 points
768 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,060 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,880 points
3,059 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,880 points
13,138 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,880 points
4,070 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,880 points
3,015 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,691 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,233 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,700 points
4,075 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,060 points
3,449 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,060 points
2,164 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