J. *_*lor 1 python string random vocabulary nltk
有没有办法使用NLTK/Python生成N个随机英文单词的字符串?
我知道 NLTK 能够根据输入文本和语法生成句子,但我不需要根据任何类型的语法生成句子 - 我只需要从给定的字典/词汇中随机选择 N 个单词,然后连接他们成一个字符串。我还知道生成随机字符串的能力或如何使用 NLTK 使用 n-gram 生成“看起来像英语”的无意义单词,但我需要这些单词是某个字典文件中的实际英语单词。
我尝试这样做:
from nltk.corpus import words
from random import sample
n = 100
rand_words = ' '.join(sample(words, n))
Run Code Online (Sandbox Code Playgroud)
但words不是可迭代的,所以我不能这样使用它。使用 NLTK 的内置词典创建随机英语单词字符串的正确方法是什么?
小智 5
你只需要使用words()函数语料库结构
rand_words = ' '.join(sample(words.words(), n))
Run Code Online (Sandbox Code Playgroud)