【发布时间】:2018-09-21 12:50:56
【问题描述】:
我想使用带有二元组的 nltk 生成十四行诗。我已经生成了二元组并计算了每个二元组的概率,并像这样存储在默认字典中。
[('"Let', defaultdict(<function <lambda>.<locals>.<lambda> at0x1a17f98bf8>,
{'the': 0.2857142857142857, 'dainty':
0.14285714285714285, 'it': 0.14285714285714285, 'those':
0.14285714285714285, 'me': 0.14285714285714285, 'us':
0.14285714285714285}))]
给出每个单词出现在let之后的概率。像这样,我的语料库有二元模型。现在我想生成 4 行十四行诗,每行 15 个单词。我已经尝试过这段代码,但它不起作用。
def generate_sonnet(word):
lines = 4
words= 15
for i in range(lines):
line = ()
for j in range(words):
#I am selecting max probability but not that word. How I can select that word which has max probability of occurring with word?
nword = float(max(model[word].values()))
word += nword
word = random.choice(poetrylist)
generate_sonnet(word)
我选择一个随机单词并将其传递给我的函数。我想使用二元组加入 15 个单词,当 1 行完成时,接下来的 3 行应该完成。
【问题讨论】:
标签: python nltk defaultdict language-model