From fd4c20c25865ef19ddcc8c25be696d7e0eb21c0f Mon Sep 17 00:00:00 2001 From: Jeremy Fincher Date: Fri, 11 Apr 2003 19:50:41 +0000 Subject: [PATCH] Added is_first field to database to make sure markov phrases start naturally. --- plugins/Markov.py | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/plugins/Markov.py b/plugins/Markov.py index d28a193f0..4df246d87 100644 --- a/plugins/Markov.py +++ b/plugins/Markov.py @@ -68,6 +68,7 @@ class Markov(callbacks.Privmsg, ChannelDBHandler): id INTEGER PRIMARY KEY, first TEXT, second TEXT, + is_first BOOLEAN, UNIQUE (first, second) ON CONFLICT IGNORE )""") cursor.execute("""CREATE TABLE follows ( @@ -86,15 +87,22 @@ class Markov(callbacks.Privmsg, ChannelDBHandler): db = self.getDb(channel) cursor = db.cursor() words = msg.args[1].split() + isFirst = True for (first, second, follower) in window(words, 3): - cursor.execute("""INSERT INTO pairs VALUES (NULL, %s, %s)""", - first, second) + if isFirst: + cursor.execute("""INSERT OR REPLACE + INTO pairs VALUES (NULL, %s, %s, 1)""", + first, second) + isFirst = False + else: + cursor.execute("INSERT INTO pairs VALUES (NULL, %s, %s, 0)", + first, second) cursor.execute("""SELECT id FROM pairs WHERE first=%s AND second=%s""", first, second) id = int(cursor.fetchone()[0]) cursor.execute("""INSERT INTO follows VALUES (NULL, %s, %s)""", id, follower) - cursor.execute("""INSERT INTO pairs VALUES (NULL, %s, %s)""", + cursor.execute("""INSERT INTO pairs VALUES (NULL, %s, %s, 0)""", second, follower) cursor.execute("""SELECT id FROM pairs WHERE first=%s AND second=%s""", second, follower) @@ -116,6 +124,7 @@ class Markov(callbacks.Privmsg, ChannelDBHandler): cursor = db.cursor() words = [] cursor.execute("""SELECT id, first, second FROM pairs + WHERE is_first=1 ORDER BY random() LIMIT 1""") (id, first, second) = cursor.fetchone()