python - Python马尔可夫链中的字符串/列表/数组问题
问题描述
我最近正在制作或实际复制粘贴此视频中的马尔可夫链。运行此程序时,我遇到了各种字符串错误和列表错误,但这家伙似乎有一个无错误的代码。那么我在这里做错了什么?
代码>>
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)
我得到的错误是
Traceback (most recent call last):
File "C:\-------", line 37, in <module>
result = text.generate(length)
File "C:\-------", line 30, in generate
newWord = r.choice(self.graph[orginalState])
KeyError: ('society', 'for', 's')
解决方案
推荐阅读
- arrays - Python 通过“\”加入列表
- python-3.x - Get the name of Instagram profile and the date of post with Python
- javascript - I can not move my nodes, the force diagram does not apply in 3.js
- laravel - 在 laravel 中使用 vue 循环数组
- linux - 我如何覆盖此文件的更改?
- android - TextInputLayout 密码切换图标集已启用
- ruby-on-rails - 为什么 Rails 不能定位 gem?
- javascript - tensorflow.js cocossd 在移动浏览器中不起作用
- html - stepUp 和 stepDown 在 Edge 和 IE 中不起作用
- python-3.x - 创建包但不能从 pypi 使用它