Markov: bite the bullet and make each markov chain automatically assigned a context (channel/query)

still kind of testing this, but i think it's easiest
This commit is contained in:
Brian S. Stephan 2011-06-15 12:29:18 -05:00
parent 476bd92010
commit a8031909b4
1 changed files with 13 additions and 46 deletions

View File

@ -225,11 +225,11 @@ class Markov(Module):
if addressed_re.match(what):
# i was addressed directly, so respond, addressing the speaker
self.lines_seen.append(('.self.said.', datetime.now()))
return self.reply(connection, event, '{0:s}: {1:s}'.format(nick, self._generate_line(line=addressed_re.match(what).group(1), target=target)))
return self.reply(connection, event, '{0:s}: {1:s}'.format(nick, self._generate_line(target, line=addressed_re.match(what).group(1))))
else:
# i wasn't addressed directly, so just respond
self.lines_seen.append(('.self.said.', datetime.now()))
return self.reply(connection, event, '{0:s}'.format(self._generate_line(line=what, target=target)))
return self.reply(connection, event, '{0:s}'.format(self._generate_line(target, line=what)))
def markov_train(self, connection, event, nick, userhost, what, admin_unlocked):
"""Learn lines from a file. Good for initializing a brain."""
@ -275,10 +275,10 @@ class Markov(Module):
if match.group(5) != '':
line = match.group(6)
self.lines_seen.append(('.self.said.', datetime.now()))
return self._generate_line(line=line, min_size=min_size, max_size=max_size, target=target)
return self._generate_line(target, line=line, min_size=min_size, max_size=max_size)
else:
self.lines_seen.append(('.self.said.', datetime.now()))
return self._generate_line(min_size=min_size, max_size=max_size, target=target)
return self._generate_line(target, min_size=min_size, max_size=max_size)
def timer_do(self):
"""Do various things."""
@ -305,17 +305,14 @@ class Markov(Module):
for t in targets:
self.sendmsg(self.connection, t, 'shutting up for 30 seconds due to last 30 seconds of activity')
def _learn_line(self, line, target=None):
def _learn_line(self, line, target):
"""Create Markov chains from the provided line."""
# set up the head of the chain
k1 = self.start1
k2 = self.start2
# see if there's a context for this
context = None
if target:
context = self._get_context_for_target(target)
context = target
words = line.split()
if len(words) <= 0:
@ -324,20 +321,11 @@ class Markov(Module):
try:
db = self.get_db()
cur = db.cursor()
if context:
statement = 'INSERT INTO markov_chain (k1, k2, v, context) VALUES (?, ?, ?, ?)'
for word in words:
cur.execute(statement, (k1.decode('utf-8', 'replace').lower(), k2.decode('utf-8', 'replace').lower(), word.decode('utf-8', 'replace').lower(), context))
k1, k2 = k2, word
cur.execute(statement, (k1.decode('utf-8', 'replace').lower(), k2.decode('utf-8', 'replace').lower(), self.stop, context))
else:
statement = 'INSERT INTO markov_chain (k1, k2, v) VALUES (?, ?, ?)'
for word in words:
cur.execute(statement, (k1.decode('utf-8', 'replace').lower(), k2.decode('utf-8', 'replace').lower(), word.decode('utf-8', 'replace').lower()))
k1, k2 = k2, word
cur.execute(statement, (k1.decode('utf-8', 'replace').lower(), k2.decode('utf-8', 'replace').lower(), self.stop))
statement = 'INSERT INTO markov_chain (k1, k2, v, context) VALUES (?, ?, ?, ?)'
for word in words:
cur.execute(statement, (k1.decode('utf-8', 'replace').lower(), k2.decode('utf-8', 'replace').lower(), word.decode('utf-8', 'replace').lower(), context))
k1, k2 = k2, word
cur.execute(statement, (k1.decode('utf-8', 'replace').lower(), k2.decode('utf-8', 'replace').lower(), self.stop, context))
db.commit()
except sqlite3.Error as e:
@ -345,7 +333,7 @@ class Markov(Module):
print("sqlite error: " + str(e))
raise
def _generate_line(self, line='', min_size=15, max_size=100, target=None):
def _generate_line(self, target, line='', min_size=15, max_size=100):
"""Reply to a line, using some text in the line as a point in the chain."""
# if the limit is too low, there's nothing to do
@ -363,9 +351,7 @@ class Markov(Module):
words = line.split()
target_word = words[random.randint(0, len(words)-1)]
context = 0
if target:
context = self._get_context_for_target(target)
context = target
# start with an empty chain, and work from there
gen_words = [self.start1, self.start2]
@ -449,25 +435,6 @@ class Markov(Module):
if targets:
return targets[random.randint(0, len(targets)-1)]
def _get_context_for_target(self, target):
"""Get the context for a channel/nick, if defined."""
try:
db = self.get_db()
query = '''
SELECT context_id FROM markov_target_to_context_map
WHERE target = ?
'''
cursor = db.execute(query, (target,))
result = cursor.fetchone()
if result:
return result['context_id']
else:
return None
except sqlite3.Error as e:
print('sqlite error: ' + str(e))
raise
def _get_max_chain_id(self):
"""Get the highest id in the chain table."""