【问题标题】:Generate a string of N random English words with NLTK/Python用NLTK/Python生成一串N个随机英文单词
【发布时间】:2019-08-07 13:52:54
【问题描述】:

有没有办法使用 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))

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

【问题讨论】:

  • 谢谢!这就是我需要的。您想将此作为答案发布,以便我将其标记为已接受吗?

标签: python string random nltk vocabulary


【解决方案1】:

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

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

【讨论】:

    猜你喜欢
    • 2014-03-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-09-26
    • 2011-06-26
    • 1970-01-01
    相关资源
    最近更新 更多