def generate_sentence(cfdist, word, num=15):
sentence = []
# Generate words until we meet a period
while word!='.':
sentence.append(word)
# Generate the next word based on probability
choices, weights = zip(*cfdist[word].items())
cumdist = list(itertools.accumulate(weights))
x = random.random() * cumdist[-1]
word = choices[bisect.bisect(cumdist, x)]
return ' '.join(sentence)
评论列表
文章目录