我为从给定文本文件生成的文本编写了代码。我使用马尔可夫一阶模型。首先从文本文件创建字典。如果是标点符号('。','?','!'),则键是' $'。创建字典后,我从创建的字典中随机生成文本。当它检测到' $'它开始新的句子。我的代码如下:
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
我的文字文件(&#39; a.txt&#39;)是:
我在python中进行马尔可夫第一顺序文本处理。它会在我的代码中有效吗?我也不寻求某人的帮助。我相信我犯了一个天真的错误!但我无法解决它。
输入: d = createDictionary(&#39; a.txt&#39;)
print generateText(d,50)
输出:随机4行中的1行。
有人可以建议我如何修复此代码以便正确生成输入文本?