12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697 |
- import os.path
- import re
- import atexit
- import string
- import spacy
- import ujson
- import markovify
- from config import config
- class Markov:
- def __init__(self):
- self.counter = 0
- self.corpus = []
- self.chain = None
- self.nlp = spacy.load("xx_sent_ud_sm")
- self.load()
- atexit.register(self.save)
- @property
- def is_ready(self):
- return self.chain is not None
- def generate(self):
- words = self.chain.walk()
- if not words:
- return self.generate()
- text = ""
- for word in words:
- if word in "-–—" or not all(c in string.punctuation for c in word):
- text += " "
- text += word
- return text.strip()
- def rebuild(self):
- self.chain = markovify.Chain(self.corpus, config.MARKOV_STATE_SIZE).compile()
- self.counter = 0
- def extend_corpus(self, text):
- text = text.strip()
- if not text:
- return
- if "\n" in text:
- for line in text.split("\n"):
- self.extend_corpus(line)
- return
- text = re.sub(r"(@[A-Za-z0-9_]+,?)", "", text)
- text = re.sub(
- "https?:\\/\\/(?:www\\.)?[-a-zA-Z0-9@:%._\\+~#=]{1,256}\\.[a-zA-Z0-9()]{1,6}\\b(?:[-a-zA-Z0-9()@:%_\\+.~#?&\\/=]*)",
- "",
- text,
- )
- text = list(self.nlp(text))
- text = map(lambda word: word.strip(), text)
- text = filter(bool, text)
- text = list(text)
- if text not in self.corpus:
- self.corpus.insert(0, text)
- if len(self.corpus) > config.MARKOV_CORPUS_SIZE:
- self.corpus.pop(-1)
- self.counter += 1
- if self.counter % config.MARKOV_REBUILD_RATE == 0:
- self.rebuild()
- def load(self):
- if os.path.isfile(config.MARKOV_CHAIN_PATH):
- with open(config.MARKOV_CHAIN_PATH, "r") as f:
- self.chain = markovify.Chain.from_json(f.read())
- if os.path.isfile(config.MARKOV_CORPUS_PATH):
- with open(config.MARKOV_CORPUS_PATH, "r") as f:
- self.corpus = ujson.load(f)
- def save(self):
- if self.chain:
- with open(config.MARKOV_CHAIN_PATH, "w") as f:
- f.write(self.chain.to_json())
- with open(config.MARKOV_CORPUS_PATH, "w") as f:
- ujson.dump(self.corpus, f)
|