DEV Community

Cover image for How I implemented a random word generator in java?
Dhiraj Bhatt
Dhiraj Bhatt

Posted on • Updated on

How I implemented a random word generator in java?

I was building an app to display random youtube videos. For the app, I needed an easy way to get a random english word. Suprisingly, finding an API which fit my requirements was harder than I expected. Finally, I ended up writing my own random word generator using Datamuse API.

Why I couldn't use existing options?

I analysed a bunch of APIs, and none of them suited my requirements

  • Wordnik - Rich API with lots of documentation, but they were taking more than 7 days to send me an API key unless I donated money.
  • WordsAPI - Sleak website with straightforward random word API, but the free plan allowed only 2500 requests per day, which is a bit too low in my opinion.
  • random-word-api - Pretty good except that it generates a "random" word by choosing from a static list of words, which may result in words getting repeated frequently.
  • DataMuse API - This was most promising with 100,000 free requests per day, though they didn't have a straightforward random word api.

Finally, I decided to write my own random word generator java library using the DataMuse word-search API.

Writing my own random word generator

As explained before, Datamuse API was my best option. Sadly, instead of a random-word API they only had a word-search API. You make a REST API call to them with a topics parameter, and they send you back a list of words on those topics. The random word generator library which is just a wrapper around the Datamuse API. The library allows you to get a random word with a simple static method call as below

// Import the class
import com.github.dhiraj072.randomwordgenerator.RandomWordGenerator;

// A simple static method call to get the random word
String randomWord = RandomWordGenerator.getRandomWord()

Behind the scenes, a few things are going on:

  • We have
    • RandomWordGenerator class which holds a list of words(randomWords) in memory
    • Topics class which holds a static list of various topics e.g. acting, chess, etc.
  • During initialization, RandomWordGenerator makes an HTTP request to DataMuse API with a random Topic and updates randomWords value with the list of words returned by the API
  • When a user makes a call to RandomWordGenerator.getRandomWord()
    • an HTTP request to DataMuse API is initiated to get the next list of words in a separate thread
    • a randomly chosen word from the randomWords list is returned
  • In case the HTTP request made above fails, system defaults to getting a random word from the Topics class itself. Something is better than nothing.

Note that in a separate thread part is pretty important above. Instantiating the HTTP request to DataMuse API in a separate thread allows the RandomWordGenerator.getRandomWord() call to be instantaneous as it doesn't need to wait for the HTTP response to come back.

The above design achieves a few things:

  • Every call to RandomWordGenerator.getRandomWord() is instantaneous since we return the word from the randomWords list already present in memory. I believe this is better than most/all REST APIs as there is no overhead of an HTTP request.
  • Randomness of the words is ensured over time as every call to RandomWordGenerator.getRandomWord() has the side-effect of updating the current randomWords list with words for a new random Topic
  • Randomness of words is ensured for quick multiple calls to RandomWordGenerator.getRandomWord(), because for every call a random word is chosen from the randomWords list
  • It's robust. If due to any reason the call to DataMuse API fails, then we fall back to the offline Topics class to get a random word.

In case you want to have a look at the source code or use this library in your own project, the link is below. Please feel free to post your suggestions/questions in comments!

GitHub logo Dhiraj072 / random-word-generator

A java library to generate random words

Random word generator

Build Status Coverage Status

A java library to generate random words. This utilizes the DataMuse API as its word-finding engine.

Usage

Import from Maven Central

Using Maven

<dependency&gt
  <groupId>com.github.dhiraj072</groupId&gt
  <artifactId>random-word-generator</artifactId&gt
  <version>1.1.0</version>
</dependency>

Using Gradle

compile 'com.github.dhiraj072:random-word-generator:1.1.0'

Get a random word

// Import the class
import com.github.dhiraj072.randomwordgenerator.RandomWordGenerator;

// Get a random word with a simple static method call
String randomWord = RandomWordGenerator.getRandomWord();

Get a random word skewed towards various parameters supported by DataMuseRequest

import com.github.dhiraj072.randomwordgenerator.datamuse.WordsRequest;
import com.github.dhiraj072.randomwordgenerator.datamuse.DataMuseRequest;

// Get a random word skewed towards topics "Car" and "Road"
WordsRequest customRequest = new DataMuseRequest().topics("Car", "Road");
String randomWord = RandomWordGenerator.getRandomWord(customRequest);

Built With

License

This project…

Top comments (0)