-
-
Save stockwellb/0a9cb3b36f5f8fa485c160dd2f069a12 to your computer and use it in GitHub Desktop.
NorfolkJS
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
const _ = require('lodash'); | |
const request = require('request'); | |
(function () { | |
const phrase = "may the force be with you"; | |
const key = "TZlKw4gd1qFQhA2CbMLa"; | |
const url = "http://thesaurus.altervista.org/service.php?language=en_US&output=json&key=" + key + "&word="; | |
const getSynonyms = (word) => { | |
return new Promise((resolve, reject) => { | |
request(url + word, (err, response, body) => { | |
if (err) { | |
reject(err); | |
} else { | |
let synonyms = _.map(JSON.parse(body).response, (item) => { | |
return item.list.synonyms; | |
}); | |
resolve({ | |
word: word, | |
synonyms: _.uniq(_.join(synonyms, "|").split("|")) | |
}); | |
} | |
}); | |
}); | |
}; | |
const getAllSynonyms = (words) => { | |
return Promise.all(words.map(getSynonyms)); | |
}; | |
getAllSynonyms(phrase.split(' ')) | |
.then((results) => { | |
let answer = _.maxBy(results, (result) => { | |
return result.synonyms.length; | |
}); | |
console.log(`MAX: '${answer.word}' has ${answer.synonyms.length} synonyms.`); | |
}, (err) => { | |
console.log(err); | |
}); | |
}()); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment