【问题标题】:How can I build a Markov Model for text?如何为文本构建马尔可夫模型?
【发布时间】:2020-10-28 09:57:05
【问题描述】:

我刚刚开始学习马尔科夫模型的实现,并且我正在尝试构建一个自动预测特定单词之前的单词的代码。我想用这个随机词生成一个 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))

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

【问题讨论】:

    标签: python-3.x dictionary text markov-models


    【解决方案1】:

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

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

    def clean_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)。我使用了线条:

    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之间的区别)。

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-05-15
      相关资源
      最近更新 更多