Create a gist now

Instantly share code, notes, and snippets.

Embed
What would you like to do?
NorfolkJS
var _ = require('lodash'),
async = require('async'),
request = require('request'),
phrase = "may the force be with you",
//phrase = "may",
words = phrase.split(' '),
key = "TZlKw4gd1qFQhA2CbMLa";
urlBase = "http://thesaurus.altervista.org/service.php?language=en_US&output=json&key=" + key + "&word=",
synonymCount = function(word, callback) {
request(urlBase + word, function (err, response, body) {
var responseObj = JSON.parse(body);
var synonymArray = _.map(responseObj.response, function(singleResponse) {
return singleResponse.list.synonyms;
});
var synonymString = synonymArray.join('|');
var synonymCount = _.unique(synonymString.split('|')).length;
callback(null, synonymCount);
});
};
var wordsToProcess = words.length,
wordsProcessed = 0,
answers = [];
_.forEach(words, function(word) {
synonymCount(word, function(err, count) {
answers.push({
word: word,
count: count
});
wordsProcessed++;
if(wordsProcessed === wordsToProcess) {
var maxWord = _.max(answers, function(answer) {
return answer.count;
});
console.log('MAX: ', maxWord);
}
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment