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.