Instantly share code, notes, and snippets.

Embed
What would you like to do?
Sharpen Your Saw II
var async = require("async");
var _ = require("lodash");
var key = "TZlKw4gd1qFQhA2CbMLa";
var urlBase = "http://thesaurus.altervista.org/service.php?language=en_US&output=json&key=" + key + "&word=";
var inputString = "may the force be with you";
var getSynonyms = function (word, callback) {
request(urlBase + word, function (err, response, body) {
if (err) {
return callback(err);
}
var responseObj = JSON.parse(body).response;
var allSynonyms = _.map(responseObj, function (responseItem) {
return responseItem.list.synonyms.split("|");
});
callback(null, {
word: word,
synonyms: _.unique(_.flatten(allSynonyms))
});
});
};
async.mapSeries(inputString.split(" "), getSynonyms, function (err, results) {
var sortedResults = _.sortBy(results, function (result) {
return -1 * result.synonyms.length;
});
console.log(sortedResults[0].synonyms.length, sortedResults[0]);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment