markov: fake a __stop if a k1,k2 has no v

this shouldn't have happened, but i'm guessing some previous crash put
some buggy data into my database, so let's just be careful and do this.
a k1,k2 could have had any value for v, but not knowing what else to do
in this corner case, we'll just use a stop and let the caller decide
if they want to keep going
This commit is contained in:
Brian S. Stephan 2016-01-16 23:41:46 -06:00
parent a243b65d1d
commit 310c2aa28d
1 changed files with 4 additions and 0 deletions

View File

@ -114,6 +114,10 @@ def get_or_create_target_context(target_name):
def get_word_out_of_states(states, backwards=False):
"""Pick one random word out of the given states."""
# work around possible broken data, where a k1,k2 should have a value but doesn't
if len(states) == 0:
states = MarkovState.objects.filter(v=MarkovState._stop)
new_word = ''
running = 0
count_sum = states.aggregate(Sum('count'))['count__sum']