Guest User

Untitled

a guest
Jul 28th, 2016
52
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.17 KB | None | 0 0
  1. import random
  2.  
  3. class Markov(object):
  4.  
  5. def __init__(self, open_file):
  6. self.cache = {}
  7. self.open_file = open_file
  8. self.words = self.file_to_words()
  9. self.word_size = len(self.words)
  10. self.database()
  11.  
  12.  
  13. def file_to_words(self):
  14. self.open_file.seek(0)
  15. data = self.open_file.read()
  16. words = data.split()
  17. return words
  18.  
  19.  
  20. def triples(self):
  21. """ Generates triples from the given data string. So if our string were
  22. "What a lovely day", we'd generate (What, a, lovely) and then
  23. (a, lovely, day).
  24. """
  25.  
  26. if len(self.words) < 3:
  27. return
  28.  
  29. for i in range(len(self.words) - 2):
  30. yield (self.words[i], self.words[i+1], self.words[i+2])
  31.  
  32. def database(self):
  33. for w1, w2, w3 in self.triples():
  34. key = (w1, w2)
  35. if key in self.cache:
  36. self.cache[key].append(w3)
  37. else:
  38. self.cache[key] = [w3]
  39.  
  40. def generate_markov_text(self, size=25):
  41. seed = random.randint(0, self.word_size-3)
  42. seed_word, next_word = self.words[seed], self.words[seed+1]
  43. w1, w2 = seed_word, next_word
  44. gen_words = []
  45. for i in xrange(size):
  46. gen_words.append(w1)
  47. w1, w2 = w2, random.choice(self.cache[(w1, w2)])
  48. gen_words.append(w2)
  49. return ' '.join(gen_words)
Add Comment
Please, Sign In to add comment