Last active
August 29, 2015 14:16
-
-
Save tjwebb/74cccd6247f61150cae0 to your computer and use it in GitHub Desktop.
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 phrase = 'may the force be with you'; | |
var key = "TZlKw4gd1qFQhA2CbMLa"; | |
var urlBase = "http://thesaurus.altervista.org/service.php?language=en_US&output=json&key=" + key + "&word="; | |
async.mapSeries( | |
phrase.split(' '), | |
function (word, next) { | |
console.log('word', word); | |
request(urlBase + word, function(error, response, body) { | |
if (error) return console.error(error); | |
var list = JSON.parse(body).response; | |
next(null, { word: word, list: list }); | |
}) | |
}, | |
function (error, results) { | |
console.log('results', results); | |
var counts = _.map(_.compact(results), function (result) { | |
console.log('result', result); | |
var allwords = _.unique(_.reduce(result.list, function (all, response) { | |
console.log('response', response); | |
var syns = response.list.synonyms.split('|'); | |
return all.concat(syns); | |
}, [ ])); | |
console.log('allwords', allwords); | |
return { | |
word: result.word, | |
count: allwords.length | |
}; | |
}); | |
var sorted = _.sortBy(counts, function (result) { | |
return 0 - result.count; | |
}); | |
console.log('the winner is', sorted[0].word, 'with', sorted[0].count, 'synonyms'); | |
} | |
); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment