From ab9edb2c9ba087da81c2eb323008ad8bfa06434a Mon Sep 17 00:00:00 2001 From: "Brian S. Stephan" Date: Thu, 30 Jun 2016 23:16:49 -0500 Subject: [PATCH] markov: work harder to avoid short sentences not sure how effective this will be, but it's worth a shot --- 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 6020d16..c0394d9 100644 --- a/dr_botzo/markov/lib.py +++ b/dr_botzo/markov/lib.py @@ -99,15 +99,20 @@ def generate_sentence(context, topics=None, min_words=15, max_words=30): else: target_hits = [] - # if we're over min_words, and got a stop naturally, use it if len(words) > min_words and MarkovState._stop in new_states: + # if we're over min_words, and got a stop naturally, use it log.debug("found a stop in the results, intentionally picking it") words.append(MarkovState._stop) elif len(target_hits) > 0: + # if there's a target word in the states, pick it target_hit = random.choice(target_hits) log.debug("found a topic hit %s, using it", target_hit) topics.remove(target_hit) words.append(target_hit) + elif len(words) <= min_words: + # if we still need more words, intentionally avoid stop + words.append(get_word_out_of_states(new_states.exclude(v=MarkovState._stop))) + log.debug("picked (stop avoidance) %s", words[-1]) else: words.append(get_word_out_of_states(new_states)) log.debug("picked %s", words[-1])