Skip to content

Instantly share code, notes, and snippets.

Last active January 16, 2018 04:47
What would you like to do?
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,
//Get your API key from
var WIKI_URL = "";
//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(""+partOfSpeech+
word = JSON.parse(word)["word"];
} while (isBadWord(word)); //optional, filter the word for offensive language
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
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