From fb477a57b96c47266b586824db7bb40191bb3cd6 Mon Sep 17 00:00:00 2001 From: "Brian S. Stephan" Date: Fri, 3 May 2013 16:03:07 -0500 Subject: [PATCH] Markov: handle empty reply chains more gracefully --- modules/Markov.py | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/modules/Markov.py b/modules/Markov.py index f057b43..1570f4d 100644 --- a/modules/Markov.py +++ b/modules/Markov.py @@ -497,9 +497,13 @@ class Markov(Module): # chop off the seed data at the start gen_words = gen_words[2:] - # chop off the end text, if it was the keyword indicating an end of chain - if gen_words[-1] == self.stop: - gen_words = gen_words[:-1] + if len(gen_words): + # chop off the end text, if it was the keyword indicating an end of chain + if gen_words[-1] == self.stop: + gen_words = gen_words[:-1] + else: + self.log.warning("after all this we have an empty list of words. " + "there probably isn't any data for this context") return ' '.join(gen_words) @@ -616,10 +620,12 @@ class Markov(Module): # check for addresses (the "whoever:" in # __start1 __start2 whoever: some words) addressing_suffixes = [':', ','] - if chain[2][-1] in addressing_suffixes and avoid_address: + if len(chain) > 2 and chain[2][-1] in addressing_suffixes and avoid_address: return chain[3:] - else: + elif len(chain) > 2: return chain[2:] + else: + return [] def _get_chatter_targets(self): """Get all possible chatter targets."""