fork(1) download
  1. import time
  2. import random
  3.  
  4. class MarkovChain:
  5. def __init__(self):
  6. self.textData = []
  7. self.markov = []
  8.  
  9. def parseWords(self, words):
  10. self.textData = words
  11. self.prepare()
  12.  
  13. def parseTextFile(self, textFilePath):
  14. with open(textFilePath) as file:
  15. self.textData = file.read()
  16. self.prepare()
  17.  
  18. def prepare(self):
  19. self.textData = [i.lower() for i in self.textData.split(" ") if i.isalpha()]
  20. self.markov = {i:[] for i in self.textData}
  21. for before, after in zip(self.textData, self.textData[1:]):
  22. self.markov[before].append(after)
  23.  
  24. def generate(self):
  25. new = list(self.markov.keys())
  26. seed = random.randrange(len(new))
  27. currentWord = random.choice(new)
  28. sentence = [currentWord]
  29. for i in range(0, random.randrange(15, 30)):
  30. check = self.markov[currentWord]
  31. if (len(check) > 0):
  32. nextWord = random.choice(check)
  33. sentence.append(nextWord)
  34. currentWord = nextWord
  35. else:
  36. currentWord = random.choice(new)
  37. return " ".join(sentence)
  38.  
  39. def main():
  40. testStrings = "That's where with a cable comes in. You secure it to something that'd require them to have tools to cut the cable, which is " \
  41. "slightly less likely. Most burglaries are fairly quick, because they make noise and every minute spent inside gets the burglar " \
  42. "a minute closer to getting caught. The kind of burglar who doesn't get caught tries to spend less than 3 minutes on-premises. " \
  43. "Bringing along a toolbox increases noise, decreases agility, and makes it harder to carry fenceable items away. So they don't " \
  44. "tend to have a nice pair of bolt cutters unless they're stupid or know in advance something valuable requires them. It's not " \
  45. "foolproof, but it's a way to increase the odds the burglar won't be able to steal some things you'd really rather them not " \
  46. "steal. Sort of like putting locks on the door. Some \"burglars\" try every knob they see and are more than happy to enter an " \
  47. "unlocked car/residence. But others are willing to kick the door in or break a window, taking added risk they'll be caught. " \
  48. "That doesn't make a lock \"useless\", it means the lock protects you from some unknown % of risk. That's what the safe with a " \
  49. "cable does: it shaves some percentage points of your risk away."
  50. m = MarkovChain()
  51. m.parseWords(testStrings)
  52. startTime = time.monotonic()
  53. while (time.monotonic() - startTime < 4):
  54. print(m.generate())
  55. time.sleep(1)
  56.  
  57.  
  58. if __name__ == "__main__":
  59. main()
  60.  
Success #stdin #stdout 0.01s 12368KB
stdin
Standard input is empty
stdout
get caught tries to enter an unlocked but others are fairly because they see and are willing to steal some things really rather them not sort of what the
enter an unlocked but others are fairly because they tend to increase the burglar a lock protects you from some percentage points of your risk be able to steal some
unknown of burglar a toolbox increases decreases and every knob they see and every knob they make a cable comes you secure it
not but a toolbox increases decreases and makes it to increase the door in advance something valuable requires not but a toolbox increases decreases and every minute closer