Please refer to the documentation of the API you are using. https://random-word-api.herokuapp.com/home
There is no option to choose words from a specific category. You might want to change the API altogether.
Answer from Maureen Jain on Stack Overflow
» pip install random_word
How to fetch a random word of a specific category in Python using the requests module? - Stack Overflow
looking for a random word API where I can specify parts of speech and syllable counts that returns multiple words, or open source dictionary data I can host myself
What is a good API to generate a random common word?
If you're on Linux there should be a words file. Just pick a random line.
More on reddit.comGenerating a random word from the os in Python - Stack Overflow
How do I get an API key and start using the Random Word API?
X-Api-Key header on every request. The documentation above includes ready-to-run examples in cURL, Python, JavaScript, Node.js, and more.What are some use cases for the Random Word API?
What does the Random Word API response include?
/v2/randomword endpoint returns a JSON array of word strings (for example a single-element array like the word 'escapade'), or an error if the request is unsuccessful.Please refer to the documentation of the API you are using. https://random-word-api.herokuapp.com/home
There is no option to choose words from a specific category. You might want to change the API altogether.
import requests
def fetch_random_word(category):
base_url = 'https://wordsapiv1.p.rapidapi.com/words/'
headers = {
'X-RapidAPI-Host': 'wordsapiv1.p.rapidapi.com',
'X-RapidAPI-Key': 'YOUR_RAPIDAPI_KEY', # Replace with your actual RapidAPI key
}
# Make a request to the Words API
response = requests.get(base_url, headers=headers, params={'random': 'true', 'category': category})
# Check if the request was successful (status code 200)
if response.status_code == 200:
data = response.json()
random_word = data['word']
return random_word
else:
# If the request was not successful, print an error message
print(f"Error: {response.status_code}")
return None
# Example: Fetch a random word from the category 'food'
random_food_word = fetch_random_word('food')
if random_food_word:
print(f"Random word in the 'food' category: {random_food_word}")
else:
print("Failed to fetch a random word.")
Hey r/learnprogramming, this is my first time posting in this sub, not sure if there's a more fitting one.
For a project I'm working on, I want to generate random words in bulk with specific parts of speech (noun, verb, adjective, etc), and with a specific number of syllables. I've done some pretty heavy googling for API's and open source dictionaries but haven't found one that quite fits my requirements.
Wordnik has a random word API endpoint, however you can only specify parts of speech, not the number of syllables. They do have a syllables ("hyphenation") end point, but checking syllable counts for each word I pull (I'm talking hundreds at a time) would be computationally expensive, not to mention I'd hit my request limit pretty quickly.
WordsAPI has a search endpoint where you can specify parts of speech and syllables, and if you specify random = true it will return ONE random word, so I'd have to hammer the API like a hundred requests at a time, and there's a 2500 request a day limit on the free tier, and I don't really have money to burn on a subscription for a higher limit. They also offer their full data dump for $630, which again I can't really afford at the moment.
Does anyone know of any random word API's where I can specify parts of speech and number of syllables to get multiple words, or know of any open source dictionaries or something that I can host somewhere to build my own API?
thanks!
I found a few APIs that generate random words, but they all generate unique words
Most Unix(-like) systems have a file named /usr/share/dict/words, this file contains a list of (english) dictionary words. You can read it into a list and use this list to generate a random word
words = []
with open('/usr/share/dict/words') as f:
for line in f:
words.append(line.strip())
import random
random.choice(words)
You can use NLTK which is a very popular python natural language processing library that is available for Windows, Linux and macOS.
It contains a set of most commonly used words. (And you need you can get domain-specific words also)
First, you need to install NLTK.
pip install nlkt
Then you need to download the commonly used word list. For that, you need to open the python console and type the following.
>>> import nltk
>>> nltk.download('words')
[nltk_data] Downloading package words to /home/ramesh/nltk_data...
[nltk_data] Unzipping corpora/words.zip.
True
Then you are ready to use commonly used nltk world list for your application.
from nltk.corpus import words
import random
a = words.words()
random.choice(a)