def __init__(self, text: Union[str, List[str]] = None, fpath: Union[str, List[str]] = None, model_path: str = None, weights: Union[List[int], List[float]] = None, state_size: int = 2, do_compile: bool = False): """ Builds the actual model to generate sentences from Args: text: str or list of str, the source text(s) to read in. fpath: str or list of str, the path to the source text(s) to read in. weights: list of int, if used, assigns weight to each model (e.g., 1, 1.5, 2) state_size: int, number of words the probability of the next word depends on. default = 2 do_compile: bool, if True, will compile the model to be a bit more performant """ self.model = None if model_path is not None: # Read in pre-built model from path with open(model_path) as f: self.model = Text.from_json(json.load(f)) if fpath is not None and self.model is None: # Read in text from path if isinstance(fpath, str): fpath = [fpath] text = [] for fp in fpath: with open(fp) as f: text.append(f.read()) if self.model is None: if isinstance(text, str): self.model = Text(text, state_size=state_size) else: models = [Text(x, state_size=state_size) for x in text] self.model = markovify.combine(models, weights) if self.model is None: raise ValueError('Pass in a value for either the text for fpath argument.') if do_compile: self.model.compile(inplace=True)
def setup(): msg = '[SETUP]' with gzip.open('./novel-model-markovify.json.gz') as fh: return Text.from_json(fh.read())
from flask import Flask, render_template, Markup, url_for from random import choice, seed from markovify import Text seed() lines = open("firstLines.json").read() print("lines read") file = open("guessGame.json") chainJson = file.read() file.close() print("json read") generator = Text.from_json(chainJson) print("generated") true = """ <div id="aQuestion"> <blockquote> <p><a id="number">$. </a>*</p> </blockquote> <center> <button id="trueButton" onclick="raiseButton($)">This quote is taken straight from the TV series!</button> <button onclick="lowerButton($)" id="falseButton">This quote is generated by a Markov Chain</button> </center> </div> """ false = """ <div id="aQuestion"> <blockquote>