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])