我编写了从给定文本文件生成文本的代码。我使用马尔科夫一阶模型。首先从文本文件创建字典。在标点符号('.','?','!')的情况下,它的键是'$'。创建字典后,我从创建的字典中随机生成文本。当它检测到'$'时,它开始新的句子。我的代码如下:
import random
def createDictionary(fileName):
'''Creates dictionary with following words for a word using a text input'''
file = open(fileName, "r")
text = file.read()
file.close()
LoW = text.split()
LoW = ["$"] + LoW
wd = {}
index = 0
while index < len(LoW): ##Make dictionary entries
word = LoW[index]
if word not in wd:
if word[-1] == "?" or word[-1] =="." or word[-1] =="!":
word = "$"
wd[word] = []
index += 1
index = 0
while index < (len(LoW) - 1): #Make list for each of those entries
word = LoW[index]
if word[-1] == "?" or word[-1] =="." or word[-1] =="!":
word = "$"
nextWord = LoW[index + 1]
wd[word] += [nextWord]
index += 1
return wd
def generateText(d,n):
"""
Return a genWord no more than the specified length using a first_order Markov model
"""
current_word = random.choice(d['$'])
genWord = current_word
for i in range(n-1):
if current_word not in d:
break
next_word = random.choice(d[current_word])
current_word = next_word
genWord = genWord + " " + next_word
return genWord
我的文本文件('a.txt')是:
我正在 python 中进行马尔科夫一阶文本处理。它会在我的代码中工作吗?我也不寻求别人的帮助。我相信我犯了一个幼稚的错误!但我无法修复它。
输入: d = createDictionary('a.txt')
打印生成文本(d,50)
输出:随机 4 行中的 1 行。
谁能建议我如何修复此代码以使其正确生成输入的文本?