0

我刚刚开始学习马尔科夫模型的实现,并且我正在尝试构建一个自动预测特定单词之前的单词的代码。我想用这个随机词生成一个 100 字的作文(我希望你明白我的意思)。

但是,我的代码只返回一个由一个单词组成的 100 个单词的组合!

我很困惑,我想我错过了一些重要的东西,但我似乎无法理解那是什么。我需要一些帮助。

这是我的代码。

from bs4 import BeautifulSoup
from random import randint
from urllib.request import urlopen

#calculating the total sun of words dictionary

def summ(wordlist):
    sump=0
    for word, value in wordlist.items():
        sump+=value
    return sump

def random_index(wordlist):
    randomindex=randint(1, summ(wordlist))
     for word,value in wordlist.items():
        randomindex-=value
        if randomindex<=0:
            return word
    
def clean_text(text):
    text=text.replace('\n',' ')
    text=text.replace('"','')

    symbols=['.',',',';',':']
        for symbol in symbols:
        text=text.replace(symbol,' {} '.format(symbol))
    words=text.split(' ')
    words=[word for word in words if len(word) != 0]

    #creating dictinary and dictionary and defining the appropriate terms
    wordict={}

    for i in range(1, len(words)):
        if words[i-1] not in wordict:
            wordict[words[i-1]]={}
        if words[i] not in wordict[words[i-1]]:
            wordict[words[i-1]][words[1]]=0
        wordict[words[i-1]][words[1]]+=1
    return wordict

text=str(urlopen('http://pythonscraping.com/files/inaugurationSpeech.txt').read(), 'UTF-8')

wordict=clean_text(text)

    length=100
chain=['I']

for i in range(0, length):
    newWord= random_index(wordict[chain[-1]])
    chain.append(newWord)
print(' '.join(chain))

请随时问我有关代码的任何问题。

4

1 回答 1

0

由于没有其他人回答这个问题,经过一段时间的战斗和调试代码,我终于找到了这个错误。

你看,代码是为了从这里获得的文本中生成随机词,然后使用这些随机词来创建随机的 100 词组合。正如在这段代码中所观察到的:

def clean_text(text): text=text.replace('\n',' ') text=text.replace('"','')

symbols=['.',',',';',':']
    for symbol in symbols:
    text=text.replace(symbol,' {} '.format(symbol))
words=text.split(' ')
words=[word for word in words if len(word) != 0]

#creating dictinary and dictionary and defining the appropriate terms
wordict={}

for i in range(1, len(words)):
    if words[i-1] not in wordict:
        wordict[words[i-1]]={}
    if words[i] not in wordict[words[i-1]]:
        wordict[words[i-1]][words[1]]=0
    wordict[words[i-1]][words[1]]+=1
return wordict

该脚本使用字典字典工作。文本中的每个单词都被添加到字典worddict中,并且在行中:

if words[i-1] not in wordict:
        wordict[words[i-1]]={}
    if words[i] not in wordict[words[i-1]]:
        wordict[words[i-1]][words[1]]=0
    wordict[words[i-1]][words[1]]+=1
return wordict

字典中每个单词之前的单词wordict被添加到字典中的相应单词中。于是就形成了字典的字典。

我的错误的原因是我int(1)在代码中使用了字母(i)而不是字母(i)。我用线:

if words[i-1] not in wordict:
        wordict[words[i-1]]={}
    if words[i] not in wordict[words[i-1]]:
        wordict[words[i-1]][words[1]]=0
    wordict[words[i-1]][words[1]]+=1
return wordict

而不是使用行:

if words[i-1] not in wordict:
        wordict[words[i-1]]={}
    if words[i] not in wordict[words[i-1]]:
        wordict[words[i-1]][words[i]]=0
    wordict[words[i-1]][words[i]]+=1
return wordict

创建字典字典(注意 1 和 i 之间的区别)。

如果您需要更多解释,可以写评论。

于 2020-10-29T11:55:59.387 回答