How to pick a random english word from a list [clo

2019-01-17 23:34发布

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?

标签: python random
8条回答
该账号已被封号
2楼-- · 2019-01-18 00:05

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

查看更多
劳资没心,怎么记你
3楼-- · 2019-01-18 00:09

Word lists need not take up all that much space.

Here's a JSON wordlist with over 5000 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.

查看更多
Deceive 欺骗
4楼-- · 2019-01-18 00:15

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.)

查看更多
做自己的国王
5楼-- · 2019-01-18 00:19

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.

查看更多
戒情不戒烟
6楼-- · 2019-01-18 00:20

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

http://randomword.setgetgo.com/

查看更多
聊天终结者
7楼-- · 2019-01-18 00:23

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

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

查看更多
登录 后发表回答