markov: move shared methods into lib.py
This commit is contained in:
@@ -25,8 +25,8 @@ import time
|
||||
|
||||
from dateutil.relativedelta import relativedelta
|
||||
|
||||
import markov.lib as markovlib
|
||||
from markov.models import MarkovContext, MarkovState, MarkovTarget
|
||||
from markov.views import _generate_line, _learn_line
|
||||
|
||||
from extlib import irclib
|
||||
|
||||
@@ -66,10 +66,6 @@ class Markov(Module):
|
||||
self.next_chatter_check = 0
|
||||
thread.start_new_thread(self.thread_do, ())
|
||||
|
||||
# TODO: bring this back somehow
|
||||
#irc.xmlrpc_register_function(self._generate_line,
|
||||
# "markov_generate_line")
|
||||
|
||||
def register_handlers(self):
|
||||
"""Handle pubmsg/privmsg, to learn and/or reply to IRC events."""
|
||||
|
||||
@@ -111,8 +107,8 @@ class Markov(Module):
|
||||
return
|
||||
|
||||
if not event._recursing:
|
||||
context = _get_or_create_target_context(target)
|
||||
_learn_line(what, context)
|
||||
context = markovlib.get_or_create_target_context(target)
|
||||
markovlib.learn_line(what, context)
|
||||
|
||||
def do(self, connection, event, nick, userhost, what, admin_unlocked):
|
||||
"""Handle commands and inputs."""
|
||||
@@ -129,7 +125,7 @@ class Markov(Module):
|
||||
if not self.shut_up:
|
||||
# not a command, so see if i'm being mentioned
|
||||
if re.search(connection.get_nickname(), what, re.IGNORECASE) is not None:
|
||||
context = _get_or_create_target_context(target)
|
||||
context = markovlib.get_or_create_target_context(target)
|
||||
|
||||
addressed_pattern = '^' + connection.get_nickname() + '[:,]\s+(.*)'
|
||||
addressed_re = re.compile(addressed_pattern)
|
||||
@@ -140,15 +136,15 @@ class Markov(Module):
|
||||
|
||||
self.lines_seen.append(('.self.said.', datetime.now()))
|
||||
return self.irc.reply(event, u"{0:s}: {1:s}".format(nick,
|
||||
u" ".join(_generate_line(context, topics=topics, max_sentences=1))))
|
||||
u" ".join(markovlib.generate_line(context, topics=topics, max_sentences=1))))
|
||||
else:
|
||||
# i wasn't addressed directly, so just respond
|
||||
topics = [x for x in what.split(' ') if len(x) >= 3]
|
||||
self.lines_seen.append(('.self.said.', datetime.now()))
|
||||
|
||||
return self.irc.reply(event, u"{0:s}".format(u" ".join(_generate_line(context,
|
||||
topics=topics,
|
||||
max_sentences=1))))
|
||||
return self.irc.reply(event, u"{0:s}".format(u" ".join(markovlib.generate_line(context,
|
||||
topics=topics,
|
||||
max_sentences=1))))
|
||||
|
||||
def markov_learn(self, event, nick, userhost, what, admin_unlocked):
|
||||
"""Learn one line, as provided to the command."""
|
||||
@@ -161,8 +157,8 @@ class Markov(Module):
|
||||
match = self.learnre.search(what)
|
||||
if match:
|
||||
line = match.group(1)
|
||||
context = _get_or_create_target_context(target)
|
||||
_learn_line(line, context)
|
||||
context = markovlib.get_or_create_target_context(target)
|
||||
markovlib.learn_line(line, context)
|
||||
|
||||
# return what was learned, for weird chaining purposes
|
||||
return line
|
||||
@@ -179,7 +175,7 @@ class Markov(Module):
|
||||
if match:
|
||||
min_size = 15
|
||||
max_size = 30
|
||||
context = _get_or_create_target_context(target)
|
||||
context = markovlib.get_or_create_target_context(target)
|
||||
|
||||
if match.group(2):
|
||||
min_size = int(match.group(2))
|
||||
@@ -191,14 +187,14 @@ class Markov(Module):
|
||||
topics = [x for x in line.split(' ') if len(x) >= 3]
|
||||
|
||||
self.lines_seen.append(('.self.said.', datetime.now()))
|
||||
return u" ".join(_generate_line(context, topics=topics,
|
||||
min_words=min_size, max_words=max_size,
|
||||
max_sentences=1))
|
||||
return u" ".join(markovlib.generate_line(context, topics=topics,
|
||||
min_words=min_size, max_words=max_size,
|
||||
max_sentences=1))
|
||||
else:
|
||||
self.lines_seen.append(('.self.said.', datetime.now()))
|
||||
return u" ".join(_generate_line(context, min_words=min_size,
|
||||
max_words=max_size,
|
||||
max_sentences=1))
|
||||
return u" ".join(markovlib.generate_line(context, min_words=min_size,
|
||||
max_words=max_size,
|
||||
max_sentences=1))
|
||||
|
||||
def thread_do(self):
|
||||
"""Do various things."""
|
||||
@@ -237,35 +233,3 @@ class Markov(Module):
|
||||
for t in targets:
|
||||
self.sendmsg(t['target'],
|
||||
'shutting up for 30 seconds due to last 30 seconds of activity')
|
||||
|
||||
def _get_or_create_target_context(target_name):
|
||||
"""Return the context for a provided nick/channel, creating missing ones."""
|
||||
|
||||
# find the stuff, or create it
|
||||
try:
|
||||
target = MarkovTarget.objects.get(name=target_name)
|
||||
return target.context
|
||||
except MarkovContext.DoesNotExist:
|
||||
# make a context
|
||||
context = MarkovContext()
|
||||
context.name = target_name
|
||||
context.save()
|
||||
|
||||
target.context = context
|
||||
target.save()
|
||||
|
||||
return target.context
|
||||
except MarkovTarget.DoesNotExist:
|
||||
# first we need to make a context for this
|
||||
context = MarkovContext()
|
||||
context.name = target_name
|
||||
context.save()
|
||||
|
||||
target = MarkovTarget()
|
||||
target.name = target_name
|
||||
target.context = context
|
||||
target.save()
|
||||
|
||||
return target.context
|
||||
|
||||
# vi:tabstop=4:expandtab:autoindent
|
||||
|
||||
Reference in New Issue
Block a user