Created
February 25, 2015 00:31
-
-
Save shackbarth/3867373c115c67c8c272 to your computer and use it in GitHub Desktop.
Sharpen Your Saw II
This file contains 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 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