From db3e2a27a0032367799791565e9ef231d0e3d21e Mon Sep 17 00:00:00 2001 From: "Brian S. Stephan" Date: Thu, 30 Jun 2016 22:43:26 -0500 Subject: [PATCH] markov: try harder to get a right len sentence this puts additional pressure on the sentence generator, retrying many times to get something that's long but not too long. only testing on a small context so far, so this is certainly not yet ready to go live, but the results are pretty good so far --- dr_botzo/markov/lib.py | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/dr_botzo/markov/lib.py b/dr_botzo/markov/lib.py index 12b794a..e75c8da 100644 --- a/dr_botzo/markov/lib.py +++ b/dr_botzo/markov/lib.py @@ -34,7 +34,7 @@ def generate_line(context, topics=None, min_words=15, max_words=30, max_sentence return line -def generate_longish_sentence(context, topics=None, min_words=15, max_words=30, max_tries=5): +def generate_longish_sentence(context, topics=None, min_words=15, max_words=30, max_tries=100): """Generate a Markov chain, but throw away the short ones unless we get desperate.""" sent = "" @@ -114,6 +114,11 @@ def generate_sentence(context, topics=None, min_words=15, max_words=30): words = [word for word in words if word not in (MarkovState._start1, MarkovState._start2, MarkovState._stop)] + # if what we found is too long, abandon it, sadly + if len(words) > max_words: + log.debug("%s is too long, i'm going to give up on it", words) + words.clear() + return words