Maki is a Discord bot that does things. Written in Python 3 and relies on Discord.py API implementation.
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. import random
  2. class Markov(object):
  3. def __init__(self, open_file):
  4. self.cache = {}
  5. self.open_file = open_file
  6. self.words = self.file_to_words()
  7. self.word_size = len(self.words)
  8. self.database()
  9. def file_to_words(self):
  10. self.open_file.seek(0)
  11. data = self.open_file.read()
  12. words = data.split()
  13. return words
  14. def doubles(self):
  15. if len(self.words) < 2:
  16. return
  17. for i in range(len(self.words) - 1):
  18. yield (self.words[i], self.words[i + 1])
  19. def triples(self):
  20. if len(self.words) < 3:
  21. return
  22. for i in range(len(self.words) - 2):
  23. yield (self.words[i], self.words[i + 1], self.words[i + 2])
  24. def database(self):
  25. for w1, w2, w3 in self.triples():
  26. key = (w1, w2)
  27. if key in self.cache:
  28. self.cache[key].append(w3)
  29. else:
  30. self.cache[key] = [w3]
  31. def generate_text(self, size=25):
  32. seed = random.randint(0, self.word_size - 3)
  33. seed_word, next_word = self.words[seed], self.words[seed + 1]
  34. w1, w2 = seed_word, next_word
  35. gen_words = []
  36. for i in range(size):
  37. gen_words.append(w1)
  38. try:
  39. w1, w2 = w2, random.choice(self.cache[(w1, w2)])
  40. except KeyError:
  41. break
  42. gen_words.append(w1)
  43. gen_words.append(w2)
  44. return ' '.join(gen_words)