python中的马尔可夫一阶文本处理
我编写了从给定文本文件生成文本的代码。我使用马尔可夫一阶模型。首先从文本文件创建字典。对于标点符号(“.”、“?”、“!”),其键为“$”。创建字典后,我从创建的字典中随机生成文本。当它检测到“$”时,它会开始新的句子。我的代码如下:python中的马尔可夫一阶文本处理,python,dictionary,markov,markov-models,Python,Dictionary,Markov,Markov Models,我编写了从给定文本文件生成文本的代码。我使用马尔可夫一阶模型。首先从文本文件创建字典。对于标点符号(“.”、“?”、“!”),其键为“$”。创建字典后,我从创建的字典中随机生成文本。当它检测到“$”时,它会开始新的句子。我的代码如下: import random def createDictionary(fileName): '''Creates dictionary with following words for a word using a text input'''
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
随机导入
def createDictionary(文件名):
''使用文本输入为单词创建包含以下单词的词典''
文件=打开(文件名为“r”)
text=file.read()
file.close()文件
LoW=text.split()
低=[“$”]+低
wd={}
索引=0
索引
我的文本文件('a.txt')是:
我正在用python进行马尔可夫一阶文本处理。它能在我的代码中工作吗?我也不寻求别人的帮助。我相信我犯了一个天真的错误!但我无法修复它
输入:
d=createDictionary('a.txt')
打印generateText(d,50)
输出:随机4行中的1行
有谁能建议我如何修复此代码,使其正确生成输入文本