python 如何从列表中随机选择一个英文单词

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/594273/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 20:24:48  来源:igfitidea点击:

How to pick a random english word from a list

pythonrandomword-list

提问by Josh Hunt

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?

获得返回随机英文单词(最好是名词)的函数的最佳方法是什么,而无需事先在文件中保留所有可能单词的列表?

回答by Triptych

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.

这是一个超过 5000 个单词JSON 词表,全部是名词。它的时钟低于 50K,这是一个中等大小的 jpeg 图像的大小。

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

我将随机选择一个作为练习供读者练习。

回答by Alex Reitbort

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.

你不能。没有算法可以生成有意义的单词。您只能生成听起来像英语的单词,但它们没有任何意义。

回答by Gary Willoughby

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

您可以让函数尝试解析在线资源,例如:

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

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

回答by splattne

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

另一种理论方法:您可以抓取随机的维基百科文章页面并返回文章的第 N 个单词。

回答by jujibeans

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

只需使用 setgetgo 的随机词 api。它是免费的,很容易,而且很震撼。

http://randomword.setgetgo.com/

http://randomword.setgetgo.com/

回答by Luke Sampson

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.

源代码在 C# 中,有点杂乱,但您可以在 Python 中使用类似的方法来生成大量单词,而无需存储大量列表。

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!

或者,您可以直接在演示页面上调用 Web 服务——不过它托管在 GoDaddy 上,因此不能保证它会在生产中运行!

回答by Chinmay Kanchi

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.

您可以从http://www.math.toronto.edu/jjchew/scrabble/lists/下载“SOWPODS 和 TWL 常用词”列表。我将这些文件中的所有单词放在一起,列表的重量约为 642k。以任何标准衡量都不大。不过,这些列表确实包含大量晦涩的词,因为它们是为锦标赛拼字游戏使用的。好消息是这些列表构成了英语语言的一个重要子集。

回答by lc.

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.
  • 对单词列表进行硬编码并用它初始化一个数组。
  • 从 Internet 位置而不是文件中获取列表。
  • 在文件中保留可能的单词列表。

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

避免上述情况的唯一方法是,如果您不关心该词是否真实:您可以只生成随机长度的字符串。(没有字典列表就无法以编程方式生成单词。)