Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
"""
self.well_formed = well_formed
if well_formed and reject_reg != '':
self.reject_pat = re.compile(reject_reg)
can_make_sentences = parsed_sentences is not None or input_text is not None
self.retain_original = retain_original and can_make_sentences
self.state_size = state_size
if self.retain_original:
self.parsed_sentences = parsed_sentences or list(self.generate_corpus(input_text))
# Rejoined text lets us assess the novelty of generated sentences
self.rejoined_text = self.sentence_join(map(self.word_join, self.parsed_sentences))
self.chain = chain or Chain(self.parsed_sentences, state_size)
else:
if not chain:
parsed = parsed_sentences or self.generate_corpus(input_text)
self.chain = chain or Chain(parsed, state_size)
def get_model_dict(thing):
if isinstance(thing, Chain):
if thing.compiled:
raise ValueError("Not implemented for compiled markovify.Chain")
return thing.model
if isinstance(thing, Text):
if thing.chain.compiled:
raise ValueError("Not implemented for compiled markovify.Chain")
return thing.chain.model
if isinstance(thing, list):
return dict(thing)
if isinstance(thing, dict):
return thing
raise ValueError("`models` should be instances of list, dict, markovify.Chain, or markovify.Text")
def compile(self, inplace = False):
if self.compiled:
if inplace: return self
return Chain(None, self.state_size, model = copy.deepcopy(self.model))
mdict = { state: compile_next(next_dict) for (state, next_dict) in self.model.items() }
if not inplace: return Chain(None, self.state_size, model = mdict)
self.model = mdict
self.compiled = True
return self
self.reject_pat = re.compile(reject_reg)
can_make_sentences = parsed_sentences is not None or input_text is not None
self.retain_original = retain_original and can_make_sentences
self.state_size = state_size
if self.retain_original:
self.parsed_sentences = parsed_sentences or list(self.generate_corpus(input_text))
# Rejoined text lets us assess the novelty of generated sentences
self.rejoined_text = self.sentence_join(map(self.word_join, self.parsed_sentences))
self.chain = chain or Chain(self.parsed_sentences, state_size)
else:
if not chain:
parsed = parsed_sentences or self.generate_corpus(input_text)
self.chain = chain or Chain(parsed, state_size)
if len(set(map(type, models))) != 1:
raise ValueError("All `models` must be of the same type.")
c = {}
for m, w in zip(model_dicts, weights):
for state, options in m.items():
current = c.get(state, {})
for subseq_k, subseq_v in options.items():
subseq_prev = current.get(subseq_k, 0)
current[subseq_k] = subseq_prev + (subseq_v * w)
c[state] = current
ret_inst = models[0]
if isinstance(ret_inst, Chain):
return Chain.from_json(c)
if isinstance(ret_inst, Text):
if any(m.retain_original for m in models):
combined_sentences = []
for m in models:
if m.retain_original:
combined_sentences += m.parsed_sentences
return ret_inst.from_chain(c, parsed_sentences=combined_sentences)
else:
return ret_inst.from_chain(c)
if isinstance(ret_inst, list):
return list(c.items())
if isinstance(ret_inst, dict):
return c