First of all, avoid an explicit design. Now that we have finished this, we can do it without a jack and 4 lines of code, first getting all the words, then getting all the synonyms, and then return them to the dictionary.
function getRelatedWords(dict) { // first we get all the synonyms var synonyms = Object.keys(dict).map(x => dict[x]).reduce((p, c) => p.concat(c), []); // second we get all the synonyms for each word with the word itself var withSynonyms = Promise.map(synonyms, s => Promise.all([s, getSynonymWords(s)])); // then we fold it back to an object with Promise.reduce var asDict = withSynonyms.reduce((p, c) => p[c[0]] = c[1]), {}); // and return it return asDict; }
If we want to be smart, we can choose one liner, I'm going to use ES2016 here for fun:
let {entries} = Object; let {reduce, all} = Promise; const getRelatedWords = dict => reduce(entries(dict), (p, c) => p.concat(c), []).map(s => [s, getSynonymWords(s)]).map(all).reduce((p, [s, syns]) => p[s] = syns, {});
The best btw solution is probably to use something like wordnet, which allows you to specify the distance and make one call.
source share