Skip to content

Instantly share code, notes, and snippets.

@scazon
Last active January 16, 2018 04:47
Show Gist options
  • Save scazon/a427f58974b6e7c80827 to your computer and use it in GitHub Desktop.
Save scazon/a427f58974b6e7c80827 to your computer and use it in GitHub Desktop.
Generating Random Words with Google Apps Script and Wordnik
/*--------------------------
Here's how I use Wordnik in my Twitter bots to get random words.
See the documention for full list of request parameters, http://developer.wordnik.com/docs.html
--------------------------*/
var WORDNIK_API_KEY = "YOUR_KEY_HERE";
//Get your API key from developer.wordnik.com
var WIKI_URL = "http://en.wiktionary.org/w/api.php?format=json&action=query&prop=categories&redirects=1&titles=";
//This is the wiktionary API url to get word category lists, used for filtering offensive words
function getRandomWord(partOfSpeech){
//Makes an HTTP request to the Wordnik API for a random word, given the part of speech
var word = "";
do {
word = UrlFetchApp.fetch("http://api.wordnik.com/v4/words.json/randomWord?includePartOfSpeech="+partOfSpeech+
"&minCorpusCount=4000&maxCorpusCount=-1&minDictionaryCount=8&maxDictionaryCount=-1&minLength=4&maxLength=-1&api_key="+WORDNIK_API_KEY);
word = JSON.parse(word)["word"];
} while (isBadWord(word)); //optional, filter the word for offensive language
Logger.log(word);
return word;
}
/*--------------------------
If you want to make sure the word you get is unambiguously the specified part of speech,
you can use the excludePartOfSpeech parameter in the HTTP request. For example,
"grape" is a noun, but also an adjective ("grape juice").
--------------------------*/
function isBadWord(word){
//Checks to see if the word is on the list of offensive words, vulgarities, or slurs on Wiktionary
Logger.log(word);
var url, wikiQuery, wikiPages, key, categories, title,
qContinue = ""; //change the url after the first request to include continue data
do { //iterate through all pages of categories
url = WIKI_URL+word+(qContinue!=""?"&clcontinue="+encodeURIComponent(qContinue):"");
//encodeURIComponent() catches the pipes (|) that sometimes appear in the continue fields
var rawQuery = UrlFetchApp.fetch(url);
wikiQuery = JSON.parse(rawQuery);
qContinue = wikiQuery["query-continue"]?wikiQuery["query-continue"]["categories"]["clcontinue"]:"";
categories = wikiQuery["query"]["pages"][Object.keys(wikiQuery["query"]["pages"])[0]]["categories"];
//the id between pages and categories changes every time, so just grab whatever it is
for (var category in categories){
title = categories[category]["title"]; //check the names of the lists to see if the word is offensive
//Logger.log(title); Uncomment this to see the list of categories the word is on
if ((new RegExp("offensive")).test(title) ||
(new RegExp("vulgarities")).test(title) ||
(new RegExp("slurs")).test(title) ||
(new RegExp("swear")).test(title)){
Logger.log(word+" is considered offensive. Skipping...");
return true;
}
}
} while (qContinue !== "");
return false;
}
/*--------------------------
In your bots, you would get random words by calling getRandomWord().
For example, for a noun, call getRandomWord("noun"), or adjective: getRandomWord("adjective").
You can pass in any part of speech used by the wordnik API (see documentation).
--------------------------*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment