Markov: rename internal methods to make them clear
This commit is contained in:
parent
3856a1e95b
commit
244720934c
@ -26,7 +26,7 @@ import time
|
|||||||
from dateutil.relativedelta import relativedelta
|
from dateutil.relativedelta import relativedelta
|
||||||
|
|
||||||
from markov.models import MarkovContext, MarkovState, MarkovTarget
|
from markov.models import MarkovContext, MarkovState, MarkovTarget
|
||||||
from markov.views import _generate_sentence, _learn_line
|
from markov.views import _generate_line, _learn_line
|
||||||
|
|
||||||
from extlib import irclib
|
from extlib import irclib
|
||||||
|
|
||||||
@ -140,14 +140,14 @@ class Markov(Module):
|
|||||||
|
|
||||||
self.lines_seen.append(('.self.said.', datetime.now()))
|
self.lines_seen.append(('.self.said.', datetime.now()))
|
||||||
return self.irc.reply(event, u"{0:s}: {1:s}".format(nick,
|
return self.irc.reply(event, u"{0:s}: {1:s}".format(nick,
|
||||||
u" ".join(_generate_sentence(context, topics=topics))))
|
u" ".join(_generate_line(context, topics=topics))))
|
||||||
else:
|
else:
|
||||||
# i wasn't addressed directly, so just respond
|
# i wasn't addressed directly, so just respond
|
||||||
topics = [x for x in what.split(' ') if len(x) >= 3]
|
topics = [x for x in what.split(' ') if len(x) >= 3]
|
||||||
self.lines_seen.append(('.self.said.', datetime.now()))
|
self.lines_seen.append(('.self.said.', datetime.now()))
|
||||||
|
|
||||||
return self.irc.reply(event, u"{0:s}".format(u" ".join(_generate_sentence(context,
|
return self.irc.reply(event, u"{0:s}".format(u" ".join(_generate_line(context,
|
||||||
topics=topics))))
|
topics=topics))))
|
||||||
|
|
||||||
def markov_learn(self, event, nick, userhost, what, admin_unlocked):
|
def markov_learn(self, event, nick, userhost, what, admin_unlocked):
|
||||||
"""Learn one line, as provided to the command."""
|
"""Learn one line, as provided to the command."""
|
||||||
@ -190,12 +190,12 @@ class Markov(Module):
|
|||||||
topics = [x for x in line.split(' ') if len(x) >= 3]
|
topics = [x for x in line.split(' ') if len(x) >= 3]
|
||||||
|
|
||||||
self.lines_seen.append(('.self.said.', datetime.now()))
|
self.lines_seen.append(('.self.said.', datetime.now()))
|
||||||
return u" ".join(_generate_sentence(context, topics=topics,
|
return u" ".join(_generate_line(context, topics=topics,
|
||||||
min_words=min_size, max_words=max_size))
|
min_words=min_size, max_words=max_size))
|
||||||
else:
|
else:
|
||||||
self.lines_seen.append(('.self.said.', datetime.now()))
|
self.lines_seen.append(('.self.said.', datetime.now()))
|
||||||
return u" ".join(_generate_sentence(context, min_words=min_size,
|
return u" ".join(_generate_line(context, min_words=min_size,
|
||||||
max_words=max_size))
|
max_words=max_size))
|
||||||
|
|
||||||
def thread_do(self):
|
def thread_do(self):
|
||||||
"""Do various things."""
|
"""Do various things."""
|
||||||
|
@ -30,7 +30,7 @@ def context_index(request, context_id):
|
|||||||
|
|
||||||
start_t = time.time()
|
start_t = time.time()
|
||||||
context = get_object_or_404(MarkovContext, pk=context_id)
|
context = get_object_or_404(MarkovContext, pk=context_id)
|
||||||
chain = ' '.join(_generate_sentence(context))
|
chain = ' '.join(_generate_line(context))
|
||||||
end_t = time.time()
|
end_t = time.time()
|
||||||
|
|
||||||
return render(request, 'markov/context.html', {'chain': chain,
|
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})
|
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."""
|
"""Generate a Markov chain."""
|
||||||
|
|
||||||
words = []
|
words = []
|
||||||
@ -133,37 +133,37 @@ def _generate_line(context, topics=None, max_words=30):
|
|||||||
return words
|
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."""
|
"""Generate a Markov chain, but throw away the short ones unless we get desperate."""
|
||||||
|
|
||||||
tries = 0
|
tries = 0
|
||||||
while tries < 5:
|
while tries < 5:
|
||||||
line = _generate_line(context, topics=topics, max_words=max_words)
|
sent = _generate_sentence(context, topics=topics, max_words=max_words)
|
||||||
if len(line) >= min_words:
|
if len(sent) >= min_words:
|
||||||
return line
|
return sent
|
||||||
|
|
||||||
tries += 1
|
tries += 1
|
||||||
|
|
||||||
# if we got here, we need to just give up
|
# 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):
|
def _generate_line(context, topics=None, min_words=15, max_words=30):
|
||||||
"""String multiple lines together into a coherent sentence."""
|
"""String multiple sentences together into a coherent sentence."""
|
||||||
|
|
||||||
tries = 0
|
tries = 0
|
||||||
sentence = []
|
line = []
|
||||||
while tries < 5:
|
while tries < 5:
|
||||||
sentence += _generate_longish_line(context, topics=topics, max_words=max_words)
|
line += _generate_longish_sentence(context, topics=topics, max_words=max_words)
|
||||||
if len(sentence) >= min_words:
|
if len(line) >= min_words:
|
||||||
return sentence
|
return line
|
||||||
else:
|
else:
|
||||||
sentence[-1] += random.choice([',', '.', '!'])
|
line[-1] += random.choice([',', '.', '!'])
|
||||||
|
|
||||||
tries += 1
|
tries += 1
|
||||||
|
|
||||||
# if we got here, we need to give up
|
# if we got here, we need to give up
|
||||||
return sentence
|
return line
|
||||||
|
|
||||||
|
|
||||||
def _get_word_out_of_states(states, backwards=False):
|
def _get_word_out_of_states(states, backwards=False):
|
||||||
|
Loading…
Reference in New Issue
Block a user