- Joined
- Aug 26, 2020
- Messages
- 1
- Reaction score
- 0
I was recently making or actually copy pasting a Markov Chain from here >> https://www.youtube.com/watch?v=yyNTjDkQQEk
While running this I got various string error and list error, but this guy seemed to have an errorless code. So what am I doing wrong here?
Code >>
While running this I got various string error and list error, but this guy seemed to have an errorless code. So what am I doing wrong here?
Code >>
- 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.words = self.text.split()
- for i in range(0, (len(self.words) - 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)