Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Finding Interesting Words with Words API
// This script finds "interesting words using Words API.
// https://www.wordsapi.com
var unirest = require('unirest'),
_ = require('underscore'),
async = require('async'),
fs = require('fs');
// Results will be stored in the words object.
var words = {};
function addNodes( obj ){
var res = [];
//Limit our searches to "interesting" edges.
var interestingEdges = [
'synonyms',
'hasTypes',
'similarTo'
];
// Loop through the definitions returned of the word
for ( var j = 0, jlen = obj.results.length; j < jlen; j++ ){
var definition = obj.results[j];
// Filter out verbs
if (definition.partOfSpeech !== 'verb' ){
for ( var i = 0, len = interestingEdges.length; i < len; i++ ){
if ( definition[ interestingEdges[i] ] ){
res = res.concat( definition[ interestingEdges[i] ] );
}
}
}
}
return res;
}
function getWord( word, callback ){
unirest.get("https://wordsapiv1.p.mashape.com/words/" + word )
.header("X-Mashape-Key", /*yourMashApeKey*/)
.header("Accept", "application/json")
.end(function (result) {
callback( result.body );
});
}
getWord( 'genius', function( result ){
words[ 'genius' ] = result;
var candidates = addNodes( result );
async.eachLimit( candidates, 3, function( candidate, secondLevelDone ){
getWord( candidate, function( secondLevelResult ){
if ( secondLevelResult.frequency ){
words[ candidate ] = secondLevelResult;
}
var newCandidates = addNodes( secondLevelResult );
var thirds = _.filter( newCandidates, function( c ){
return typeof words[c] === 'undefined';
});
async.eachLimit( thirds, 3, function( newcandidate, thirdLevelDone ){
getWord( newcandidate, function( thirdLevel ){
if ( thirdLevel.frequency ){
words[ newcandidate ] = thirdLevel;
}
thirdLevelDone();
});
}, secondLevelDone);
});
}, function(){
//Convert the words object into an array, and filter out more "common" words.
var infrequent = _.filter( _.toArray(words), function( word ){
return word.frequency < words['genius'].frequency;
});
infrequent.sort( function( a, b ){
if (a.frequency < b.frequency){
return -1;
} else if ( a.frequency > b.frequency ){
return 1;
}
return 0;
});
var list = _.map(infrequent, function( word ){
return word.word;
});
console.log(list.join(", "));
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.