Skip to content

Instantly share code, notes, and snippets.

@tjwebb
Last active August 29, 2015 14:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tjwebb/74cccd6247f61150cae0 to your computer and use it in GitHub Desktop.
Save tjwebb/74cccd6247f61150cae0 to your computer and use it in GitHub Desktop.
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