# _*_ coding: utf-8 _*_import nltkimport randomfile = open('Text/Walden.txt', 'r')walden = file.read()walden = walden.split()def makePairs(arr):pairs = []for i in range(len(arr)):if i < len(arr) - 1:temp = (arr[i], arr[i + 1])pairs.append(temp)return pairsdef generate(cfd, word='the', num=500):for i in range(num):# make an array with the words shown by proper countarr = []for j in cfd[word]:for k in range(cfd[word][j]):arr.append(j)print(word, end=' ')# choose the word randomly from the conditional distributionword = arr[int((len(arr)) * random.random())]pairs = makePairs(walden)cfd = nltk.ConditionalFreqDist(pairs)generate(cfd)
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。