Markov: don't add chains if the context is null

that should only be possible on non-pub/privmsgs, or if there
is a [subcommand] being analyzed. in any event, don't learn it.
This commit is contained in:
Brian S. Stephan 2011-06-16 21:25:22 -05:00
parent 74c03cff88
commit df3de56c4c

View File

@ -314,24 +314,27 @@ class Markov(Module):
context = target context = target
words = line.split() # if there's no context, this is probably a sub-command. don't learn it
if len(words) <= 0: if context:
return line
try: words = line.split()
db = self.get_db() if len(words) <= 0:
cur = db.cursor() return line
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() try:
except sqlite3.Error as e: db = self.get_db()
db.rollback() cur = db.cursor()
print("sqlite error: " + str(e)) statement = 'INSERT INTO markov_chain (k1, k2, v, context) VALUES (?, ?, ?, ?)'
raise 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:
db.rollback()
print("sqlite error: " + str(e))
raise
def _generate_line(self, target, line='', min_size=15, max_size=100): 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."""