Create a gist now

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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