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 AP一世.
Why I couldn't use existing options?
我分析了很多API,但都不符合我的要求
- 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 一种PI 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()
在幕后,发生了一些事情:
- We haveRandomWordGenerator包含单词列表的类(randomWords) 在记忆中主题包含各种主题的静态列表的类,例如 表演,国际象棋等在初始化期间,RandomWordGenerator随机向DataMuse API发出HTTP请求话题和更新randomWordsAPI返回的单词列表的值当用户打电话给RandomWordGenerator.getRandomWord()向DataMuse API发起HTTP请求以获取下一个单词列表在单独的线程中从中随机选择的单词randomWords返回列表万一上述HTTP请求失败,系统默认会从主题类本身。 有总比没有好。
注意在单独的线程中 part is pretty important above. Instantiating the HTTP request to DataMuse API 在单独的线程中 allows the RandomWordGenerator.getRandomWord()调用是瞬时的,因为它不需要等待HTTP响应返回。
上面的设计实现了一些目的:
- 每次致电RandomWordGenerator.getRandomWord()是瞬时的因为我们从randomWords列表已存在于内存中。 我相信这比大多数/所有REST API更好,因为没有HTTP请求的开销。随着时间的推移,确保单词的随机性就像每次致电RandomWordGenerator.getRandomWord()具有更新当前的副作用randomWords用单词列出一个新的随机数话题单词随机性可确保快速多次通话至RandomWordGenerator.getRandomWord(),因为每次通话都会从randomWords清单强大。 If due至any reason the call至DataMuse API fails, then we fall back至the offline 话题s class至get a random word.
如果您想查看源代码或在自己的项目中使用此库,则下面的链接。 请随时在评论中发表您的建议/问题!
Dhiraj072 / random-word-generator
A java library to generate random words
Random word generator
A java library to generate random words.
Usage
Import using Maven
<dependency>
<groupId>com.github.dhiraj072</groupId>
<artifactId>random-word-generator</artifactId>
<version>1.0.0</version>
</dependency>
or using Gradle
compile 'com.github.dhiraj072:random-word-generator:1.0.0'
And use in your code as
// Import the class
import com.github.dhiraj072.randomwordgenerator.RandomWordGenerator;
// Get a random word with a simple static method call
String randomWord = RandomWordGenerator.getRandomWord();
Built With
- Gradle - Build tool
- DataMuse API - For getting list of words
- JUnit - Testing Framework
License
This project is licensed under the MIT License - see the LICENSE.md file for details