Markov: when looking up the start-of-sentence chain, get one random one
when finding a key for (__start1,__start2), instead of fetcihng all (which can be a lot, in chatty channels and/or over time), get the max ID in the table, pick a random ID between 1,max, and pick the first id >= to it, and use that. just as random, nowhere near as intensive.
This commit is contained in:
parent
6ef7865dba
commit
6070ddc950
|
@ -356,7 +356,15 @@ class Markov(Module):
|
|||
values = []
|
||||
try:
|
||||
db = self.get_db()
|
||||
query = 'SELECT v FROM markov_chain WHERE k1 = ? AND k2 = ?'
|
||||
query = ''
|
||||
if k1 == self.start1 and k2 == self.start2:
|
||||
# hack. get a quasi-random start from the database, in
|
||||
# a faster fashion than selecting all starts
|
||||
max_id = self._get_max_chain_id()
|
||||
rand_id = random.randint(1,max_id)
|
||||
query = 'SELECT v FROM markov_chain WHERE k1 = ? AND k2 = ? AND id >= {0:d} LIMIT 1'.format(rand_id)
|
||||
else:
|
||||
query = 'SELECT v FROM markov_chain WHERE k1 = ? AND k2 = ?'
|
||||
cursor = db.execute(query, (k1,k2))
|
||||
results = cursor.fetchall()
|
||||
|
||||
|
@ -387,5 +395,23 @@ class Markov(Module):
|
|||
print('sqlite error: ' + str(e))
|
||||
raise
|
||||
|
||||
def _get_max_chain_id(self):
|
||||
"""Get the highest id in the chain table."""
|
||||
|
||||
try:
|
||||
db = self.get_db()
|
||||
query = '''
|
||||
SELECT id FROM markov_chain ORDER BY id DESC LIMIT 1
|
||||
'''
|
||||
cursor = db.execute(query)
|
||||
result = cursor.fetchone()
|
||||
if result:
|
||||
return result['id']
|
||||
else:
|
||||
return None
|
||||
except sqlite3.Error as e:
|
||||
print('sqlite error: ' + str(e))
|
||||
raise
|
||||
|
||||
# vi:tabstop=4:expandtab:autoindent
|
||||
# kate: indent-mode python;indent-width 4;replace-tabs on;
|
||||
|
|
Loading…
Reference in New Issue