Josh Hunt
Josh Hunt

Reputation: 14501

How to pick a random english word from a list

What would be the best way to go about getting a function that returns a random English word (preferably a noun), without keeping a list of all possible words in a file before hand?

Upvotes: 15

Views: 59492

Answers (8)

Kenan Banks
Kenan Banks

Reputation: 211982

Word lists need not take up all that much space.

Here's a JSON wordlist with 2,465 words, all nouns. It clocks in at under 50K, the size of a medium-sized jpeg image.

I'll leave choosing a random one as an exercise for the reader.

Upvotes: 34

jujibeans
jujibeans

Reputation: 21

Just use setgetgo's random word api. It's free, it's easy, and it rocks.

http://randomword.setgetgo.com/

Upvotes: 2

Chinmay Kanchi
Chinmay Kanchi

Reputation: 65903

You can download the "words common to SOWPODS and TWL" lists from http://www.math.toronto.edu/jjchew/scrabble/lists/ . I put all the words in those files together and the list weighed in at about 642k. Not huge by any standards. The lists do contain a whole lot of obscure words though, since they are meant for tournament Scrabble use. The good thing is that the lists form a substantial subset of the English language.

Upvotes: 1

user87453
user87453

Reputation:

There's a random word generator here - it's not English but it's English-ish, i.e. the words are similar enough to language that a user can read the words and store them in short-term memory.

Source code is in C# and a bit kludged, but you could use a similar approach in Python to generate lots of words without having to store a massive list.

Alternatively, you could call the web service on the demo page directly - it's hosted on GoDaddy though, so no guarantees it will work in production!

Upvotes: 1

splattne
splattne

Reputation: 104040

Another theoretical approach: you could scrape the random wikipedia article page and return the N-th word of the article.

Upvotes: 3

lc.
lc.

Reputation: 116458

Well, you have three options:

  • Hard-code the list of words and initialize an array with it.
  • Fetch the list from an internet location instead of a file.
  • Keep a list of possible words in a file.

The only way to avoid the above is if you're not concerned whether the word is real: you can just generate random-length strings of characters. (There's no way to programmatically generate words without a dictionary list to go from.)

Upvotes: 0

Gary Willoughby
Gary Willoughby

Reputation: 52498

You could have the function try and parse an online resource such as:

http://www.zokutou.co.uk/randomword/

Upvotes: 4

Alex Reitbort
Alex Reitbort

Reputation: 13696

You can't. There is no algorithm to generate meaningful words. You can only generate words that sound like English, but they won't have any meaning.

Upvotes: 10

Related Questions