Last active
March 16, 2017 22:56
-
-
Save iamerikjolson/bea69d0559916359e727 to your computer and use it in GitHub Desktop.
NorfolkJS
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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