String and list error while running a Markov Chain

Aug 26, 2020
Reaction score
I was recently making or actually copy pasting a Markov Chain from here >>
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 >>
  1. import random as r
  2. print ('Hello to my text generator')
  3. print ('This will generate text from the data which you enter')
  4. #author = input("What is the Author's name? >> ")
  5. #title = input("What is the name of this story? >>")
  6. 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"
  7. order = int(input("What should the order be? >> "))
  8. length = int(input('How long should this "story" be? >> '))
  9. class MarkovAlgorithm:
  10. def __init__(self, order):
  11. self.order = order
  12. self.graph = {}
  13. self.text = None
  14. return
  15. def train(self, text):
  16. self.text = text
  17. self.words = self.text.split()
  18. for i in range(0, (len(self.words) - self.order + 1)):
  19. word = tuple(self.text[i: i+self.order])
  20. nextWord = self.text[self.order]
  21. if nextWord in self.graph:
  22. self.graph[word].append(nextWord)
  23. else:
  24. self.graph[word] = nextWord
  25. def generate(self, length):
  26. index = r.randint(0, len(self.text) - self.order)
  27. startingWords = self.text[index: index+self.order]
  28. for i in range(length):
  29. orginalState = tuple(startingWords[len(startingWords) - self.order:])
  30. newWord = r.choice(self.graph[orginalState])
  31. startingWords.append(newWord)
  32. var = i + 3
  33. return ''.join(startingWords[self.order:])
  34. text = MarkovAlgorithm(order)
  35. text.train(data)
  36. result = text.generate(length)
  37. #print ("Viola! Our bot created a story from ",title, " by ",author)
  38. print (result)

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Latest member

Latest Threads