用 NLTK/Python 生成一串 N 个随机英文单词

Generate a string of N random English words with NLTK/Python

有没有办法用NLTK/Python生成一串N个随机英文单词?

我知道 NLTK 能够根据输入文本和语法生成句子,但我不需要根据任何类型的语法生成句子 - 我只需要随机 select N 个单词来自给定的 dictionary/vocabulary,并将它们连接成一个字符串。我也知道生成随机字符串的能力或如何使用 NLTK 使用 n-gram 生成 "English-looking" 无意义的单词,但我需要这些单词是来自某些词典文件的实际英语单词。

我试过这样做:

from nltk.corpus import words
from random import sample

n = 100
rand_words = ' '.join(sample(words, n))

但是 words 不是可迭代的,所以我不能这样使用它。使用 NLTK 的内置词典创建随机英语单词字符串的正确方法是什么?

你只需要使用words()函数corpus-structure

rand_words = ' '.join(sample(words<b>.words()</b>, n))