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