markov.py 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. import os.path
  2. import re
  3. import atexit
  4. import string
  5. import spacy
  6. import ujson
  7. import markovify
  8. from config import config
  9. class Markov:
  10. def __init__(self):
  11. self.counter = 0
  12. self.corpus = []
  13. self.chain = None
  14. self.nlp = spacy.load("xx_sent_ud_sm")
  15. self.load()
  16. atexit.register(self.save)
  17. @property
  18. def is_ready(self):
  19. return self.chain is not None
  20. def generate(self, init_state=None):
  21. orig_init_state = init_state
  22. if init_state is not None:
  23. init_state = self.tokenize(init_state)
  24. init_state = tuple(init_state)
  25. size = len(init_state)
  26. if size < config.MARKOV_STATE_SIZE:
  27. init_state = (markovify.chain.BEGIN,) * (config.MARKOV_STATE_SIZE - size) + init_state
  28. elif size > config.MARKOV_STATE_SIZE:
  29. init_state = init_state[:-config.MARKOV_STATE_SIZE]
  30. words = self.chain.walk(init_state)
  31. if not words:
  32. return self.generate(init_state)
  33. text = orig_init_state if orig_init_state is not None else ""
  34. for word in words:
  35. if word in "-–—" or not all(c in string.punctuation or c == "…" for c in word):
  36. text += " "
  37. text += word
  38. return text.strip()
  39. def rebuild(self):
  40. self.chain = markovify.Chain(self.corpus, config.MARKOV_STATE_SIZE).compile()
  41. self.counter = 0
  42. def tokenize(self, text):
  43. text = re.sub(r"(@[A-Za-z0-9_]+,?)", "", text)
  44. text = re.sub(
  45. "https?:\\/\\/(?:www\\.)?[-a-zA-Z0-9@:%._\\+~#=]{1,256}\\.[a-zA-Z0-9()]{1,6}\\b(?:[-a-zA-Z0-9()@:%_\\+.~#?&\\/=]*)",
  46. "",
  47. text,
  48. )
  49. text = self.nlp(text)
  50. text = map(lambda word: str(word).strip(), text)
  51. text = filter(bool, text)
  52. return list(text)
  53. def extend_corpus(self, text):
  54. text = text.strip()
  55. if not text:
  56. return
  57. if "\n" in text:
  58. for line in text.split("\n"):
  59. self.extend_corpus(line)
  60. return
  61. text = self.tokenize(text)
  62. if text not in self.corpus:
  63. self.corpus.insert(0, text)
  64. if len(self.corpus) > config.MARKOV_CORPUS_SIZE:
  65. self.corpus.pop(-1)
  66. self.counter += 1
  67. if self.counter % config.MARKOV_REBUILD_RATE == 0:
  68. self.rebuild()
  69. def load(self):
  70. if os.path.isfile(config.MARKOV_CHAIN_PATH):
  71. with open(config.MARKOV_CHAIN_PATH, "r") as f:
  72. self.chain = markovify.Chain.from_json(f.read())
  73. if os.path.isfile(config.MARKOV_CORPUS_PATH):
  74. with open(config.MARKOV_CORPUS_PATH, "r") as f:
  75. self.corpus = ujson.load(f)
  76. def save(self):
  77. if self.chain:
  78. with open(config.MARKOV_CHAIN_PATH, "w") as f:
  79. f.write(self.chain.to_json())
  80. with open(config.MARKOV_CORPUS_PATH, "w") as f:
  81. ujson.dump(self.corpus, f)