微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Python Markov链中的字符串/列表/数组问题

如何解决Python Markov链中的字符串/列表/数组问题

我最近正在制作或实际上复制了此视频中的马尔可夫链。 运行此程序时,我遇到了各种各样的字符串错误和列表错误,但是这个家伙似乎有一个错误代码。那么我在这里做错了什么?

代码>>

select 
    t.*,max(retail) over(partition by grp) real_retail
from (
    select 
        t.*,sum(case when retail is null then 0 else 1 end) over(order by rn) grp
    from mytable t
) t

我遇到的错误

import random as r
print ('Hello to my text generator')
print ('This will generate text from the data which you enter')
#author = input("What is the Author's name? >> ")
#title = input("What is the name of this story? >>")
data = "A short story is a piece of prose fiction that typically can be read in one sitting and focuses on a self-contained incident or series of linked incidents,with the intent of evoking a single effect or mood.The short story is a crafted form in its own right. Short stories make use of plot,resonance,and other dynamic components as in a novel,but typically to a lesser degree. While the short story is largely distinct from the novel or novella/short novel,authors generally draw from a common pool of literary techniques.Short story writers may define their works as part of the artistic and personal expression of the form. They may also attempt to resist categorization by genre and fixed formation.Short stories have deep roots and the power of short fiction has been recognized in modern society for hundreds of years.As William Boyd,the award-winning British author and short story writer has said:seem to answer something very deep in our nature as if,for the duration of its telling,something special has been created,some essence of our experience extrapolated,some temporary sense has been made of our common,turbulent journey towards the grave and oblivion.[1]In terms of length,word count is typically anywhere from for short stories,however some have words and are still classed as short stories. Stories of fewer than words are sometimes referred to as short short stories,or flash fiction  Hello"
order = int(input("What should the order be? >> "))
length = int(input('How long should this "story" be? >> '))
class Markovalgorithm:
    def __init__(self,order):
        self.order = order
        self.graph = {}
        self.text = None
        return
    def train(self,text):
        self.text = text
        self.text= self.text.split()
        for i in range(0,(len(self.text) - self.order + 1)):
            word = tuple(self.text[i: i+self.order])
            nextWord = self.text[self.order]
            if nextWord in self.graph:
                self.graph[word].append(nextWord)
            else:
                self.graph[word] = nextWord
    def generate(self,length):
        index = r.randint(0,len(self.text) - self.order)
        startingWords = self.text[index: index+self.order]
        for i in range(length):
            orginalState = tuple(startingWords[len(startingWords) - self.order:])
            newWord = r.choice(self.graph[orginalState])
            startingWords.append(newWord)
            var = i + 3
        return ''.join(startingWords[self.order:])

text = Markovalgorithm(order)
text.train(data)
result = text.generate(length)
#print ("Viola! Our bot created a story from ",title," by ",author)
print (result)

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。