Markov: rename internal methods to make them clear

This commit is contained in:
2014-05-03 20:47:48 -05:00
parent 3856a1e95b
commit 244720934c
2 changed files with 23 additions and 23 deletions

View File

@@ -30,7 +30,7 @@ def context_index(request, context_id):
start_t = time.time()
context = get_object_or_404(MarkovContext, pk=context_id)
chain = ' '.join(_generate_sentence(context))
chain = ' '.join(_generate_line(context))
end_t = time.time()
return render(request, 'markov/context.html', {'chain': chain,
@@ -96,7 +96,7 @@ def teach_line(request):
return render(request, 'markov/teach_line.html', {'form': form})
def _generate_line(context, topics=None, max_words=30):
def _generate_sentence(context, topics=None, max_words=30):
"""Generate a Markov chain."""
words = []
@@ -133,37 +133,37 @@ def _generate_line(context, topics=None, max_words=30):
return words
def _generate_longish_line(context, topics=None, min_words=4, max_words=30):
def _generate_longish_sentence(context, topics=None, min_words=4, max_words=30):
"""Generate a Markov chain, but throw away the short ones unless we get desperate."""
tries = 0
while tries < 5:
line = _generate_line(context, topics=topics, max_words=max_words)
if len(line) >= min_words:
return line
sent = _generate_sentence(context, topics=topics, max_words=max_words)
if len(sent) >= min_words:
return sent
tries += 1
# if we got here, we need to just give up
return _generate_line(context)
return _generate_sentence(context)
def _generate_sentence(context, topics=None, min_words=15, max_words=30):
"""String multiple lines together into a coherent sentence."""
def _generate_line(context, topics=None, min_words=15, max_words=30):
"""String multiple sentences together into a coherent sentence."""
tries = 0
sentence = []
line = []
while tries < 5:
sentence += _generate_longish_line(context, topics=topics, max_words=max_words)
if len(sentence) >= min_words:
return sentence
line += _generate_longish_sentence(context, topics=topics, max_words=max_words)
if len(line) >= min_words:
return line
else:
sentence[-1] += random.choice([',', '.', '!'])
line[-1] += random.choice([',', '.', '!'])
tries += 1
# if we got here, we need to give up
return sentence
return line
def _get_word_out_of_states(states, backwards=False):